Green Square stops moving if there are no bullets

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Green Square stops moving if there are no bullets

Post by HedgeHog builder »

I'm not sure why, but if the bullets fly of the screen and disappear, the green 'enemies' stop moving. If I keep firing the enemies move normally, but if the bullets collide into something and get removed, the green squares stop moving left or spawning at all.
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
User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Re: Green Square stops moving if there are no bullets

Post by whiteland92 »

if you want the green square to always move you need to move one of the 'end' at line 53-57 and place it at line 33

indenting the code will make it easier to find faults like this :monocle:

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
  end  -- end at line 57 should be here

    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 ... missplaced
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
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Green Square stops moving if there are no bullets

Post by HedgeHog builder »

May I also know why the green squares do not disappear when I hit them? Did I do something wrong with the 'index ,enemy' part of the loop?
I added a score meter, and the score does go up if I hit one of the squares, and the bullets disappear, but the green squares don't disappear?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Green Square stops moving if there are no bullets

Post by MadByte »

HedgeHog builder wrote: Sun Aug 20, 2017 9:04 am May I also know why the green squares do not disappear when I hit them? Did I do something wrong with the 'index ,enemy' part of the loop?
yes, the problem is that you use the same variable name for the entity and the table holding all enemies.
Here is your code with some changes I would suggest to enhance your code a bit..

What has been changed?
  • changed variable name case for Score and EnemyDebug (score instead of Score, enemyDebug instead of EnemyDebug)
  • added code indentation for functions and loops. You may want to do that to keep the code better organized and easier to read for everyone.
  • enemy table renamed to enemies because it correlated with the local "enemy" (the entity itself) and caused problems (entities not removing itself for example)
  • local variables in general to avoid variable conflicts in future
  • no vsync to make it look smoother while developing the game
  • added screenWidth and screenHeight variables to avoid recalling love.graphics.getWidth() / getHeight() over and over
  • Moved enemy updating loops out of the big bullet updating loop, that caused the "enemies only updating when bullets move"-bug.
  • Changed if / elseif combo for movement to only if states to eliminate weird movement stops
  • fixed that debug text gets colored in by the previous set color (enemy)
  • changed enemy despawn point on the left to not despawn while still been visible
  • changed enemy y-spawn variable to prevent enemies being cut off by the screen borders
  • changed bullet velocity, just to make it feel more useful
  • Grabbed the mouse to avoid losing window focus while shooting
  • added keypressed callback and escape key to quit the game (I hate if I have to use the mouse for that)
  • moved the enemy spawnpoint to the outer right side of the screen

Code: Select all


local screenWidth, screenHeight = love.graphics.getDimensions()
local player, bullets, bulletspeed, enemies, enemySpawn, enemySpawnMax, score, enemyDebug

function love.load()
  love.window.setMode(screenWidth, screenHeight, {vsync=false})
  love.mouse.setGrabbed(true)
  player = {}
  player.width = 32
  player.height = player.width
  player.x = player.width
  player.y = screenHeight/2 - player.height/2
  bullets = {}
  bulletspeed = 600
  enemies = {}
  enemySpawn = 0.5
  enemySpawnMax = 1
  score = 0
  enemyDebug = 0
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



function love.update(dt)
  if love.keyboard.isDown('a') and player.x > 0 then player.x = player.x - (150*dt) end
  if love.keyboard.isDown('d') and player.x <(screenWidth - player.width) then player.x = player.x + (150*dt) end
  if love.keyboard.isDown('s') and player.y < (screenHeight - player.height) then player.y = player.y + (150*dt) end
  if 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 > (screenWidth - 10) or v.y < 0 or v.y > (screenHeight - 10) then
      table.remove(bullets,i)
    end
    for i,v in ipairs(bullets) do
      for index,enemy in ipairs (enemies) do
        if checkCollision(v.x,v.y,10,10, enemy.x,enemy.y,30,30) then
          table.remove(bullets,i)
          table.remove(enemies,index)
          score = score + 1
        end
      end
    end
  end
  
  enemySpawn = enemySpawn - (1*dt)
  
  if enemySpawn < 0 then
    enemySpawn = enemySpawnMax
    local newOpponent = {x = screenWidth, y = 0, width = 32, height = 32}
    newOpponent.y = math.random(0, screenHeight-newOpponent.height)
    table.insert(enemies, newOpponent)
  end
    
  for i,enemy in ipairs(enemies) do
    enemy.x = enemy.x - (150*dt)
    if enemy.x < -enemy.width then
      table.remove(enemies, i)
    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)
    love.graphics.setColor(255,255,255)
  end
  for i,v in ipairs(enemies) do
    love.graphics.setColor(1,255,100)
    love.graphics.rectangle('fill',v.x,v.y,30,30)
    love.graphics.setColor(255,255,255)
  end
  love.graphics.print('Your score is '..score,10,10)
  love.graphics.print('Enemies:'..#enemies,10,25)
  love.graphics.print('Bullets:'..#bullets,10,40)
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 love.keypressed(key)
  if key == "escape" then love.event.quit() end
end


HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Green Square stops moving if there are no bullets

Post by HedgeHog builder »

Thanks ! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 92 guests