
-----------------------------------
Weapon X
Fri Apr 22, 2005 8:55 pm

multiple actions in an &quot;if&quot; statement?
-----------------------------------

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")?

-----------------------------------
Flikerator
Fri Apr 22, 2005 9:10 pm


-----------------------------------
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 :)

-----------------------------------
Cervantes
Sat Apr 23, 2005 8:36 am


-----------------------------------
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).  


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  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

-----------------------------------
Flikerator
Sat Apr 23, 2005 2:42 pm


-----------------------------------
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).  


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 