
-----------------------------------
Johnny19931993
Tue Dec 30, 2008 3:14 pm

Need help regaring shooting(Urgent)
-----------------------------------
I wrote a game using Turing
A UFO is moving across the screen
A tank is moved by left and right arrow and fire is shot using enter key
The purpose is to shoot at the UFO
but when I shoot, the UFO stopped
I was stuck for a long time and I don't know how to solve this problem
Can anyone help me

This is the program:

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

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) then
            for bullet_y : 70 .. maxy by 10
                drawfilloval (move_x + 25, bullet_y, 10, 10, 41)
                Pic.Draw (ufo, ufo_x, 300, picMerge)
                tank_move
                View.Update
                cls
            end for
        end if
        View.Update
        cls 
    end for 
end loop

-----------------------------------
Tony
Tue Dec 30, 2008 4:00 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
can you describe what is happening inside this loop? Make sure to mention when it starts and how long it takes.


if key (KEY_ENTER) then
            for bullet_y : 70 .. maxy by 10
                drawfilloval (move_x + 25, bullet_y, 10, 10, 41)
                Pic.Draw (ufo, ufo_x, 300, picMerge)
                tank_move
                View.Update
                cls
            end for
        end if


-----------------------------------
Johnny19931993
Tue Dec 30, 2008 6:04 pm

Re: Need help regaring shooting(Urgent)
-----------------------------------
if key (KEY_ENTER) then  %The output when the user press the enter key
            for bullet_y : 70 .. maxy by 10   %The path and the speed of the bullet
                drawfilloval (move_x + 25, bullet_y, 10, 10, 41) %Draw the bullet
                Pic.Draw (ufo, ufo_x, 300, picMerge)  %Draw the UFO
                tank_move  %Draw and control the tank
                View.Update 
                cls 
            end for 
        end if 

-----------------------------------
Tony
Tue Dec 30, 2008 7:46 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
Let me try to rephrase the questions.

Inside that loop, how many times will the UFO be drawn?
List all position(s) (X/Y center) of the UFO, while inside that loop.

-----------------------------------
Johnny19931993
Tue Dec 30, 2008 8:03 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
Oh..I get it...
I only drew the UFO once in this loop
And it's at the same position repeating until the fire is gone
So how can I fix this problem?

-----------------------------------
Tony
Tue Dec 30, 2008 9:49 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
Good.

Now the problem has to do with the fact that you have multiple loops nested, and they are doing different things. The fact that you use tank_move in multiple places should be a clue that the design of your game loop is likely not ideal.

Keep in mind what a for loop really is.

for i : 1 .. 10
end for

is the same as

var i : int := 1
loop
   exit when i = 10
   i += 1
end loop


You already have a loop, so you can take out the for-loops inside, and rewrite them to use tank_move just once (and only one View.Update, and only one cls).

Also, using setscreen ("offscreenonly") inside a loop is kind of silly. Setting it once is enough.

-----------------------------------
Johnny19931993
Wed Dec 31, 2008 10:54 am

RE:Need help regaring shooting(Urgent)
-----------------------------------
Oh...I see
Thanks

-----------------------------------
Johnny19931993
Wed Dec 31, 2008 1:19 pm

Re: Need help regaring shooting(Urgent)
-----------------------------------
I changed my game and this is the result
but the shooting seems it bit wierd
It requires the player to press and hold the enter key while shooting
and when I release the enter key and press it again
it starts off again at the place where it disappears
How can I fix this problem? 


setscreen ("graphics,offscreenonly")

const UFO_HEIGHT := 150
const UFO_WIDTH := 130
const TANK_SPEED := 7
const UFO_SPEED := 5
var ufo, tank : int
var ufo_x : int := 0
var ufo_y : int 
var move_x : int := maxx div 2 - 25
var bullet_y : int := 70
var any_key : string (1)
var key : array char of boolean

drawfillarc (100, 80, 20, 10, 180, 0, 40)
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

randint (ufo_y, maxy div 2, maxy - UFO_HEIGHT)
proc ufo_move
    ufo_x += UFO_SPEED
    Pic.Draw (ufo, ufo_x, ufo_y, picMerge)
    if ufo_x = (maxx div UFO_SPEED) * UFO_SPEED then
        ufo_x := 0
        randint (ufo_y, maxy div 2, maxy - UFO_HEIGHT)
    end if
end ufo_move

proc shoot
    Input.KeyDown (key)
    if key (KEY_ENTER) then
        bullet_y += 10
        drawfilloval (move_x + 25, bullet_y, 10, 10, 41)
        if Math.Distance (ufo_x + 50, ufo_y + 50, move_x + 25, bullet_y) < 60 then
            ufo_x := 0
            randint (ufo_y, maxy div 2, maxy - UFO_HEIGHT)
            bullet_y := 70
        elsif bullet_y = (maxy div 10) * 10 then
            bullet_y := 70
        end if
    end if
end shoot

loop
    tank_move
    shoot
    ufo_move
    View.Update
    cls
end loop 

-----------------------------------
Tony
Wed Dec 31, 2008 2:05 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
This looks much better!

The problem with shooting now, is that everything is inside the if key (KEY_ENTER) then block. You could use a variable to keep track if the key has been previously pressed.


var bullet_on_screen : boolean := false

Input.KeyDown (key)
if key (KEY_ENTER) then
   bullet_on_screen = true
end if

if bullet_on_screen then
   bullet_y += 10
   drawfilloval (move_x + 25, bullet_y, 10, 10, 41) 
   ...


You'd have to reset the flag back to false when you don't want the bullet to fly.

Also, why is the same variable used for both the bullet's and tank's X location?

-----------------------------------
Johnny19931993
Wed Dec 31, 2008 2:40 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
Yes, that's another problem too
I want the bullet to be shot at the place where the tank is
But i can't do it except making the x value of both the bullet and the tank the same

-----------------------------------
Tony
Wed Dec 31, 2008 2:50 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
you could use a different variable. bullet_x perhaps?

-----------------------------------
Johnny19931993
Wed Dec 31, 2008 2:54 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
But what value does bullet_x has
If it's the same as tank_x, then it won't make any difference
How can I cut it's connection with tank_x after it's shot?

-----------------------------------
Tony
Wed Dec 31, 2008 3:10 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------

tank_x = 10
bullet_x = tank_x
tank_x = 15

what is the value of tank_x and bullet_x?

-----------------------------------
Johnny19931993
Wed Dec 31, 2008 4:10 pm

RE:Need help regaring shooting(Urgent)
-----------------------------------
I see...
But because of the loop, the x value of the tank keep changing every time it loops
So the x value of the bullet keep changing too...

-----------------------------------
Homer_simpson
Wed Dec 31, 2008 4:13 pm

Re: Need help regaring shooting(Urgent)
-----------------------------------
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
