Computer Science Canada

Need help with my Tank Game

Author:  Dragoon [ Fri May 23, 2003 2:03 pm ]
Post subject:  Need help with my Tank Game

I having trouble trying to get my tank to move while shooting.

code:
setscreen ("graphics:800;500")
setscreen ("nocursor")
var Bullet: int
var Game : int
var Tank : int
var x : int := 0
var reply : string (1)
var Tank2 : int
Game := Pic.FileNew ("Game2.bmp") %Locates the picture
Pic.Draw (Game, 0, 0, 2) %Draws the picture
Tank := Pic.FileNew ("Tank.bmp") %Locates the picture
Pic.Draw (Tank, 320, -230, 2) %Draws the picture
Bullet := Pic.FileNew ("bullet.bmp") %Locates the picture
Tank2 := Pic.FileNew ("Tank2.bmp") %Locates the picture
Pic.Draw (Tank2, 240, 220, 2) %Draws the picture

loop
    getch (reply)
    if reply = "d" then
    Pic.Draw (Game, 0, 0, 2)
    Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
    Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
    x := x + 12
    end if
    if reply = "a" then
    Pic.Draw (Game, 0, 0, 2)
    Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
    Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
    x:= x - 12
    end if
    if reply = "x" then
    for i: 1..400
    Pic.Draw (Bullet, 80 + x, -170 + i, 2) %Draws the picture
    end for
    end if
    if x = 0 then
    Pic.Draw (Game, 0, 0, 2)
    Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
    end if
end loop


By the way a is to move left, d is to move right and x is to fire.

Author:  Asok [ Fri May 23, 2003 3:23 pm ]
Post subject: 

rule of thumb. ALWAYS upload your external bitmaps.

just zip them up and attach them so we can actually run this.

Author:  Tony [ Fri May 23, 2003 4:25 pm ]
Post subject: 

if might help if you'd use Input.KeyDown instead... read tutorial... everybody who knows how to use that recomments it over getch...

now, you should make a separate procedure for the bullet that will move it along its trajectory. Then set a flag variable in the main loop so that the procedure keeps on getting called to move the bullet while its on screen. This way it moves bullet 1 spot, then returns to tank controls to allow you to move it as well..

are you with me so far?

Author:  Dragoon [ Fri May 23, 2003 4:51 pm ]
Post subject: 

How would i use Input.KeyRight in my code? Can you give me an example please.

Author:  Dragoon [ Fri May 23, 2003 6:03 pm ]
Post subject: 

Okay , i got everything to work except for the Bullet, i made a procedure and now how do i have it so the bullet moves while the tank moves too?

Author:  Tony [ Fri May 23, 2003 7:17 pm ]
Post subject: 

structure it like this:

procedure moveBullet
bulletX += movementX
bulletY += movementY
%maye some other stuff like check if it hit something or what not
%reset bulletFlag back to flase once bullet is done moving
end moveBullet

%main loop
loop
input.ketdown stuff
if chars('f') then
%or whatever button you want to fire with
bulletFlag = true
end if

if bulletFlag then
moveBullet
end if

move tank here
end loop

/\Something like that above

Author:  Dragoon [ Sat May 24, 2003 6:52 am ]
Post subject: 

Below is the code i have right now.

Author:  Dragoon [ Sat May 24, 2003 7:28 am ]
Post subject: 

Okay, iam having trouble understanding what you just put, what do i have to do with the bullety and x stuff in the procedure? Also do i have my Draw Bullet in the right spot?

code:
setscreen ("graphics:800;500")
setscreen ("nocursor")
var Bullet : int
var Game : int
var Tank : int
var x : int := 0
var reply : string (1)
var Tank2 : int
var bulletX : int
var movementX : int
var movementY : int
var bulletY : int
var bulletflag : boolean := true
Game := Pic.FileNew ("Game2.bmp") %Locates the picture
Pic.Draw (Game, 0, 0, 2) %Draws the picture
Tank := Pic.FileNew ("Tank.bmp") %Locates the picture
Pic.Draw (Tank, 320, -230, 2) %Draws the picture
Bullet := Pic.FileNew ("bullet.bmp") %Locates the picture
Tank2 := Pic.FileNew ("Tank2.bmp") %Locates the picture
Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
var chars : array char of boolean


procedure moveBullet
    bulletX += movementX
    bulletY += movementY
    Pic.Draw (Bullet, 80 + x, -170, 2)  %Draws the picture
    bulletflag := false
end moveBullet

loop
    Input.KeyDown (chars)
    if chars (KEY_ENTER) then
        bulletflag := true
    end if
    if bulletflag then
        moveBullet
    end if
    if chars (KEY_RIGHT_ARROW) then
        Pic.Draw (Game, 0, 0, 2)
        Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
        Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
        x := x + 12
    end if
    if chars (KEY_LEFT_ARROW) then
        Pic.Draw (Game, 0, 0, 2)
        Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
        Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
        x := x - 12
    end if
end loop


Author:  Tony [ Sat May 24, 2003 1:10 pm ]
Post subject: 

your procedure should look like this:

code:

procedure moveBullet
    bulletX += movementX
    bulletY += movementY
    Pic.Draw (Bullet, bulletX, bulletY, 2)  %Draws the picture

if bulletY>maxy then
    bulletflag := false
end if

end moveBullet


so the flag is set to false, only when the bullet goes over the top of the screen.

I think your movement variables should be procedure parameters, because otherwise if you turn your tank around, the bullet will turn too.

Author:  Dragoon [ Sat May 24, 2003 3:16 pm ]
Post subject: 

Do i have to declare the BulletX, BulletY, MovementX, MovementY anything?

Author:  Tony [ Sat May 24, 2003 4:36 pm ]
Post subject: 

yes

Author:  Dragoon [ Sat May 24, 2003 4:47 pm ]
Post subject: 

What would i delcare them? Sorry for asking too many questions.

Author:  Tony [ Sat May 24, 2003 4:56 pm ]
Post subject: 

bulletX/Y is a location of the bullet
movementX/Y is vector components of bullet's trajectory. You add them to bulletX/Y to move it.

tankX/Y is location of your tank. When bullet if first fired, bulletX/Y = tankX/Y...

tankMovementX/Y also vector components, store tank movement direction. When bullet is first fired, movementX/Y = tankMovementX/Y

you can declear them as integers, but if you want more accurate numbers, use real instead and just round for drawing.

Author:  Dragoon [ Sat May 24, 2003 5:13 pm ]
Post subject: 

Okay, i understand it now, but whenever i start my program the bullet fires. This is what i have.
code:
setscreen ("graphics:800;500")
setscreen ("nocursor")
var Bullet : int
var Game : int
var Tank : int
var x : int := 0
var y : int := 1
var reply : string (1)
var Tank2 : int
var bulletX : int := 80
var movementX : int := 0
var movementY : int := 1
var bulletY : int := -170
var bulletflag : boolean := true
Game := Pic.FileNew ("Game2.bmp") %Locates the picture
Pic.Draw (Game, 0, 0, 2) %Draws the picture
Tank := Pic.FileNew ("Tank.bmp") %Locates the picture
Pic.Draw (Tank, 320, -230, 2) %Draws the picture
Bullet := Pic.FileNew ("bullet.bmp") %Locates the picture
Tank2 := Pic.FileNew ("Tank2.bmp") %Locates the picture
Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
var chars : array char of boolean


procedure moveBullet
    bulletX += movementX
    bulletY += movementY
    Pic.Draw (Bullet, bulletX, bulletY, 2)  %Draws the picture

if bulletY>maxy then
bulletflag := false
end if

end moveBullet


loop
    Input.KeyDown (chars)
    if chars (KEY_ENTER) then
        bulletflag := true
    end if
    if bulletflag = true then
    moveBullet
    end if
    if chars (KEY_RIGHT_ARROW) then
        Pic.Draw (Game, 0, 0, 2)
        Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
        Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
        x := x + 12
    end if
    if chars (KEY_LEFT_ARROW) then
        Pic.Draw (Game, 0, 0, 2)
        Pic.Draw (Tank, 320 + x, -230, 2) %Draws the picture
        Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
        x := x - 12
    end if
   
    end loop


Author:  Tony [ Sat May 24, 2003 8:03 pm ]
Post subject: 

thats cuz you initialize your bulletFlag variable to true when you declear it. Make it false instead.

Author:  Dragoon [ Sun May 25, 2003 6:51 am ]
Post subject: 

Okay, thank you.

Author:  Dragoon [ Sun May 25, 2003 7:21 am ]
Post subject: 

I have one more question. How do i get my bullet to start where my tank is? Because each time i fire it fires where my tank started which is in the middle.

Author:  Tony [ Sun May 25, 2003 11:05 am ]
Post subject: 

bulletX = tank's Curren Position X
bulletY = tank's Curren Position Y

but you do that only when the bullet is just fired

Author:  Dragoon [ Sun May 25, 2003 11:22 am ]
Post subject: 

Do i have to do bulletX += TankX and bulletY += TankY? And i tried putting it under my Pic.Draw Bullet but nothing worked.

code:
setscreen ("graphics:800;500")
setscreen ("nocursor")
var Bullet : int
var Game : int
var Tank : int
var Tankx : int := 320
var Tanky : int := -230
var x : int := 0
var y : int := 1
var Tank2 : int
var bulletX : int := 80
var movementX : int := 0
var movementY : int := 1
var bulletY : int := -170
var bulletflag : boolean := false
var chars : array char of boolean

Game := Pic.FileNew ("Game2.bmp") %Locates the picture
Pic.Draw (Game, 0, 0, 2) %Draws the picture
Tank := Pic.FileNew ("Tank.bmp") %Locates the picture
Pic.Draw (Tank, Tankx, Tanky, 2) %Draws the picture
Bullet := Pic.FileNew ("bullet.bmp") %Locates the picture
Tank2 := Pic.FileNew ("Tank2.bmp") %Locates the picture
Pic.Draw (Tank2, 240, 220, 2) %Draws the picture


procedure moveBullet
    bulletX += movementX
    bulletY += movementY
    Pic.Draw (Bullet, bulletX, bulletY, 2)  %Draws the picture
    if bulletY >= 220 then
        bulletflag := false
        cls
        locate (20, 10)
        put "YOU WON!"
    end if

end moveBullet


loop
    Input.KeyDown (chars)
    if chars (KEY_ENTER) then
        bulletflag := true
    end if
    if bulletflag = true then
        moveBullet
    end if
    if chars (KEY_RIGHT_ARROW) then
        Pic.Draw (Game, 0, 0, 2)
        Pic.Draw (Tank, Tankx + x, Tanky, 2) %Draws the picture
        Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
        x := x + 12


    end if
    if chars (KEY_LEFT_ARROW) then
        Pic.Draw (Game, 0, 0, 2)
        Pic.Draw (Tank, Tankx + x, Tanky, 2) %Draws the picture
        Pic.Draw (Tank2, 240, 220, 2) %Draws the picture
        x := x - 12

    end if

end loop


Author:  Tony [ Sun May 25, 2003 11:24 am ]
Post subject: 

no...

code:

if chars (KEY_ENTER) then
        bulletflag := true
        bulletX = tankX
        bulletY = tankY
end if


: