Skip to content

Basic Platformer Template Using Scene Group Arrays in AGK Studio

// Project: TrapDodge
// Created: 19-04-05

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle("TrapDodge")
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1) // allow the user to resize the window

// set display properties
SetVirtualResolution(1024, 768) // doesn't have to match the window
SetOrientationAllowed(1, 1, 1, 1) // allow both portrait and landscape on mobile devices
SetSyncRate(60, 0) // 30fps instead of 60 to save battery
SetScissor(0, 0, 0, 0) // use the maximum available screen space, no black borders
UseNewDefaultFonts(1)

#insert "constants.agc"
#insert "Game.scene"

img_Dave = LoadImage( "dave.png" )
spr_Dave = CreateSprite(img_Dave)
SetSpriteAnimation(spr_Dave, 32, 32, 12)

global hspeed as float = 0
global vspeed as float = 0
global WalkSpeed as float = 2.5
global Gravity as float = 0.3
global Move as integer

global KeyLeft as integer
global KeyRight as integer
global KeyJump as integer

global Player as integer
global PlayerX as float
global PlayerY as float

PlayerX = GetSpriteX(spr_Dave)
PlayerY = GetSpriteY(spr_Dave)

global DaveState as integer = 0



Game_setup()

do
	Game_sync()

	KeyLeft = GetRawKeyState(KEY_LEFT)
	KeyRight = GetRawKeyState(KEY_RIGHT)
	KeyJump = GetRawKeyState(KEY_CONTROL)
	
	
	Move = KeyRight - KeyLeft
	hspeed = Move * WalkSpeed
	vspeed = vspeed + Gravity
	
	if Move = 0 and DaveState = 0
//		SetSpriteFlip(spr_Dave, 0, 0)
		PlaySprite(spr_Dave, 6, 1, 7, 10 )
		DaveState = -1
	elseif Move = 1 and DaveState = -1
		SetSpriteFlip(spr_Dave, 0, 0)
		PlaySprite(spr_Dave, 8, 1, 1, 6)
		DaveState = 0
	elseif Move = -1 and DaveState = -1
		SetSpriteFlip(spr_Dave, 1, 0)
		PlaySprite(spr_Dave, 8, 1, 1, 6)
		DaveState = 0	
	endif

	
	for x = 0 to Game_CubeArray.length
		if RectsOverlap(PlayerX, PlayerY+1, 28, 32, GetSpriteX(Game_CubeArray[x]), GetSpriteY(Game_CubeArray[x]), 32, 32) and KeyJump = 1
			vspeed = -6.5
		endif
		//Horizontal Collision
		if RectsOverlap(PlayerX+hspeed,PlayerY, 28, 32, GetSpriteX(Game_CubeArray[x]), GetSpriteY(Game_CubeArray[x]), 32, 32)
			while not RectsOverlap(PlayerX+sin(hspeed),PlayerY, 32, 32, GetSpriteX(Game_CubeArray[x]), GetSpriteY(Game_CubeArray[x]), 32, 32)
				PlayerX = PlayerX + sin(hspeed)
			endwhile
			hspeed = 0
		endif
		//Vertical Collision
		if RectsOverlap(PlayerX, PlayerY+vspeed, 28, 32, GetSpriteX(Game_CubeArray[x]), GetSpriteY(Game_CubeArray[x]), 32, 32)
			while not RectsOverlap(PlayerX, PlayerY+sin(vspeed), 32, 32, GetSpriteX(Game_CubeArray[x]), GetSpriteY(Game_CubeArray[x]), 32, 32)
				PlayerY = PlayerY + sin(vspeed)
			endwhile
			vspeed = 0
		endif
	next
	
	PlayerX = PlayerX + hspeed
	PlayerY = PlayerY + vspeed
	
	SetSpritePosition(spr_Dave, PlayerX, PlayerY)
	print(Move)
	Sync()
loop

function RectsOverlap(x1, y1, w1, h1, x2, y2, w2, h2)
	value = 1
	If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then value = 0
	If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then value = 0
endfunction value