Une version différente, ici le joueur (bloc rouge) ne s'arrête que lorsqu'il rencontre un obstacle ou alors qu'il trouve la sortie (bloc vert)
F1: régénère une nouvelle map
Esc: Quitte
Code : Tout sélectionner
Enumeration
#windows_main
#Sprite_ScreenPlay
#Sprite_Player
EndEnumeration
Enumeration
#GO_UP
#GO_DOWN
#GO_LEFT
#GO_RIGHT
#GO_NOWHERE
EndEnumeration
#difficulty = 3 ; hard:2 .. whatever:easy
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Error", "Can't open the sprite system", 0)
End
EndIf
If OpenWindow(#windows_main, 0, 0, 641, 481, "Find the green exit - F1: Reset - Esc: End", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(#windows_main), 0, 0, 641, 481, 1, 0, 0)
;player
If CreateSprite(#Sprite_Player,19,19)
StartDrawing(SpriteOutput(#Sprite_Player))
Box(0,0,19,19,RGB(255,0,0))
StopDrawing()
EndIf
Else
MessageRequester("Error", "Can't open windowed screen!", 0)
End
EndIf
EndIf
Global playerX, playerY, starttime, playermove, _Direction
Global Dim tab.i(32,24)
Procedure Init()
; randomize walls
For i = 0 To 31
For j = 0 To 23
t = Random(#difficulty)
; put a wall around the playground
If i = 0 Or i = 31 Or j = 0 Or j = 23
t = 1
EndIf
If t = 1
tab(i,j) = 1
Else
tab(i,j) = 0
EndIf
Next
Next
; randomize player position
Repeat
playerX = Random(31)
playerY = Random(23)
valplayer = tab(playerX, playerY)
If valPlayer = 0
Break
EndIf
ForEver
; randomize Exit
; Repeat
; exit_x = Random(31)
; exit_y = Random(23)
; valexit = tab(exit_x, exit_y)
; If valexit = 0
; tab(exit_x, exit_y) = 3
; Break
; EndIf
; ForEver
; randomize Exit
exit_x = playerX
exit_y = playerY
For i=0 To 31*23*#difficulty
new_x = exit_x
new_y = exit_y
Select Random(3)
Case 0
If new_x > 0 : new_x - 1 : EndIf
Case 1
If new_y > 0 : new_y - 1 : EndIf
Case 2
If new_x < 31 : new_x + 1 : EndIf
Case 3
If new_y < 23 : new_y + 1 : EndIf
EndSelect
valexit = tab(new_x, new_y)
If valexit = 0
exit_x = new_x
exit_y = new_y
EndIf
Next i
tab(exit_x, exit_y) = 3
; create our playground
If CreateSprite(#Sprite_ScreenPlay,641,481)
StartDrawing(SpriteOutput(#Sprite_ScreenPlay))
For i = 0 To 31
For j = 0 To 23
If tab(i,j) = 1
Box(i*20,j*20,20,20,RGB(0,0,255))
EndIf
If tab(i,j) = 3
Box(i*20,j*20,20,20,RGB(0,255,0))
EndIf
Next
Next
For j = 0 To 480 Step 20
Line(0,j,640,1,$C0C0C0) ;RGB(Random(255),Random(255),Random(255)))
Next
For i = 0 To 640 Step 20
Line(i,0,1,480,$C0C0C0) ;RGB(Random(255),Random(255),Random(255)))
Next
StopDrawing()
EndIf
; player on you mark, ready, steady ... go !
starttime = ElapsedMilliseconds()
playermove = 0
_Direction = #GO_NOWHERE
EndProcedure
Procedure CheckPosition(Dir)
If tab(playerX, playerY) = 1
_Direction = #GO_NOWHERE
Select Dir
Case #GO_UP
playerY + 1
Case #GO_DOWN
playerY - 1
Case #GO_RIGHT
playerX - 1
Case #GO_LEFT
playerX + 1
EndSelect
; Else
; If playerX = 0 : _Direction = #GO_NOWHERE : EndIf
; If playerX = 31 : _Direction = #GO_NOWHERE : EndIf
; If playerY = 0 : _Direction = #GO_NOWHERE : EndIf
; If playerY = 23 : _Direction = #GO_NOWHERE : EndIf
; Debug "Direction = "+Str(_Direction)
; Debug "PlayerX = "+Str(playerX)
; Debug "PlayerY = "+Str(playerY)
EndIf
EndProcedure
Init()
Repeat
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
Until Event = 0
ExamineKeyboard()
If KeyboardReleased(#PB_Key_Up)
If _Direction = #GO_NOWHERE
_Direction = #GO_UP
playermove + 1
EndIf
EndIf
If KeyboardReleased(#PB_Key_Down)
If _Direction = #GO_NOWHERE
_Direction = #GO_DOWN
playermove + 1
EndIf
EndIf
If KeyboardReleased(#PB_Key_Left)
If _Direction = #GO_NOWHERE
_Direction = #GO_LEFT
playermove + 1
EndIf
EndIf
If KeyboardReleased(#PB_Key_Right)
If _Direction = #GO_NOWHERE
_Direction = #GO_RIGHT
playermove + 1
EndIf
EndIf
If KeyboardReleased(#PB_Key_F1)
Init()
EndIf
; Draw our playground on the screen
DisplaySprite(#Sprite_ScreenPlay,0,0)
; empty box, so we draw our player
Select _Direction
Case #GO_UP
playerY - 1
If playerY < 0 : playerY = 0 : EndIf
CheckPosition(#GO_UP)
Case #GO_DOWN
playerY + 1
If playerY > 23 : playerY = 23 : EndIf
CheckPosition(#GO_DOWN)
Case #GO_LEFT
playerX - 1
If playerX < 0 : playerX = 0 : EndIf
CheckPosition(#GO_LEFT)
Case #GO_RIGHT
playerX + 1
If playerX > 31 : playerX = 31 : EndIf
CheckPosition(#GO_RIGHT)
EndSelect
DisplaySprite(#Sprite_Player, playerX * 20 + 1, playerY * 20 + 1)
; The exit has been reached
If tab(playerX, playerY) = 3
time = ElapsedMilliseconds() - starttime
If MessageRequester("You made it !","Elapsed time : "+Str(time/1000)+" seconds for "+Str(playermove)+" moves."+#CRLF$+#CRLF$+"----- Start Again ?",#PB_MessageRequester_YesNo) = 6
Init()
Else
End
EndIf
EndIf
; _Direction = #GO_NOWHERE
FlipBuffers()
Until Quit Or KeyboardPushed(#PB_Key_Escape)
C'est plus compliqué pour réussir et on ne peut pas réussir à tous les coups, je prévois de faire des maps plus petites et définies par avance
**EDIT** : petite modification dans les directions pour ne pas bloquer si on change de direction pendant le déplacement
**EDIT**

des petites erreurs sur ce programme (si c'est au bord vous êtes bloqués

)
**EDIT** : En mettant un bord tout autour ca marche mieux (a défaut de trouver une parade)