Discord
Login
Community
DARK THEME

Help with my game

I'm trying to make a game, but I'm having trouble making the bullets appear, continue until they leave the screen, then delete themselves/disappear. Any ideas?

init = function()
  gameState = "menu"
  wave = 1
  xp = 0
  
end 

initBullet = function()
  bullet = object
    x = 0
    y = 0
    rotation = 0
  end
end

shootBullet = function()
  screen.drawSprite("bullet", bullet.x, bullet.y, 10, 10)
end

update = function()
  if gameState == "shoot" then
    if keyboard.SPACE  then
      if shot == 1 then
        shot = 0
        audio.playSound("shot")
        shootBullet()
      end
      else
        shot = 1
    end
    if keyboard.LEFT then
      angle += 4
      else if keyboard.RIGHT then
      angle -= 4
      end
    end
  else
    if gameState == "upgrade" or gameState == "menu" then
      if mouse.press then
        audio.playSound("click") 
      end
    end
  end
end


draw = function()
  if gameState == "shoot" then
    screen.clear()
    screen.setDrawRotation(0)
    max_xp = wave * 100
    screen.setFont("grapesoda")
    screen.drawText("Wave: " + wave,0,90,15,"rgb(255,255,255)")
    screen.drawText("XP: " + xp + "/" + max_xp,0,80,10,"rgb(255,255,255)")

    screen.drawSprite("base", 0, 0, 25, 25)
    screen.setDrawRotation(angle)
    screen.drawSprite("barrel", 0, 0, 25, 25)
    
  else if gameState == "menu" then 
    screen.drawSprite("startbutton", 0, 0, 50, 50)
    if mouse.y < 10 and mouse.y > -10 and mouse.x < 10 and mouse.x > -10 then
      if mouse.press then
        gameState = "shoot"
      end
    end
  end 
end
end

Use triple backticks (```) around your code to make it readable.

Markdown:

```
print("Hello, World!")
```

Result:

print("Hello, World!")

Try

if bullet.x >= 200 or bullet.x <= -200 then
    bullet.x = 50000
end

if bullet.y >= 100 or bullet.y <= -100 then
    bullet.y = 50000
end

Basically what it means is that if it passes a certain point it will be held in a sort of purgatory till you want it again, I don't think you can delete an object unless its like created in a list or something. Hope this helps

Sincerely DanMan97

There can also be a boolean variable for if the bullet should be updated and drawn.

https://microstudio.io/i/Loginus/bullet/

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community