BASIC

Author Topic: Jump`n Run Tutorial 1 (Animation)  (Read 7736 times)

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Jump`n Run Tutorial 1 (Animation)
« 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
« Last Edit: 2009-Feb-20 by Kitty Hello »
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Brick Redux

  • Mr. Drawsprite
  • **
  • Posts: 60
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #1 on: 2013-Apr-29 »
Nice one Schranz0r, thanks for sharing.
A mournful owner of a HP HDX18 Laptop that has died...FECK!

Offline CW

  • Mr. Polyvector
  • ***
  • Posts: 155
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #2 on: 2013-Apr-29 »
Hey that's Fun!  :good:

-CW

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #3 on: 2013-Apr-29 »
BOAH... thats OLD :D
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline CW

  • Mr. Polyvector
  • ***
  • Posts: 155
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #4 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!

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #5 on: 2013-Apr-29 »
(Sigh) If that's old... than I'm Ancient!

 :P
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrTAToad

  • Guest
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #6 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...

Offline Corax

  • Mr. Drawsprite
  • **
  • Posts: 57
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #7 on: 2013-Jul-18 »
I enhanced this code a bit:
JaRE.zip - 12.6 Mb

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).

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #8 on: 2013-Jul-24 »
We need a new version ?!
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4179
    • View Profile
    • Portfolio
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #9 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? ;)
 

Offline Brick Redux

  • Mr. Drawsprite
  • **
  • Posts: 60
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #10 on: 2013-Jul-29 »
Look to me like your code isnt old anymore Schranz0r. :)
A mournful owner of a HP HDX18 Laptop that has died...FECK!

Offline Corax

  • Mr. Drawsprite
  • **
  • Posts: 57
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #11 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?)

Offline Brick Redux

  • Mr. Drawsprite
  • **
  • Posts: 60
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #12 on: 2013-Aug-09 »
I see what youve done and hope you will rewrtie it.  Nice one. :good:
A mournful owner of a HP HDX18 Laptop that has died...FECK!

Offline Heiko

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 498
    • View Profile
Re: Jump`n Run Tutorial 1 (Animation)
« Reply #13 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.
Ryzen 5 2400G - MSI B450 Tomahawk