mario!

Community Forums/Showcase/mario!

z80jim(Posted 2008) [#1]
I have plenty enough to show here, so I think it's worth it's own thread. I'm just trying to learn and have fun as usual, and I figured a mario like game would be a great place to start. All images were downloaded at http://gsarchives.net/index2.php?category=all&system=nes&game=super_mario_bros_3
and all sounds were downloaded at http://vgwallpaper.classicgaming.gamespy.com/sounds.htm
all are registered trademarks of nintendo. You will need to replace my images with your own before it will run, animation is not necessary to display my achievements.
I started with an unknown persons basic mario collision code, then built off of it, adding my own images and map etc..
have fun, crits and ideas wanted!


SuperStrict
'load images, sounds, and a few misc variables.
Global Tiles:TImage = LoadAnimImage("tiles.png", 32, 32, 0, 10)
Global mario:TImage = LoadImage("mario.png",0)
Global marioLeft:timage = LoadAnimImage("mario left.png", 16, 26, 0, 4)
Global marioright:timage = LoadAnimImage("mario right.png", 16, 26, 0, 4)
Global coin:TSound=LoadSound("Coin.wav",0)
Global win:TSound=LoadSound("win stage.wav",0)
Global start:TSound=LoadSound("start stage.wav",0)
Global music:TSound=LoadSound("sm3 grass.wav",0)
Global jump:TSound=LoadSound("mario jump.wav",0)
Global bump:TSound=LoadSound("cannon shot.wav",0)
Global score:Int=0
Global total:Int
Global tilex:Int
Global tiley:Int
Global i:Int
Global loadmap:Int=0

Graphics 1400 , 900 , 0


Const Gravity:Float = .1'gravity... have fun with this number :D


Type TPlayer
Field x:Float,y:Float
Field width:Float , height:Float
Field speedx:Float , speedy:Float
Field leftanim:Float= 0
Field rightanim:Float= - 1

Method Draw()'draw our hero!
'DrawImage mario, x , y' , width , height
If leftanim=>0 Then DrawImage marioleft, x, y, leftanim
If rightanim=>0 Then DrawImage marioright, x, y, rightanim
EndMethod

Method Update()

'ground friction
speedx:* .85

'x controls
'this section is messy but I will make it neater eventually
If KeyDown(key_left)
speedx = - 2
If KeyDown(KEY_LCONTROL) Then speedx = -4
leftanim=leftanim+.5
Rightanim = - 1
If leftanim=4 Then leftanim=0
EndIf
If KeyDown(KEY_right)
speedx = 2
If KeyDown(key_lcontrol) Then speedx = 4
rightanim=rightanim+.5
leftanim = - 1
If rightanim=4 Then rightanim=0
EndIf
If Not KeyDown(key_left) And Not KeyDown(key_right)
If leftanim=>0 Then leftanim=0
If rightanim=>0 Then rightanim=0
EndIf

'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
PlaySound jump
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
Field location:Int=0

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
'draws the map
If array[x, y] = 0
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 0
ElseIf array[x, y] = 1
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 1
ElseIf array[x, y] = 2
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 2
ElseIf array[x, y] = 3
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 3
ElseIf array[x, y] = 4
DrawImage Tiles, x* TILESIZE, y * TILESIZE, 4
ElseIf array[x, y] = 5
DrawImage Tiles, x * TILESIZE,y * TILESIZE, 5
ElseIf array[x, y] = 6
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 6
ElseIf array[x, y] = 7
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 7
ElseIf array[x, y] = 8
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 8
ElseIf array[x, y] = 9
DrawImage Tiles, x * TILESIZE, y * TILESIZE, 9
EndIf
Next
Next
EndMethod


'Returns True if the rectangle collides with the map.
'This one and only collision function can be used for everything.
'I did not make this, I can not claim to yet understand very much what is going on in here, though I did add all the "exceptions" to collisions.
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

'check each tile that the rect is in
For tiley = y1 To y2
For tilex = x1 To x2
If array[tilex , tiley] => 1 And array[tilex , tiley]<>6 And array[tilex , tiley]<>8 And array[tilex , tiley]<>9 And array[tilex , tiley]<>7 Then
Return True
EndIf
If array[tilex , tiley]=9
SetColor 255,255,255
you.draw()
map.draw()
SetColor 0,0,0
PlaySound win
Delay 4000
For i=1 To score
PlaySound coin
total=total+1
Delay 150
Next
score=0
Flip
Delay 1000
Cls
End
EndIf
If array[tilex , tiley]=8
array[tilex , tiley]= 0
PlaySound coin
score=score+1
EndIf
If array[tilex , tiley]=7
PlaySound bump
array[tilex , tiley]=1
you.x=you.x-you.speedx
you.y=you.y-you.speedy
EndIf
Next
Next

Return False
EndMethod

EndType


'//create a player
Global You:tplayer = New tplayer
you.x = 800
you.y = 800
you.width = 16
you.height = 26


Global Map:tmap = New tmap
map.Load()'loads the map below


PlaySound start'mario begin level sound
Repeat'main loop
Cls
SetColor 0,255,255
DrawRect 0,0,1500,1000
SetColor 255,255,255
you.update()
you.draw()
map.draw()
SetColor 0,0,0
DrawText "score: "+score,you.x-TextWidth("score: "+score)/2,you.y-13
SetColor 255,255,255
Flip
If KeyHit(key_escape) Then End
Forever

'0 is air, 1 is brick, 2 is left platform, 3 is middle platform, 4 is right platform, 5 is blue square, 6 is fake brick.
'7 is invisible brick, 8 is coin, 9 is flag.
'//map
DefData 150,30
DefData "1111111111111111111111111111111111111111111111"
DefData "1000000000000000000000000000000000000000000091"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1800000000000000000000000000000000000000000001"
DefData "1800000000000000000000000000000000000000000001"
DefData "1800000000000000000000000000000000000000000001"
DefData "1800800000000000000000000000000000000000000001"
DefData "1808000000000000000000000000000000000000000001"
DefData "1887000000000000000000000000000000000000000001"
DefData "1807000000000000000000000000000000000000000001"
DefData "1000700000000000000000000000000000000000000001"
DefData "1600070000000000000000000000000000000000000001"
DefData "1610007000700000000000000000000000000000000001"
DefData "1610000700700000000000000000000000000000000001"
DefData "1610000070706000000000000000000000000000000001"
DefData "1611000007701007770000000000000000000000000001"
DefData "1611100000701000000000000000000000000000000001"
DefData "1666610000071000000000000000000000000000000001"
DefData "1111611000007000000000000000000000000000000001"
DefData "1111611100000700000000000000000000000000000001"
DefData "1666611110000700000000000000000000000000000001"
DefData "1666611111000700000000000000000000000000000001"
DefData "1666611111100700000000000000000000000000000001"
DefData "1611111111117777000000000000000000000000000001"
DefData "1666666666666697000000000000000000000000000001"
DefData "1111111111111111111111111111111111111111111111"

Rem
this is just to copy and paste a cleared board for when you want to make a new map
DefData "1111111111111111111111111111111111111111111111"
DefData "1000000000000000000000000000000000000000000091"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1000000000000000000000000000000000000000000001"
DefData "1111111111111111111111111111111111111111111111"
EndRem


GfK(Posted 2008) [#2]
You will need to replace my images with your own before it will run
I'm pretty sure that the above sentence alone is going to ensure that nobody even looks at this.

Nobody of sound mind is going to download a heap of graphics and sounds just so they can look at your code.

You need to compile an executable, include some proper graphics, then come back with that. Because what you're asking us to do is, frankly, too much trouble.


Digital Anime(Posted 2008) [#3]
Same here, haven't tried it yet, but some nice coding so far.

One idea is to save away the level data in a file.

I did this in my Pacman clone project RGB_Man, with loose level files which can be made with notepad :-)


GfK(Posted 2008) [#4]
Same here, haven't tried it yet, but some nice coding so far.
Um, not really. The code at a quick glance exhibits a bit of inexperience. For example, the Draw() method, where 9x more code has been written than is necessary:

Method Draw()
Local x:Int , y:Int

For y = 0 To height
For x = 0 To width
DrawImage Tiles, x * TILESIZE, y * TILESIZE, array[x, y]
Next
Next
EndMethod



z80jim(Posted 2008) [#5]
first, I'm sorry I was unexpectedly cut off and I didn't have time to finish. I said that you should replace my tiles with simple squares or something, but never mind about that. I will put up the folder with the images, sounds and code as soon as I can. I have a mac so the app won't work for most of you, so you will have to compile it yourself in the end.

digital, I did that with an earlier version, but for instant and easy gratification, and until I get an editor working with this revised edition, I will just do it like this.

gfk, just for the record I never said my code is great :D I just want it to get done what I want it to do at the moment, once I've done that I try shortening it. Thanks for pointing that out anyways though!

So in conclusion, as soon as I find a proper file sharing website to sign up with, I will give you guys the complete folder including the images and sounds. Btw, does anyone know where I can get a level background music in .wav format? apparently midi and mp3 aren't accepted by blitzmax :(


therevills(Posted 2008) [#6]
Hi z80jim

I find a proper file sharing website to sign up with


If you have got a Gmail account, you can create a googlepages website where you get 100MB storage space free...

http://pages.google.com/


z80jim(Posted 2008) [#7]
I don't but I found a way to. I already could put em up this way but it's a pain when you have a lot of files to put up, but I will do it soon.


z80jim(Posted 2008) [#8]
here is the website with all the files available for downloading.
http://web.mac.com/lipton_lover/mario/my_mario.html


therevills(Posted 2008) [#9]
Why dont you just zip the files up and upload the zip file?


z80jim(Posted 2008) [#10]
here ya guys go then
http://web.mac.com/lipton_lover/mario/my_mario.html
I was tired lol.
Btw, for this to work you need to always go there from the latest link I give you, even though they are identical the changes won't be there otherwise so use the link in this post instead of the other one. I also included an email me button, so if you have changes to the code you would like to show me, or files like the mario music in .wav format I mention on the website, you can just email me!


Amon(Posted 2008) [#11]
I zipped it all up with an executable.

Can be downloaded Here

:)


z80jim(Posted 2008) [#12]
thanks amon :) but you're going to have to do that every time I update it then...


Amon(Posted 2008) [#13]
That's ok. Just send the updates to me (email in profile) and I'll upload it to my server. :)


z80jim(Posted 2008) [#14]
thanks :)
and as a side note, included in the file I upload is a compiled mac build, called as I'm sure you know blockmario.debug.
Also, to find the date and what was included in each update, check the website, though I will also post the release notes here for easy viewing.


z80jim(Posted 2008) [#15]
I'm having a bit of trouble...
I know I won't be using the current setup for the end product's map editor, I will use a graphical one, but I want to get it to go to the next level. Looking at the code, can someone give me an easy way to read a selected bit of the lines of data, so when I finish a map it can load the next set of data lines for the next level? I can't get it to work...


z80jim(Posted 2008) [#16]
updated.
http://web.mac.com/lipton_lover/mario/my_mario.html
here's the release note:

Monday, March 3rd 2008
Added bouncy boxes that you can walk through, most of my efforts went towards working on a graphical map editor, not having much luck so far though.


Amon(Posted 2008) [#17]
Cool! Will check it out in a bit. I'm just messing with some of Brucey's modules.

As for your data reading problem use restoredata and a label.

Run the code below and press space to change level.

SuperStrict

Graphics 800, 600

Global Array:Int[25, 18] 

Global Change_Level:Int = 0

Global Level:Int = 1

While Not KeyHit(KEY_ESCAPE) 
	Cls
		
		If KeyHit(KEY_SPACE) 
			Change_Level = 1
		End If
		
		If Change_Level = 1 And level = 1
			RestoreData Level2
			Local data:Int
			For Local y:Int = 0 Until 18
				For Local x:Int = 0 Until 25
					ReadData data
					Array[x, y] = data
				Next
			Next
			Change_Level = 0
			Level = 2
		ElseIf Change_Level = 1 And Level = 2
			RestoreData Level1
			Local data:Int
			For Local y:Int = 0 Until 18
				For Local x:Int = 0 Until 25
					ReadData data
					Array[x, y] = data
				Next
			Next
			Change_Level = 0
			Level = 1
		End If
		
		
		For Local x:Int = 0 Until 25
			For Local y:Int = 0 Until 18
				If Array[x, y] = 1
					DrawRect X * 32, y * 32, 32, 32
				End If
			Next
		Next
		
		
	Flip
Wend

#Level1
DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 1, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1



z80jim(Posted 2008) [#18]
thanks a lot for that code, but I really don't know a lot of what's going on lol. Why do I have to use restore data instead of simply specifying a new section of data to grab? Why did you do int defdata instead of string? lol. Just please add some comments and I should be good then :)


Amon(Posted 2008) [#19]
Ok, I've commented the code below.

You specify a new set of data to be read by using a label.
this telld the readdata command to read a new set of data statements.

I did int defdata because it is just a simple example. You could use tsrings if you wanted but I find it simpler to use ints to represent my tile data.





z80jim(Posted 2008) [#20]
I can see how ints would be easier, I can't go past 9 atm lol. I'll see if I can figure out how to change mine to ints.... can I get away with simply changing it in the data, adding the commas?