Computer Science Canada

multiple actions in an "if" statement?

Author:  Weapon X [ Fri Apr 22, 2005 8:55 pm ]
Post subject:  multiple actions in an "if" statement?

code:

loop
           
        Input.KeyDown (chars)
           
             if chars (KEY_ENTER) then
                 for i:1..15
                 drawoval(x,y,4+i,4+i,blue)
                 delay(20)
                 end for
             end if
            if chars (KEY_UP_ARROW) then
                y:=y+5
            end if
            if chars (KEY_RIGHT_ARROW) then
                x:=x+5
            end if
            if chars (KEY_LEFT_ARROW) then
                x:=x-5
            end if
            if chars (KEY_DOWN_ARROW) then
                y:=y-5
            end if
           
            drawoval(x,y,4,4,red)
            delay(20)
cls
           
        end loop

In an code like this can the "if" statements have mulitple actions? For example: If i press Up arrow can the ball go like 5 up slowley, and then the ball automatically comes back down 5slowley (and by slwley i mean "You can see the ball go up the come back down")?

Author:  Flikerator [ Fri Apr 22, 2005 9:10 pm ]
Post subject: 

code:
var x, y : int := 25
var chars : array char of boolean

loop

    Input.KeyDown (chars)

    if chars (KEY_ENTER) then
        for i : 1 .. 15
            drawoval (x, y, 4 + i, 4 + i, blue)
            delay (20)
        end for
    end if
    if chars (KEY_UP_ARROW) then
        for i : 1 .. 5
            y := y + 1
            cls
            drawoval (x, y, 4, 4, red)
            delay (100)
        end for
        y := y - 5
    end if
    if chars (KEY_RIGHT_ARROW) then
        for i : 1 .. 5
            x := x + 1
            cls
            drawoval (x, y, 4, 4, red)
            delay (100)
        end for
        x := x - 5
    end if
    if chars (KEY_LEFT_ARROW) then
        for i : 1 .. 5
            x := x - 1
            cls
            drawoval (x, y, 4, 4, red)
            delay (100)
        end for
        x := x + 5
    end if
    if chars (KEY_DOWN_ARROW) then
        for i : 1 .. 5
            y := y - 1
            cls
            drawoval (x, y, 4, 4, red)
            delay (100)
        end for
        y := y + 5
    end if
    drawoval (x, y, 4, 4, red)
    delay (20)
    cls
end loop


Here you go, if you want an explanation just ask Smile

Author:  Cervantes [ Sat Apr 23, 2005 8:36 am ]
Post subject: 

That's definately not what we want, Flik. Using a for loop like that means nothing else can happen while your character is moving. Though you might be able to pass it off as an ability of your character to stop time if your user is dumb enough, but that's too risky. The user might catch on and think your program is silly!
How do we fix this? Left and right movement is the easiest. If the left or right arrow keys are pressed, incriment the object's x variable appropriately. Jumping is more difficult though. Basically, we are going to give the object a certain velocity in the y plane if the up arrow is pressed. We will also use a boolean variable to keep track of whether the object is already in the air or not, just to make sure that the object can't jump off of the air (like so many crazy, unrealistic video game characters).

Turing:

var x, y, vy : real
x := maxx / 2
y := 20
vy := 0
const jumpVelocity := 5
const speedX := 2  %speed of horizontal movement
const gravity := 0.15
var inAir := false
var chars : array char of boolean

loop

    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and not inAir then
        vy := jumpVelocity
        inAir := true
    end if
    if chars (KEY_RIGHT_ARROW) then
        x += speedX
    end if
    if chars (KEY_LEFT_ARROW) then
        x -= speedX
    end if

    %very basic collision detection.  Only detects when the object reaches ground level
    if inAir then
        y += vy
        vy -= gravity
        if y <= 10 then
            inAir := false
            vy := 0
        end if
    end if

    cls
    drawoval (round (x), round (y), 4, 4, red)
    delay (20)
end loop

Author:  Jonny Tight Lips [ Sat Apr 23, 2005 11:24 am ]
Post subject: 

code:

var cout : int := 0
loop
           
        Input.KeyDown (chars)
           
             if chars (KEY_ENTER) then
                 for i:1..15
                 drawoval(x,y,4+i,4+i,blue)
                 delay(20)
                 end for
             end if
            if chars (KEY_UP_ARROW) or count > 0  then
                count += 1
                y:=y+5
                if count = 5 then
                      count := -1
                end if
            end if
            if chars (KEY_RIGHT_ARROW) then
                x:=x+5
            end if
            if chars (KEY_LEFT_ARROW) then
                x:=x-5
            end if
            if chars (KEY_DOWN_ARROW) or count < 0 then
                y:=y-5
                count -= 1
                if count = -5 then
                count := 0
                end if
            end if
           
            drawoval(x,y,4,4,red)
            delay(20)
cls
           
        end loop


don't know if this code will acctualy run. I didn't have turing on this computer so I couldn't actualy run it but I think I got it correct. All I juses was a counter as a flag so it keeps going up until the couter equils 5 and then goes down 5. Hope it works

Author:  Flikerator [ Sat Apr 23, 2005 2:42 pm ]
Post subject: 

Cervantes wrote:
That's definately not what we want, Flik. Using a for loop like that means nothing else can happen while your character is moving. Though you might be able to pass it off as an ability of your character to stop time if your user is dumb enough, but that's too risky. The user might catch on and think your program is silly!
How do we fix this? Left and right movement is the easiest. If the left or right arrow keys are pressed, incriment the object's x variable appropriately. Jumping is more difficult though. Basically, we are going to give the object a certain velocity in the y plane if the up arrow is pressed. We will also use a boolean variable to keep track of whether the object is already in the air or not, just to make sure that the object can't jump off of the air (like so many crazy, unrealistic video game characters).

Turing:

var x, y, vy : real
x := maxx / 2
y := 20
vy := 0
const jumpVelocity := 5
const speedX := 2  %speed of horizontal movement
const gravity := 0.15
var inAir := false
var chars : array char of boolean

loop

    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and not inAir then
        vy := jumpVelocity
        inAir := true
    end if
    if chars (KEY_RIGHT_ARROW) then
        x += speedX
    end if
    if chars (KEY_LEFT_ARROW) then
        x -= speedX
    end if

    %very basic collision detection.  Only detects when the object reaches ground level
    if inAir then
        y += vy
        vy -= gravity
        if y <= 10 then
            inAir := false
            vy := 0
        end if
    end if

    cls
    drawoval (round (x), round (y), 4, 4, red)
    delay (20)
end loop


I didn't know thats what he wanted.

Author:  Cervantes [ Sat Apr 23, 2005 3:54 pm ]
Post subject: 

Haha. Okay, maybe it's not what he wanted. But it's definately what he should want! Laughing

Author:  Flikerator [ Sat Apr 23, 2005 4:54 pm ]
Post subject: 

Cervantes wrote:
Haha. Okay, maybe it's not what he wanted. But it's definately what he should want! Laughing


Agreed Smile

Are const justs var's that you cannot change?

Author:  Cervantes [ Sat Apr 23, 2005 6:05 pm ]
Post subject: 

Yep. You know, I should have made jumpVelocity and speedX variables, in case the player drinks some gaterade and gets an energy boost Wink

Author:  Flikerator [ Sat Apr 23, 2005 7:08 pm ]
Post subject: 

Cervantes wrote:
Yep. You know, I should have made jumpVelocity and speedX variables, in case the player drinks some gaterade and gets an energy boost Wink


Yah I was thinking about that lolz Smile

There is so much you could do with that little program. Definitly gaterade Smile

Author:  Weapon X [ Mon Apr 25, 2005 12:12 pm ]
Post subject:  Now What?

i created a program with the advice from ppl. here is wht io got but there is still a problem. can someone help me out AGAIN. Very Happy

code:

setscreen ("offscreenonly")
var score1, score2, ballx, ballx2, bally, bally2, xmod, xmod2, ymod, ymod2 : int
var chars : array char of boolean
var slimex, slimey, vy : int
slimex := 200
slimey := 20
vy := 0
ballx := 320
bally := 0
const jumpVelocity := 5
const speedX := 2  %speed of horizontal movement
const gravity := 0.07
var inAir := false
xmod := 1
ymod := 1
xmod2 := 1
ymod2 := 1
score1 := 0
score2 := 0
loop
if Input.hasch then
        Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and not inAir then
        vy := jumpVelocity
        inAir := true
    end if
    if chars (KEY_RIGHT_ARROW) then
        slimex += speedX
    end if
    if chars (KEY_LEFT_ARROW) then
        slimex -= speedX
    end if

    %very basic collision detection.  Only detects when the object reaches ground level
    if inAir then
        slimey += vy
        vy -= gravity
        if slimey <= 10 then
            inAir := false
            vy := 0
        end if
    end if

        if chars (KEY_RIGHT_ARROW) then
            slimex += 10
            end if
        if chars (KEY_LEFT_ARROW) then
            slimex -= 10
        end if
        if chars (KEY_DOWN_ARROW) then
            slimey -= 10
        end if

        View.Update
    end if

    drawfillbox (0, 0, maxx, maxy, 0)
    drawfillarc (slimex, slimey, -50, -50, 180, 0, blue)
    drawline (0, 126, 107, 126, blue) %|net 1 top
    drawline (0, 125, 100, 125, black) %|
    drawline (0, 124, 100, 124, black) %|
    drawline (0, 123, 100, 123, black) %|
    drawline (0, 122, 100, 122, black) %|net 1
    drawline (0, 121, 100, 121, black) %|
    drawline (0, 120, 100, 120, black) %|
    drawline (0, 119, 100, 119, black) %|
    %____________________________________________

    drawline (100, 0, 100, 125, black) %|net 1
    drawline (101, 0, 101, 125, black) %|net 1
    drawline (102, 0, 102, 125, black) %|net 1
    drawline (103, 0, 103, 125, black) %|net 1
    drawline (104, 0, 104, 125, black) %|net 1
    drawline (105, 0, 105, 125, black) %|net 1
    drawline (106, 0, 106, 125, black) %|net 1
    drawline (107, 0, 107, 125, black) %|net 1
    %____________________________________________

    drawline (0, 110, 100, 110, black) %|net 1
    drawline (0, 100, 100, 100, black) %|net 1
    drawline (0, 90, 100, 90, black) %|net 1
    drawline (0, 80, 100, 80, black) %|net 1
    drawline (0, 70, 100, 70, black) %|net 1
    drawline (0, 60, 100, 60, black) %|net 1
    drawline (0, 50, 100, 50, black) %|net 1
    drawline (0, 40, 100, 40, black) %|net 1
    drawline (0, 30, 100, 30, black) %|net 1
    drawline (0, 20, 100, 20, black) %|net 1
    drawline (0, 10, 100, 10, black) %|net 1

    %____________________________________________

    drawline (90, 0, 90, 119, black) %|net 1
    drawline (80, 0, 80, 119, black) %|net 1
    drawline (70, 0, 70, 119, black) %|net 1
    drawline (60, 0, 60, 119, black) %|net 1
    drawline (40, 0, 40, 119, black) %|net 1
    drawline (50, 0, 50, 119, red) %|net 1
    drawline (28, 0, 28, 119, blue) %|net 1
    drawline (20, 0, 20, 119, black) %|net 1
    drawline (10, 0, 10, 119, black) %|net 1

    %______________________________END OF NET 1________________________________________________________________________________________________

    drawline (640, 126, 534, 126, blue) %|net 2 top
    drawline (640, 125, 540, 125, black) %|net 2
    drawline (640, 124, 540, 124, black) %|net 2
    drawline (640, 123, 540, 123, black) %|net 2
    drawline (640, 122, 540, 122, black) %|net 2
    drawline (640, 121, 540, 121, black) %|net 2
    drawline (640, 120, 540, 120, black) %|net 2
    drawline (640, 119, 540, 119, black) %|net 2
    %____________________________________________

    drawline (541, 0, 541, 125, black) %|net 2
    drawline (540, 0, 540, 125, black) %|net 2
    drawline (539, 0, 539, 125, black) %|net 2
    drawline (538, 0, 538, 125, black) %|net 2
    drawline (537, 0, 537, 125, black) %|net 2
    drawline (536, 0, 536, 125, black) %|net 2
    drawline (535, 0, 535, 125, black) %|net 2
    drawline (534, 0, 534, 125, black) %|net 2

    %____________________________________________

    drawline (640, 110, 540, 110, black) %|net 2
    drawline (640, 100, 540, 100, black) %|net 2
    drawline (640, 90, 540, 90, black) %|net 2
    drawline (640, 80, 540, 80, black) %|net 2
    drawline (640, 70, 540, 70, black) %|net 2
    drawline (640, 60, 540, 60, black) %|net 2
    drawline (640, 50, 540, 50, black) %|net 2
    drawline (640, 40, 540, 40, black) %|net 2
    drawline (640, 30, 540, 30, black) %|net 2
    drawline (640, 20, 540, 20, black) %|net 2
    drawline (640, 10, 540, 10, black) %|net 2

    %____________________________________________

    drawline (550, 0, 550, 125, black) %|net 2
    drawline (560, 0, 560, 125, black) %|net 2
    drawline (570, 0, 570, 125, black) %|net 2
    drawline (580, 0, 580, 125, black) %|net 2
    drawline (590, 0, 590, 125, black) %|net 2
    drawline (600, 0, 600, 119, green) %|net 2
    drawline (610, 0, 610, 125, black) %|net 2
    drawline (620, 0, 620, 125, black) %|net 2
    drawline (630, 0, 630, 125, black) %|net 2

    %_________________________________END OF NET 2_____________________________________________________________________________________________

    Draw.FillOval (ballx, bally, 10, 10, 12)

    ballx += xmod
    bally += ymod
    if ballx >= maxx then
        xmod := -xmod
    elsif ballx <= 0 then
        xmod := -xmod
    end if
    if bally >= maxy then
        ymod := -ymod
    elsif bally <= 0 then
        ymod := -ymod
    end if
    if whatdotcolour (ballx + 11, bally) = blue or whatdotcolour (ballx - 11, bally) = 1 then
        xmod := -xmod
    end if
    if whatdotcolour (ballx, bally + 11) = blue or whatdotcolour (ballx, bally - 11) = 1 then
        ymod := -ymod
    end if
    if whatdotcolour (ballx, bally + 11) = red then
        score1 += 1
    end if
    if whatdotcolour (ballx + 11, bally) = red then
        score1 += 1
    end if

    if score1 <= 1 then
        locatexy (0, 400)
        put "", score1, ""
    elsif score1 >= 1 then
        locatexy (0, 400)
        put "", score1, ""
    end if

    View.Update


end loop

Author:  jamonathin [ Mon Apr 25, 2005 1:28 pm ]
Post subject: 

Gravity is a real variable, and it would normall be okay to add it to an integer variable, as long as the number is whole. 0.07 is not a whole number, and cannot be added to an integer variable. try making vy real, and have slimey add round (vy).

Author:  Weapon X [ Mon Apr 25, 2005 2:47 pm ]
Post subject:  TRU!!

TRU, i forgot to round. thanx to everyone who helped me. Smile


: