
And crazy codedavido wrote: After all you called 'Crazy Snake'

And crazy codedavido wrote: After all you called 'Crazy Snake'
This is not normal. I have not coded a sprite moving down. !!davido wrote:the playing area suddenly drops!
Yes : Rotate, Zoom, Bounce and flashing.davido wrote:I presume the changes which occur later are programmed by you
Code: Select all
; ======================================
If GameOver = 2 Or FirstStart = #True
; ....
ElseIf GameOver = 1 ; to allow restarting the game
If KeyboardReleased(#PB_Key_Up)
GameOver = 2
EndIf
EndIf
; ======================================
Code: Select all
; ======================================
Global inScore.i = 0 ; needed new variable
; ....
If GameOver = 2 Or FirstStart = #True
If KeyboardReleased(#PB_Key_Right) ; 2 keys to en-/decrease StartingScore
If inScore < 33
inScore +1
Else
inScore = 0
EndIf
SetWindowTitle(#MainForm, "Crazy Snake ~ "+ Str(inScore))
EndIf
If KeyboardReleased(#PB_Key_Left)
If inScore > 0
inScore -1
Else
inScore = 33
EndIf
SetWindowTitle(#MainForm, "Crazy Snake ~ "+ Str(inScore))
EndIf
If KeyboardReleased(#PB_Key_Down) ; key for a random StartingScore
inScore = Random(33)
SetWindowTitle(#MainForm, "Crazy Snake ~ "+ Str(inScore))
EndIf
If KeyboardReleased(#PB_Key_Up)
FirstStart = 0
; ....
Boom = 0
Score = inScore ; org= 0 ... apply specific startingscore vs
; ======================================
Code: Select all
; ==================================
;-TOP
; Crazy Snake by falsam - 07/Juil/2015 - PB 5.31 ~ nearly fully crossplatform
; http://www.purebasic.fr/french/viewtopic.php?f=2&t=15291
; http://www.purebasic.fr/english/viewtopic.php?f=16&t=62548
; added: 08.07 - Ar-s: mouvement de fenetre (Dir.s, WX, WY)
; changed / added ...26.07 - Vera:
; - commented all commands that 'issued' on Linux [rotate, zoom]
; - enabled restart at game-end [ElseIf GameOver = 1]
; - 3 arrow-key-commands (reft/right/down) to choose StartingScore [range 0-33]
; + windowtitle-hint
; - added SnakeEndColor
; - various playing with the game- and snake-colors and 'endscreen'
; ~~~ playing to be continued :-)
; ==================================
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, SnakeEndColor
Global Text.s, PosX, PosY.f
Global Dir.s ; neu Ar-S
Global WX,WY ; neu Ar-S
Global inScore.i = 0 , colSwitch = 0 ; vs
;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", 25)
Font40 = LoadFont(#PB_Any, "Courier New", 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)
SnakeEndColor = RGB(167, 193, 123) ; vs
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,0,0,0)
;Create Game
Game = CreateSprite(#PB_Any, 400, 400)
TimeOut = 200
Repeat
WX = WindowX(#MainForm);, #PB_Window_InnerCoordinate) ; new Ar-S
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
;- START
If GameOver = 2 Or FirstStart = #True
If KeyboardReleased(#PB_Key_Right) ; vs
If inScore < 33
inScore +1
Else
inScore = 0
EndIf
SetWindowTitle(#MainForm, "Crazy Snake ~ "+ Str(inScore))
EndIf
If KeyboardReleased(#PB_Key_Left)
If inScore > 0
inScore -1
Else
inScore = 33
EndIf
SetWindowTitle(#MainForm, "Crazy Snake ~ "+ Str(inScore))
EndIf
If KeyboardReleased(#PB_Key_Down)
inScore = Random(33)
SetWindowTitle(#MainForm, "Crazy Snake ~ "+ Str(inScore))
EndIf
SnakeEndColor = RGB(167, 193, 123) ; reset defaults vs
SnakeBodyColor = RGB(255, 248, 220)
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)
DisplaySprite(Game, 400, 400)
Boom = 0
Score = inScore ; org= 0 ... other score to start in specific level vs
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
ElseIf GameOver = 1 ; vs to allow restarting the game
If KeyboardReleased(#PB_Key_Up)
GameOver = 2
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" ; 4x neu Ar-S
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)
If Mod(tx, 16) = 0
TargetCreate = #False
EndIf
Wend
TargetCreate = #True
;New y
While TargetCreate = #True
ty = Random(384)
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
SquareColor = SnakeEndColor ; vs
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
SnakeBodyColor = RGB(255, Random(248), 220)
Case 6 To 9 ;Right Rotate
TimeOut = 200
If Angle < 45
Angle + 0.05
; RotateSprite(Game, 0.05, #PB_Relative)
EndIf
If colSwitch = 0
SnakeBodyColor = RGB(255, 248, 220)
GameColor = RGB(Random(143), 188, 143)
colSwitch = 1 : EndIf
Case 10 To 14 ;Left Rotate
TimeOut = 200
If Angle > 0
Angle - 0.05
; RotateSprite(Game, -0.05, #PB_Relative)
EndIf
If colSwitch = 1
GameColor = GameDefaultColor
colSwitch = 0 : EndIf
Select Dir ; new Ar-S (new place)
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 15 To 19 ;Reduce the size of the game.
TimeOut = 200
If SpriteWidth(Game) <> 250
; ZoomSprite(Game, SpriteWidth(Game) - 1, SpriteWidth(Game) - 1)
EndIf
If colSwitch = 0
GameColor = RGB(143, Random(188), 143)
colSwitch = 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
If colSwitch = 1
SnakeBodyColor = RGB(Random(255), 248, 220)
GameColor = GameDefaultColor
colSwitch = 0 : EndIf
Case 23 ;Change the speed
TimeOut = 150
SnakeBodyColor = RGB(255, 248, 220)
; Select Dir ; new Ar-S (org-place)
; 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 24 ;Change the speed
TimeOut = 100
SnakeHeadColor = RGB(210, 16, 9) ; vs
Case 25 ;Change the speed
TimeOut = 50
BounceX + 0.03 : BounceY + 0.03 ; vs
Angle + 0.1
PosX = BounceX * Cos(Angle)
PosY = BounceY * Cos(Angle)
If Angle = 1
Angle = 0
EndIf
Case 26 ;Change the speed
TimeOut = 150
SnakeHeadColor = RGB(210, 180, 140) ; vs
SnakeBodyColor = RGB(255, 88, 82)
Case 27 To 29 ;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
If colSwitch = 0 ; vs ...
SnakeBodyColor = RGB(255, 248, 220)
GameColor = RGB(143, Random(188), 143)
colSwitch = 1 : EndIf
Case 30 To 32 ;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
If colSwitch = 1
GameColor = GameDefaultColor
colSwitch = 0 : EndIf
Case 33 To 36 ;Random color
TimeOut = 150
GameColor = RGB(Random(255),Random(255),Random(255))
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 ; org: 1 , 2-um sofort neu zu spielen
If SpriteWidth(Game) <> 10
; ZoomSprite(Game, SpriteWidth(Game)-10, SpriteWidth(Game) - 10)
GameColor = RGB(0, 0, 0)
SnakeBodyColor = RGB(228, 222, 197)
SnakeEndColor = RGB(Random(167), Random(193), Random(123))
Else
GameOver = 2 ; this never occurs vs
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"
Text = "S.o°l°o.ng Snake" ; vs
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)
; =========================================
pps: I just saw you've updated your Snake todayIf Case > 40 to 70 Step 5
Change color xy // or speed ...
EndIf
Hooo thank Davido. No problem with Mac OS ?davido wrote:Very good, thank you.
Hey that's nice to hearfalsam wrote:Hello Vera. I played with your code. I'll change my code with some of your suggestions.
indeed _I think you'll have fun with the code I updated today. Demivec added stunning animations.
With the code of the first message?bbanelli wrote:I don't know if that's a feature or a bug, but sometimes, new dot/cookie/apple/whatever snake eats appear within it's current occupying space.
Code: Select all
If SpriteWidth(Game) <> 10
; If TileSeparation > 0
; TileSeparation = 0
; ; ClipSprite(Game, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
; EndIf