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

Username:   Password: 
 RegisterRegister   
 Need help with my Tank Game
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
Dragoon




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




PostPosted: Fri May 23, 2003 3:23 pm   Post subject: (No subject)

rule of thumb. ALWAYS upload your external bitmaps.

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




PostPosted: Fri May 23, 2003 4:25 pm   Post subject: (No 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dragoon




PostPosted: Fri May 23, 2003 4:51 pm   Post subject: (No subject)

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




PostPosted: Fri May 23, 2003 6:03 pm   Post subject: (No 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?
Tony




PostPosted: Fri May 23, 2003 7:17 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dragoon




PostPosted: Sat May 24, 2003 6:52 am   Post subject: (No subject)

Below is the code i have right now.
Dragoon




PostPosted: Sat May 24, 2003 7:28 am   Post subject: (No 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




Tank.zip
 Description:

Download
 Filename:  Tank.zip
 Filesize:  12.03 KB
 Downloaded:  470 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat May 24, 2003 1:10 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dragoon




PostPosted: Sat May 24, 2003 3:16 pm   Post subject: (No subject)

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




PostPosted: Sat May 24, 2003 4:36 pm   Post subject: (No subject)

yes
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dragoon




PostPosted: Sat May 24, 2003 4:47 pm   Post subject: (No subject)

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




PostPosted: Sat May 24, 2003 4:56 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dragoon




PostPosted: Sat May 24, 2003 5:13 pm   Post subject: (No 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

Tony




PostPosted: Sat May 24, 2003 8:03 pm   Post subject: (No subject)

thats cuz you initialize your bulletFlag variable to true when you declear it. Make it false instead.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: