Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Imhotep), 567 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Ultra Simple Platformer Script #458738
03/28/16 17:15
03/28/16 17:15
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline OP
Expert
MasterQ32  Offline OP
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
Just a small demonstration of how to create a simple platformer system with jumping and collision with platforms in 2D.
No use of any engine functions except for draw_quad and draw_line.

Code:
/**
 * Licence:
 * Public Domain
 */

#include <acknex.h>
#include <keys.c>

typedef struct {
	float x0, x1, y;
} Platform;

Platform platforms[10];

// Noteworthy information:
// the game logic handles y coordinates in mathematical way: +y is up, -y is down.
// y = 0 is the bottom of the screen
// all coordinates are in pixels, pixels/tick or pixels/tick²

function main()
{
	fps_max = 60;
	int i;
	
	// Initialize some random platforms
	for(i = 0; i < 10; i++) {
		platforms[i].x0 = random(screen_size.x - 64);
		platforms[i].x1 = platforms[i].x0 + 64;
		platforms[i].y = 64 + random(screen_size.y - 128);
	}
	
	wait(1);
	
	// Start on the first platform
	var playerX = 0.5 * (platforms[0].x0 + platforms[0].x1);
	var playerY = platforms[0].y;
	
	var velX = 0.0;
	var velY = 0.0;
	
	wait(1);
	
	while(1) {
	
		// move left/right
		if(key_cul) {
			velX -= 10 * time_step;
		} else if(key_cur) {
			velX += 10 * time_step;
		}
		
		// damp our movement a little so we will come to a stop
		velX *= 0.8;
		
		// Apply gravity
		velY -= 5 * time_step;
		
		// Check collision with each platform to disable downwards movement
		var isStanding = 0;
		for(i = 0; i < 10; i++) {
			// Check for left/right bound
			if((playerX + 10) < platforms[i].x0) {
				continue;
			}
			if((playerX - 10) > platforms[i].x1) {
				continue;
			}
			
			// If we are in range around the platform, we are standing on it
			if(abs(playerY - platforms[i].y) <= 1) {
				isStanding = 1;
			}
			
			// Extrapolate player movement in newY
			var oldY = playerY;
			var newY = playerY + velY * time_step;
			
			// If we start moving below the platform, just ignore the platform
			if(oldY < platforms[i].y)
				continue;
			
			// If we end moving above the platform, also ignore the platform
			if(newY > platforms[i].y)
				continue;
			
			// If we move from above the platform below the platform, we should collide with it:
			
			// Cheat ourselves to the platform top
			playerY = platforms[i].y;
			
			// Stop moving in vertical direction
			velY = 0.0;
			
			// We are also standing if we collided with the platform
			isStanding = 1;
		}
		
		if(isStanding == 1) {
			// Apply jump force
			if(key_hit("space"))
				velY += 32;
		}
		
		// Move our player
		playerX += velX * time_step;
		playerY += velY * time_step;
	
	
		// Draw the player. (playerX, playerY) is the center position of our players feet.
		draw_quad(
			NULL,
			vector(playerX - 10, screen_size.y - playerY - 40, 0),
			NULL,
			vector(20, 40, 0),
			NULL,
			COLOR_BLUE,
			100,
			0);
	
		// Draw each platform
		for(i = 0; i < 10; i++) {
			// this is engine specific, moves the drawing cursor
			draw_line(vector(platforms[i].x0, screen_size.y - platforms[i].y, 0), NULL, 100);
			
			draw_line(vector(platforms[i].x0, screen_size.y - platforms[i].y, 0), COLOR_RED, 100);
			draw_line(vector(platforms[i].x1, screen_size.y - platforms[i].y, 0), COLOR_RED, 100);
		}
	
		wait(1);
	}
}



Visit my site: www.masterq32.de
Re: Ultra Simple Platformer Script [Re: MasterQ32] #458744
03/28/16 22:07
03/28/16 22:07
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline
Newbie
Nicros  Offline
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Works very nice so far wink .
But had to include the function 'randomize()' at the beginning to make the positions of the platforms really random.

Last edited by Nicros; 03/28/16 22:08.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1