Computer Science Canada Need help regaring shooting(Urgent) |
Author: | Johnny19931993 [ Tue Dec 30, 2008 3:14 pm ] | ||
Post subject: | 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:
|
Author: | Tony [ Tue Dec 30, 2008 4:00 pm ] | ||
Post subject: | 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.
|
Author: | Johnny19931993 [ Tue Dec 30, 2008 6:04 pm ] | ||
Post subject: | Re: Need help regaring shooting(Urgent) | ||
|
Author: | Tony [ Tue Dec 30, 2008 7:46 pm ] |
Post subject: | 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. |
Author: | Johnny19931993 [ Tue Dec 30, 2008 8:03 pm ] |
Post subject: | 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? |
Author: | Tony [ Tue Dec 30, 2008 9:49 pm ] | ||||
Post subject: | 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.
is the same as
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. |
Author: | Johnny19931993 [ Wed Dec 31, 2008 10:54 am ] |
Post subject: | RE:Need help regaring shooting(Urgent) |
Oh...I see Thanks |
Author: | Johnny19931993 [ Wed Dec 31, 2008 1:19 pm ] | ||
Post subject: | 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?
|
Author: | Tony [ Wed Dec 31, 2008 2:05 pm ] | ||
Post subject: | 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.
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? |
Author: | Johnny19931993 [ Wed Dec 31, 2008 2:40 pm ] |
Post subject: | 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 |
Author: | Tony [ Wed Dec 31, 2008 2:50 pm ] |
Post subject: | RE:Need help regaring shooting(Urgent) |
you could use a different variable. bullet_x perhaps? |
Author: | Johnny19931993 [ Wed Dec 31, 2008 2:54 pm ] |
Post subject: | 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? |
Author: | Tony [ Wed Dec 31, 2008 3:10 pm ] | ||
Post subject: | RE:Need help regaring shooting(Urgent) | ||
what is the value of tank_x and bullet_x? |
Author: | Johnny19931993 [ Wed Dec 31, 2008 4:10 pm ] |
Post subject: | 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... |
Author: | Homer_simpson [ Wed Dec 31, 2008 4:13 pm ] |
Post subject: | 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 |