Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EnableExplicit
- InitSprite()
- InitKeyboard()
- #Window_Main = 0
- #Screen_Width = 640
- #Screen_Height = 360
- #Square_Size = 20
- #Snake_Initial_Length = 8
- #Snake_Initial_X = #Screen_Width / 2
- #Snake_Initial_Y = #Screen_Height / 2
- #Snake_Color = $FFFFFF ; white
- #Apple_Color = $0000FF ; red
- #Bg_Color = $320000 ; Dark blue
- #Bg_Color_Light = $460000 ; Less Dark Blue
- Enumeration Sprite
- #Sprite_Snake
- #Sprite_Apple
- #Sprite_BgTile
- EndEnumeration
- Global.q startStep = 0
- Global.q stepDuration = 100 ; speed = 10 cells / s
- Global leftPushed.i = #False
- Global rightPushed.i = #False
- Global Dim snake.Point(0)
- Global speed.Point
- Global alive.i = #True
- Global applePos.Point
- ;------------------------------------------
- ; Returns if a point belong to the snake.
- ; @param *pos Point The point to test.
- ; @Returns #True if the point belongs to the snake,
- ; #False otherwise.
- Procedure.i IsSnakeCell(*pos.Point)
- Protected snakeCell
- For snakeCell = 0 To ArraySize(snake())
- If snake(snakeCell)\x = *pos\x And
- snake(snakeCell)\y = *pos\y
- ProcedureReturn #True
- EndIf
- Next snakeCell
- ProcedureReturn #False
- EndProcedure
- ;------------------------------------------
- ; Sets the new position of the apple.
- ; Ensures that the apple does not appear
- ; on the snake.
- Procedure NewApple()
- Repeat
- applePos\x = Random(#Screen_Width/#Square_Size-1)*#Square_Size
- applePos\y = Random(#Screen_Height/#Square_Size-1)*#Square_Size
- Until IsSnakeCell(@applePos) = #False
- EndProcedure
- ;------------------------------------------
- ; Initializes the game (snake + apple)
- Procedure Init()
- ; Sets the snake to its initial length
- ReDim snake(#Snake_Initial_Length - 1)
- ; Sets the snake to its initial position
- Protected xHead.i = #Snake_Initial_X
- Protected yHead.i = #Snake_Initial_Y
- Protected snakeCell.i
- For snakeCell = 0 To ArraySize(snake())
- snake(snakeCell)\x = xHead
- snake(snakeCell)\y = yHead
- xHead - #Square_Size
- Next snakeCell
- ; Sets the snake speed
- speed\x = #Square_Size
- speed\y = 0
- ; The snake is alive
- alive = #True
- ; "Create" a new apple
- NewApple()
- ; Initializes keyboard key states
- leftPushed = #False
- rightPushed = #False
- EndProcedure
- ;------------------------------------------
- ; Draw the game background (the checkerboard)
- Procedure DrawBackground()
- Protected x, y
- For y = 0 To #Screen_Height Step 2 * #Square_Size
- For x = 0 To #Screen_Width Step 2 * #Square_Size
- DisplaySprite(#Sprite_BgTile, x, y)
- Next x
- Next y
- EndProcedure
- ;------------------------------------------
- ; Checks if the snake is alive.
- ; @Param *pos Point The point to test.
- ; @Returns #False if the point is outside the screen
- ; or on the snake.
- ; #True otherwise.
- Procedure.i IsAlive(*pos.Point)
- If *pos\x >= #Screen_Width Or
- *pos\x < 0 Or
- *pos\y >= #Screen_Height Or
- *pos\y < 0 Or
- IsSnakeCell(*pos)
- ProcedureReturn #False
- EndIf
- ProcedureReturn #True
- EndProcedure
- ;------------------------------------------
- ; The main function.
- ; Creates the window, the screen, the sprites
- ; and manages the main game loop.
- Procedure Main()
- ; Open the window and the screen
- OpenWindow(#Window_Main, 0, 0, #Screen_Width, #Screen_Height, "Snake", #PB_Window_BorderLess)
- OpenWindowedScreen(WindowID(#Window_Main), 0, 0, #Screen_Width, #Screen_Height, #True, 0, 0)
- ; Resize the window
- Define desktopCount = ExamineDesktops()
- ResizeWindow(#Window_Main, #PB_Ignore, #PB_Ignore,
- DesktopWidth(0)/DesktopResolutionX(),
- DesktopHeight(0)/DesktopResolutionY())
- ; Create the sprite for the snake cell
- CreateSprite(#Sprite_Snake, #Square_Size, #Square_Size)
- StartDrawing(SpriteOutput(#Sprite_Snake))
- Box(0, 0, #Square_Size, #Square_Size, #Snake_Color)
- StopDrawing()
- ; Create the sprite for the apple
- CreateSprite(#Sprite_Apple, #Square_Size, #Square_Size)
- StartDrawing(SpriteOutput(#Sprite_Apple))
- Circle(#Square_Size/2, #Square_Size/2, #Square_Size/2-1, #Apple_Color)
- StopDrawing()
- ; Create the sprite for the background tile
- CreateSprite(#Sprite_BgTile, 2 * #Square_Size, 2 * #Square_Size)
- StartDrawing(SpriteOutput(#Sprite_BgTile))
- Box(0, 0, 2 * #Square_Size, 2 * #Square_Size, #Bg_Color)
- Box(0, 0, #Square_Size, #Square_Size, #Bg_Color_Light)
- Box(#Square_Size, #Square_Size, #Square_Size, #Square_Size, #Bg_Color_Light)
- StopDrawing()
- ; Initializes the game
- Init()
- Protected event
- Protected snakeCell
- Protected currentTime
- Protected tmpSpeed
- Protected nextHead.Point
- ; Main screen loop
- Repeat
- ; Empty the message queue
- Repeat
- event = WindowEvent()
- Select event
- Case #PB_Event_CloseWindow
- End
- EndSelect
- Until Event = 0
- FlipBuffers()
- currentTime = ElapsedMilliseconds()
- If IsScreenActive()
- If currentTime - startStep > stepDuration
- ; reinit startStep
- startStep = currentTime
- If snake(0)\x + speed\x = applePos\x And snake(0)\y + speed\y = applePos\y
- ; The snake has eaten the apple, it grows by one
- ReDim snake(ArraySize(snake()) + 1)
- ; and a new apple appears
- NewApple()
- EndIf
- ; Compute the next position of the snake head
- nextHead\x = snake(0)\x + speed\x
- nextHead\y = snake(0)\y + speed\y
- ; Check if the snake will be alive or dead after the move
- alive = IsAlive(@nextHead)
- If alive
- ; The snake is alive, move it.
- ; First its body,
- For snakeCell = ArraySize(snake()) To 1 Step -1
- snake(snakeCell)\x = snake(snakeCell-1)\x
- snake(snakeCell)\y = snake(snakeCell-1)\y
- Next snakeCell
- ; then its head
- snake(0)\x = nextHead\x
- snake(0)\y = nextHead\y
- Else
- ; The snake is dead, restart the game
- Init()
- EndIf
- EndIf
- ; Display the background
- DrawBackground()
- ; Display the snake
- For snakeCell = 0 To ArraySize(snake())
- DisplaySprite(#Sprite_Snake, snake(snakeCell)\x, snake(snakeCell)\y)
- Next snakeCell
- ; Display the apple
- DisplayTransparentSprite(#Sprite_Apple, applePos\x, applePos\y)
- EndIf
- ; Check if a key has been pressed
- ExamineKeyboard()
- If KeyboardPushed(#PB_Key_Left) And Not leftPushed
- ; The left arrow has been pressed,
- ; rotate the speed vector counterclockwise.
- leftPushed = #True
- tmpSpeed = speed\x
- speed\x = speed\y
- speed\y = -tmpSpeed
- ElseIf KeyboardReleased(#PB_Key_Left)
- ; The left arrow has been released.
- leftPushed = #False
- EndIf
- If KeyboardPushed(#PB_Key_Right) And Not rightPushed
- ; The right arrow has been pressed,
- ; rotate the speed vector clockwise.
- rightPushed = #True
- tmpSpeed = speed\x
- speed\x = -speed\y
- speed\y = tmpSpeed
- ElseIf KeyboardReleased(#PB_Key_Right)
- ; The right arrow has been released.
- rightPushed = #False
- EndIf
- Until KeyboardPushed(#PB_Key_Escape)
- EndProcedure
- Main()
Advertisement
Add Comment
Please, Sign In to add comment