Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with game
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Thu Dec 22, 2011 6:29 pm   Post subject: RE:Need help with game

With some effort, it will work. I'll make a small program to show you:

Turing:

setscreen ("offscreenonly")
var y, x, vy : real := 0
var key : array char of boolean
var jumping : boolean := false

const gravity := 0.1
const speed := 2

loop
    Input.KeyDown (key)

    if key (KEY_UP_ARROW) and jumping = false then
        vy := 6
        jumping := true
    end if
    if not key (KEY_UP_ARROW) and jumping = true and vy > 0.5 then
        vy := 0.5
    end if
   
    if key (KEY_RIGHT_ARROW) then
        x += speed
    elsif key (KEY_LEFT_ARROW) then
        x -= speed
    end if

    if y < 5 then
        y := 5
        vy := 0
        jumping := false
    end if

    y += vy

    if jumping = true then
        vy -= gravity
    end if
   
    Draw.FillOval (round (x), round (y), 3, 3, brightblue)
    View.Update
    delay (5)
    cls

end loop


I'll explain this briefly. You have your x and y, obviously. The vy is also called "the change in y". You don't want to change y by a constant amount, because it isnt realistic; therefore, we need a changing number. This will suffice. The last important one is the jumping variable, which we use to detect if a character is in the air.

First, we check to see if the player has pressed the up button. If the player hasnt already jumped, then the character will begin to "jump", or we set the vy variable. The part next to it isnt nesessary. All it does it see if the player has let go while the character is jumping. If they have, it stops going up and goes down. This only happens, however, when the character's vy is greater than 0.5. After this, the function will be counterproductive.

"if y < 5 then..."
I set 5 to be the ground. When he goes below the ground, I say he's not longer jumping and set vy to 0. Lastly, I add vy to y and if the player is jumping, I take gravity away from vy. Thats basically it.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 16 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: