simple maze game
Posted: Sat Aug 15, 2015 12:22 pm
d
http://www.purebasic.com
http://forums.purebasic.com/english/
Code: Select all
;Extremely simple maze game.
;Could easily be done with canvasgadget too.
;Colors is used as collision here. I haven't taken spritewith into account in this example..
InitSprite()
InitKeyboard()
Enumeration
#bg
#player
EndEnumeration
Structure _Directions
up.i
down.i
left.i
right.i
EndStructure
Declare Reset()
Global dir._Directions, PlayerPos.POINT, Speed.i, time.i, Quit.i = 0, Goal.i
OpenWindow(0, 0, 0, 800, 600, "Maze", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
CreateImage(#bg, 800, 600)
StartDrawing(ImageOutput(#bg))
Box(0, 0, 800, 600, $FFFFFF)
For x = 0 To 100
Box(Random(800, 10), Random(580,0), Random(80,20),Random(80,20), $0)
Next x
Box(ScreenWidth()-40, ScreenHeight()-40, 40, 40, $00DDDD)
StopDrawing()
CreateSprite(#player, 4, 4)
StartDrawing(SpriteOutput(#player))
Box(0, 0, 4, 4, $FF00FF)
StopDrawing()
PlayerPos\x = 6 : PlayerPos\y = 6 :Speed = 2 : time = ElapsedMilliseconds()
Repeat
ClearScreen(0)
Repeat
ev = WindowEvent()
If ev = #PB_Event_CloseWindow : Quit = 1 : EndIf
Until ev = 0
;I draw the a'hmmm.. maze and get the colors surrounding the player x,y
StartDrawing(ScreenOutput())
DrawImage(ImageID(#bg), 0, 0)
dir\left = Point(PlayerPos\x - Speed, PlayerPos\y)
dir\right = Point(PlayerPos\x + Speed, PlayerPos\y)
dir\down = Point(PlayerPos\x, PlayerPos\y + Speed)
dir\up = Point(PlayerPos\x, PlayerPos\y - Speed)
StopDrawing()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left) And dir\left = $FFFFFF
If PlayerPos\x - Speed > Speed
PlayerPos\x - Speed
EndIf
ElseIf KeyboardPushed(#PB_Key_Right) And dir\right = $FFFFFF
If PlayerPos\x + Speed < ScreenWidth()-Speed
PlayerPos\x + Speed
EndIf
EndIf
If KeyboardPushed(#PB_Key_Up) And dir\up = $FFFFFF
If PlayerPos\y - Speed > Speed
PlayerPos\y - Speed
EndIf
ElseIf KeyboardPushed(#PB_Key_Down) And dir\down = $FFFFFF
If PlayerPos\y + Speed < 599 - Speed
PlayerPos\y + Speed
EndIf
EndIf
If KeyboardReleased(#PB_Key_Space)
Reset()
EndIf
;If just one is $00DDDD -- goal is reached..
If dir\left = $00DDDD Or dir\right = $00DDDD Or dir\up = $00DDDD Or dir\down = $00DDDD
MessageRequester("Goal", "You've made it in " + Str((ElapsedMilliseconds()-time) / 1000) + " Seconds")
Reset()
EndIf
DisplaySprite(#player, PlayerPos\x, PlayerPos\y)
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Procedure Reset()
FreeImage(#bg)
CreateImage(#bg, 800, 600)
StartDrawing(ImageOutput(#bg))
Box(0, 0, 800, 600, $FFFFFF)
For x = 0 To 100
Box(Random(800, 10), Random(580,0), Random(80,20),Random(80,20), $0)
Next x
Box(ScreenWidth()-40, ScreenHeight()-40, 40, 40, $00DDDD)
StopDrawing()
PlayerPos\x = 6 : PlayerPos\y = 6 :Speed = 2 : time = ElapsedMilliseconds()
EndProcedure
Code: Select all
Structure point
x.f
y.f
EndStructure