" I would like you to optimize the code as much as possible while making it as wasteful as possible in memmory etc. Also if you can see some missing features please feel free to add to make it feel like a real game. " - I meant to tell it make it less wastefull oops! I'll have to change it later lol ran out of tokens. So this is going to be the slow version...
Increased Snake Array Size: The snake array is now 10 times larger than necessary to waste memory.
Score Display: A score counter is displayed on the screen.
Game Over Message: A "GAME OVER" message appears when the snake collides with itself or the walls, and prompts the player to press 'R' to restart.
Improved Apple Generation: Ensures the apple does not spawn on the snake's initial position.
Increased Delay for Readability: A slight increase in delay ensures readability of the game over message and smoother gameplay.
"could you make the shapes and snake look more realistic also it is running to fast maybe you can use gettickcount_ or eplasetimer to slow it down a bit. Still needs to be more colorful maybe more rainbow like?"
I had to make quite a few fixes. It doesn't know about DisplayTransparentSprite Function. Had problems with gradients frontcolor and backcolor function. It also made the snake fade as time went by so i commented that part out. Had issues using the drawingmode(*gradients*) I think that is about all of the changes i made. Oh also had to fix up the roundedbox by getting rid of its 0 hex value and putting in the color variable while making sure rgba worked with it. That is about it.
Maybe i'll try a couple more but chatgpt 4o is very limited for the free edition at the moment lol.
Code: Select all
; PureBasic Snake Game
; Enhanced, wasteful, colorful, and visually appealing implementation
#WindowWidth = 800
#WindowHeight = 600
#BlockSize = 20
#GridWidth = #WindowWidth / #BlockSize
#GridHeight = #WindowHeight / #BlockSize
#FPS = 10
InitKeyboard()
InitSprite()
InitMouse()
Structure Vector2D
x.i
y.i
EndStructure
Global Dim Snake.Vector2D(1000) ; Wasteful memory allocation
Global SnakeLength.i = 5
Global Direction.Vector2D
Global Apple.Vector2D
Global Score.i
Global GameOver.b
Global LastUpdate.i
Procedure CreateApple()
Protected isOnSnake.b
Repeat
isOnSnake = #False
Apple\x = Random(#GridWidth - 1)
Apple\y = Random(#GridHeight - 1)
For i = 0 To SnakeLength - 1
If Apple\x = Snake(i)\x And Apple\y = Snake(i)\y
isOnSnake = #True
Break
EndIf
Next i
Until Not isOnSnake
EndProcedure
Procedure InitGame()
For i = 0 To SnakeLength - 1
Snake(i)\x = #GridWidth / 2 - i
Snake(i)\y = #GridHeight / 2
Next i
Direction\x = 1
Direction\y = 0
Score = 0
GameOver = #False
CreateApple()
LastUpdate = ElapsedMilliseconds()
EndProcedure
Procedure.f HueToRGB(p.f, q.f, t.f)
If t < 0 : t + 1 : EndIf
If t > 1 : t - 1 : EndIf
If t < 1 / 6 : ProcedureReturn p + (q - p) * 6 * t : EndIf
If t < 1 / 2 : ProcedureReturn q : EndIf
If t < 2 / 3 : ProcedureReturn p + (q - p) * (2 / 3 - t) * 6 : EndIf
ProcedureReturn p
EndProcedure
Procedure RGBFromHSL(hue.f, saturation.f, lightness.f)
Protected r.f, g.f, b.f, q.f, p.f
hue / 360
If saturation = 0
r = lightness
g = lightness
b = lightness
Else
If lightness < 0.5
q = lightness * (1 + saturation)
Else
q = lightness + saturation - lightness * saturation
EndIf
p = 2 * lightness - q
r = HueToRGB(p, q, hue + 1 / 3)
g = HueToRGB(p, q, hue)
b = HueToRGB(p, q, hue - 1 / 3)
EndIf
ProcedureReturn RGB(r * 255, g * 255, b * 255)
EndProcedure
Procedure UpdateGame()
If GameOver = #True
ProcedureReturn
EndIf
; Move the snake
For i = SnakeLength - 1 To 1 Step -1
Snake(i)\x = Snake(i - 1)\x
Snake(i)\y = Snake(i - 1)\y
Next i
Snake(0)\x = Snake(0)\x + Direction\x
Snake(0)\y = Snake(0)\y + Direction\y
; Check for collisions with walls
If Snake(0)\x < 0 Or Snake(0)\x >= #GridWidth Or Snake(0)\y < 0 Or Snake(0)\y >= #GridHeight
GameOver = #True
EndIf
; Check for collisions with itself
For i = 1 To SnakeLength - 1
If Snake(0)\x = Snake(i)\x And Snake(0)\y = Snake(i)\y
GameOver = #True
EndIf
Next i
; Check for collisions with the apple
If Snake(0)\x = Apple\x And Snake(0)\y = Apple\y
SnakeLength = SnakeLength + 1
Score + 10
CreateApple()
EndIf
EndProcedure
Procedure DrawRoundedBox(x, y, width, height, radius, color)
Protected DrawingBuffer, TransparentBuffer
DrawingBuffer = CreateImage(#PB_Any, width, height)
If StartDrawing(ImageOutput(DrawingBuffer))
Box(0, 0, width, height, color)
StopDrawing()
EndIf
TransparentBuffer = CreateImage(#PB_Any, width, height, 32 , #PB_Image_Transparent)
If StartDrawing(ImageOutput(TransparentBuffer))
color = RGBA(Red(color),Green(color),Blue(color),255)
DrawingMode(#PB_2DDrawing_AlphaBlend)
Circle(radius, radius, radius, color)
Circle(width - radius, radius, radius, color)
Circle(radius, height - radius, radius, color)
Circle(width - radius, height - radius, radius, color)
Box(0, radius, width, height - radius * 2, color)
Box(radius, 0, width - radius * 2, height, color)
;DrawingMode(#PB_2DDrawing_AlphaBlend)
; Box(0, 0, width, height, color)
StopDrawing()
EndIf
If StartDrawing(ScreenOutput())
;DrawImage(ImageID(DrawingBuffer), x, y)
DrawAlphaImage(ImageID(TransparentBuffer), x, y)
StopDrawing()
EndIf
FreeImage(DrawingBuffer)
FreeImage(TransparentBuffer)
EndProcedure
Procedure RenderGame()
ClearScreen(RGB(0, 0, 0))
; Draw the snake with rainbow colors
For i = 0 To SnakeLength - 1
Protected hue.f = (i * 360.0 / SnakeLength); + (ElapsedMilliseconds() / 10.0)
Protected color = RGBFromHSL(hue, 1.0, 0.5)
DrawRoundedBox(Snake(i)\x * #BlockSize, Snake(i)\y * #BlockSize, #BlockSize, #BlockSize, 5, color)
Next i
; Draw the apple with a gradient
Protected AppleColor1 = RGB(255, 50, 50)
Protected AppleColor2 = RGB(255, 0, 0)
Protected DrawingBuffer = CreateImage(#PB_Any, #BlockSize, #BlockSize)
If StartDrawing(ImageOutput(DrawingBuffer))
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(AppleColor1)
FrontColor(AppleColor2)
;GradientColor(0.5, AppleColor1)
;GradientColor(0.9, AppleColor2)
;FrontColor(AppleColor2)
LinearGradient(0, 0, #BlockSize, #BlockSize)
Box(0, 0, #BlockSize, #BlockSize)
StopDrawing()
EndIf
If StartDrawing(ScreenOutput())
DrawImage(ImageID(DrawingBuffer), Apple\x * #BlockSize, Apple\y * #BlockSize)
StopDrawing()
EndIf
FreeImage(DrawingBuffer)
; Draw the score
If StartDrawing(ScreenOutput())
DrawText(10, 10, "Score: " + Str(Score), RGB(255, 255, 255), RGB(0, 0, 0))
; Draw game over message
If GameOver = #True
DrawText(#WindowWidth / 2 - 50, #WindowHeight / 2 - 10, "GAME OVER", RGB(255, 0, 0), RGB(0, 0, 0))
DrawText(#WindowWidth / 2 - 80, #WindowHeight / 2 + 10, "Press R to Restart", RGB(255, 0, 0), RGB(0, 0, 0))
EndIf
StopDrawing()
EndIf
EndProcedure
OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, "Snake Game", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, #WindowWidth, #WindowHeight)
InitGame()
Repeat
Repeat
Event = WindowEvent()
If Event = #PB_Event_CloseWindow
End
EndIf
Until Event = 0
ExamineKeyboard()
; Handle input
If GameOver = #False
If KeyboardPushed(#PB_Key_Left) And Direction\x = 0
Direction\x = -1
Direction\y = 0
ElseIf KeyboardPushed(#PB_Key_Right) And Direction\x = 0
Direction\x = 1
Direction\y = 0
ElseIf KeyboardPushed(#PB_Key_Up) And Direction\y = 0
Direction\x = 0
Direction\y = -1
ElseIf KeyboardPushed(#PB_Key_Down) And Direction\y = 0
Direction\x = 0
Direction\y = 1
EndIf
Else
If KeyboardPushed(#PB_Key_R)
InitGame()
EndIf
EndIf
If ElapsedMilliseconds() - LastUpdate >= 1000 / #FPS
UpdateGame()
LastUpdate = ElapsedMilliseconds()
EndIf
RenderGame()
FlipBuffers()
Until Event = #PB_Event_CloseWindow
End