GLBasic forum

Main forum => Tutorials => Topic started by: Schranz0r on 2007-Aug-31

Title: Jump`n Run Tutorial 1 (Animation)
Post by: Schranz0r on 2007-Aug-31
Hallo Freunde,

hier ein neues Tutorial, wie man ein Jump&Run Spiel macht. Bitte alle Kommentare sorgfältig lesen.

Code: GLBasic [Select]

// pos_x = Mitte der Spielfigur!!!!
// pos_y = Fuß der Figur!!!

pos_x = 200             // legt die Start Position in x Richtung fest
pos_y = 50              // legt die Start Position in y Richtung fest



// Ein Feld, in dem die Platformen sind
// Ein Wert=0 ist Luft, alles andere
// muss man ausbauen
// platform[x][y] ist der Wert des Blockes
// Hier kann man z.B. speichern, ob Feuer/Wasser an dem Block ist
DIM platform[20][10]

// Zufällige Platformen setzen
FOR i=0 TO 20
        platform[RND(19)][3+RND(6)] = 1
NEXT


DRAWRECT 0,0,32,32,RGB(255,0,0)
GRABSPRITE 0,0,0,32,32

WHILE TRUE
        // Diese Werte kann man anpassen, um ein anderes Gefühl für das
        // Spiel zu erzeugen.
        air_friction = 0.1 // Bremswirkung links/rechts
        gravity      = 0.2 // Erdanziehungskraft
        run_force    = 0.5 // Kraft wenn man links/rechts drückt
        jump_force   = 6   // Sprungkraft

       
        // Die Variable "dx" gibt an, um wieviel "Pixel" der Spieler
        // sich seitlich bewegt. Also die aktuelle "Geschwindigkeit".
        // Das Selbe gilt für dy, eben als Fallgeschwindigkeit (dy>0)
        // oder Sprunggeschwindigkeit (dy<0).
       
        // *** Sprung ***
        IF KEY(57) // Space Taste
                IF OnGround(pos_x, pos_y, platform[]) // Steht er auch auf dem Boden?
                        dy = -jump_force                  // Super-Sprungkraft, indem er schnell hoch"fliegt"
                ENDIF
        ELSE // Sprungtaste wurde losgelassen
                IF dy<0 THEN dy=0 // falls er noch springt, stoppen
        ENDIF // Sprungtaste
       
       
        // *** Erdanziehung ***
        dy=dy + gravity     // immer schneller fallen
        IF dy>4 THEN dy=4       // Fall-Geschwindigkeit begrenzen,
                                                // damit er nicht auf einmal 32 Pixel fällt
                                                // und durch eine Platform durchfällt
       
       
        // *** Seitliche Bewegung ***
        IF KEY  (203) THEN dx=dx -run_force             // Tastendruck nach links, bewegt den Ball nach links
        IF KEY  (205) THEN dx=dx +run_force             // Tastendruck nach rechts, bewegt den Ball nach rechts
        IF dx> 2 THEN dx=2
        IF dx<-2 THEN dx=-2

        // "Luftreibung" -> den Spieler langsamer werden lassen
        IF dx>0 THEN dx=MAX(0.0, dx - air_friction)
        IF dx<0 THEN dx=MIN(0.0, dx + air_friction)


        // *** Kollision ***
       
        // Versuchen, ob man seitlich bewegen kann
        IF dx<>0 AND IsValidPosition(pos_x+dx, pos_y, platform[])
                pos_x = pos_x + dx
        ELSE
                dx=0 // angestoßen
        ENDIF
       
        // Versuchen zu springen/fallen
        IF IsValidPosition(pos_x, pos_y+dy, platform[])
                pos_y = pos_y+dy
        ELSE
                dy=0 // kopf angehauen
        ENDIF



        // *** Anzeige ***     
        ShowPlatforms(platform[])

        // Spieler pos_x ist die Mitte, also -16
        //         pos_y ist unten, also -32
        //         Mein "Spieler" ist 32x32 Pixel groß
        DRAWSPRITE 0, pos_x-16, pos_y-32

        SHOWSCREEN

WEND



// -----------------------------------------
// Zeigt alle Platformen an
// -----------------------------------------
FUNCTION ShowPlatforms: plats[]
LOCAL x,y
        FOR x=0 TO BOUNDS(plats[],0)-1
                FOR y=0 TO BOUNDS(plats[],1)-1
                        IF plats[x][y] = 1 // platform
                                DRAWRECT x*32, y*32, 32, 32, RGB(0,0,255)
                        ENDIF
                NEXT
        NEXT
ENDFUNCTION




// -----------------------------------------
// Gibt den Block-Wert im Feld plats[] zurück,
// der bei den Pixel-Koordinaten pos_x, pos_y ist
// -----------------------------------------
FUNCTION GetBlockAt: pos_x, pos_y, plats[]
LOCAL x,y
        x = INTEGER(pos_x/32)
        y = INTEGER(pos_y/32) // 32 = Blockgröße -> Von pixeln in Blocks umrechnen

        // Sicherheits-Check, damit wir nicht abfragen
        // wo das Feld nicht definiert ist
        IF x<0 OR x>=BOUNDS(plats[],0) THEN RETURN 0 // am Rand ist leer
        IF y<0 THEN RETURN 0 // über dem Feld ist Luft
        IF y>=BOUNDS(plats[],1) THEN RETURN 1 // unter dem Feld ist Boden
       
        RETURN plats[x][y]
ENDFUNCTION

// -----------------------------------------
// Kann ein Spieler an der Position pos_x, pos_y
// stehen, oder "haut er sich den Kopf an"?
// -----------------------------------------
FUNCTION IsValidPosition: pos_x, pos_y, plats[]
LOCAL x, y, hit
        // Von -15 bis x+15 pixeln prüfen, ob hier was ist
        FOR x = -15 TO 16 STEP 5
                // vom Kopf bis zu den Füßen prüfen
                FOR y=-30 TO 0 STEP 5
                        // Was ist an dem Punkt?
                        hit = GetBlockAt(pos_x+x, pos_y+y, plats[])
                        // Eine Wand? Da darf man nicht stehen!
                        IF hit=1 THEN RETURN FALSE
                NEXT
        NEXT
        RETURN TRUE
ENDFUNCTION

// -----------------------------------------
// Steht eine Figur an pos_x, pos_y auf dem
// Boden?
// -----------------------------------------
FUNCTION OnGround: pos_x, pos_y, plats[]
LOCAL hit, x
       
       
        FOR x=-15 TO 15 STEP 5 // komplett under dem Spieler prüfen
                hit = GetBlockAt(pos_x + x, pos_y+4, plats[])
                SELECT hit
                        CASE 1 // Boden - ja, darauf kann man stehen
                                RETURN TRUE
                ENDSELECT
        NEXT

        RETURN 0 // scheint keinen Boden zu haben
ENDFUNCTION

 


Ich danke für Ihre Aufmerksamkeit :D
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Brick Redux on 2013-Apr-29
Nice one Schranz0r, thanks for sharing.
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: CW on 2013-Apr-29
Hey that's Fun!  :good:

-CW
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Schranz0r on 2013-Apr-29
BOAH... thats OLD :D
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: CW on 2013-Apr-29
2007? LOL. I didn't see that.
Still fun though.  I might make a game out of it. :)
-CW

(Sigh) If that's old... than I'm Ancient!
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Schranz0r on 2013-Apr-29
(Sigh) If that's old... than I'm Ancient!

 :P
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: MrTAToad on 2013-May-03
You might also be interested in the platforming code conversions I did a while ago, that are somewhere on these forums...
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Corax on 2013-Jul-18
I enhanced this code a bit:
JaRE.zip - 12.6 Mb (http://www2.zshares.net/ur4jy8otci7d)

I tried to translate the german comments.
SchranzOr:
How do you translate: Super-Sprungkraft, indem er schnell hoch"fliegt"?

If you know more efficient ways for this program - please post them.
Don´t forget to comment them, since this is a tutorial thread.

Since SchranzOr already coded the hard stuff (physics), it should
be fairly easy to add other things like scrolling, adventure-elements
(like in "Cave Story"), NPCs etc.

The current code should be easy to analyze but here are some
additional explanations:

-
Click on the "?"-icon (in title screen) for basic information

-
If you want to create own levels, press [^] (under ESC) in game mode,
draw your level and don´t forget to press [´s´] to save it before
leaving this mode! You can navigate through the levels with [ARROW LEFT]
and [ARROW RIGHT]. They are saved with their index + ".lvl" (in the "Levels"
directory). When the program doesn´t find a file with the next higher index
after finishing a level, it executes "fnVictory()".

-
You can access the "settings" mode with [TAB] in the editor, but
the only functional icon is "Exit" yet.

Set Rednag:
Hold down [Strg], left click at starting point, move mouse to destination
point and release the key.

Set Ghostfloater:
Hold down [Strg], left click, move mouse to starting
point and release the key.

[Strg] + [´x´]: erases all creatures on map
NOTE: you can place up to ten creatures per level

-
How to implement a new Creature:

Copy the graphics into the "GFX" directory and load them in "subINI:".

Create three new SUBs (in "Creatures.gbas"):
The first one is executed when setting the creature in editor mode.
The second one displays the creature in editor mode.
The third SUB computes the creature in the actual game.
To make it easier, I created three templates for this.
Name convention:
"subEditC" + n(next higher index), "subDisplayC" + n and "subC" + n.

Increase cMAX_C_IDX by 1 (jump to "_CONSTANTs:")
This is important, because the code will ignore your code otherwise.

- - -
I wanted to add more features, but don´t had the time.
I know that the "game" is crappy in it´s current state, so don´t be
shy with criticism (and optimization proposals).
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Schranz0r on 2013-Jul-24
We need a new version ?!
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: erico on 2013-Jul-24
I gave it a go, works great!

I didn´t read the code, but here are some criticism:

- collision detection could have a smaller radius/square then current. It feels strange when the player stops on an invisible wall, particularly the borders of platforms, where the tile is not quite square.
- when jumping on a no-space-to-jump situation, the jump sound gets repeated while no action goes on.


Again, it works great, now who is going to make a King´s Valley with it? ;)
 
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Brick Redux on 2013-Jul-29
Look to me like your code isnt old anymore Schranz0r. :)
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Corax on 2013-Aug-01
Quote
We need a new version ?!

Since you named the thread "Jump`n Run Tutorial 1 (Animation)",
I thought there could be a "Jump`n Run Tutorial 2 (...)".
Do you plan something like this?

Quote
- collision detection could have a smaller radius/square then current. It feels strange when the player stops on an invisible wall, particularly the borders of platforms, where the tile is not quite square.
- when jumping on a no-space-to-jump situation, the jump sound gets repeated while no action goes on.

The game is pretty crude right now. If there is enough interest, I
could rewrite the code with pixel-perfect collision and fix the
sound problem.

I was informed that:

Super-Sprungkraft, indem er schnell hoch"fliegt"?

would tranlsate to:
Super jump power, by making him fly up fast.
(or "faster" - I think Schranz0r changed the start jump speed for a high-power jump, right?)
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Brick Redux on 2013-Aug-09
I see what youve done and hope you will rewrtie it.  Nice one. :good:
Title: Re: Jump`n Run Tutorial 1 (Animation)
Post by: Heiko on 2013-Sep-07
auch wenns alt ist...sehr schön....
nun noch scrolling dazu wenn sich der player ausserhalb eines x_min - x_max blockes bewegt....
wenn ich mein jump and run fertig hab, poste ich mal den code.
hab bisher nur tilemap scrolling mit einem festen player.