Code : Tout sélectionner
;Crazy Snake by falsam
;PB 5.31
EnableExplicit
Enumeration
#MainForm
EndEnumeration
Global Event
Global UpdateSquares.b
Global Vx=1, Vy=0 ;Velocity x & y
Global x1, y1, x2, y2
Global KLR.b = #True ;Left Key & Right Key enable
Global KUD.b = #True ;Up key & down key enable
Global n
Global FirstStart = #True, GameOver = 0
Global TargetCreate, tx, ty
Global SquareColor
Global Score, BestScore
Global Boom.b
Global Font15, Font20, font25, font40
Global Angle.f
Global ZoomX, ZoomY, BounceX.f, BounceY.f, Sens
Global StartTime.f, TimeOut.f
Global ScreenDefaultColor, GameDefaultColor, GameColor, TextColor, LineColor
Global SnakeHeadColor, SnakeBodyColor, SnakeOutlineColor
Global Text.s, PosX, PosY.f
Global Dir.s
Global wx,wy
;Game Sprite
Global Game
;Snake
Structure Snake
x.i
y.i
EndStructure
Global NewList Snakes.Snake()
;Engine Init
InitSprite()
InitKeyboard()
;Load Font
Font15 = LoadFont(#PB_Any, "System", 15)
Font20 = LoadFont(#PB_Any, "System", 20)
Font25 = LoadFont(#PB_Any, "System", 23)
Font40 = LoadFont(#PB_Any, "System", 40,#PB_Font_Bold)
;Color
ScreenDefaultColor = RGB(127, 182, 127)
GameDefaultColor = RGB(143, 188, 143)
LineColor = RGB(210, 180, 140)
TextColor = RGB(255, 255, 255)
SnakeHeadColor = RGB(210, 180, 140)
SnakeBodyColor = RGB(255, 248, 220)
SnakeOutlineColor = RGB(184, 134, 11)
;Screen
OpenWindow(#MainForm, 0, 0, 600, 600, "Crazy Snake", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 600, 600)
;Create Game
Game = CreateSprite(#PB_Any, 400, 400)
TimeOut = 200
Repeat
WX = WindowX(#MainForm, #PB_Window_InnerCoordinate)
WY= WindowY(#MainForm, #PB_Window_InnerCoordinate)
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event=0
FlipBuffers()
ClearScreen(RGB(220, 220, 220))
ExamineKeyboard()
If FirstStart = #True
ClearList(Snakes())
For n = 0 To 3
AddElement(Snakes())
With Snakes()
\x = 192 - 16 * n
\y = 192
x1 = \x
y1 = \y
EndWith
Next
EndIf
If GameOver = 2 Or FirstStart = #True
If KeyboardReleased(#PB_Key_Up)
FirstStart = 0
;Add 4 Squares to snake
ClearList(Snakes())
For n = 0 To 3
AddElement(Snakes())
With Snakes()
\x = 192 - 16 * n
\y = 192
x1 = \x
y1 = \y
EndWith
Next
;Reset Game Setup
StartTime.f = 0
RotateSprite(Game, 0, #PB_Absolute)
ZoomSprite(Game, 400, 400)
Boom = 0
Score = 0
KLR = #True ;Left Key & Right Key enable
KUD = #True ;Up key & down key enable
vx = 1 : vy = 0 ;Snake starts right
TargetCreate = #True
Angle = 0 :
ZoomX = 0 : ZoomY = 0
BounceX = 0 : BounceY = 0 : Sens = 1
GameColor = GameDefaultColor
GameOver = #False
EndIf
EndIf
If GameOver = #False
;-Keybord events
If KeyboardPushed(#PB_Key_Left) And KLR = #True
vx = - 1 : vy = 0 : KLR = #False : KUD = #True
dir.s = "G"
ElseIf KeyboardPushed(#PB_Key_Right) And KLR = #True
vx = 1 : vy = 0 : KLR = #False : KUD = #True
dir.s = "D"
ElseIf KeyboardPushed(#PB_Key_Up) And KUD = #True
vy = -1 : vx = 0 : KUD = #False : KLR = #True
dir.s = "H"
ElseIf KeyboardPushed(#PB_Key_Down) And KUD = #True
vy = 1 : vx = 0 : KUD = #False : KLR = #True
dir.s = "B"
EndIf
;- Updates the position of the snake
If ElapsedMilliseconds() - StartTime > TimeOut
StartTime = ElapsedMilliseconds()
ForEach Snakes()
If ListIndex(Snakes()) = 0
;Updates the head
x1 = Snakes()\x ;Memorise old X position
y1 = Snakes()\y ;Memorise old Y position
PushListPosition(Snakes())
NextElement(Snakes())
If x1 + 16 * vx = Snakes()\x And y1 + 16 * vy = Snakes()\y
vx * -1
vy * -1
EndIf
PopListPosition(Snakes())
Snakes()\x + 16 * vx
Snakes()\y + 16 * vy
;-Collide(head, target)
If Snakes()\x = tx And Snakes()\y = ty
TargetCreate = #True
UpdateSquares = #True
Score + 1
EndIf
;-Collide(head, Body)
PushListPosition(Snakes())
While NextElement(Snakes())
If Snakes()\x = x1 And Snakes()\y = y1
Boom = #True
EndIf
Wend
PopListPosition(Snakes())
Else
;-Updates the body
x2 = Snakes()\x
y2 = Snakes()\y
Snakes()\x = x1
Snakes()\y = y1
x1 = x2
y1 = y2
EndIf
Next
If UpdateSquares = #True
;Adds an element to the body of the snake
AddElement(Snakes())
Snakes()\x = x1
Snakes()\y = y1
UpdateSquares = #False
EndIf
n = 0
;EndIf
;-Create target
While TargetCreate = #True
;New X
While TargetCreate = #True
tx = Random(384, 0)
If Mod(tx, 16) = 0
TargetCreate = #False
EndIf
Wend
TargetCreate = #True
;New y
While TargetCreate = #True
ty = Random(384, 0)
If Mod(ty, 16) = 0
TargetCreate = #False
EndIf
Wend
TargetCreate = #True
;Intersection with the snake ?
ForEach Snakes()
With Snakes()
If \x <> Tx And \y <> Ty
;UpdateSquares = #True
TargetCreate = #False
EndIf
EndWith
Next
Wend
EndIf
EndIf
;- Drawing Game
;Draw Score
StartDrawing(ScreenOutput())
Box(0, 0, 600, 600, ScreenDefaultColor)
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(FontID(Font20))
DrawText(20, 15, "Crazy Snake", TextColor)
DrawText(450, 15, "Score " + Str(Score), TextColor)
DrawingFont(FontID(Font15))
DrawText(400, 560, "falsam (2015-2015)", TextColor)
StopDrawing()
;Draw GameSprite
StartDrawing(SpriteOutput(Game))
Box(0, 0, 400, 400, GameColor)
;Draw Grid
Global gx
For gx = 0 To 399 Step 16
LineXY(gx, 0, gx, 399, LineColor)
LineXY(0, gx, 399, gx, LineColor)
Next
;Draw Grid outline
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, 400, 400, LineColor)
DrawingMode(#PB_2DDrawing_Default)
;Draw Snake
ForEach Snakes()
With Snakes()
Select ListIndex(Snakes())
Case 0 ;First square
SquareColor = SnakeHeadColor
Case ListSize(Snakes()) - 1 ;Last square
SquareColor = SnakeHeadColor
Default
SquareColor = SnakeBodyColor
EndSelect
DrawingMode(#PB_2DDrawing_Default)
Box(\x, \y, 16, 16, SquareColor)
DrawingMode(#PB_2DDrawing_Outlined)
Box(\x, \y, 16, 16, SnakeOutlineColor)
EndWith
Next
;Draw target
DrawingMode(#PB_2DDrawing_Default)
Box(tx, ty, 16, 16, RGB(127, 255, 0))
DrawingMode(#PB_2DDrawing_Outlined)
Box(tx, ty, 16, 16, RGB(0, 0, 0))
StopDrawing()
;Display game
DisplaySprite(Game, (600 - SpriteWidth(Game))/2 + PosX, ((600 - SpriteHeight(Game))/2) + PosY)
;- Game effect
If GameOver = #False
Select Score
Case 0 To 2 ;Bounce game ++
TimeOut = 200
BounceX + 0.03 : BounceY + 0.03
Angle + 0.1
PosX = BounceX * Cos(Angle)
PosY = BounceY * Cos(Angle)
If Angle = 1
Angle = 0
EndIf
Case 3 To 4 ;Bounce Game --
If BounceX > 0
BounceX - 0.04 : BounceY - 0.04
EndIf
Angle + 0.1
PosX = BounceX * Cos(Angle)
PosY = BounceY * Cos(Angle)
If Angle = 1
Angle = 0
EndIf
Case 5
Angle = 0 : PosX = 0 : PosY = 0
Case 6 To 9 ;Right Rotate
TimeOut = 200
If Angle < 45
Angle + 0.05
RotateSprite(Game, 0.05, #PB_Relative)
EndIf
Case 10 To 14 ;Left Rotate
TimeOut = 200
If Angle > 0
Angle - 0.05
RotateSprite(Game, -0.05, #PB_Relative)
EndIf
Case 15 To 19 ;Reduce the size of the game.
TimeOut = 200
If SpriteWidth(Game) <> 250
ZoomSprite(Game, SpriteWidth(Game) - 1, SpriteWidth(Game) - 1)
EndIf
Case 20 To 22 ;Reduce the size of the game.
TimeOut = 200
If SpriteWidth(Game) <> 400
ZoomSprite(Game, SpriteWidth(Game) + 1, SpriteWidth(Game) + 1)
EndIf
Case 23 ;Change the speed
TimeOut = 150
Select Dir
Case "G"
ResizeWindow(#MainForm, WX-10, #PB_Ignore, #PB_Ignore, #PB_Ignore)
Dir.s="none"
Case "D"
ResizeWindow(#MainForm, WX+10, #PB_Ignore, #PB_Ignore, #PB_Ignore)
Dir.s="none"
Case "H"
ResizeWindow(#MainForm, #PB_Ignore, WY+10, #PB_Ignore, #PB_Ignore)
Dir.s="none"
Case "B"
ResizeWindow(#MainForm, #PB_Ignore, WY-10, #PB_Ignore, #PB_Ignore)
Dir.s="none"
EndSelect
Case 30 ;Change the speed
TimeOut = 100
Case 31 ;Change the speed
TimeOut = 50
Case 32;Change the speed
TimeOut = 150
Case 33 To 37 ;Reduce the size of the game. (Strange: the sprite moves upward)
TimeOut = 200
If SpriteWidth(Game) <> 250
ZoomX=-1
ZoomSprite(Game, SpriteWidth(Game) + ZoomX, 400)
EndIf
Case 38 To 41 ;Original Size
TimeOut = 200
If SpriteWidth(Game) <> 400
ZoomX = 1
If SpriteHeight(Game) <> 400
ZoomY = 1
Else
ZoomY = 0
EndIf
ZoomSprite(Game, SpriteWidth(Game) + ZoomX, SpriteWidth(Game) + ZoomY)
EndIf
Case 41 To 43 ;Random color
TimeOut = 150
GameColor = RGB(Random(255, 0),Random(255, 0),Random(255, 0))
Default ; Fastest speed
TimeOut - 0.001
GameColor = GameDefaultColor
EndSelect
EndIf
;Out of bound or Game over
FirstElement(Snakes())
With Snakes()
If (\x > 384 Or \x < 0 Or \y > 384 Or \y < 0 Or Boom = #True) And GameOver <> 2
GameOver = 1
If SpriteWidth(Game) <> 10
ZoomSprite(Game, SpriteWidth(Game)-10, SpriteWidth(Game) - 10)
Else
GameOver = 2
EndIf
EndIf
EndWith
;Game Over
If GameOver = 2 Or FirstStart = #True
If Score > BestScore
BestScore = Score
EndIf
StartDrawing(ScreenOutput())
Box(0, 0, 600, 600, ScreenDefaultColor)
DrawingMode(#PB_2DDrawing_Transparent)
If FirstStart = #True
DrawingFont(FontID(Font40))
Text = "Crazy Snake"
DrawText((600 - TextWidth(Text))/2, 100, Text, TextColor)
Else
DrawingFont(FontID(Font40))
Text = "You Died"
DrawText((600 - TextWidth(Text))/2, 100, Text, TextColor)
DrawingFont(FontID(Font25))
Text = "Score: " + Str(Score)
DrawText((600 - TextWidth(Text))/2, 200, Text, TextColor)
DrawingFont(FontID(Font25))
Text = "Best Score: " + Str(BestScore)
DrawText((600 - TextWidth(Text))/2, 300, Text, TextColor)
EndIf
Text = "Press up arrow key to start new game"
Angle + 0.1
PosY = 20 * Cos(Angle)
If angle = 1
angle = 0
EndIf
DrawingFont(FontID(Font25))
DrawText((600 - TextWidth(Text))/2, 500 + PosY, Text, TextColor )
StopDrawing()
EndIf
Until KeyboardPushed(#PB_Key_Escape)