
-----------------------------------
Wing_Wing
Sat Mar 31, 2007 8:05 pm

I Can't Move While Shooting Projectiles
-----------------------------------
My code is as follows: 

Freakman Edit: Added code tags. Remember your code tags children!


setscreen ("offscreenonly")
var x, y, addx, addy : int
var key : array char of boolean
var face : string

x := 200
y := 200

proc movement
    Input.KeyDown (key)
    if key (KEY_RIGHT_ARROW) then
        addx := 3
        face := "right"
    elsif key (KEY_LEFT_ARROW) then
        addx := -3
        face := "left"
    else
        addx := 0
    end if

    if key (KEY_UP_ARROW) then
        face := "up"
        addy := 3
    elsif key (KEY_DOWN_ARROW) then
        face := "down"
        addy := -3
    else
        addy := 0
    end if
end movement

proc shoot
    Input.KeyDown (key)
    if key (KEY_END) and face = "up" then
        for i : y .. maxy by 3
            cls
            drawfilloval (x, i, 5, 5, black)
            delay (5)
            View.Update
        end for
    elsif key (KEY_END) and face = "down" then
        for decreasing i : y .. 1 by 3
            cls
            drawfilloval (x, i, 5, 5, black)
            delay (5)
            View.Update
        end for
    elsif key (KEY_END) and face = "right" then
        for i : x .. maxx by 3
            cls
            drawfilloval (i, y, 5, 5, black)
            delay (5)
            View.Update
        end for
    elsif key (KEY_END) and face = "left" then
        for decreasing i : x .. 1 by 3
            cls
            drawfilloval (i, y, 5, 5, black)
            delay (5)
            View.Update
        end for
    end if
end shoot

loop
    cls
    movement
    shoot
    x := x + addx
    y := y + addy

    drawdot (x, y, black)
    drawbox (x - 10, y - 10, x + 10, y + 10, black)
    delay (5)

    locate (1, 1)
    put "x= ", x, "   ", "y= ", y

    if x  maxx - 21 then
        x := maxx - 21
    end if

    if y  maxy - 21 then
        y := maxy - 21
    end if
    View.Update
end loop


After running, I can't make the box move while the projectile is firing. Why is that?

-----------------------------------
ericfourfour
Sun Apr 01, 2007 1:42 am

Re: I Can't Move While Shooting Projectiles
-----------------------------------
First, let's clean up your code in the shoot procedure.

Every if/elsif you do this calculation:
key (KEY_END)

Basically, that whole if structure can be surrounded by:
if key (KEY_END) then

end if
That will increase performance and cut down the number of characters typed.

Inside every for loop this is what is executed:
cls
drawfilloval (x, i, 5, 5, black)
delay (5)
View.Update
It can be very tedious and annoying to write the same thing four times. Instead have one for loop and precalculate i's range and what to increase x and y by every iteration.

Ex:
for i : start .. ending by 3
    cls

    % up: xInc = 0 and yInc = 1
    % down: xInc = 0 and yInc = -1
    % left: xInc = -1 and yInc = 0
    % right: xInc = 1 and yInc = 0
    drawfilloval (x + i * xInc, y + i * yInc, 5, 5, black)
    delay (5)
    View.Update
end for

If you implement that idea correctly, repost it and it will be much easier to help you get both the square and the bullet moving at the same time (it essentially involves running everything in one loop).

-----------------------------------
Wing_Wing
Sun Apr 01, 2007 3:22 pm

RE:I Can\'t Move While Shooting Projectiles
-----------------------------------
explain plz? sorry those words that you used... were too complicated... lol

-----------------------------------
Wing_Wing
Sun Apr 01, 2007 8:11 pm

Re: I Can't Move While Shooting Projectiles
-----------------------------------
However in this case, what is the "start" and "ending" of the code in which you put?

First, let's clean up your code in the shoot procedure.

Every if/elsif you do this calculation:
key (KEY_END)

Basically, that whole if structure can be surrounded by:
if key (KEY_END) then

end if
That will increase performance and cut down the number of characters typed.

Inside every for loop this is what is executed:
cls
drawfilloval (x, i, 5, 5, black)
delay (5)
View.Update
It can be very tedious and annoying to write the same thing four times. Instead have one for loop and precalculate i's range and what to increase x and y by every iteration.

Ex:
for i : start .. ending by 3
    cls

    % up: xInc = 0 and yInc = 1
    % down: xInc = 0 and yInc = -1
    % left: xInc = -1 and yInc = 0
    % right: xInc = 1 and yInc = 0
    drawfilloval (x + i * xInc, y + i * yInc, 5, 5, black)
    delay (5)
    View.Update
end for

If you implement that idea correctly, repost it and it will be much easier to help you get both the square and the bullet moving at the same time (it essentially involves running everything in one loop).

-----------------------------------
ericfourfour
Mon Apr 02, 2007 2:55 pm

Re: I Can't Move While Shooting Projectiles
-----------------------------------
I'll give you two cases:

If face equals "up", start is going to equal 0 and end is going to equal maxy - y.
If face equals "down", start is going to equal 0 and end is going to equal y.

I'm going to let you figure out the rest. Try and implement this into your code. After that it will be much easier to do what you want to do.

-----------------------------------
Wing_Wing
Tue Apr 03, 2007 8:36 am

RE:I Can\'t Move While Shooting Projectiles
-----------------------------------
i see i see now i get you thx

-----------------------------------
Wing_Wing
Tue Apr 03, 2007 8:37 am

RE:I Can\'t Move While Shooting Projectiles
-----------------------------------
and that goes into the main loop?

-----------------------------------
Wing_Wing
Tue Apr 03, 2007 8:41 am

Re: I Can't Move While Shooting Projectiles
-----------------------------------
and are you sure about the start equaling to 0 and not y when face is up because ur drawing to maxx? or do you know what i mean?

I'll give you two cases:

If face equals "up", start is going to equal 0 and end is going to equal maxy - y.
If face equals "down", start is going to equal 0 and end is going to equal y.

I'm going to let you figure out the rest. Try and implement this into your code. After that it will be much easier to do what you want to do.

-----------------------------------
ericfourfour
Tue Apr 03, 2007 11:09 pm

RE:I Can\'t Move While Shooting Projectiles
-----------------------------------
I understand you :)

Try and write this stuff out on a piece of paper. See if you can understand what I'm saying.

If you want to travel from y to maxx, what would you add to y in the first loop iteration? 0.

First iteration:
y + 0 = y

Last iteration:
y + (maxy - y) = maxy

When you are done the animation, you can assign a new value to y.

And keep all of that stuff in a single for loop like I discussed in my first post. After, we will work toward getting everything in a single loop. It is much easier to do things step-by-step.

-----------------------------------
CodeMonkey2000
Thu Apr 05, 2007 8:54 pm

RE:I Can\'t Move While Shooting Projectiles
-----------------------------------
Why is this in the turing submissions?
