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

Username:   Password: 
 RegisterRegister   
 Simple gravity program
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zacebdal




PostPosted: Thu Oct 19, 2006 6:49 pm   Post subject: Simple gravity program

I was just playing around and trying to figure out an algorithm to simulate gravity and i came up with this little program. I know that there is probably an easier way to do this but i thought i would submit this anyways. I have only recently started to program so for me its kinda a big deal. any comments or suggestions are much appreciated. Thanks
code:
var x, y, jump : int
var key : array char of boolean
setscreen ("graphics")
x := 10
y := 50
procedure draw
    drawoval (x, y, 10, 10, black)
    drawline (x, y - 10, x, y - 30, black)
    drawline (x, y - 30, x - 10, y - 40, black)
    drawline (x, y - 30, x + 10, y - 40, black)
    drawline (x - 10, y - 20, x + 10, y - 20, black)
    drawline (1, 10, maxx, 10, black)
end draw
draw
loop
    Input.KeyDown (key)
    if key ('w') then
        jump := 32
        for i : 1 .. 5
            jump := jump div 2
            y := y + jump
            delay (50)
            cls
            draw
        end for
        y := y + 1
        jump := 1
        for i : 1 .. 4
            jump := jump * 2
            y := y - jump
            delay (50)
            cls
            draw
            if i = 4 then
                y := y - 2
                cls
                draw
            end if
        end for
    end if
    if key ('d') then
        x := x + 5
        delay (50)
        cls
        draw
    end if
    if key ('a') then
        x := x - 5
        delay (50)
        cls
        draw
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu Oct 19, 2006 9:53 pm   Post subject: (No subject)

A good try, but it's fundamentally wrong.

You should not have for loops within those if statements. Why? Because if you put a for loop in there, the execution will remain in there while the character moves and no other part of your program will run. It's like this: "You want to move a step to the right? Sure, but doing so will prevent your enemies or allies from moving."

Instead, you need to create variables that store the velocity of your character. If you hit the up arrow and you're on the ground, set the y velocity to some value, say 5. Then while you're in the air, decrease the y velocity by some constant value with every iteration of the loop. That's acceletaion at work.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: