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

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




PostPosted: Sun Oct 22, 2006 7:03 pm   Post subject: Gravity Redone

This is yet another attempt at a gravity program..again, any suggestions or comments are more than welcome and very much appreciated. Thanks.
code:
  var x, check, y, spd : int
var key : array char of boolean
setscreen ("graphics,offscreenonly")
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
check := 0
spd := 0
loop
    View.Update
    Input.KeyDown (key)
    if check = 0 then
        if key ('w') then
            spd := 32
            check := 0
        end if
    end if
    if key ('d') then
        x := x + 5
    elsif key ('a') then
        x := x - 5
    end if
    if y >= 51 then
        check := 1
    elsif y = 50 then
        check := 0
    end if
    if spd > 0 then
        spd := spd div 2
    elsif spd < 0 then
        spd := spd * 2
    end if
    if spd = 1 then
        spd := -1
    end if
    y := y + spd
    if y <= 50 then
        spd := 0
        y := 50
    end if
    put y
    delay (50)
    cls
    draw
end loop
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun Oct 22, 2006 9:17 pm   Post subject: (No subject)

First, I suggest you use reals instead of ints and just round them before drawing. It's more precise that way.

Second, your approach of multiplying / dividing by 2 is interesting, but still fundamentally wrong. Gravity doesn't work by doubling the speed of a falling object with every step of time. Rather, it increases the speed of a falling object, linearly. That means the speed of the falling object increases linearly with time. That means I'm adding a constant value to the speed of the falling object with each step of time.

Here's my modifications on your code:
code:

setscreen ("graphics,offscreenonly")
x := 10
y := 50
vy := 0
procedure draw
    drawoval (round (x), round (y), 10, 10, black)
    drawline (round (x), round (y) - 10, round (x), round (y) - 30, black)
    drawline (round (x), round (y) - 30, round (x) - 10, round (y) - 40, black)
    drawline (round (x), round (y) - 30, round (x) + 10, round (y) - 40, black)
    drawline (round (x) - 10, round (y) - 20, round (x) + 10, round (y) - 20, black)
    drawline (1, 10, maxx, 10, black)
end draw
draw
loop
    View.Update
    Input.KeyDown (key)
    if key ('w') and y <= 50 then
        vy := 8
    end if
    if key ('d') then
        x := x + 5
    elsif key ('a') then
        x := x - 5
    end if
    y += vy % same thing as y := y + vy
    vy -= 1
    if y <= 50 then
        vy := 0
        y := 50
    end if
    put y
    delay (50)
    cls
    draw
end loop
Nigrev




PostPosted: Mon Dec 04, 2006 6:12 pm   Post subject: (No subject)

ah...yes vey good... now n00b question...

what is purpose of put y?
Cervantes




PostPosted: Mon Dec 04, 2006 10:48 pm   Post subject: (No subject)

It was probably for debugging -- to see the value of a variable in real time.
Nigrev




PostPosted: Tue Dec 05, 2006 9:53 pm   Post subject: (No subject)

put y... it's not displayed on the window, but anyway...
I need some explanation on some values for the variables. I'm trying to implement gravity on a program (2d scroller game) and so this is is the only way I can learn. I used some of this code to experiment, but it's screwing up my background pictures...whoops.

Does this work with BSprites? The cls and View.Updates, it seems, needs to be in a certain location around the gravity mechanism, but that would mean fiddling with the module, or at least studying it. My head hurts.
ericfourfour




PostPosted: Tue Dec 05, 2006 10:43 pm   Post subject: (No subject)

BSprites shouldn't be that hard to use. I've never used it but from what I understand it is just add the images, tell it when to switch images, and draw the images. Don't go giving yourself a headache over it. Libraries like BSprite are made so you don't have to worry about what it does on the inside (that means no need to study it). Not that there is anything wrong with studying it.

The way I've always used View.Update and cls was like this.
code:
render (draw) everything
View.Update ()
Time.DelaySinceLast (10)
cls
All you have to remember is the order of the 3 lines and know that it goes after you draw everything. It should work fine with sprites.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: