Add an object/class called Bullet.
init = function()
bulletList = []
updateCount = 1000
//create a bullet object so you can create a new bullet whenever a player fires
Bullet = object
constructor = function(x, y, velocity, direction, size, index)
this.x = x
this.y = y
this.velocity = velocity
this.direction = direction
this.size = size
this.index = index
end
//because all active bullets are stored in a list, we need to know which index it is so when it's removed we know which index in the list to remove.
//when we remove a bullet, we also need to change the index of every proceeding bullet in the list.
removeBullet = function()
bulletList.removeAt(index)
for i in bulletList
if i.index > index then
i.index -= 1
end
end
end
drawBullet = function()
screen.setRotation(direction)
screen.drawSprite("bullet", x, y, size, size)
end
moveBullet = function()
x += velocity*cos(direction*180/pi)
y += velocity*sin(direction*180/pi)
//delete it if it goes offscreen. you should also do this if it collides with an enemy
if abs(x)>screen.width/2 and abs(y)>screen.height/2 then
removeBullet()
end
end
end
end
update = function()
updateCount += 1
if updateCount > 5 then
updateCount = 0
//always add bullets to the end of the list and set the index to the bulletList length
//this is because if we add it to the start, all of their indexes will be 0.
bulletList.push(new Bullet(0, 0, random.nextInt(3)+0.5, random.nextInt(361), random.nextInt(6)+10, bulletList.length-1))
end
for v in bulletList
v.moveBullet()
end
end
draw = function()
screen.clear()
screen.setColor("#FFF")
for v in bulletList
v.drawBullet()
end
screen.setRotation(0)
end
There's a lot of extra code for display purposes, but basically, you create a bullet object and, whenever you create a new bullet, store it in a list. when you no longer need the bullet, you delete it -- that is, when it hits an enemy or something. In this case, its just deleting when it goes off-screen, because the code shouldn't run if its off-screen (BIG memory leak if you don't), but because we're messing with a list, we need to store the index inside the bullet so it knows which one it is, so it can delete it from the list. Because we're deleting things from a list, we have to tell each object in the list that we're doing that, or else it'll delete the wrong object (for example, if we deleted the 1st bullet, but didnt tell that to every other bullet, the bullet that thinks it has an index of 2 now actually has an index of 1 in the list, so when we try and delete that bullet, it tells the program to delete the bullet at index 2, when its actually at index 1 in the list. As you can imagine, that causes problems.)
The actual bullet functionality is pretty simple. Store the x, y, velocity, direction, size, and index of the bullet inside of itself. then, draw the bullet using it's x, y, and size, and set its rotation to be the rotation provided. when moving the bullet, we move it in it's direction times the velocity it has (using some simple trig, though oddly its in radians rather than degrees, even though screen.rotation is in degrees; we have to account for that, which is that extra math in the degrees), and then we remove the bullet from the list using the earlier-described method if it's offscreen. Adding a new bullet is as simple as creating a new object in the list like I did, and adding new functionality to it (such as enemy collision) is as simple as adding a new function.
You should also add a class for enemys, or really object in your game that you'll repeatedly add. They're useful for other things as well but that's the main thing -- they're pretty easy once you get the hang of em, like anything else.