There is an easy way to make characters jump (looks like the original mario brothers) and there is a hard way (looks like newer games). I'll explain the easy way since it is easier to understand. After you know and understand how to make characters jump the easy way, doing it the hard way will be simple.
You will need a variable to hold the y (vertical) position of your character, and a variable to hold what the y position was last time around (ex: y%, oldy%). You will also need a jump% variable to hold whether you are jumping, falling, or neither.
Keep looking at the y position while you are jumping. If it is at the top of your jump, turn your jump around to make the character fall. If you are jumping and then you hit the ground, make your jump variable zero.
The following is an example program with a little dude that you can move around. You can jump, then move while you are in the jump. It is similar to how mario jumps in the regular nintendo games.
REM Initialize stuff:
y% = 170: x% = 10
'''oy% = 171: ox% = 11
jump% = 0
COLOR 15: PRINT "4 - left; 6 - right; space - jump"
LINE (0, 176)-(319, 182), 2, BF
REM Main Loop:
REM Redraw character ONLY if it moves
IF (ox% <> x% OR oy% <> y%) THEN
LINE (ox% - 5, oy% - 5)-(ox% + 5, oy% + 5), 0, BF
ox% = x%: oy% = y%
CIRCLE (x%, y%), 4, 6
PAINT (x%, y%), 6
LINE (x% - 2, y% - 1)-(x% - 1, y% - 2), 15, BF
LINE (x% + 2, y% - 1)-(x% + 1, y% - 2), 15, BF
LINE (x% - 5, y% + 5)-(x% - 3, y% + 3), 1
LINE (x% + 4, y% + 5)-(x% + 2, y% + 3), 1
REM Do physics:
oy% = y%
y% = y% - jump% * 3
IF (y% < 100) THEN jump% = -1
IF (y% = 170) AND (jump%) THEN jump% = 0
REM Get keypresses, and do timer stuff
REM --Also a good place for mouse input
timers! = TIMER 'Keep it from running too fast
DO: LOOP UNTIL timers! + .01 < TIMER '"
cmd$ = INKEY$
LOOP UNTIL cmd$ <> ""
DO: n$ = INKEY$: LOOP UNTIL n$ = ""'<-A good way to clear the keyboard buffer
REM Keypress (and any other input) handling goes next:
IF (cmd$ = "4" AND x% > 3) THEN ox% = x%: x% = x% - 3
IF (cmd$ = "6" AND x% < 307) THEN ox% = x%: x% = x% + 3
IF (cmd$ = " " AND y% = 170) THEN jump% = 1
IF (cmd$ = CHR$(27)) THEN SCREEN 0: WIDTH 80, 25: END
|This message has been edited by ComputerGhost on Apr 18, 2006 7:09 PM|
This message has been edited by ComputerGhost on Apr 18, 2006 7:08 PM