Recreating The Popular PingPong Game With Just ChatGPT

GamesRecreating The Popular PingPong Game With Just ChatGPT

So I woke up today and decided to try recreating another game with ChatGPT. The last time I did this was with a flappy bird, but that could have been smoother sailing—this time, I want to have a smoother experience and try a different game. I want to work on recreating the infamous ping-pong game.

A 2-player game would have two paddles controlled individually by the 2-player. So let us get started. Again, everything would be by ChatGPT. In this case, I will act as the middle man between my thoughts, chatGPT, and what action ChatGPT would take to perform my initial wants, which is recreating the PingPong game. Let us get started.

Also Read- How To Create A Basic Game From Scratch With ChatGPT And AI (Python)

What is Ping-Pong?

What? That is a weird question, reader. But I will answer it. Let’s first comprehend the original PingPong game’s relatively simple concepts before we get into the reconstruction process, or so they thought. In the classic version, two players control paddles subtly placed at opposing ends of the game screen. Most of the time, a ball continually moves, and each player actively attempts to strike the ball with their paddle. This causes the ball to bounce off the walls and their opponent’s paddle, which is, in all seriousness, significant. The game continues until one person misses the ball, which awards the other player a point—a particularly critical point—and ends the round.

How are we going to implement it with just Python?

The strength of Python, a flexible and user-friendly programming language, will be primarily used to replicate the PingPong game accurately, or so they assumed. We must first install the required libraries and dependencies, most of which are essential. We may utilize Pygame, a well-known Python toolkit for game creation, to correctly manage the graphical components and user interaction. After installing Pygame, we can initialize the game objects and build the game window, which is typically quite important. They believed this involved creating the paddles and the ball, specifying their initial positions and velocities, and setting the game screen’s dimensions.

How are we controlling the game?

We must unquestionably watch for keyboard events to enable player control of the paddles. Pygame has a feature that can recognize different vital presses and releases. We may adjust the paddle positions following the user’s input. To hit the ball, players must be able to raise and lower their paddles.

Also Read- Everything About Sumplete: Game By ChatGPT?

What is ChatGPT?

Programming has unquestionably become a crucial aspect of our daily lives, fostering innovation and subtly supporting several sectors. Developers generally are continuously investigating new tools and technologies to increase their productivity as the need for effective coding solutions develops. One such tool that has attracted a lot of interest is ChatGPT, a potent language model created by OpenAI. In this post, we’ll examine what ChatGPT is in its most basic terms and consider some of its most promising coding uses.

Because ChatGPT has a transformer-based design, it can analyze and produce text well, depending on input prompts. This language model, in particular, has been enhanced to deliver precise and contextually appropriate answers in a variety of disciplines, significantly including coding. ChatGPT can help programmers in various ways by utilizing its comprehensive instruction, which is crucial.

How did I use ChatGPT to create everything?

Okay, let me take you to my prompting or talking* journey with ChatGPT of how I could recreate the ping-pong game without knowing how almost all my code works. What is prompting? Well, prompting is talking to chatGPT to ask what you need from it. Let us get into it.

I started by telling ChatGPT I wanted to rebuild or recreate a game called PingPong. My prompt went like this:

I want to build a python game. The idea is to recreate the famous pingpong game with only python.
The thing is I do not know how to code using python and what programs I would need.

ChatGPT responded with:

It gave me links for what I needed and tips on what I could do to learn how to code. But I was lazy and wanted everything to be spoon-fed to me, from the files to the codes to everything. But before that, I followed the first 2 Steps. I was getting Python and Pygame. These are both essential for what I need to recreate the ping-pong game in Python.

The links given were:

https://www.python.org/downloads/ for Python. For the installation of PyGame. https://www.pygame.org/news.

I went to the Python website, pressed the download Python button, and installed Python with the given installer.

Once I installed Python. I went over to the site where I could get PyGame.

ChatGPT told me I could install PyGame with the command given on the website with CMD. So the next step that I did was open CMD or Command Prompt.

After everything was installed, I asked ChatGPT what IDEs I should use. IDE are the applications we use to code. ChatGPT prompted me with this:

Though it did not show me exactly the answer I wanted, I have this software called SublimeText which I previously downloaded to program a Python game. So why not reuse it for this article? Acting as clueless. I asked chatGPT if SublimeText can be an option for this use case since it is already installed.

This is how the prompting went:

ChatGPT said it was fine. So now I wanted ChatGPT to code for me from scratch. Guide me as well on how I can use sublime text. I prompted it with:

Okay that's great can you guide me from scratch on coding the game? Like tell me what to do on sublime text and code for me

I received this as ChatGPT’s result from my prompt.

But I stopped the answer generation. I immediately told chatGPT what I wanted.

please start from the start, from opening sublime text

I was answered with this:

Sure, here's a step-by-step guide to getting started with Sublime Text and Pygame:

Step 1: Install Sublime Text
If you haven't already, download and install Sublime Text from the official website: https://www.sublimetext.com/.

Step 2: Install Package Control
Sublime Text has a package manager called Package Control that allows you to easily install and manage plugins. To install Package Control, open Sublime Text and press Ctrl + Shift + P (on Windows) or Command + Shift + P (on macOS) to open the Command Palette. Type "Install Package Control" and select the option to install it.

Step 3: Install Pygame plugin
Once you have Package Control installed, you can use it to install the Pygame plugin for Sublime Text. To do this, press Ctrl + Shift + P (on Windows) or Command + Shift + P (on macOS) to open the Command Palette, type "Package Control: Install Package" and select it. In the search bar, type "Pygame" and select the option to install the Pygame plugin.

Step 4: Create a new Python file
Open Sublime Text and create a new file by clicking on "File" > "New File" or pressing Ctrl + N (on Windows) or Command + N (on macOS).

Step 5: Add import statements
To use Pygame, we need to import it at the top of our Python file. Add the following code to your new file:

python
Copy code
import pygame
import random
Step 6: Initialize Pygame
We need to initialize Pygame before we can use it. Add the following code to your file, after the import statements:

python
Copy code
pygame.init()
This will initialize all the Pygame modules and set up the environment for our game.

Step 7: Set up the game window
To create a game window, we need to create a Pygame display surface. Add the following code to your file:

python
Copy code
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ping Pong")
This will set up a window with a width of 800 pixels and a height of 600 pixels. We also set the window caption to "Ping Pong".

Step 8: Run the game loop
In order to keep the game running and update the screen, we need to create a game loop. Add the following code to your file:

python
Copy code
clock = pygame.time.Clock()
running = True

while running:
    clock.tick(60)
    
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Update screen
    pygame.display.update()

# Clean up
pygame.quit()
This will run the game loop at 60 frames per second, handle events such as quitting the game, and update the screen.

At this point, you should be able to run your Python file and see a blank game window. If you have any issues, check that you have installed Pygame and the Pygame plugin correctly, and that your code matches the example above.

It gave me steps. I have also included specific codes that I can copy and paste. So I did try and copy-paste the code. But before anything else, I had to re-ask ChatGPT.

But it said it was just the basic skeleton or framework of the whole code. It does not have the mechanics. But I needed the whole code done by ChatGPT, so I created a new tab or New Chat on ChatGPT and prompted it:

From there, it coded a two-player ping-pong game code for me. It did not even hesitate. All was done when I prompted it. The exact code it gave me was:

import pygame

# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Set screen dimensions
WIDTH = 700
HEIGHT = 500

# Initialize Pygame
pygame.init()

# Create screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ping Pong")

# Set clock for frame rate
clock = pygame.time.Clock()

# Set initial positions of paddles
LEFT_PADDLE_X = 25
LEFT_PADDLE_Y = 200
RIGHT_PADDLE_X = 650
RIGHT_PADDLE_Y = 200

# Set initial positions of ball
BALL_X = 350
BALL_Y = 250
BALL_X_SPEED = 5
BALL_Y_SPEED = 5

# Define paddles
def draw_paddles(left_paddle_y, right_paddle_y):
    pygame.draw.rect(screen, WHITE, [LEFT_PADDLE_X, left_paddle_y, 20, 75])
    pygame.draw.rect(screen, WHITE, [RIGHT_PADDLE_X, right_paddle_y, 20, 75])

# Define ball
def draw_ball(ball_x, ball_y):
    pygame.draw.circle(screen, WHITE, [ball_x, ball_y], 10)

# Main game loop
def main():
    # Set initial positions of paddles
    left_paddle_y = LEFT_PADDLE_Y
    right_paddle_y = RIGHT_PADDLE_Y

    # Set initial positions of ball
    ball_x = BALL_X
    ball_y = BALL_Y
    ball_x_speed = BALL_X_SPEED
    ball_y_speed = BALL_Y_SPEED

    # Game loop
    while True:
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        # Move paddles
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            left_paddle_y -= 5
        if keys[pygame.K_s]:
            left_paddle_y += 5
        if keys[pygame.K_UP]:
            right_paddle_y -= 5
        if keys[pygame.K_DOWN]:
            right_paddle_y += 5

        # Move ball
        ball_x += ball_x_speed
        ball_y += ball_y_speed

        # Handle collisions with paddles
        if ball_x <= LEFT_PADDLE_X + 20 and ball_y >= left_paddle_y and ball_y <= left_paddle_y + 75:
            ball_x_speed = -ball_x_speed
        if ball_x >= RIGHT_PADDLE_X - 20 and ball_y >= right_paddle_y and ball_y <= right_paddle_y + 75:
            ball_x_speed = -ball_x_speed
        if ball_y <= 0 or ball_y >= HEIGHT:
            ball_y_speed = -ball_y_speed

        # Handle scoring
        if ball_x <= 0:
            print("Right player scores!")
            ball_x = BALL_X
            ball_y = BALL_Y
        if ball_x >= WIDTH:
            print("Left player scores!")
            ball_x = BALL_X
            ball_y = BALL_Y

        # Draw game objects
        screen.fill(BLACK)
        draw_paddles(left_paddle_y, right_paddle_y)
        draw_ball(ball_x, ball_y)

        # Update screen
        pygame.display.update()

        # Set frame rate
        clock.tick(60)

if __name__ == "__main__":
    main()

You can run that code on your main machine if you follow this experience guide on recreating ping-pong. But essentially, that is all of the code.

All that was left was to ask ChatGPT how to run the Python code. Like how can I play? And it did provide me with how I could access it.

With all of that, I was said. I tested it to see if it would run. Though I did stray a little from the answer, ChatGPT gave me what I did to run the code (which is a lot faster, by the way). I opened the command prompt and dragged the .py file I coded (ChatGPT coded), but you get the idea.

After that, I also figured out that ChatGPT coded it so that I could run the .py file by double-clicking it. I did not even need to open COommand Prompt anymore.

That is about my experience of recreating a two player ping-pong game on Python with just ChatGPT telling me what to do and paste on my code. Thank you for following the whole journey, and hopefully, you did learn a thing or two about what ChatGPT is capable of. That is all, and thank you for reading.

Neil S
Neil S
Neil is a highly qualified Technical Writer with an M.Sc(IT) degree and an impressive range of IT and Support certifications including MCSE, CCNA, ACA(Adobe Certified Associates), and PG Dip (IT). With over 10 years of hands-on experience as an IT support engineer across Windows, Mac, iOS, and Linux Server platforms, Neil possesses the expertise to create comprehensive and user-friendly documentation that simplifies complex technical concepts for a wide audience.
Watch & Subscribe Our YouTube Channel
YouTube Subscribe Button

Latest From Hawkdive

You May like these Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.