
-----------------------------------
gafiga
Sun Feb 24, 2008 4:03 am

2d gaming like mario
-----------------------------------
hey, im trying to make my own 2d game and was wonder if someone could give me some help or pointers. the things i need help with are the health, enemys and the ground. I can't get it so my guy jumps ontop of things, he just falls through. I dont have much done because i change it around so much. but ya, if anyone could help that would be awesome. thanks.

This is NOT my work, i got it off of this site. this is pretty much what im useing.




% some constants, tweak this to your liking 
const GROUND_HEIGHT := 120 
const RUN_SPEED := 10 
const JUMP_SPEED := 30 
const GRAVITY := 2 

var win := Window.Open ("graphics:640;480,offscreenonly") 
var chars : array char of boolean 

% player position and velocity 
var posx, posy : int 
var velx, vely : real 

posx := 20 
velx := 0 
posy := 400 
vely := 0 

loop 
    Input.KeyDown (chars) 
    if chars ('q') then 
        exit 
    end if 
    
    % to make the player move 
    if chars (KEY_LEFT_ARROW) then 
        velx := -RUN_SPEED 
    elsif chars (KEY_RIGHT_ARROW) then 
        velx := RUN_SPEED 
    else 
        velx := 0 
    end if 
    
    % remember, in order to jump you MUST be on the ground when pressing UP 
    if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then 
        vely := JUMP_SPEED 
    end if 

    % subtract your "gravity" constant from the velocity EACH frame 
    vely -= GRAVITY 
    posx += round (velx) 
    posy += round (vely) 

    % simple "collision" check. just makes sure the player stays above ground 
    if posy < GROUND_HEIGHT then 
        posy := GROUND_HEIGHT 
        vely := 0 
    end if 

    % different colours just to illustrate whether or not you may jump 
    if posy = GROUND_HEIGHT then 
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, green) 
    else 
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, red) 
    end if 

    drawline (0, GROUND_HEIGHT, maxx, GROUND_HEIGHT, blue) 
    View.Update 
    delay (25) 
    cls 
end loop

-----------------------------------
Tony
Sun Feb 24, 2008 11:33 am

Re: 2d gaming like mario
-----------------------------------
I can't get it so my guy jumps ontop of things, he just falls through.
The code is the same as what keeps your guy on the ground. Read it over and understand it well enough to implement the same idea for "things". If you don't understand the code, you should not be using it.

It's also a good idea to actually credit parts of the code taken. Like author's name and a link to where it was originally posted.

-----------------------------------
gafiga
Sun Mar 09, 2008 1:55 am

RE:2d gaming like mario
-----------------------------------
ok, i got the health bar and enemys figured out but i still cant get my guy to stand on a box. when i make a box the lines are always from 0-maxx or maxy. any help or tips?

-----------------------------------
Sean
Sun Mar 09, 2008 11:06 am

Re: 2d gaming like mario
-----------------------------------
You are programming it to land onto the Ground.

If your character is at a certain position, x and y position and there is a box, then the program needs to reconize it has to land on that.

-----------------------------------
gafiga
Sun Mar 09, 2008 7:45 pm

RE:2d gaming like mario
-----------------------------------
thats my problem, i dont know how to do that. i'v only been useing Turing for a few months. 

oh, and thanks to the people above. :)

-----------------------------------
Sean
Sun Mar 09, 2008 8:53 pm

Re: 2d gaming like mario
-----------------------------------

% simple "collision" check. just makes sure the player stays above ground 
if posy < GROUND_HEIGHT then 
posy := GROUND_HEIGHT 
vely := 0 
end if 


This does your colision with ground, this will also give you an idea of how to do something like what you want.

All you need it to do, is check your posy and posx and make sure it is greater then your box, and inbetween its dimentions width wise.

-----------------------------------
gafiga
Sun Mar 09, 2008 11:42 pm

RE:2d gaming like mario
-----------------------------------
ya, thats the part i dont get how to do. is there anyways i can use the "array" command?

-----------------------------------
Sean
Mon Mar 10, 2008 6:54 am

Re: 2d gaming like mario
-----------------------------------
arrays are a collection, they wouldn't work in the argument.

you need it to check the posx and posy of the character. You want to set boundaries, a start posx boundary, then an end boundary, which is the exact length of the platform, then the posy which is the top of the platform.

You want it to check if the character greater then the platform, and then for it to equal it so it lands on it. You are comparing three things in the if statement.

-----------------------------------
BigBear
Mon Mar 10, 2008 9:42 am

Re: 2d gaming like mario
-----------------------------------
There is a solution to creating platforms in another thread.
http://compsci.ca/v3/viewtopic.php?p=155984

-----------------------------------
Sean
Mon Mar 10, 2008 9:46 am

Re: 2d gaming like mario
-----------------------------------
See BigBear, that is what you don't want to do, do not give them the answers, let them learn!

Attempt to what I mentioned above gafiga.

-----------------------------------
gafiga
Mon Mar 10, 2008 3:52 pm

RE:2d gaming like mario
-----------------------------------
alright, i'll give it a few trys. thanks. :)

-----------------------------------
Sean
Mon Mar 10, 2008 5:18 pm

Re: 2d gaming like mario
-----------------------------------
No problem. Any further questions, keep asking.

-----------------------------------
gafiga
Mon Mar 10, 2008 10:07 pm

Re: 2d gaming like mario
-----------------------------------
hello again, i have a new problem now. i cant figure out how to make the text line go away. i put some text in my game but from where it starts all the way to the right it leaves a white line. 



% some constants, tweak this to your liking
const GROUND_HEIGHT := 120
const RUN_SPEED := 10
const JUMP_SPEED := 30
const GRAVITY := 2

var BOX_x1 : int := 170  %box
var BOX_y1 : int := 170
var BOX_x2 : int := 300
var BOX_y2 : int := 200
var BOX1_x1 : int := 80  %HP pad
var BOX1_y1 : int := 380
var BOX1_x2 : int := 120
var BOX1_y2 : int := 390
var BOX2_x1 : int := 380 % death pad
var BOX2_y1 : int := 380
var BOX2_x2 : int := 420
var BOX2_y2 : int := 390

var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean

% player position and velocity
var posx, posy : int
var velx, vely : real

var health : real := 100
var damage : int := 1
var HPup : real := 1
var HPleft : real := 100
var HPminus : int := 1
var HPgain : real := 0.01
var changeX : int := 5
posx := 20
velx := 0
posy := 400
vely := 0
loop
    Input.KeyDown (chars)
    if chars ('q') then
        exit
    end if

    % to make the player move
    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if

    % remember, in order to jump you MUST be on the ground when pressing UP
    if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if
    %again follow the ubove if statement if the movable box is on the ground then so make it the platform to allow jumping from platform
    if chars (KEY_UP_ARROW) and posy = BOX_y2 and posx > BOX_x1 and posx < BOX_x2 then
        vely := JUMP_SPEED
    end if

    %health up pad jump
    if chars (KEY_UP_ARROW) and posy = BOX1_y2 and posx > BOX1_x1 and posx < BOX1_x2 then
        vely := JUMP_SPEED
    end if

    % death pad
    if chars (KEY_UP_ARROW) and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
        vely := JUMP_SPEED
    end if


    % subtract your "gravity" constant from the velocity EACH frame
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

    % simple "collision" check. just makes sure the player stays above ground
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if
    %so if movable box isn't on the ground and he is in between box then set his y position to the height of the platform
    if posy not= GROUND_HEIGHT and posy < BOX_y2 and posx + 10 > BOX_x1 and posx < BOX_x2 then
        posy := BOX_y2
        vely := 0
    end if

    %health pad config
    if posy not= GROUND_HEIGHT and posy < BOX1_y2 and posx + 10 > BOX1_x1 and posx < BOX1_x2 then
        posy := BOX1_y2
        vely := 0
        health := health + HPup
        HPleft := HPleft - HPminus
        if HPleft = 0 then
            HPminus := 0
        end if
    end if

    %HP pad gaining life
    if HPleft < 100 then
        HPleft := HPleft + HPgain
    end if


    %death pad
    if posy not= GROUND_HEIGHT and posy < BOX2_y2 and posx + 10 > BOX2_x1 and posx < BOX2_x2 then
        posy := BOX2_y2
        vely := 0
        health := health - damage
    end if

    %HP and death config
    if HPleft = 0 then
        HPup := 0
        if health < 0 then
            cls
            put "you died"
            exit
        end if
    end if


    % different colours just to illustrate whether or not you may jump
    if posy = GROUND_HEIGHT then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, green)
        %or he is on the platform change colours
    elsif posy = BOX_y2 then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, green)
        %end of my revisions
    else
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, red)
    end if




    drawfillbox (BOX_x1, BOX_y1, BOX_x2, BOX_y2, 5)
    drawfillbox (BOX1_x1, BOX1_y1, BOX1_x2, BOX1_y2, green) % HP up pad
    drawfillbox (BOX2_x1, BOX2_y1, BOX2_x2, BOX2_y2, black) % death pad
    put "Health ", health % health
    locate (8, 12)
    put HPleft
    drawline (0, GROUND_HEIGHT, maxx, GROUND_HEIGHT, blue)
    View.Update
    delay (25)
    cls
end loop




ya, if you jump on the middle block and then jump up you will see what im talking about. and i couldn't figure out the block thing on my own so i did copy from that link by Bigbear, but i have an idea of how to do it. i'll just keep fooling around with it untill i get it right.

i dont know how to do the turing text in the replys either. :S 
sorry for being useless. haha

-----------------------------------
richcash
Mon Mar 10, 2008 10:21 pm

Re: 2d gaming like mario
-----------------------------------
To find out how to use syntax and code tags (and other various tags), read Font.Draw instead of put. It's because put is meant for text mode, not graphics mode. Example of Font.Draw :
colourback (red)
cls
Font.Draw ("text", 100, 100, Font.New ("sanserif:12"), blue)

-----------------------------------
BigBear
Mon Mar 10, 2008 10:22 pm

Re: 2d gaming like mario
-----------------------------------
to post code you can type[code] and [/code]  to end it or and [syntax="Turing"]
Also those white lines appear when you use put you can try using Font.Draw ("words", x1, y1, type of font, colour) Press F10 to read more about it.

-----------------------------------
gafiga
Mon Mar 10, 2008 11:02 pm

RE:2d gaming like mario
-----------------------------------
alright thanks. you guys helped out alot. :)
