Can someone tell me what is wrong and why the squares stop moving?
This is the code:
Code: Select all
function love.load()
player = {}
player.x = 0
player.y = love.graphics.getHeight()/2
player.width = 30
player.height = player.width
bullets = {}
bulletspeed = 300
enemy = {}
enemySpawn = 0.5
enemySpawnMax = 1
Score = 0
EnemyDebug = 0
end
function love.update(dt)
if love.keyboard.isDown('a') and player.x > 0 then
player.x = player.x - (150*dt)
elseif love.keyboard.isDown('d') and player.x <(love.graphics.getWidth() - player.width) then
player.x = player.x + (150*dt)
elseif love.keyboard.isDown('s') and player.y < (love.graphics.getHeight() - player.height) then
player.y = player.y + (150*dt)
elseif love.keyboard.isDown('w') and player.y > 0 then
player.y = player.y - (150*dt)
end
for i,v in ipairs(bullets) do
v.x = v.x + (v.dx * dt)
v.y = v.y + (v.dy * dt)
if v.x < 0 or v.x > (love.graphics.getWidth() - 10) or v.y < 0 or v.y > (love.graphics.getHeight() - 10) then
table.remove(bullets,i)
end
enemySpawn = enemySpawn - (1*dt)
if enemySpawn < 0 then
enemySpawn = enemySpawnMax
randomNumber = math.random(100,love.graphics.getWidth())
newOpponent = {x = 500 , y = randomNumber , width = 30,height = 30}
table.insert(enemy,newOpponent)
end
for i,enemy in ipairs(enemy) do
enemy.x = enemy.x - (150*dt)
if enemy.x < 0 then
table.remove(enemy,i)
end
end
for i,v in ipairs(bullets) do
for index,enemy in ipairs (enemy) do
if CheckCollision(v.x,v.y,10,10, enemy.x,enemy.y,30,30) then
table.remove(bullets,i)
table.remove(enemy,index)
Score = Score + 1
end
end
end
end
end
function love.draw()
love.graphics.setColor(255,255,255)
love.graphics.rectangle('fill',player.x,player.y,player.width,player.height)
for i,v in ipairs(bullets) do
love.graphics.setColor(0,0,255)
love.graphics.rectangle('fill',v.x,v.y,10,10)
end
for i,v in ipairs(enemy) do
love.graphics.setColor(1,255,100)
love.graphics.rectangle('fill',v.x,v.y,30,30)
end
love.graphics.print('Your score is '..Score,0,0)
end
function love.mousepressed(x,y,button)
if button == 1 then
local bulletX = player.x + (player.width/2)
local bulletY = player.y + (player.height/2)
local mouseX = x
local mouseY = y
local angle = math.atan2((mouseY - bulletY),(mouseX - bulletX))
local bulletDX = bulletspeed * math.cos(angle)
local bulletDY = bulletspeed * math.sin(angle)
table.insert(bullets,{x = bulletX , y = bulletY , dx = bulletDX , dy = bulletDY})
end
end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
Thanks