Computer Science Canada

Creating Mario-Like Game, Need Help

Author:  FearSonic [ Sat Apr 10, 2004 9:57 am ]
Post subject:  Creating Mario-Like Game, Need Help

I'm creating a Mario-like platformer game, and I'm having trouble having gravity implemented. I'm moving things around using Input.KeyDown, and using whatdotcolour for collision detection, since I don't know any other way. I just have a few questions:

1) How do I put gravity in, if my code is somewhat like this:
code:
loop
    setscreen ("offscreenonly")
    Draw.FillBox (0, 0, maxx, maxy, 255)
    Draw.FillBox (400, 200, 500, 300, 0)
    Draw.FillOval (ballx, bally, 10, 10, 0)

    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        bally := bally + 1
    elsif chars (KEY_DOWN_ARROW) then
        bally := bally - 1
    elsif chars (KEY_LEFT_ARROW) then
        ballx := ballx - 1
    elsif chars (KEY_RIGHT_ARROW) then
        ballx := ballx + 1
    end if

    if bally + 10 >= maxy then
        bally := bally - 1
    elsif ballx + 10 >= maxx then
        ballx := ballx - 1
    elsif bally - 10 <= 0 then
        bally := bally + 1
    elsif ballx - 10 <= 0 then
        ballx := ballx + 1
    end if

    if whatdotcolor (ballx + 11, bally) = 0 and chars (KEY_RIGHT_ARROW) then
        ballx := ballx - 1
    elsif whatdotcolor (ballx - 11, bally) = 0 and chars (KEY_LEFT_ARROW) then
        ballx := ballx + 1
    elsif whatdotcolor (ballx, bally + 11) = 0 and chars (KEY_UP_ARROW) then
        bally := bally - 1
    elsif whatdotcolor (ballx, bally - 11) = 0 and chars (KEY_DOWN_ARROW) then
        bally := bally + 1
    end if

    colorback (255)
    View.Update
end loop


2) Is there another way to implement collision detection using a more efficient way? I found that if I didn't use the elsif with the 'and' operator, that it would work on one side, but not on the other.

3) How do I implement sprites? I've looked at Dan's Sprite program, but it was WAYYY too confusing for me.

Thanks in advance! It's for my Grade 10 Computer Science ISU!

Author:  FearSonic [ Sat Apr 10, 2004 10:02 am ]
Post subject: 

Also, I'm wondering if I can make the ball go in a diagonal direction, I tired using:

code:
if chars (KEY_UP_ARROW) and chars (KEY_LEFT_ARROW) then
x := x - 1
y := y + 1
end if


And it doesn't work, any suggestions?

Author:  Tony [ Sat Apr 10, 2004 10:05 am ]
Post subject: 

gravity is acceleration. It is applied to unit's velocity variable. When unit is in the air, each time though the loop, gravity (usually negative) is applied to the velocity and velocity moves the unit in the direction.

When you first jump, velocity is set to a positive value and unit begins to rise up, but as gravity reduces the value of the velocity, unit slows down and eventually stops (that is the highest point of the jump). At that point unit begins to fall down, picking up speed as gravity turns velocity variable into a negative value.

As for sprites, it's ether Sprite module in v3.1 or DanSprite in v4.* Which are preaty much the same. DanSprite is not complicated. You don't have to understand the coding behind it, just use methods exported (same way as you don't have to understand how Draw.Box works)

Author:  FearSonic [ Sat Apr 10, 2004 10:17 am ]
Post subject: 

Thanks Tony! Would you happen to have an equation for gravity that would work? The other equations I've seen were all conflicting, and I couldn't tell which to use!

Author:  Tony [ Sat Apr 10, 2004 10:35 am ]
Post subject: 

code:

const gravity:int := -1
var velocity:int := 25
var y:int := 0
var x:int := 100

for i:1..100
velocity += gravity
y += velocity
Draw.FillOval(i,y,5,5,red)
delay(100)
if y<0 then
exit
end if
end for

Author:  FearSonic [ Sat Apr 10, 2004 10:46 am ]
Post subject: 

Thanks again Tony! I tried implementing it so that it did it everytime I pressed the Up arrow, but it doesn't work for some reason. Heres the code:

code:
    if chars (KEY_UP_ARROW) then
        for i : ballx .. ballx + 20
            velocity += gravity
            bally += velocity
            Draw.FillOval (i, bally, 10, 10, 0)
            delay (100)
            if bally < x then
                exit
            end if
        end for
    end if

Author:  FearSonic [ Sat Apr 10, 2004 4:02 pm ]
Post subject: 

I changed up your program a bit, so instead of just going up and going straight down, I made it kinda bounce going lower and lower. It isn't the correct physics, but hey it's a start.

code:
const gravity : int := -1
var y : int := 10
var v : int := 30
var velocity : int := v

loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        for i : 50 .. maxx
            velocity += gravity
            y += velocity
            Draw.FillOval (i, y, 10, 10, red)
            delay (20)
            Draw.FillOval (i, y, 10, 10, 0)
            if y < 0 then
                velocity := v - 5
                v := v - 5
                y := 10
            end if
            View.Update
        end for
    end if
end loop


My only problem now is implementing it into the game!

Author:  the_short1 [ Sun Apr 11, 2004 1:08 am ]
Post subject: 

man... if i had known it was this simple...

also now they i have finished physics in science... it makes more sense... hehehe


nice code there tony..... im sure itll come in handy eventually.... so i saved to HD.,


: