I NEED HELP!

Monkey Forums/Monkey Beginners/I NEED HELP!

CanisLupus(Posted April) [#1]
I have been making a platform game and when I enter the "PLAYING" GameState, the player seems to react with the lava even though they clearly didn't interfere. WHY?

This is my code:
Import mojo

Global map:Int[][] = [ [1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1],
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1],
[1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,3,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1],
[1,0,0,0,0,0,1,1,1,0,0,1,1,1,0,1,0,0,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1],
[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,1,1,1,1],
[1,1,1,0,0,0,0,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,1,1,1],
[1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,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,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,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
[1,0,0,0,0,0,0,0,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,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] ]

Const mapwidth:Int=21
Const mapheight:Int=110

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 playerimage:Image
Global enemyimage:Image
Global brick:Image
Global spring:Image
Global egg:Image
Global win:Image
Global menu:Image
Global gameover:Image

Global rock_collection:List<ROCK>
Global lava_collection:List<LAVA>

Global life:Int=3
Global jump:Sound
Global springJump:Sound
Global hit:Sound

Global p:player = New player

Class MyGame Extends App

Global GameState:String = "MENU"

Method OnCreate()
SetUpdateRate(60)

menu = LoadImage ("menu.png")
playerimage = LoadImage ("player.png")
p.sprite = playerimage
gameover = LoadImage ("gameover.png")
win = LoadImage("win.png")
brick = LoadImage("brick.png")
spring = LoadImage("spring.png")
egg = LoadImage("egg.png")
jump = LoadSound("jump.ogg")
springJump = LoadSound("FlutterJump.ogg")
hit = LoadSound("Hit.ogg")


rock_collection=New List<ROCK>
rock_collection.AddLast(New ROCK(200,150))
rock_collection.AddLast(New ROCK(400,200))

lava_collection = New List<LAVA>
lava_collection.AddLast(New LAVA(0,480))

End Method

Method OnUpdate()
Select GameState
Case "MENU"
If KeyHit (KEY_SPACE) Then GameState="PLAYING"

Case "PLAYING"
If KeyHit (KEY_ESCAPE) Then GameState="MENU"

If life = 0 Then
GameState="GAMEOVER"
life = 3
End

If rectsoverlap(p.x,p.y,p.w,p.h,560,90,32,38) Then GameState = "WIN"

p.update
alignmap

For Local rock:=Eachin rock_collection
rock.Move(-1)
If rock.y>480 Then
rock_collection.Remove rock
rock_collection.AddLast(New ROCK(Rnd(0,400),Rnd(0,200)))
End
End

For Local lava:=Eachin lava_collection
lava.Move(+.1)
End

For Local rock:=Eachin rock_collection
If rectsoverlap(p.x,p.y,p.w,p.h,rock.x,rock.y,25,25) Then
PlaySound(hit)
rock_collection.Remove rock
rock_collection.AddLast(New ROCK(Rnd(0,500),Rnd(0,200)))
life -=1
End
Next

For Local lava:=Eachin lava_collection
If rectsoverlap(p.x,p.y,p.w,p.h,lava.x,lava.y,640,200) Then
PlaySound(hit)
End
Next


Case "GAMEOVER"
If KeyHit (KEY_TAB) Then GameState = "MENU"
Case "WIN"
If KeyHit (KEY_TAB) Then GameState = "MENU"
End

End Method
Method OnRender()
Select GameState
Case "MENU"
DrawImage menu, 0,0
Case "PLAYING"
Cls 0,0,0
p.draw
drawmap
For Local rock:=Eachin rock_collection
DrawImage rock.sprite, rock.x, rock.y
Next
For Local lava:=Eachin lava_collection
DrawImage lava.sprite, lava.x, lava.y
Next
SetColor 255,255,255
DrawText "live = "+Int(life),0,0
Case "GAMEOVER"
DrawImage gameover,0,0
Case "WIN"
Cls 0,250,154
DrawImage win,0,0
End
End Method
End Class


Class player
Field x:Float=32
Field y:Float=3300
Field w:Int=32
Field h:Int=48
Field incy:Float=0
Field isjumping:Bool = False
Field sprite:Image
Method update()
' Here the springy is handled
If isjumping = True
If incy>0
If psc(0,1) = True
incy=-5
End If
End If
End If
'Left and Right movement
If KeyDown(KEY_RIGHT)
For Local i=0 Until 4 ' move with 4 pixels at a time
If ptc(1,0) = False
x+=1
End If
Next
End If
If KeyDown(KEY_LEFT)
For Local i=0 Until 4
If ptc(-1,0) = False
x-=1
End If
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
End If
' jump
If KeyDown(KEY_SPACE)
PlaySound(jump)
If isjumping = False
isjumping = True
incy=-4
End If
End If
' if we are in a jump/falling down
If isjumping=True
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()
DrawImage p.sprite, p.x, p.y
End Method

End Class

Class ROCK
Field sprite:Image=LoadImage("rock.png")
Field x:Float
Field y:Float
Method New(x_spawn:Float,y_spawn:Float)
x = x_spawn
y = y_spawn
End
Method Move(y_distance:Float)
y-=y_distance
End
End

Class LAVA
Field sprite:Image=LoadImage("lava.png")
Field x:Float
Field y:Float
Method New(x_spawn:Float ,y_spawn:Float )
x = x_spawn
y = y_spawn
End
Method Move(y_distance:Float)
y-=y_distance
End
End


'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

'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
PlaySound(springJump)
Return True
End If
End If
End If
Next
Next
Return False
End Function

Function drawmap:Void()
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
DrawImage(brick,x1,y1)
Case 2
DrawImage(spring,x1+3,y1+16)
Case 3
DrawImage(egg,x1,y1-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

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

Function Main()
New MyGame()
End Function


Gerry Quinn(Posted April) [#2]
Nobody's going to debug all that by eye, and without your images etc. it can't run.

I do notice the lava sections are rather large:
rectsoverlap(p.x,p.y,p.w,p.h,lava.x,lava.y,640,200)
Is this intended?

Anyway, the easiest way to find is to put in some print commands that print out the player and lava data when they interact. That should probably tell you whatever is where it is not supposed to be...


CanisLupus(Posted April) [#3]
Nevermind, I just added an If that make sure the player only dies after touching the lava the second time. Thanks anyway.