Mario Collisions

BlitzMax Forums/BlitzMax Programming/Mario Collisions

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here you go, I made this. Tell me if anything is confusing.
Oh I stole your map :P

SuperStrict

Graphics 800 , 600 , 0


Const Gravity:Float = .1


Type TPlayer
	Field x:Float,y:Float
	Field width:Float , height:Float
	Field speedx:Float , speedy:Float
	
	
	Method Draw()
		DrawRect x , y , width , height
	EndMethod
	
	Method Update() 
		
		'ground friction
		speedx:* .85
		
		'x conrtols
		If KeyDown(key_left) Then speedx = - 2
		If KeyDown(key_right) Then speedx = 2
		
		'x movement
		x:+ speedx
		
		'if you ran into a wall
		If map.rectcollide(x , y , width , height) Then
			'move back out of the wall
			x:- speedx
			'stop your speed
			speedx = 0
		EndIf
		
		
		'y controls (jump)
		If KeyHit(key_up) Then
			'make sure you are on the ground
			If map.rectcollide(x , y + 1 , width , height) Then
				'jump!
				speedy = - 5
			EndIf
		EndIf
		
		'y movement
		speedy:+ gravity
		y:+ speedy
		
		'if you hit a wall
		If map.rectcollide(x , y , width , height) Then
			'move back out of the wall
			y:- speedy
			'stop your speed
			speedy = 0
		EndIf
		
	EndMethod
EndType


Type TMap
	Field width:Int , height:Int
	Field array:Int[,]
	Const tilesize:Int = 30
	
	Method Load()
		Local x:Int,y:Int
		Local line:String
		
		ReadData width
		ReadData height
		
		array=New Int[width,height]
		
		For y = 0 Until height
			ReadData line
			For x = 0 Until width
				array[x , y] = Int(Mid(line , x + 1 , 1) )
			Next
		Next
	EndMethod
	
	
	Method Draw()
		Local x:Int , y:Int
		
		For y = 0 Until height
			For x = 0 Until width
				If array[x , y] = 1 Then
					DrawRect x * tilesize , y * tilesize , tilesize , tilesize
				EndIf
			Next
		Next
	EndMethod
	
	
	'Returns True if the rectangle collides with the map.
	'This one and only collision function can be used for everything.
	Method RectCollide:Int(x:Float , y:Float , width:Float , height:Float)
		Local x1:Int , y1:Int
		Local x2:Int , y2:Int
		
		'convert pixel positions to tile positions
		x1 = x / tilesize
		y1 = y / tilesize
		x2 = (x + width) / tilesize
		y2 = (y + height) / tilesize
		
		Local tilex:Int , tiley:Int
		
		'check each tile that the rect is in
		For tiley = y1 To y2
			For tilex = x1 To x2
				If array[tilex , tiley] = 1 Then
					Return True
				EndIf
			Next
		Next
		
		Return False
	EndMethod
	
EndType



Global You:tplayer = New tplayer
you.x = 200
you.y = 100
you.width = 20
you.height = 25


Global Map:tmap = New tmap
map.load()



Repeat
	you.update()
	you.draw()
	map.draw()
	Flip
	Cls
	If KeyHit(key_escape) Then End
Forever



DefData 20,15
DefData "11111111111111111111"
DefData "11010101010101010101"
DefData "10000000000000000001"
DefData "11110000000000111111"
DefData "10000000000000000001"
DefData "10000000000000000001"
DefData "10000000111100000001"
DefData "10000000000000000001"
DefData "11110000000000000001"
DefData "10000000000000000001"
DefData "10000000001110001111"
DefData "10000000000000000001"
DefData "10000000000000000001"
DefData "10000000000000000001"
DefData "11111111111111111111"