Chat GPT 4o Code
Posted: Sat May 18, 2024 2:12 am
I thought this would be interesting to show. It seems that Chat Gpt can finally write good purebasic code. It only took one attempt.
i had to add initkeyboard() , ExamineKeyboard(), StartDrawing(ScreenOutput()), stopdrawing()
it was only missing these 4 lines.
i had to add initkeyboard() , ExamineKeyboard(), StartDrawing(ScreenOutput()), stopdrawing()
it was only missing these 4 lines.
Code: Select all
; PureBasic Snake Game
; Basic implementation
#WindowWidth = 800
#WindowHeight = 600
#BlockSize = 20
#FPS = 10
InitKeyboard()
Structure Vector2D
x.i
y.i
EndStructure
Global Dim Snake.Vector2D(100)
Global SnakeLength.i = 5
Global Direction.Vector2D
Global Apple.Vector2D
Procedure CreateApple()
Apple\x = Random(#WindowWidth / #BlockSize - 1) * #BlockSize
Apple\y = Random(#WindowHeight / #BlockSize - 1) * #BlockSize
EndProcedure
Procedure InitGame()
For i = 0 To SnakeLength - 1
Snake(i)\x = #WindowWidth / 2 - i * #BlockSize
Snake(i)\y = #WindowHeight / 2
Next i
Direction\x = #BlockSize
Direction\y = 0
CreateApple()
EndProcedure
Procedure UpdateGame()
; 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 + Direction\x
Snake(0)\y + Direction\y
; Check for collisions with walls
If Snake(0)\x < 0 Or Snake(0)\x >= #WindowWidth Or Snake(0)\y < 0 Or Snake(0)\y >= #WindowHeight
InitGame() ; Restart the game
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
InitGame() ; Restart the game
EndIf
Next i
; Check for collisions with the apple
If Snake(0)\x = Apple\x And Snake(0)\y = Apple\y
SnakeLength + 1
CreateApple()
EndIf
EndProcedure
Procedure RenderGame()
ClearScreen(RGB(0, 0, 0))
; Draw the snake
StartDrawing(ScreenOutput())
For i = 0 To SnakeLength - 1
Box(Snake(i)\x, Snake(i)\y, #BlockSize, #BlockSize, RGB(0, 255, 0))
Next i
; Draw the apple
Box(Apple\x, Apple\y, #BlockSize, #BlockSize, RGB(255, 0, 0))
StopDrawing()
EndProcedure
InitSprite()
OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, "Snake Game", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, #WindowWidth, #WindowHeight)
InitGame()
Repeat
Event = WindowEvent()
ExamineKeyboard()
; Handle input
If KeyboardPushed(#PB_Key_Left) And Direction\x = 0
Direction\x = -#BlockSize
Direction\y = 0
ElseIf KeyboardPushed(#PB_Key_Right) And Direction\x = 0
Direction\x = #BlockSize
Direction\y = 0
ElseIf KeyboardPushed(#PB_Key_Up) And Direction\y = 0
Direction\x = 0
Direction\y = -#BlockSize
ElseIf KeyboardPushed(#PB_Key_Down) And Direction\y = 0
Direction\x = 0
Direction\y = #BlockSize
EndIf
UpdateGame()
RenderGame()
FlipBuffers()
Delay(1000 / #FPS)
Until Event = #PB_Event_CloseWindow
End