Discord
Login
Community
DARK THEME

How to add fire rate for bullets?

So I'm making a battle platformer with guns in it, and at the moment, the bullets shoot in an almost beam instead of shooting like 1 per second for example. keep in mind this game isn't complete, or anywhere close. At the moment the code I have for the bullet is this:

Bullet = class constructor = function(position) this.x = position[0] this.y = position[1] end

velocity = 1

move = function() this.x += velocity end

end

if keyboard.SPACE then bullets.push( new Bullet([ hero.x, hero.y])) end

for i=0 to bullets.length - 1 bullets[i].move() end

drawbullet = function() for i = 0 to bullets.length - 1 screen.drawSprite("bullet", bullets[i].x, bullets[i].y, 10) end end

Here is the entire code if anyone needs that as well.

Bullet = class constructor = function(position) this.x = position[0] this.y = position[1] end

velocity = 1

move = function() this.x += velocity end

end

This is the hero set up

inithero = function() hero = object end hero.x = 0 hero.y = 0 gravity = 2 hero.speed = 3 hero.jump = 3 facing = 1 landed = 0 yvelocity = 0 landed = 0 bullets = [] end

updatehero = function() global gridx = floor((hero.x+150)*40/300) global gridy = floor((hero.y+100)*20/200) global top = floor((hero.y+5+100)*20/200) global right = floor((hero.x+7+150)*40/300) global left = floor((hero.x-7+150)*40/300) global bottom = floor((hero.y-5+100)*20/200)

if keyboard.UP and landed then //checks for collision if maps ["testmap"].get(gridx, top) != "wall" then yvelocity = 3.5 end end

if keyboard.DOWN then if maps ["testmap"].get(gridx, bottom) != "wall" then hero.y -= hero.speed end end

if keyboard.RIGHT then facing = 1 if maps ["testmap"].get(right, gridy) != "wall" then hero.x += hero.speed end end

if keyboard.LEFT then facing = -1 if maps ["testmap"].get(left, gridy) != "wall" then hero.x -= hero.speed end end

if keyboard.SPACE then bullets.push( new Bullet([ hero.x, hero.y])) end

for i=0 to bullets.length - 1 bullets[i].move() end

hero.y += yvelocity end

drawbullet = function() for i = 0 to bullets.length - 1 screen.drawSprite("bullet", bullets[i].x, bullets[i].y, 10) end end

This is the main

init = function() global mode = 'title' inithero() initscreen() global fireRate = 1 end

update = function() updatephysics() if mode == 'title' then updatescreen() elsif mode == 'play' then updatehero() updatedebug() // bullets.forEach( function( item ) item.update() end) end end

draw = function() screen.clear() if mode == 'title' then drawscreen() elsif mode == 'play' then screen.drawSprite( "hero_left", hero.x, hero.y, 13, 9 ) screen.drawMap( "testmap", 0, 0,300, 200 ) drawbullet() elsif mode == 'workinprogress' then screen.drawText('This is still under construction', 0, 0,23, 20 "rgb(255,255,255)") end end

The physics

updatephysics = function() //doesn't work yet

global gridx = floor((hero.x+150)*40/300) global gridy = floor((hero.y+100)*20/200) global top = floor((hero.y+5+100)*20/200) global right = floor((hero.x+7+150)*40/300) global left = floor((hero.x-7+150)*40/300) global bottom = floor((hero.y-5+100)*20/200) yvelocity -= 0.2 if yvelocity<0 and maps["testmap"].get(gridx,bottom) then //hero.y = -100+bottom*300+30 yvelocity = 0 landed = 1 elsif yvelocity>0 and maps["testmap"].get(gridx,bottom+1) then yvelocity = 0 else landed = 0 end hero.y+= yvelocity end

And the titlescreen set up

initscreen = function() storybuttonx = 0 storybuttony = 0 survivalbuttonx = 0 survivalbuttony = -38 end

updatescreen = function() if mouse.x >= storybuttonx -30 and mouse.x <= storybuttonx +30 and mouse.y >= storybuttony -12.5 and mouse.y <= storybuttony +12.5 then if mouse.left then global mode = 'workinprogress' end end

if mouse.x >= survivalbuttonx -30 and mouse.x <= survivalbuttonx +30 and mouse.y >= survivalbuttony -12.5 and mouse.y <= survivalbuttony +12.5 then if mouse.left then global mode = 'play' end end end

drawscreen = function() screen.clear() screen.drawMap("titlescreen", 0,0,300, 200) screen.drawSprite('survivalbutton',0,-38,60,25) screen.drawSprite('storybutton',0, 0, 60, 25) sprites["volumebutton"].setFrame(0) screen.drawSprite('volumebutton',135,85, 26, 22) end

You can use a variable to control a delay before the next bullet can be fired. Each time a bullet is pushed first check if this delay value is 0. If it is, push a new bullet then set the delay to say 200. Decrement the delay each loop.

Psuedo code

Bullet = class
  constructor=function()
     delay=0
  end
end

update=function()
  if keyboard.SPACE and bullets.delay==0 then 
     bullets.push ..
     bullets.delay=200
  end
  // decrement delay until 0
  bullets.delay-=(bullets.delay>0)
end

Post a reply

Progress

Status

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