- Joined
- Jan 2, 2020
- Messages
- 1,414
Here's a Mojo2 version of the tiles and springs example, using simple sprite graphics, instead of a rectangle as in the original.
I included a simple right-animation sequence as an example, which makes it a good startingpoint.
Graphics
License
Copyright/Attribution Notice:
Credit IndigoFenix. You can use or edit this sprite. If you make any good edits, post them to OGA! (OpenGameArt.org)
I included a simple right-animation sequence as an example, which makes it a good startingpoint.
Graphics
License
Copyright/Attribution Notice:
Credit IndigoFenix. You can use or edit this sprite. If you make any good edits, post them to OGA! (OpenGameArt.org)
Cerberus:
' #GLFW_WINDOW_WIDTH = 1920
' #GLFW_WINDOW_HEIGHT = 1080
' #GLFW_WINDOW_RENDER_WHILE_RESIZING = True
' #ANDROID_SCREEN_ORIENTATION = "landscape"
' #HTML5_CANVAS_WIDTH = 1920
' #HTML5_CANVAS_HEIGHT = 1080
#MOJO_AUTO_SUSPEND_ENABLED = False
Import mojo2
Function Main()
New MyGame
End
' ------------------------------------------------------------------
Const mapwidth:Int = 40
Const mapheight:Int = 30
Const tilewidth:Int = 32
Const tileheight:Int = 32
Global mapx:Int = 0
Global mapy:Int = 0
Global mapsx:Int = 0
Global mapsy:Int = 0
Global spritecell:Int = 0
Global animcounter:Int = 0
Global animdelay:Int = 0
Global map:Int[][] = [ [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,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,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,1,1,1,1,1],
[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]
Global p:player = New player
' ------------------------------------------------------------------
Class MyGame Extends App
Field canvas:Canvas
Field heroine:Image
Method OnCreate()
canvas = New Canvas()
canvas.Clear 0,0,0
' Sync to Vertical blanking
SetSwapInterval 1
SetUpdateRate 0
heroine = Image.Load("sprites.png",0,0,0)
End Method
Method OnUpdate()
p.update
alignmap
End Method
Method OnRender()
canvas.Clear 0,0,0
' Animate at a good pace
animdelay = animdelay + 1
If animdelay >= 3
animdelay = 0
animcounter = animcounter + 1
animcounter = animcounter Mod 3 ' Same as If animcounter >= 3 Then animcounter = animcounter - 3
Endif
drawmap(canvas)
p.draw(canvas, heroine)
canvas.SetColor 0,0,0
canvas.DrawRect 0,0,DeviceWidth(),32
canvas.SetColor 255,255,255
canvas.DrawText "Use Cursor left/right to move, space to jump",0,0
canvas.DrawText "Fall on a red springy to jump higher",0,16
canvas.Flush
End Method
End Class
' ------------------------------------------------------------------
'
Class player
Field x:Float=32*3
Field y:Float=32*6
Field w:Int=32
Field h:Int=32
Field incy:Float=0
Field isjumping:Bool = False
Method update()
' Here the springy is handled
If isjumping = True
If incy>0
If psc(0,1) = True
incy=-10
End If
End If
End If
spritecell = 0 ' default cell, standing still
' Left and Right movement
If KeyDown(KEY_RIGHT)
spritecell = 4
For Local i=0 Until 4 ' Move with 4 pixels at a time
If ptc(1,0) = False ; x+=1
Next
End If
If KeyDown(KEY_LEFT)
spritecell = 4 ' We'll fix left graphics later
For Local i=0 Until 4
If ptc(-1,0) = False ; x-=1
Next
End If
' Player gravity part
' If in the air and not in jump
If isjumping = False
If ptc(0,1) = False ; isjumping=True ; incy=0
End If
' Jump
If KeyDown(KEY_SPACE)
spritecell = 0
If isjumping = False ; isjumping = True ; incy=-4
End If
' If we are in a jump/falling down
If isjumping=True
spritecell = 0 ' jumping cell
If incy>=0 ' If we are going down
If incy<4 Then incy+=.1
For Local i=0 Until(incy)
If ptc(0,1) = False
y+=1
Else
isjumping = False
End If
Next
End If
If incy<0 ' If we are going up
incy+=.1
For Local i=0 Until Abs(incy)
If ptc(0,-1) = False
y-=1
Else
incy=0
End If
Next
End If
End If
End Method
Method draw(canvas:Canvas,heroine:Image)
canvas.SetColor 1,1,1 ' Use white to draw normal sprite without any tinting
' We got 4 x 2 sprites horisontally in the spritesheet, and we want to address them using spritecell simply using a number 0-some maximum number of sprites in the sheet
If spritecell = 0 Then animcounter = 0 ' Standing still or jumping has no animation sequence now
Local cellx:Int = (spritecell + animcounter) Mod 4
Local celly:Int = (spritecell + animcounter) / 4
canvas.DrawRect x,y, w,h, heroine, cellx*w,celly*h,w,h ' Draw sprite from spritesheet heroine, cut from the sheet at (0*w,0*h,w,h) and draw to canvas at (x,y, w,h)
End Method
End Class
' ------------------------------------------------------------------
' Functions outside everything, accesible everywhere
' ------------------------------------------------------------------
Function alignmap:Bool()
For Local i=0 Until 4
If p.x > DeviceWidth / 2
If mapx+20 < mapwidth-1
mapsx-=1
If mapsx < 0 Then
mapsx = 31
mapx += 1
Endif
p.x-=1
End If
End If
Next
For Local i=0 Until 4
If p.x < DeviceWidth / 2
If mapx > 0
mapsx+=1
If mapsx > 32 Then
mapsx = 0
mapx -= 1
Endif
p.x+=1
End If
End If
Next
' Scrolling down
For Local i=0 Until 16
If p.y > DeviceHeight / 2
If mapy+14 < mapheight-1
mapsy-=1
If mapsy < 0 Then
mapsy = 31
mapy += 1
Endif
p.y-=1
End If
End If
Next
' Scrolling up
For Local i=0 Until 16
If p.y < DeviceHeight / 2
If mapy > 0
mapsy+=1
If mapsy > 31 Then
mapsy = 0
mapy -= 1
Endif
p.y+=1
End If
End If
Next
End Function
' Player springy collision
Function psc:Bool(offsetx:Int=0,offsety:Int=0)
Local cx = (p.x+offsetx)/tilewidth+mapx
Local cy = (p.y+offsety)/tileheight+mapy
For Local y2=cy-1 Until cy+4
For Local x2=cx-1 Until cx+4
If x2>=0 And x2<mapwidth And y2>=0 And y2<mapheight
If map[y2][x2] = 2
Local x3 = (x2-mapx)*tilewidth-32+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy
If rectsoverlap(p.x+offsetx,p.y+offsety,p.w,p.h,x3,y3+tileheight/2,tilewidth,tileheight/2) = True
Return True
End If
End If
End If
Next
Next
Return False
End Function
' Player collide with solid blocks true/false
Function ptc:Bool(offsetx:Int=0,offsety:Int=0)
Local cx = (p.x+offsetx)/tilewidth+mapx
Local cy = (p.y+offsety)/tileheight+mapy
For Local y2=cy-1 Until cy+4
For Local x2=cx-1 Until cx+4
If x2>=0 And x2<mapwidth And y2>=0 And y2<mapheight
If map[y2][x2] = 1
Local x3 = (x2-mapx)*tilewidth-32+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy
If rectsoverlap(p.x+offsetx,p.y+offsety,p.w,p.h,x3,y3,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
Return False
End Function
Function drawmap:Void(canvas:Canvas)
For Local y=0 To 14
For Local x=0 To 20
Local x1 = ((x*tilewidth)+mapsx)-tilewidth
Local y1 = ((y*tileheight)+mapsy)
Select map[y+mapy][x+mapx]
Case 1 ' Wall
canvas.SetColor 100/255.0,100/255.0,100/255.0 ' Mojo2 uses color 0.0-1.0 and not 0-255 as Mojo
canvas.DrawRect x1,y1,tilewidth,tileheight
Case 2 ' Springy
canvas.SetColor 1,0,0
canvas.DrawRect x1,y1+tileheight/2,tilewidth,tileheight/2
End Select
Next
Next
End Function
Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
Return True
End Function
' ------------------------------------------------------------------
Attachments
Last edited: