It is currently Sun Jul 05, 2020 5:16 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: simple maze game
PostPosted: Sat Aug 15, 2015 12:22 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Dec 07, 2006 8:52 am
Posts: 106
Location: New Zealand
d


Last edited by sirrab on Sun Aug 16, 2015 1:12 am, edited 4 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: simple maze game
PostPosted: Sat Aug 15, 2015 1:45 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1766
Location: Uttoxeter, UK
@sirrab,

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

viewtopic.php?p=468824#p468824

You will find a lot of examples. You might be amazed?!

_________________
DE AA EB


Top
 Profile  
Reply with quote  
 Post subject: Re: simple maze game
PostPosted: Sun Aug 16, 2015 8:14 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 10:06 am
Posts: 853
Location: Denmark
Code:
;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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.


Top
 Profile  
Reply with quote  
 Post subject: Re: simple maze game
PostPosted: Sun Aug 16, 2015 10:22 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Nov 09, 2011 8:58 am
Posts: 604
Very nice example!

I think the following is needed under Linux/Mac:

Code:
Structure point
  x.f
  y.f
EndStructure

_________________
My new project: A simple HTML/CSS UI engine for PB desktop apps
viewtopic.php?f=7&t=74582


Top
 Profile  
Reply with quote  
 Post subject: Re: simple maze game
PostPosted: Sun Aug 16, 2015 11:23 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Dec 07, 2006 8:52 am
Posts: 106
Location: New Zealand
Thanks for the example, something for me to play with and learn :D


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye