Posted: Mon Aug 15, 2011 4:08 pm Post subject: Shooting Multiple Shots
I want to be able to fire multiple shots, but I can only figure out how to shoot one shot at a time. I was wondering what ways there are to do this (ie. a particle engine)?
Sponsor Sponsor
crossley7
Posted: Mon Aug 15, 2011 4:22 pm Post subject: RE:Shooting Multiple Shots
a simple way is to duplicate your variables or convert them to arrays and have an array of boolean that will say whether it is in the air or not. Then just loop around your array and depending on how you do it, you can have up to n shots if n is the size of the array.
It really depends how you have it set up to help you much with getting multiples
Insectoid
Posted: Mon Aug 15, 2011 5:01 pm Post subject: RE:Shooting Multiple Shots
You'll want an array of bullets, yeah. I suggest you calculate the maximum possible number of bullets on the screen at a time. This is how big your array should be.
chaos
Posted: Mon Aug 15, 2011 5:09 pm Post subject: Re: Shooting Multiple Shots
I want to limit it to about 10 bullets on screen at a time. Also, is it possible to do this using a particle engine for bullets?
Insectoid
Posted: Mon Aug 15, 2011 5:23 pm Post subject: RE:Shooting Multiple Shots
Uh, why? Bullets move just like characters. Particle engines are for like, smoke & stuff like that.
chaos
Posted: Mon Aug 15, 2011 5:28 pm Post subject: Re: Shooting Multiple Shots
So, is creating an array each for the number if bullets, the bullet's x-coordinate, and the bullet's y-coordinate the correct way to go?
Insectoid
Posted: Mon Aug 15, 2011 6:08 pm Post subject: RE:Shooting Multiple Shots
Have a look at records. There should be something about them in the Turing Walkthrough.
chaos
Posted: Tue Aug 16, 2011 8:08 pm Post subject: Re: Shooting Multiple Shots
I took a look at records and this is what I have come up with. I have one problem. The error message says there is no value for the variable in the line where the bullet is drawn.
Turing:
setscreen("offscreenonly") var x:int:=0 var y:int:=maxydiv2
var ship:int:=Pic.FileNew("G:/Turing Game/SpaceShip.bmp") var bullet:int:=Pic.FileNew("G:/Turing Game/Bullet.bmp") var count:int:=0 type Bullet:
record
x,y,speed:real
bullet:boolean endrecord
var Shoot:array1..10of Bullet
for a:1..10
Shoot(a).bullet:=false endfor var chars:arraycharofboolean loop Input.KeyDown(chars)
if chars (KEY_UP_ARROW)and y+27<maxy then
y+=1 endif if chars (KEY_DOWN_ARROW)and y>0then
y-=1 endif if chars (' ')then
count+=1 if count=11then
count:=1 endif for bulletcheck:1..10 if bulletcheck=count then
Shoot(bulletcheck).bullet:=true endif if Shoot(bulletcheck).bullet=truethen
Shoot(bulletcheck).x:=67
Shoot(bulletcheck).y:=y+8 endif Pic.Draw(bullet,round(Shoot(bulletcheck).x),round(Shoot(bulletcheck).y),picCopy)
Shoot(bulletcheck).x+=5 endfor
Posted: Wed Aug 17, 2011 7:43 am Post subject: RE:Shooting Multiple Shots
You have this organized very strangely. When you shoot you want to do all that setting of it's values and stuff in one section, and in a different section you check if it's alive and draw it if it is.
Right now your drawing all the bullets that don't exist as soon as you hit the space bar.
chaos
Posted: Wed Aug 17, 2011 11:14 am Post subject: Re: Shooting Multiple Shots
@mirhagk: Thanks. I fixed the problem.
Here is the fixed code. Though I need some help on delaying the movement of the bullet from the movement of the ship.
Turing:
setscreen("offscreenonly") var x:int:=0 var y:int:=maxydiv2
var ship:int:=Pic.FileNew("G:/Turing Game/SpaceShip.bmp") var bullet:int:=Pic.FileNew("G:/Turing Game/Bullet.bmp") var count:int:=0 type Bullet:
record
x,y,speed:real
bullet:boolean endrecord
var Shoot:array1..10of Bullet
for a:1..10
Shoot(a).bullet:=false
Shoot(a).speed:=5 endfor var chars:arraycharofboolean loop Input.KeyDown(chars)
if chars (KEY_UP_ARROW)and y+27<maxy then
y+=1 endif if chars (KEY_DOWN_ARROW)and y>0then
y-=1 endif if chars (' ')then
count+=1 if count=11then
count:=1 endif for check:1..10 if check=count then
Shoot(check).bullet:=true
Shoot(check).x:=67
Shoot(check).y:=y+8 endif endfor endif for firing:1..10 if Shoot(firing).bullet=truethen
Shoot(firing).x+=Shoot(firing).speed
if Shoot(firing).x>maxx then
Shoot(firing).bullet:=false else Pic.Draw(bullet,round(Shoot(firing).x),round(Shoot(firing).y),picCopy) endif endif endfor
And as for your most recent quetion, there's no need to delya them seperately. Just move them at different speeds.
Also, you're going to encounter one more problem in the future, depending on how you do it. But to make it easier, I'd move every dead bullet to a ludicrous position so they do not get in the way. When I did my spaceship game, it took me a while to figure out why ships were disappearing at random, and its because (again, depending on how you do it) the dead bullets kill ships though they dont move and are not drawn.
chaos
Posted: Mon Aug 29, 2011 10:24 pm Post subject: Re: Shooting Multiple Shots
I have now included enemies and collision detection (somewhat). The problem is that after you kill the enemy and shoot bullets again in the same location, the bullets disappear without the the presence of an enemy for some time--about less than a second, but noticeable. How can I fix this? Any help is appreciated. I have attached the enemy ship below. Here is my code:
Turing:
setscreen("offscreenonly") var x:int:=0 var y:int:=maxydiv2
var ship:int:=Pic.FileNew("G:/Turing Game/SpaceShip.bmp")%Spaceship pic (68x27 pixels) var bullet:int:=Pic.FileNew("G:/Turing Game/Bullet.bmp")%Bullet pic (20x8 pixels) var count:int:=0 var EnemyShip:int:=Pic.FileNew("G:/Turing Game/EnemyShip.bmp")%EnemyShip pic (77x39 pixels) var background:int:=Pic.FileNew("G:/Turing Game/scrollingBackground.bmp")%Background pic (2,110x300 pixels) const screenx:=640 const picx:=2110 var backx:int:=0
const eships:=5%Max # of enemy ships
type Enemy: %Record of variables for enemy ships record
ex,ey,espeed:real
onoff:boolean endrecord
var BadGuys:array1..eships of Enemy %Array for enemy ships
for i:1..eships %Assigning initial values of enemy ships
BadGuys(i).ex:=maxx%EnemyShip x-coordinate
BadGuys(i).ey:=Rand.Int(39,maxy-39)%EnemyShip y-coordinate
BadGuys(i).espeed:=Rand.Int(3,5)%EnemyShip speed
BadGuys(i).onoff:=false endfor
const shots:=10%Max # of bullets
type Bullet: %Record of variables for bullets record
x,y,speed:real
bullet:boolean endrecord
var Shoot:array1..shots of Bullet %Array for bullets
for a:1..shots %Assigning unitial values of bullets
Shoot(a).bullet:=false%Bullet is off("false"=off;"true"=on)
Shoot(a).speed:=5%Bullet speed endfor var chars:arraycharofboolean loop%Main loop %Drawing the Background Pic.Draw(background,-backx,0,picCopy) if backx=picx then
backx:=0 elsif backx+screenx>=picx then Pic.Draw(background,picx-backx,0,picCopy) endif
%Spawning the Bad Guys for e:1..eships
BadGuys(e).ex-=BadGuys(e).espeed
var num:int:=Rand.Int(1,3) if BadGuys(e).ex<=0then
BadGuys(e).ex:=maxx
BadGuys(e).ey:=Rand.Int(39,maxy-39)
BadGuys(e).espeed:=Rand.Int(3,5) if num not=3then
BadGuys(e).onoff:=true endif endif if BadGuys(e).onoff=truethen Pic.Draw(EnemyShip,round(BadGuys(e).ex),round(BadGuys(e).ey),picMerge) endif endfor
Input.KeyDown(chars) %Movement for the Hero's Ship if chars (KEY_UP_ARROW)and y+27<maxy then%Moving Up
y+=5 endif if chars (KEY_DOWN_ARROW)and y>0then%Moving Down
y-=5 endif if chars (KEY_RIGHT_ARROW)and x+68<maxx then%Moving Right
x+=5 endif if chars (KEY_LEFT_ARROW)and x>0then%Moving Left
x-=5 endif if chars (' ')then%Firing Bullets
count+=1 if count=shots+1then%Shot Limiter
count:=1 endif for check:1..shots
if check=count then%Assigns the values of each bullet, one at a time
Shoot(check).bullet:=true%Turns bullet on
Shoot(check).x:=x+68%Sets bullet's x-coordinate
Shoot(check).y:=y+8%Sets bullet's y-coordinate endif endfor endif for firing:1..shots %Fires the bullets, one at a time if Shoot(firing).bullet=truethen
Shoot(firing).x+=Shoot(firing).speed %Moves the bullet by the given speed if Shoot(firing).x>maxx then
Shoot(firing).bullet:=false%Turns the bullet off if it goes off the screen else Pic.Draw(bullet,round(Shoot(firing).x),round(Shoot(firing).y),picMerge)%Draws the bullet
endif for collision:1..eships
if((Shoot(firing).x+20>=BadGuys(collision).ex)and((Shoot(firing).y)<=BadGuys(collision).ey+39)and(Shoot(firing).y>=BadGuys(collision).ey))then
Shoot(firing).bullet:=false
BadGuys(collision).onoff:=false endif endfor endif endfor
Posted: Mon Aug 29, 2011 10:45 pm Post subject: Re: Shooting Multiple Shots
You'll want your array of bullets to be a flexible array. That way, you can increase or decrease the amount of bullets you keep track of depending on whether the bullet still exists.
Posted: Tue Aug 30, 2011 7:53 am Post subject: RE:Shooting Multiple Shots
I don't think that's strictly necessary [Gandalf]. Flexible arrays are slow. Better to calculate the number of bullets a given character can shoot in the time it takes for a bullet to move across the maximum distance across the screen. If it takes 3 seconds to cross the screen, and you fire one bullet every second, your array needs to be 3 cells (or 4, to be safe). Then you give every character its own bullet array, and you'll have access to as many bullets as you'll need.