Hey Kaisuo, This was posted on BlitzCoder.com by Nebula. Its a wonderfully commented, simple to understand tutorial on how to make a basic basic platformer. Its invaluable to noobs.
; (Coded by Nebula)
; (date : 02-2001)(update : 04-2001)
; (Commented in 12-2003)
; Visit <A HREF="http://www.geocities.com\nebula_rve" TARGET=_blank>www.geocities.com\nebula_rve</A> for more examples
; Visit <A HREF="http://hardcorecoder.com" TARGET=_blank>http://hardcorecoder.com</A> for DaCodez
;
;
Function Introduction()
;
; Small Platform Example - Re-documented in 2003
;
;
;
;
;
End Function
Graphics 640,480
SetBuffer BackBuffer()
;
; Load 2 images. One being the 'dude' that will be controlled by the player. The other 'tile'
; will be used to draw the game playing field.
;
Global dude = CreateImage(32,32);LoadImage("dude.bmp")
Global tile = CreateImage(32,32);LoadImage("tile.bmp")
;
SetBuffer ImageBuffer(dude)
ClsColor 255,255,255
Cls
SetBuffer ImageBuffer(tile)
ClsColor 100,100,100
Cls
SetBuffer BackBuffer()
ClsColor 0,0,0
;
; Create two arrays that are required to display the map. The 'map' array will hold each tile that is drawn/displayed
; on the screen.
;The map1$ array hold the level information. This array is a string because the levels are stored inside this
; sourcecode as text. Using text means more then 10 characters can be used to build the screen.
Dim map(19,14)
Dim map1$(14)
;
; Here the regular global game values are set up. Being global means these variables can be accesed from
; anywhere inside the program.
; dudex = holds the character x position. The x position is the most left position of the sprite/image.
; dude7 = holds the character y position. This means the most top position of the sprite.
; Jump = holds TRue when the player jumped and holds false when not or when landed.
; jumptimer# = a float value that is used to time/adjust the speed at which the jumping and
; falling happens.
;
Global dudex
Global dudey
Global jump
Global jumptimer#
; Start location - The player will begin at this position when the game starts.
dudex = 200
dudey = 320
;
; Here the level is set up. Each array string hold a number of lines.
; With the 1 value we will tell the program that there is a wall/floor there
; and with 0 we tell that there is nothing there that should be drawn.
;
map1$(0) = "1111111111111111111"
map1$(1) = "1000000000000000001"
map1$(2) = "1000000000000000001"
map1$(3) = "1000001111111111001"
map1$(4) = "1000000000000000001"
map1$(5) = "1111110000000000001"
map1$(6) = "1000000000000000001"
map1$(7) = "1111111100000000001"
map1$(8) = "1000000000000000001"
map1$(9) = "1000000011111111111"
map1$(10) = "1000000000000000001"
map1$(11) = "1111111000000000001"
map1$(12) = "1000000000000000001"
map1$(13) = "1000000001111111111"
map1$(14) = "1111111111111111111"
;
; Using strings inside game loops for quickly reading information is not the fastest method.
; By converting these values into integers they will become much faster.
; Blitz automatically converts string integers into integers. Using other strings than numbers
; requires special code.
;
;
;
For y=0 To 14 ; 14 = the number of vertical tiles that can fit in the screenmode. The map array is based on this.
For x=0 To 19 ; 19 is the number of horizontal tiles that can fit in the screenmode.
n = Mid(map1$(y),x+1,1)
map(x,y)=Int(n)
Next
Next
;
; This is the game loop. It exits when the escape key is pressed (keydown(1) = true)
;
While KeyDown(1) = False
Cls
drawmap() ; draw the graphics
movedude(); read the players movement
checkjump() ; update the code that controls the jump
drawsprite() ; draw the player
Flip
Wend
End
Function movedude()
;
; This function contains all code to move the player inside the game level.
;
;
;
; Keydown 203 tells the left cursor key is pressed. When it is pressed a local variable is initiated that will tell
; the code below that the key is pressed. Same goes with the 205 keycode which is the right cursor key.
;
If KeyDown(203)=True Then moveleft=True
If KeyDown(205)=True Then moveright=True
; border checking
;
; The width of a tile in this example is 32 pixels. This means our player which x position is the most left
; position of the player graphic would touch a wall when he is on the screen position of 32. Going below
; this position would mean he is inside a wall. This is why we will not allow him to go below 32.
; By resetting the previously set value we ensure he will not go thru a wall.
;
; The right side position of the screen is the number of tiles that can fit in the graphicsmode. Seeing this
; can be calculated by dividing the screenwidth and the tilewidth we know it is 20 tiles. Blitz however
; also uses Zero as a number so this means we should substract 1 from the actual number a calculator would give.
; When knowing the player width is also 32 and his draw coordinates are from the most left this means we should
; substract another 1 to get the correct position. This example leaves one 32 pixel space open to the right which
; means another substraction. This space could be used for game info ect.
; 17*32 will return the location of the outer wall of this map.
;
If dudex<32 Then moveleft=False
If dudex>(17*32) Then moveright=False
; movement x with or without jumping
;
; In this example we can jump thru platforms above the player. This is done by checking for collisions when
; the player is NOT jumping. This way he can move to the left and right and thru objects while going up.
; When he is not jumping the collision is checked and will the land on the first tile below him.
; The actual falling back on a tile is done in the jump function.
; The collision works by checking the array using the player coordinates. By dividing these with the
; width and height of a tile the array position gets returned. The array holds 1 for a tile. So by
; checking this value and not seing it we allow the player to move around.
;
; No gravity means checking collision. And Gravity means freedom in horizontal movement.
;
;
If jump=False Then
If Not map(dudex/32,dudey/32)=1 Then
If moveleft=True Then dudex=dudex-4
End If
If Not map((dudex+32)/32,dudey/32) = 1 Then
If moveright=True Then dudex=dudex+4
End If
Else
If moveleft=True Then dudex=dudex-3
If moveright=True Then dudex=dudex+3
EndIf
End Function
Function checkjump()
; The keydown 57 signals the space key is being pressed. When this happens
; the code then checks to see if no jump is actually in progress. If it is and it is not
; check then the player would keep moving up until he is out of the screen and this
; will cause the compiler to return a error(array out of bounds).
; When the player is not jumping the jump variable is set to TRUE and the jumptimer(or height)
; is set to its value.
;
If KeyDown(57)=True
If jump=False
jump=True
jumptimer=8
End If
End If
;
; While the character is in a jump a number of things should happen. Gravity has the effect
; that once you take off the ground the speed at which you travel decreases until it becomes
; negative(falling down). This happens slowly but incremental (ea speed+1 until 0 and speed-1 until ground)
; The code below uses a float value. Floats are integers divided into integers. The amount of integers in a float
; can be very high or low just like regular numbers. The jumptimer with which the jump is measured uses
; a value of .33. Which is 1/3 of the number 1.
; Above the jumpforce is set to 8. The speed starts at 8 pixels going up every cycle and decreases by
; .33 every cycle until it becomes negative and means the player wil start to move down. This also happens
; with .33 per cycle and thus emulates gravity. ea The player will stop for a while in the air and slowly start
; to fall down;
; Once a tile below the player is found (player coordinates divided by the tileheight and one extra to compensate
; for the height of the player) the gravity is disabled.
; Because the player is falling down using large chunks he would end up inside a tile. This is why once a collision
; happens the coordinates are 'RESET'' to the tile that it collides with. This will ensure no errors happen.
; Do remember that once the speed of the falling gets above the tileheight he will fall thru no matter what. This
; can be avoided by limiting the falling speed to a maximum. Like 8 for instance.
;
;
; Here we go up
;
;
; If we are in the progress of a jump
If jump=True Then
; decrease the jumptimer
jumptimer=jumptimer-0.33
; move the y axis of the dude
dudey=dudey-jumptimer
; if the jumptimer is below zero
If jumptimer=<0 Then
; If we are on a floor
If map((dudex+16)/32,(dudey+32)/32) = 1 Then
; stopp falling
jump=False
; Align the player with the floor
tempy = dudey/32
dudey = tempy*32
End If
End If
End If
;
; To ensure the player fall down from platforms a extra piece of code is required. By checking collision
; with tiles every cycle and setting the jump value to true when no collision is happening the player will
; start to fall. The falling can be done by puttin the jumptimer to ZERO. This means in stead of going up
; when it is 8 he wil go up by ZERO And Then decrease down Until a collision happens(Floor).
;
; The jump flag is false so the player is on a tile.
; If No collision
; Fall down.
;
If jump=False Then
If Not map((dudex+16)/32,(dudey+32)/32) = 1 Then
jump=True : jumptimer=0
End If
End If
; don't allow the dude To get stuck in the ceiling
;
; The top of the player is the y coordinate of the player. This because of the way blitz draws its
; default sprites. By checking if the player y coordinate is less then zero it can be seen when
; the player is about to leave the visible screen. By resetting this value to in this case 5 he will never
; be able to leave the screen. The jump variable will also be SET to ensure the player will start to fall
; down. This again by setting the fallingtimer(or gravity) to ZERO.
;
If dudey < 0 Then
dudey = 5
jump=True : jumptimer=0
EndIf
End Function
Function drawsprite()
;
; The regular draw command.
;
DrawImage dude,dudex,dudey
End Function
Function drawmap()
;
; This routine draw the entire visible playing map. The width and height
; of the graphicsscreen are known and the width and height of the tiles are.
; Then by using a nested loop and checking when the map array contains a tile (1)
; the program draws it. The location of the tile can be calculated by taking the
; width or height and multiplying it with the nested loop value.
;
;
For y=0 To 14
For x=0 To 19
If map(x,y)=1 Then
DrawImage tile,x*32,y*32
End If
Next
Next
End Function
|