Now with Deepseek AI I did another attempt, and found that it is much more capable of this task.
I told it to write Hollywood code for a pong type game, and step by step, mostly by instructing it to add stuff or tell it to fix things that didnt work it provided working code without needing so much manual editing. I had to give it examples a few times of what was the valid syntax, but overall it seem quite capable.
The result is a playable pong game with one cpu player. (optionally 2 player game if you set leftPaddleCPU to false)
Code: Select all
@DISPLAY {Width = 640, Height = 480, Color = #BLACK, Title = "Pong Game"}
; Paddle and ball settings
paddleWidth = 10
paddleHeight = 80
ballSize = 10
paddleSpeed = 5
ballSpeedX = 2 ; Default ball speed
ballSpeedY = 1 ; Initial vertical speed
minBallSpeed = 1 ; Minimum ball speed
maxBallSpeed = 10 ; Maximum ball speed
baseBallSpeed = 2 ; Base speed of the ball
currentBallSpeed = baseBallSpeed ; Current speed of the ball
; Angle control settings
maxYXRatio = 3 ; Maximum allowed y/x ratio for the ball's angle
ySpeedAdjustment = 1 ; How much to adjust y speed when hitting upper/lower third of the paddle
; Initial positions
paddle1Y = (480 - paddleHeight) / 2
paddle2Y = (480 - paddleHeight) / 2
ballX = 640 / 2
ballY = 480 / 2
; Scoring system
player1Score = 0
player2Score = 0
matchOver = False
; Variables to store old scores
oldPlayer1Score = player1Score
oldPlayer2Score = player2Score
; CPU control settings
leftPaddleCPU = True ; Set to False for keyboard control
cpuDifficulty = 3 ; CPU difficulty level (1 = easy, 5 = very hard)
; Game speed settings
speedIncreaseRate = 0.1 ; Rate at which the game speed increases
; Create brushes for paddles and ball
CreateBrush(1, paddleWidth, paddleHeight, #RED) ; Brush for left paddle
CreateBrush(2, paddleWidth, paddleHeight, #BLUE) ; Brush for right paddle
CreateBrush(3, ballSize, ballSize, #WHITE) ; Brush for ball
; Create sprites using brushes
CreateSprite(1, #BRUSH, 1) ; Sprite for left paddle
CreateSprite(2, #BRUSH, 2) ; Sprite for right paddle
CreateSprite(3, #BRUSH, 3) ; Sprite for ball
; Function to reset the ball
Function ResetBall()
ballX = 640 / 2
ballY = 480 / 2
ballSpeedX = baseBallSpeed * Sgn(ballSpeedX) ; Reset X speed to base speed
ballSpeedY = 1 ; Reset Y speed
currentBallSpeed = baseBallSpeed ; Reset current ball speed
EndFunction
; Function to reset paddles to initial positions
Function ResetPaddles()
paddle1Y = (480 - paddleHeight) / 2
paddle2Y = (480 - paddleHeight) / 2
EndFunction
; Function to adjust y speed based on where the ball hits the paddle
Function AdjustYSpeed(paddleY)
; Calculate relative position of the ball on the paddle
relativeIntersectY = (paddleY + (paddleHeight / 2)) - ballY
normalizedRelativeIntersectY = relativeIntersectY / (paddleHeight / 2)
; Adjust Y speed based on where the ball hits the paddle
ballSpeedY = -normalizedRelativeIntersectY * maxYXRatio * Abs(ballSpeedX)
; Limit the y/x ratio to ensure the angle isn't too steep
currentYXRatio = Abs(ballSpeedY / ballSpeedX)
If currentYXRatio > maxYXRatio
ballSpeedY = ballSpeedY * (maxYXRatio / currentYXRatio)
EndIf
EndFunction
; Function to control CPU paddle movement
Function ControlCPUPaddle(paddleY, ballY, difficulty)
; Calculate the center of the paddle
paddleCenter = paddleY + (paddleHeight / 2)
; Debug print to check values
DebugPrint("paddleCenter: " .. paddleCenter .. ", ballY: " .. ballY)
; Adjust paddle speed based on difficulty
cpuPaddleSpeed = paddleSpeed * (difficulty * 0.2)
; Move paddle towards the ball
If paddleCenter < ballY
paddleY = paddleY + cpuPaddleSpeed
ElseIf paddleCenter > ballY
paddleY = paddleY - cpuPaddleSpeed
EndIf
; Ensure paddle stays within bounds
If paddleY < 0
paddleY = 0
ElseIf paddleY > 480 - paddleHeight
paddleY = 480 - paddleHeight
EndIf
; Debug print to check updated paddleY
DebugPrint("Updated paddleY: " .. paddleY)
; Return paddleY with parentheses
Return (paddleY)
EndFunction
; Initialize score sprites
SetFont("Arial", 24) ; Set a larger font size
SetFontColor(#WHITE) ; Set font color to white
CreateTextObject(10, player1Score) ; TextObject for Player 1 score
CreateSprite(10, #TEXTOBJECT, 10) ; Sprite for Player 1 score
score1Sprite = 10
CreateTextObject(11, player2Score) ; TextObject for Player 2 score
CreateSprite(11, #TEXTOBJECT, 11) ; Sprite for Player 2 score
score2Sprite = 11
; Game loop
Repeat
; Clear the screen (only needed once at the start)
If Not screenInitialized
Cls
screenInitialized = True
EndIf
; Draw background image if available
If background <> Nil
DisplayBrush(background, 0, 0)
EndIf
; Draw stapled vertical line in the center
For y = 0 To 480 Step 20
Line(320, y, 320, y + 10, #WHITE)
Next
; Draw paddles using sprites
DisplaySprite(1, 10, paddle1Y) ; Left paddle
DisplaySprite(2, 630 - paddleWidth, paddle2Y) ; Right paddle
; Draw ball using sprite
DisplaySprite(3, ballX, ballY) ; Ball
; Draw scores using TextObjects converted to sprites
SetFont("Arial", 24) ; Set a larger font size
SetFontColor(#WHITE) ; Set font color to white
; Update Player 1 score if it has changed
If player1Score <> oldPlayer1Score
; Remove old score sprite if it exists
If score1Sprite <> Nil
?RemoveSprite(score1Sprite) ; Use ? to avoid errors if sprite doesn't exist
EndIf
; Create new score sprite
CreateTextObject(10, player1Score) ; TextObject for Player 1 score
CreateSprite(10, #TEXTOBJECT, 10) ; Sprite for Player 1 score
score1Sprite = 10
; Update old score
oldPlayer1Score = player1Score
EndIf
; Update Player 2 score if it has changed
If player2Score <> oldPlayer2Score
; Remove old score sprite if it exists
If score2Sprite <> Nil
?RemoveSprite(score2Sprite) ; Use ? to avoid errors if sprite doesn't exist
EndIf
; Create new score sprite
CreateTextObject(11, player2Score) ; TextObject for Player 2 score
CreateSprite(11, #TEXTOBJECT, 11) ; Sprite for Player 2 score
score2Sprite = 11
; Update old score
oldPlayer2Score = player2Score
EndIf
; Display score sprites (ensure they exist)
If score1Sprite <> Nil
DisplaySprite(score1Sprite, 250, 10)
EndIf
If score2Sprite <> Nil
DisplaySprite(score2Sprite, 370, 10)
EndIf
; Move paddles
If leftPaddleCPU
; Control left paddle with CPU
paddle1Y = ControlCPUPaddle(paddle1Y, ballY, cpuDifficulty)
DebugPrint("paddle1Y after ControlCPUPaddle: " .. paddle1Y)
Else
; Control left paddle with keyboard
If IsKeyDown("w") And paddle1Y > 0 ; Lowercase 'w' for left paddle up
paddle1Y = paddle1Y - paddleSpeed
ElseIf IsKeyDown("s") And paddle1Y < 480 - paddleHeight ; Lowercase 's' for left paddle down
paddle1Y = paddle1Y + paddleSpeed
EndIf
EndIf
; Control right paddle with keyboard
If IsKeyDown("Up") And paddle2Y > 0 ; 'Up' arrow key for right paddle up
paddle2Y = paddle2Y - paddleSpeed
ElseIf IsKeyDown("Down") And paddle2Y < 480 - paddleHeight ; 'Down' arrow key for right paddle down
paddle2Y = paddle2Y + paddleSpeed
EndIf
; Move ball
ballX = ballX + ballSpeedX
ballY = ballY + ballSpeedY
; Debug print to check ballY and paddleY
DebugPrint("ballY: " .. ballY .. ", paddleY: " .. paddle1Y)
; Ball collision with top and bottom walls
If ballY <= 0 Or ballY >= 480 - ballSize
ballSpeedY = -ballSpeedY
EndIf
; Ball collision with paddles
If ballX <= 20 + paddleWidth And ballY + ballSize >= paddle1Y And ballY <= paddle1Y + paddleHeight
; Adjust y speed based on where the ball hits the paddle
AdjustYSpeed(paddle1Y)
; Reverse x direction
ballSpeedX = Abs(ballSpeedX)
; Move the ball outside the paddle to prevent sticking
ballX = 20 + paddleWidth
; Increase ball speed when hitting the paddle
currentBallSpeed = currentBallSpeed + speedIncreaseRate
; Cap the ball speed at maxBallSpeed
If currentBallSpeed > maxBallSpeed
currentBallSpeed = maxBallSpeed
EndIf
; Update ball speed
ballSpeedX = ballSpeedX + Sgn(ballSpeedX) * speedIncreaseRate
ballSpeedY = ballSpeedY + Sgn(ballSpeedY) * speedIncreaseRate
ElseIf ballX >= 620 - paddleWidth - ballSize And ballY + ballSize >= paddle2Y And ballY <= paddle2Y + paddleHeight
; Adjust y speed based on where the ball hits the paddle
AdjustYSpeed(paddle2Y)
; Reverse x direction
ballSpeedX = -Abs(ballSpeedX)
; Move the ball outside the paddle to prevent sticking
ballX = 620 - paddleWidth - ballSize
; Increase ball speed when hitting the paddle
currentBallSpeed = currentBallSpeed + speedIncreaseRate
; Cap the ball speed at maxBallSpeed
If currentBallSpeed > maxBallSpeed
currentBallSpeed = maxBallSpeed
EndIf
; Update ball speed
ballSpeedX = ballSpeedX + Sgn(ballSpeedX) * speedIncreaseRate
ballSpeedY = ballSpeedY + Sgn(ballSpeedY) * speedIncreaseRate
EndIf
; Ball out of bounds (left or right)
If ballX <= 0
player2Score = player2Score + 1
If player2Score >= 5
matchOver = True
Else
ResetBall()
EndIf
ElseIf ballX >= 640
player1Score = player1Score + 1
If player1Score >= 5
matchOver = True
Else
ResetBall()
EndIf
EndIf
; Check if match is over
If matchOver
; Create "Match Over" text objects and sprites
SetFont("Arial", 24) ; Set a larger font size
SetFontColor(#WHITE) ; Set font color to white
; Remove old "Match Over" sprites if they exist
If matchOverSprite1 <> Nil
?RemoveSprite(matchOverSprite1)
EndIf
If matchOverSprite2 <> Nil
?RemoveSprite(matchOverSprite2)
EndIf
; Create new "Match Over" text objects and sprites
CreateTextObject(20, "Match Over!") ; TextObject for "Match Over!"
CreateSprite(20, #TEXTOBJECT, 20) ; Sprite for "Match Over!"
matchOverSprite1 = 20
CreateTextObject(21, "Press SPACE to restart.") ; TextObject for "Press SPACE to restart."
CreateSprite(21, #TEXTOBJECT, 21) ; Sprite for "Press SPACE to restart."
matchOverSprite2 = 21
; Display "Match Over" text sprites
DisplaySprite(matchOverSprite1, #CENTER, 200)
DisplaySprite(matchOverSprite2, #CENTER, 230)
; Wait for SPACE key to restart the game
Repeat
VWait ; Wait for the next frame
Until IsKeyDown("Space")
; Reset game state
player1Score = 0
player2Score = 0
matchOver = False
ResetBall()
ResetPaddles() ; Reset paddles to initial positions
; Remove "Match Over" text sprites
If matchOverSprite1 <> Nil
?RemoveSprite(matchOverSprite1)
matchOverSprite1 = Nil
EndIf
If matchOverSprite2 <> Nil
?RemoveSprite(matchOverSprite2)
matchOverSprite2 = Nil
EndIf
; Reset old scores
oldPlayer1Score = player1Score
oldPlayer2Score = player2Score
EndIf
; Update screen
VWait
; Exit on ESC key
If IsKeyDown("Esc")
Break
EndIf
Forever