Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Shooting Multiple Shots
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chaos




PostPosted: 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
Sponsor
sponsor
crossley7




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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:=maxy div 2

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
end record

var Shoot:array 1..10 of Bullet

for a:1..10
    Shoot(a).bullet:=false
end for
var chars:array char of boolean
loop   
Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) and y+27<maxy then
        y+=1           
    end if
    if chars (KEY_DOWN_ARROW) and y>0 then
        y-=1
    end if
    if chars (' ') then
        count+=1
        if count=11 then
            count:=1
        end if
        for bulletcheck:1..10
            if bulletcheck=count then
                Shoot(bulletcheck).bullet:=true
            end if
            if Shoot(bulletcheck).bullet=true then
                Shoot(bulletcheck).x:=67
                Shoot(bulletcheck).y:=y+8
            end if
            Pic.Draw(bullet,round(Shoot(bulletcheck).x),round(Shoot(bulletcheck).y),picCopy)
            Shoot(bulletcheck).x+=5
        end for
       
       
       
    end if
   
   
    Pic.Draw(ship,x,y,picCopy)
    View.Update
    delay(10)
    cls
end loop




Bullet.bmp
 Description:
 Filesize:  6.35 KB
 Viewed:  6859 Time(s)

Bullet.bmp



SpaceShip.bmp
 Description:
 Filesize:  23.35 KB
 Viewed:  196 Time(s)

SpaceShip.bmp


Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: 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




PostPosted: 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:=maxy div 2

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
end record

var Shoot:array 1..10 of Bullet

for a:1..10
    Shoot(a).bullet:=false
    Shoot(a).speed:=5
end for
var chars:array char of boolean
loop   
Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) and y+27<maxy then
        y+=1           
    end if
    if chars (KEY_DOWN_ARROW) and y>0 then
        y-=1
    end if
    if chars (' ') then
        count+=1
        if count=11 then
            count:=1
        end if
        for check:1..10
            if check=count then
                Shoot(check).bullet:=true
                Shoot(check).x:=67
                Shoot(check).y:=y+8
            end if
        end for   
    end if
    for firing:1..10
        if Shoot(firing).bullet=true then
            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)
            end if
        end if
    end for
   
   
    Pic.Draw(ship,x,y,picCopy)
    View.Update
    delay(50)
    cls
end loop

mirhagk




PostPosted: Wed Aug 17, 2011 12:10 pm   Post subject: RE:Shooting Multiple Shots

I might be confused as to what you want, but couldn't you just decrease the speed?
Raknarg




PostPosted: Sat Aug 27, 2011 7:43 pm   Post subject: Re: Shooting Multiple Shots

Turing:

for check:1..10
     if check=count then
          Shoot(check).bullet:=true
          Shoot(check).x:=67
          Shoot(check).y:=y+8
     end if
end for


This xan be shortened to:
Turing:

Shoot(count).bullet:=true
Shoot(count).x:=67
Shoot(count).y:=y+8

since that's whats going to happen anyways.

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




PostPosted: 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:=maxy div 2

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
    end record

var BadGuys:array 1..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
end for

const shots:=10 %Max # of bullets

type Bullet:    %Record of variables for bullets
    record
        x,y,speed:real
        bullet:boolean
    end record

var Shoot:array 1..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
end for
var chars:array char of boolean
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)
    end if
   
    %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<=0 then
            BadGuys(e).ex:=maxx
            BadGuys(e).ey:=Rand.Int(39,maxy-39)
            BadGuys(e).espeed:=Rand.Int(3,5)
            if num not=3 then
                BadGuys(e).onoff:=true
            end if
        end if
        if BadGuys(e).onoff=true then
            Pic.Draw(EnemyShip,round(BadGuys(e).ex),round(BadGuys(e).ey),picMerge)
        end if
    end for   



   
    Input.KeyDown (chars)
    %Movement for the Hero's Ship
    if chars (KEY_UP_ARROW) and y+27<maxy then    %Moving Up
        y+=5       
    end if
    if chars (KEY_DOWN_ARROW) and y>0 then        %Moving Down
        y-=5
    end if
    if chars (KEY_RIGHT_ARROW) and x+68<maxx then %Moving Right
        x+=5
    end if
    if chars (KEY_LEFT_ARROW) and x>0 then        %Moving Left
        x-=5
    end if
    if chars (' ') then                           %Firing Bullets
        count+=1
        if count=shots+1 then %Shot Limiter
            count:=1
        end if
        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
            end if
        end for   
    end if
    for firing:1..shots             %Fires the bullets, one at a time
        if Shoot(firing).bullet=true then
            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
               
            end if
            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
                end if
            end for
        end if
    end for

   
    Pic.Draw(ship,x,y,picMerge)     %Draws Hero's Ship
    View.Update                     %For fluent animation
    backx+=5                        %Moves background
    delay(50)                   
    cls                             %Clears screen
end loop




EnemyShip.bmp
 Description:
 Filesize:  8.89 KB
 Viewed:  6608 Time(s)

EnemyShip.bmp


[Gandalf]




PostPosted: 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.

You can find information on flexible arrays in Part III of the Arrays tutorial from the Turing Walkthrough.
Insectoid




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: