simple maze game

Advanced game related topics
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

simple maze game

Post by sirrab »

d
Last edited by sirrab on Sun Aug 16, 2015 1:12 am, edited 4 times in total.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: simple maze game

Post by davido »

@sirrab,

Try entering maze into the search box, in the post in the link below:

http://www.purebasic.fr/english/viewtop ... 24#p468824

You will find a lot of examples. You might be amazed?!
DE AA EB
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: simple maze game

Post by DK_PETER »

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
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
firace
Addict
Addict
Posts: 902
Joined: Wed Nov 09, 2011 8:58 am

Re: simple maze game

Post by firace »

Very nice example!

I think the following is needed under Linux/Mac:

Code: Select all

Structure point 
  x.f
  y.f
EndStructure
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: simple maze game

Post by sirrab »

Thanks for the example, something for me to play with and learn :D
Post Reply