I am trying to create a simple "Pong" Game but I probably made a mistake in the way.
The ball passes through the player and computer bars and has some strange collusion with the wall where it will "stick" to it.
Here is the source code:
Code: Select all
local sw = 600
local sh = 600
local startPush = true
local comp_score = 0
local human_score = 0
local wallL_p = {
x = 100,
y = 0,
w = 10,
h = sh,
pp = 20
}
local wallR_p = {
x = 500,
y = 0,
w = 10,
h = sh
}
local player_p = {
x = wallL_p.x + wallL_p.pp,
y = sh - 40,
w = 75,
h = 19
}
local computer_p = {
x = wallL_p.x + wallL_p.pp,
y = 20,
w = 70,
h = 19,
rs = 2
}
local ball_p ={
xs = sw / 2,
ys = sh / 2,
radius = 10,
impC = 1/3
}
function love.load()
--World
--love.physics.setMeter(64)
world = love.physics.newWorld( 0, 0, true)
--Left wall
wallL = {}
wallL.b = love.physics.newBody(world, wallL_p.x, wallL_p.y, "static")
wallL.s = love.physics.newRectangleShape( wallL_p.w, wallL_p.h*3)
wallL.f = love.physics.newFixture(wallL.b, wallL.s, 1 )
---Right wall
wallR = {}
wallR.b = love.physics.newBody(world, wallR_p.x, wallR_p.y, "static")
wallR.s = love.physics.newRectangleShape( wallR_p.w, wallR_p.h*3)
wallR.f = love.physics.newFixture(wallR.b, wallR.s, 1 )
--Player
player = {}
player.b = love.physics.newBody(world, player_p.x, player_p.y, "dynamic")
player.s = love.physics.newRectangleShape( player_p.w, player_p.h)
player.f = love.physics.newFixture(wallL.b, wallL.s, 1 )
--Computer
computer = {}
computer.b = love.physics.newBody(world, computer_p.x, computer_p.y, "dynamic")
computer.s = love.physics.newRectangleShape( computer_p.w, computer_p.h)
computer.f = love.physics.newFixture(wallL.b, wallL.s, 1 )
--Ball
ball = {}
ball.b = love.physics.newBody(world, ball_p.xs, ball_p.ys, "dynamic")
ball.s = love.physics.newCircleShape(ball_p.radius)
ball.f = love.physics.newFixture(ball.b, ball.s, 1 )
ball.b:setMass(0.1)
ball.f:setRestitution(1.1)
end
function love.update(dt)
world:update(dt)
if startPush == true then
ball.b:applyLinearImpulse( randomMinus()*math.random()*ball_p.xs*ball_p.impC , randomMinus()*math.random()*ball_p.ys*ball_p.impC )
startPush = false
end
computer.b:setX(ball.b:getX())
player.b:setX(love.mouse.getX())
xb,yb = ball.b:getLinearVelocity()
if yb < 1 then
ball.b:setLinearVelocity( 0, 100 )
end
if ball.b:getY()<10 or ball.b:getY()> sh-10 then
ball.b:setLinearVelocity( 0, 0 )
ball.b:setY(ball_p.ys)
startPush = true;
end
end
function love.draw()
--Draw score
love.graphics.setColor(0, 255, 0, 255)
love.graphics.print("Human:"..human_score, 10, 40)
love.graphics.print("Computer"..comp_score, 10, 60)
--Draw walls
love.graphics.setColor(27, 126, 224, 255)
love.graphics.rectangle( "fill", wallL_p.x, wallL_p.y, wallL_p.w, wallL_p.h )
love.graphics.rectangle( "fill", wallR_p.x, wallR_p.y, wallR_p.w, wallR_p.h )
--Draw player
love.graphics.setColor(27, 224, 57, 255)
love.graphics.rectangle( "fill", player.b:getX(), player_p.y, player_p.w, player_p.h )
--Draw computer
love.graphics.setColor(217, 217, 210, 255)
love.graphics.rectangle( "fill", computer.b:getX(), computer_p.y, computer_p.w, computer_p.h )
--Draw ball
love.graphics.setColor(235, 26, 36, 255)
love.graphics.circle("fill", ball.b:getX(), ball.b:getY(), ball_p.radius)
end
function randomMinus()
if math.random() >= 0.5 then
return -1
else
return 1
end
end
Thanks in advance.