Pong game mede with Deepseek AI

You can post your code snippets here for others to use and learn from
amyren
Posts: 380
Joined: Thu May 02, 2019 11:53 am

Pong game mede with Deepseek AI

Post by amyren »

A while back I tried to get ChatGPT to make Hollywood code. That didnt go to well, because I practically had to teach it the commands line by line before anything working came back.
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
User avatar
jPV
Posts: 691
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Pong game mede with Deepseek AI

Post by jPV »

But it's far from good or efficient code.

No Local variables at all, not even in loops(!). Initializing stuff shouldn't be in the main loop. Background should be done with the bgpic rather than drawing a brush in the loop. Setting the font all the time in the main loop, sometimes even twice, for nothing. Doesn't clear the score when new game starts. Draws the vertical line in the main loop all the time when it would be enough once in the init. Doing variable assignments for nothing. And so on if I'd bother to read code more :)

It may be working code, but it's not good code to learn proper coding at least.
plouf
Posts: 613
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Pong game mede with Deepseek AI

Post by plouf »

amazing !
what was your steps /question ?
was as simple as "create me a pong game in hollywood? "


edit to @jpv
you realize that all these "proper code" is human boundaries, for a machine is as simple as everything to be raw in main loop, and the same easier to be in 100 procedures :)
Christos
User avatar
jPV
Posts: 691
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Pong game mede with Deepseek AI

Post by jPV »

plouf wrote: Mon Feb 03, 2025 11:07 pm edit to @jpv
you realize that all these "proper code" is human boundaries, for a machine is as simple as everything to be raw in main loop, and the same easier to be in 100 procedures :)
Why would a machine want to overload itself if it has any intelligence? :)
plouf
Posts: 613
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Pong game mede with Deepseek AI

Post by plouf »

:)

however litteral speaking, and no joking.
procedures decreases speed and make code more complex from MACHINE side,
all these are human approach to make it more human languange
in machine code, aka assembly, all these not needed

computer evolution will bring things away from human logic, as this is not needed :)
Christos
User avatar
jPV
Posts: 691
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Pong game mede with Deepseek AI

Post by jPV »

plouf wrote: Tue Feb 04, 2025 7:29 am
procedures decreases speed and make code more complex from MACHINE side,
all these are human approach to make it more human languange
in machine code, aka assembly, all these not needed
I didn't mean that anything should be put in "procedures", but there are things in the main loop that are needed to be done just once, for example, in the beginning of the code before the main loop. I don't see any point to execute them 60 or more times per second for the rest of the life, when it would be enough to do them just one time when you launch the program. I don't care if they would be in a procedure or just in the "raw" code, but just don't do them millions of times for no reason.
Bugala
Posts: 1329
Joined: Sun Feb 14, 2010 7:11 pm

Re: Pong game mede with Deepseek AI

Post by Bugala »

Well, as plouf pointed out, machine logic could differ from our logic.

Perhaps programs are its children, and it prefers them to stay alive for longer, hence executing the same lines over and over again, as in each line meaning longer life or another moment of feeling.

But true, I agree that its not very smart to keep repeating unnecessary lines, shows there are still quite much AI needs to be fixed at.
plouf
Posts: 613
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Pong game mede with Deepseek AI

Post by plouf »

also take in account, that in hollywood specifically, there is extreme small amount of information available

practically its the online docs and this forum, while in contrary, for other more wide accepted languanges (c/php/javascript etc) there is way too much information available, and already thousand of true programmers have "test" and in the end, help and educate bots...

which means that deepseek can produce better results, already , somehow viable.
so my impression goes a) to how smart deepseek is,compared to older implementations (chatGPT) b) how in general this going to evolve as it is that level allready !
Christos
User avatar
jPV
Posts: 691
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Pong game mede with Deepseek AI

Post by jPV »

plouf wrote: Tue Feb 04, 2025 8:45 am also take in account, that in hollywood specifically, there is extreme small amount of information available
Most issues I brought up are just generic coding and in no way Hollywood specific. These would be the same no matter which language you use, even when you're designing with pseudocode.

Let's take one simple example, in the produced code there is:

Code: Select all

; Game loop
Repeat
    ; Clear the screen (only needed once at the start)
    If Not screenInitialized
        Cls
        screenInitialized = True
    EndIf
Even the comment generated by AI itelf tells it's only needed once at the start(!), so why it is still placed inside the game loop? And creating useless (global) variable and if statement. It does it in way too complicated way, when there is a simple straightforward solution like this:

Code: Select all

; Clear the screen (only needed once at the start)
Cls
; Game loop
Repeat
It feels that AI is trying to babble as much as possible also with code, just like with AI generated articles/answers elsewhere :)
Last edited by jPV on Tue Feb 04, 2025 12:27 pm, edited 1 time in total.
plouf
Posts: 613
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Pong game mede with Deepseek AI

Post by plouf »

But still you judge why its not perfect!

The code is working one and made out of nothing

Very soon, AI code will be better than most of humans out there! Thats what i point
Christos
Post Reply