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

Username:   Password: 
 RegisterRegister   
 Shooting bullets
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
platky




PostPosted: Wed Feb 25, 2009 9:03 pm   Post subject: Shooting bullets

Im having a terrible problem with getting my shooting to work. So far there is a little box that shoots at me using a rand.int.
When the attack procedure is activatated the bullet is supposed to come out. The bullet moves with coding bullet_x-=25 which does move the bullet but not at a continuos speed. It moves but only in increments when the rand.int statement is true.
Anybody have any ideas on this. i will post code if anyone wants to see it...
Sponsor
Sponsor
Sponsor
sponsor
saltpro15




PostPosted: Wed Feb 25, 2009 9:06 pm   Post subject: RE:Shooting bullets

why use rand.int anyway? and posting the code would be helpful Very Happy
platky




PostPosted: Thu Feb 26, 2009 8:30 am   Post subject: Re: Shooting bullets

Heres the code

Turing:

View.Set("graphics:700;350,position:center;center,nobuttonbar,nocursor,title:Tank Man")

var x,y : int
var rotate:int
var velocity:int
const gravity:int:=1
var random:int

var dude : int
var arm :int
var arm2:array 0..35 of int

var title:int
var level1:int

var enter:string(1)

var health:int
var enemy_damage:int
var damage:int

%Background colour
colorback(green)


health:=100
velocity:=0
x:=100
rotate:=0
y:=0
var keys : array char of boolean

var enemy_x:int:=maxx-25
var enemy_y:int:=22


%character
dude := Pic.FileNew ("Dude.bmp")
arm := Pic.FileNew ("arm.bmp")

title:= Pic.FileNew ("title.jpg")
level1:=Pic.FileNew ("level1.bmp")

for rep:0..35
    arm2 (rep):= Pic.Rotate (arm,rep*10,Pic.Width (arm) div 2 -6,Pic.Height (arm) div 2+4)
end for
var temparm:= arm2 (0)
%Character Procedure
procedure tankman
    Pic.Draw(dude,x,y,picMerge)
    Pic.Draw(temparm,x+12,y+43,picMerge)
    Draw.Box(x,y,x+55,y+100,blue)
    Draw.Box(x,y,x+72,y+38,blue)
end tankman

proc enemy
    Draw.FillOval(enemy_x,enemy_y,3,3,white)
    Draw.FillBox (maxx-30,30,maxx-5,15,purple)
   
end enemy

proc enemy_attack

    enemy_x-=25


    if enemy_x <=x+72 and enemy_x >=x then
        health:=health-3
        enemy_x:=maxx-25
    end if
end enemy_attack

proc enemy_miss

end enemy_miss

procedure Health
health:=health-enemy_damage
if health <=0 then
    health:= 0
end if
end Health

loop
        Pic.Draw(title,0,0,picCopy)
        getch (enter)
        exit when ord(enter)=10
end loop

cls
setscreen("offscreenonly")

        loop
           
            Input.KeyDown (keys)
           
            if keys (KEY_UP_ARROW) and y<=10 then
                velocity:= 20
            end if
            if keys (KEY_RIGHT_ARROW) then
                x:=x+5
            end if
            if keys (KEY_LEFT_ARROW) then
                x:=x-5
            end if
            if keys (KEY_DOWN_ARROW) then
                y:=y-5
            end if
            if keys (KEY_ESC) then
                exit
            end if
            if keys ('a') then
                rotate-=1
                temparm :=arm2 (rotate mod 36)
            end if
            if keys ('d') then
                rotate+=1
                temparm:=arm2 (rotate mod 36)
            end if
            if keys ('p') then

            end if

            put " ":6,health,"/100"
               

           
           
           
            velocity -= gravity
            y+= velocity

           
            if y <10 then
                y:=10
                velocity:=0
            end if
           
            if y <10 then
                y:=10
                velocity:=0
            end if

            if x< -25 then
                x:=-25
            end if
           
            if x+72>=maxx then
                x:=maxx-72
            end if
           
           
           
            if x+72>300 and x+72<310 and y<90 then
                x:=228
            end if
            if y<=105 and x+72>310 and x<390 then
                y:=100
                if keys (KEY_UP_ARROW) and y=100 then
                velocity:= 20
            end if
            end if
            if x<400 and x>385 and y<90 then
                x:=400
            end if
           
            if health<=0 then
                put "GAME OVER"
                exit
            end if
           
            tankman
           

            random:= Rand.Int(1,5000)
            if random <=100 then
                enemy_attack
            else
                enemy_miss
            end if
                        enemy
           
           
            View.Update
            delay(18)
            cls
            Pic.Draw (level1,0,0,picCopy)
            Draw.Box (300,100,400,10, blue)



        end loop
S_Grimm




PostPosted: Thu Feb 26, 2009 12:12 pm   Post subject: RE:Shooting bullets

1) Post the source code and the [colour="red"]PICTURES[/colour] in a .zip file and post them as an attachment. I can't compile without them.

2)
code:

random:= Rand.Int(1,5000)
            if random <=100 then
                enemy_attack
            else
                enemy_miss
            end if


Should be in it's own procedure. call it something like IsAttacking.

3)
code:

random:= Rand.Int(1,5000)
            if random <=100 then
                enemy_attack
            else
                enemy_miss
            end if

View.Update
            delay(18)
            cls



The cls Deletes the Bullet from the screen. Use an array to track if bullets are on the screen and a procedure to move them

ie
code:


var bulletList : array 1..10 of boolean
var bulletX : array 1..10 of int
var bulletU : array 1..10 of int
var numBullet : int

procedure enemy_attack
for (int i = numBullet + 1; i <= numBullet + 1; i ++)
bulletX (i) := enemy_x
bulletY (i) := whatever your y position for bullets is

bulletList (i) := true
numBullet += 1
end for
end enemy_attack

procedure moveBullet
for (int i := 1; i <= numBullet; i++)
Draw Bullet (bulletX (i), bulletY (i))
bulletX += velocity of Bullets
end for
end moveBullet
platky




PostPosted: Thu Feb 26, 2009 2:19 pm   Post subject: RE:Shooting bullets

im getting a few errors:

Turing:

for (int i = numBullet + 1; i <= numBullet + 1; i ++)


says there is an expected ":" on int
this happens in both cases where it is written

another error is
Turing:

bulletX += bulletVelocity

it says it is the wrong type...



Game.rar
 Description:
Game

Download
 Filename:  Game.rar
 Filesize:  97.66 KB
 Downloaded:  78 Time(s)

DemonWasp




PostPosted: Thu Feb 26, 2009 7:04 pm   Post subject: RE:Shooting bullets

That's because
code:
for (int i = 1; i <= numBullet + 1; i ++)

is C, C++, Java or one of a bunch of related languages. The turing equivalent is:
code:
for i : 1 .. numBullet + 1


The other one is probably a type mismatch between integer (int) and floating-point (real). You can use the methods round, floor or ceil to convert from a real number to an integer, depending on what you want.

@AJ: Apparently the board only likes the US spelling - "color". Kinda stupid, but then it's probably more effort to change than its worth.
S_Grimm




PostPosted: Fri Feb 27, 2009 6:48 am   Post subject: RE:Shooting bullets

oops Smile my bad. I've been programming in Java for a long time now. Regardless, DemonWasp summed up to solution properly.


@DemonWasp: Its AV not AJ. Thanks tho. I wrote the code in the quick reply box, so i dind bother trying to fix it. my bad.
platky




PostPosted: Fri Feb 27, 2009 10:03 am   Post subject: Re: Shooting bullets

Thanks for the help the previous stuff is fixed but i now get a new error...

Turing:

View.Set("graphics:700;350,position:center;center,nobuttonbar,nocursor,title:Tank Man")

var x,y : int
var rotate:int
var velocity:int
var gravity:int:=1
var random:int

var dude : int
var arm :int
var arm2:array 0..35 of int

var title:int
var level1:int
var font1:int

var enter:string(1)

var health:int
var enemy_damage:int
var damage:int

var bulletList:array 1..10 of boolean
var bulletX:array 1..10 of int
var bulletY:array 1..10 of int
var bulletVeloc:int:=25
var numBullet:int:=1

%Background colour
colorback(green)


health:=100
velocity:=0
x:=100
rotate:=0
y:=0
font1:= Font.New ("arial black:12")
var keys : array char of boolean

var enemy_x:int:=maxx-25
var enemy_y:int:=22


%character
dude := Pic.FileNew ("Dude.bmp")
arm := Pic.FileNew ("arm.bmp")
title:= Pic.FileNew ("title.jpg")
level1:=Pic.FileNew ("level1.bmp")

for rep:0..35
    arm2 (rep):= Pic.Rotate (arm,rep*10,Pic.Width (arm) div 2 -6,Pic.Height (arm) div 2+4)
end for
var temparm:= arm2 (0)

%Character Procedure
procedure tankman
    Pic.Draw(dude,x,y,picMerge)
    Pic.Draw(temparm,x+12,y+43,picMerge)
    Draw.Box(x,y,x+55,y+100,blue)
    Draw.Box(x,y,x+72,y+38,blue)
end tankman

proc enemy
    Draw.FillBox (maxx-30,30,maxx-5,15,purple)
   
end enemy



proc moveBullet
for i:numBullet+1..x+72
Draw.FillOval (bulletX (i), bulletY (i),3,3,white)
bulletX(i) += bulletVeloc
end for
end moveBullet
proc enemy_attack

for  i:numBullet+1..x+72
bulletX (i):=enemy_x
bulletY(i):= enemy_y

bulletList (i) := true
numBullet +=1
moveBullet
end for
   
end enemy_attack
procedure Health
health:=health-enemy_damage
if health <=0 then
    health:= 0
end if
end Health

proc isattacking
    random:=Rand.Int(1,5000)
        if random <=100 then
            enemy_attack
        end if
end isattacking


loop
        Pic.Draw(title,0,0,picCopy)
        getch (enter)
        Input.KeyDown (keys)
        exit when ord(enter)=10
end loop

 
cls
setscreen("offscreenonly")

%The Game controls + boundaries etc...
        loop
           
            Input.KeyDown (keys)
           
            if keys (KEY_UP_ARROW) and y<=10 then
                velocity:= 20
            end if
            if keys (KEY_RIGHT_ARROW) then
                x:=x+5
            end if
            if keys (KEY_LEFT_ARROW) then
                x:=x-5
            end if
            if keys (KEY_DOWN_ARROW) then
                y:=y-5
            end if
            if keys (KEY_ESC) then
                exit
            end if
            if keys ('a') then
                rotate-=1
                temparm :=arm2 (rotate mod 36)
            end if
            if keys ('d') then
                rotate+=1
                temparm:=arm2 (rotate mod 36)
            end if
            if keys ('p') then

            end if
           
            velocity -= gravity
            y+= velocity
           
           
            locate (1,7)
            Text.Color(31)
            put health
               


           
            if y <10 then
                y:=10
                velocity:=0
            end if
           
            if y <10 then
                y:=10
                velocity:=0
            end if

            if x< -25 then
                x:=-25
            end if
           
            if x+72>=maxx then
                x:=maxx-72
            end if
           
            %Health Check
            if health<=0 then
                put "GAME OVER"
                exit
            end if
           
            %Boundaries
           
           
           
           

           

           
            if y<=118 and x+72>340 and x<440 then
                y:=119
                if keys (KEY_UP_ARROW) and y=119 then
                velocity:= 20
                end if
            end if
           
           
            tankman
           
            enemy
            isattacking
           
            View.Update
            delay(18)
            cls
            Pic.Draw (level1,0,0,picCopy)
            Draw.Box (0,218,240,200, blue)
            Draw.Box (340,118,440,10,blue)



        end loop


The error is with the Draw.FIllOval for the bullet. IT says the variable has no value. I have no idea what variable?
Sponsor
Sponsor
Sponsor
sponsor
S_Grimm




PostPosted: Fri Feb 27, 2009 11:53 am   Post subject: RE:Shooting bullets

Ok, i tried to write code myself, based on how i would write it in Java. So, what you have to do is modify it to Turing.

The error is either the bulletX(i) or bulletY (i)

Edit: Use flexible arrays to keep track of the bullets. then you can use "upper" for the bullets. Once i can use my personal computer, ill post a modified copy of your code using flexible arrays to initalize the bulletList.
platky




PostPosted: Sat Feb 28, 2009 1:54 pm   Post subject: RE:Shooting bullets

i still cant seem to fix this problem. Every time i change sumthing a new problem arises...

I also cant figure out how to get my guy to change the direction he is facing according to what button is pressed
S_Grimm




PostPosted: Sun Mar 01, 2009 9:02 pm   Post subject: RE:Shooting bullets

To change his direction, just flip the image in paint or photoshop and then import it as a second sprite. Then, have a boolean state so when left arrow is pressed it draws left (ie facingLeft = true) or vise versa
platky




PostPosted: Tue Mar 03, 2009 10:44 am   Post subject: Re: Shooting bullets

Instead of making my enemy shoot i decided to work on making my character shoot. The problem is that when i use cls and view.update the screen gets messed up. When i dont the bullet is a continuos beam... any ideas?

Turing:

setscreen ("graphics")

const UFO_HEIGHT := 150
const UFO_WIDTH := 130
const TANK_SPEED := 4
var ufo, tank : int
var ufo_y : int
var move_x : int := 0
var key : array char of boolean

drawfillarc (100, 80, 20, 10, 180, 0, 41)
drawfilloval (100, 90, 50, 15, 48)
drawfillarc (100, 100, 30, 30, 0, 180, 74)
ufo := Pic.New (UFO_HEIGHT - 100, UFO_WIDTH - 60, UFO_HEIGHT, UFO_WIDTH)

drawfillbox (0, 0, 50, 40, green)
drawfillbox (20, 40, 30, 70, green)
tank := Pic.New (0, 0, 50, 70)
cls

proc tank_move
Input.KeyDown (key)
if key (KEY_LEFT_ARROW) and move_x > 0 then
move_x -= TANK_SPEED
elsif key (KEY_RIGHT_ARROW) and move_x + 50 < maxx then
move_x += TANK_SPEED
end if
Pic.Draw (tank, move_x, 0, picMerge)
end tank_move
var bullet_y : int := 0
var bullet_fired := false
loop
setscreen ("offscreenonly")
for ufo_x : 0 .. maxx by 3
Pic.Draw (ufo, ufo_x, 300, picMerge)
tank_move
Input.KeyDown (key)
if key (KEY_ENTER) and not bullet_fired then
bullet_y := 70
bullet_fired := true

end if
if bullet_fired then
drawfilloval (move_x + 25, bullet_y, 10, 10, 41)
bullet_y += 3 %bullet speed
if bullet_y > maxy then
bullet_fired := false
end if

end if
View.Update
cls
end for
end loop

S_Grimm




PostPosted: Wed Mar 04, 2009 12:42 pm   Post subject: Re: Shooting bullets

Turing:

loop
setscreen ("offscreenonly")
for ufo_x : 0 .. maxx by 3
Pic.Draw (ufo, ufo_x, 300, picMerge)
tank_move
Input.KeyDown (key)
if key (KEY_ENTER) and not bullet_fired then
bullet_y := 70
bullet_fired := true

end if
if bullet_fired then
drawfilloval (move_x + 25, bullet_y, 10, 10, 41)
bullet_y += 3 %bullet speed
if bullet_y > maxy then
bullet_fired := false
end if

end if
View.Update
cls
end for
end loop


Error #1

code:

loop
setscreen ("offscreenonly")



Error #2

Your if statements inside the loop

Use procedures.

Also Take the View.Update and cls out of the for loop

code:

procedure movebullet
if bullet_fired then
drawfilloval (move_x + 25, bullet_y, 10, 10, 41)
bullet_y += 3 %bullet speed
if bullet_y > maxy then
bullet_fired := false
end if
end moveBullet


...........

loop
for
blah blah blah
moveBullet
end for

View.Update
cls
end loop
platky




PostPosted: Wed Mar 04, 2009 1:37 pm   Post subject: Re: Shooting bullets

I just noticed i made a huge mistake and copied someone elses code into it that i was looking at. Here is my actually code:

Turing:

View.Set("graphics:700;350,position:center;center,nobuttonbar,nocursor,title:Tank Man")
var x,y : int
var rotate:int
var velocity:int
var gravity:int:=1
var random:int
var time1:real

var dude : int
var dude_left:int
var arm :int
var arm2:array 0..35 of int
var arm_left:int
var arm_left2:array 0..35 of int
var arm_leftsolid:int
var tankman_right:boolean:=true

var wep:int:=3

var title:int
var level1:int
var font1:int

var enter:string(1)

var health:int
var enemy_damage:int
var damage:int

var bulletList:array 1..10 of boolean
var bulletX:int
var bulletY:int
var bulletVeloc:int:=10
var numBullet:int:=1


var bull_y:int

%Background colour
colorback(green)


health:=100
velocity:=0
x:=100
rotate:=0
y:=0
font1:= Font.New ("arial black:12")
var keys : array char of boolean

var enemy_x:int:=maxx-25
var enemy_y:int:=22
%Sprite Testing----
var numframes:int:=Pic.Frames ("zerosheet.gif")
var pics:array 1..numframes of int
var delaytime:int
Pic.FileNewFrames ("zerosheet.gif",pics,delaytime)
var sprite:int
sprite:=Sprite.New(pics(1))
Sprite.SetPosition (sprite,200,10,false)



%character
dude := Pic.FileNew ("Dude.bmp")
dude_left:=Pic.FileNew("Dude2.bmp")
arm := Pic.FileNew ("arm.bmp")
arm_left:=Pic.FileNew("arm_left.bmp")
title:= Pic.FileNew ("title.jpg")
level1:=Pic.FileNew ("level1.bmp")

for rep:0..35
    arm2 (rep):= Pic.Rotate (arm,rep*10,Pic.Width (arm) div 2 -6,Pic.Height (arm) div 2+4)
end for
var temparm:= arm2 (0)
for rep:0..35
    arm_left2 (rep):=Pic.Rotate (arm_left,rep*10,Pic.Width(arm_left) div 2+6,Pic.Height (arm_left)div 2+4)
end for
var temparm_left:=arm_left2(0)

arm_leftsolid:=Pic.Rotate (arm_left,45,Pic.Width(arm_left) div 2+6,Pic.Height (arm_left)div 2+4)
%Character Procedure
procedure tankman
    if tankman_right=true then
        Pic.Draw(dude,x,y,picMerge)
        Pic.Draw(temparm,x+12,y+43,picMerge)
        %Draw.Box(x,y,x+55,y+100,blue)
        %Draw.Box(x,y,x+72,y+38,blue)
    elsif tankman_right=false then
        Pic.Draw(temparm_left,x+12,y+43,picMerge)
        Pic.Draw(dude_left,x,y,picMerge)
        Pic.Draw(arm_leftsolid,x+12,y+43,picMerge)

    end if
end tankman


%Enemy Stuff --------------------------------
%--------------------------------------------
proc enemy
    Draw.FillBox (maxx-30,30,maxx-5,15,purple)
end enemy


proc moveBullet
for i:1..numBullet+1
Draw.FillOval (bulletX, bulletY,3,3,white)
bulletX-= bulletVeloc
end for
end moveBullet

proc enemy_attack
for  i:1..numBullet+1
bulletX :=enemy_x
bulletY:= enemy_y
bulletList (i) := true
numBullet +=1
moveBullet
end for
end enemy_attack

proc isattacking
    random:=Rand.Int(1,5000)
        if random <=0 then
            enemy_attack
        end if
end isattacking
%-------------------------------
%-------------------------------

procedure shoot
    for bull_x:x+72..maxx
        bull_y:=y+60
        Draw.FillOval (bull_x,bull_y,3,3,yellow)
        View.Update
        cls
    end for
end shoot

procedure Health
health:=health-enemy_damage
if health <=0 then
    health:= 0
end if
end Health

%Weapon Stuff
proc weapon
    if wep=1 then
        put"Pistol"
    elsif wep=2 then
        put"Semi-Automatic"
    elsif wep=3 then
        put"Shotgun"
    end if
end weapon

proc pistol
    damage:=25
    Time.Delay(1500)
end pistol

proc semi
    damage:=15
    Time.Delay(500)
end semi

proc shotgun
    damage:=50
    Time.Delay(2250)
end shotgun
loop
        Pic.Draw(title,0,0,picCopy)
        getch (enter)
        exit when ord(enter)=10
end loop

 
cls
setscreen("offscreenonly")

%The Game controls + boundaries etc...
        loop
            time1:=round(Time.Elapsed*0.001*100)/100
           
            Input.KeyDown (keys)
           
            if keys (KEY_UP_ARROW) and y<=10 then
                velocity:= 18
            end if
            if keys (KEY_RIGHT_ARROW) then
                x:=x+5
                if tankman_right=false then
                    tankman_right:=true
                end if
            end if
            if keys (KEY_LEFT_ARROW) then
                x:=x-5
                if tankman_right=true then
                    tankman_right:=false
                end if
            end if
            if keys (KEY_DOWN_ARROW) then
                y:=y-5
            end if
            if keys (KEY_ESC) then
                exit
            end if
            if keys ('a') then
                rotate-=1
                if tankman_right=true then
                    temparm :=arm2 (rotate mod 36)
                else
                    temparm_left:=arm_left2 (rotate mod 36)
                end if
            end if
            if keys ('d') then
                rotate+=1
                if tankman_right=true then
                    temparm:=arm2 (rotate mod 36)
                else
                   temparm_left:=arm_left2 (rotate mod 36)
                end if
            end if
            if keys ('p') then
            end if
            if keys ('s') then
                shoot
            end if
           
            velocity -= gravity
            y+= velocity
           
           
            locate (1,7)
            Text.Color(31)
            put health..
            put " ":10,"WEP: "..
            weapon
            locate (1,50)
            put "TIME (SEC): ",time1


           
            if y <10 then
                y:=10
                velocity:=0
            end if
           
            if y <10 then
                y:=10
                velocity:=0
            end if

            if x< -25 then
                x:=-25
            end if
           
            if x+72>=maxx then
                x:=maxx-72
            end if
           
            %Health Check
            if health<=0 then
                health:=0
                put "GAME OVER"
                exit
            end if
           
            %Boundaries
           
           
               
            if y<=118 and y>=85 and x+72>=340 and x <=440 then
               
                y:=119
                velocity:=0
                if keys (KEY_UP_ARROW) and y=119 then
                velocity:= 18
                end if
            end if
           
            if y<=218 and y>=199 and x<=240 then
                y:=219
                velocity:=0
                if keys (KEY_UP_ARROW) and y=219 then
                velocity:= 18
                end if
            end if
            if y<=208 and y>=185 and x+72>=447 then
                y:=209
                velocity:=0
                if keys (KEY_UP_ARROW) and y=209 then
                velocity:= 18
                end if
            end if
           
            if x+72>=maxx-30 then
                health-=3
            end if
            tankman
           
            enemy
            isattacking
           
            View.Update
            delay(18)
            cls
            Pic.Draw (level1,0,0,picCopy)
           
            Sprite.Show (sprite)



        end loop


sorry about that
S_Grimm




PostPosted: Thu Mar 05, 2009 12:50 am   Post subject: RE:Shooting bullets

try
code:

            View.Update
            delay(18)
            cls
            Pic.Draw (level1,0,0,picCopy)
           
            Sprite.Show (sprite)


to

code:

loop
            View.Update
            cls

               %rest of code
               ...........
               ...........
            delay(18)
            Pic.Draw (level1,0,0,picCopy)
            Sprite.Show (sprite)
end code
           
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  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: