Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created June 30, 2024 16:11
Show Gist options
  • Save decagondev/20df177690b15e4e025eefc2443801ab to your computer and use it in GitHub Desktop.
Save decagondev/20df177690b15e4e025eefc2443801ab to your computer and use it in GitHub Desktop.

Step 1: Setting Up the Environment

First, we need to install Pygame if it's not already installed. You can do this by running:

pip install pygame

Step 2: Importing Required Modules

At the beginning of our script, we import necessary modules from Pygame:

import pygame
import sys

Step 3: Initializing Pygame

We initialize Pygame and set up the display window:

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pong")

Step 4: Defining Game Objects

Create classes for the ball and paddles. Each object has its own properties like position, speed, and dimensions.

Ball Class

class Ball:
    def __init__(self):
        self.x = 400
        self.y = 300
        self.radius = 10
        self.speed_x = 5
        self.speed_y = 5

    def move(self):
        self.x += self.speed_x
        self.y += self.speed_y
        # Bounce off walls
        if self.x + self.radius > 800 or self.x - self.radius < 0:
            self.speed_x *= -1
        if self.y + self.radius > 600 or self.y - self.radius < 0:
            self.speed_y *= -1

Paddle Class

class Paddle:
    def __init__(self, x, y):
        self.width = 10
        self.height = 100
        self.speed = 5
        self.x = x
        self.y = y

    def move(self, up=True):
        if up:
            self.y -= self.speed
        else:
            self.y += self.speed
        # Keep paddle within bounds
        self.y = max(0, min(600 - self.height, self.y))

Step 5: Main Game Loop

The main game loop handles events, updates game state, and draws objects on the screen.

def main():
    clock = pygame.time.Clock()
    ball = Ball()
    paddle_a = Paddle(0, 300)
    paddle_b = Paddle(790, 300)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            paddle_a.move(up=True)
        if keys[pygame.K_s]:
            paddle_a.move(up=False)
        if keys[pygame.K_UP]:
            paddle_b.move(up=True)
        if keys[pygame.K_DOWN]:
            paddle_b.move(up=False)

        ball.move()

        # Check collision with paddles
        if ball.x - ball.radius <= paddle_a.x + paddle_a.width and \
           paddle_a.y <= ball.y <= paddle_a.y + paddle_a.height:
            ball.speed_x *= -1
        elif ball.x + ball.radius >= paddle_b.x and \
             paddle_b.y <= ball.y <= paddle_b.y + paddle_b.height:
            ball.speed_x *= -1

        screen.fill((0, 0, 0))
        pygame.draw.circle(screen, (255, 255, 255), (ball.x, ball.y), ball.radius)
        pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(paddle_a.x, paddle_a.y, paddle_a.width, paddle_a.height))
        pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(paddle_b.x, paddle_b.y, paddle_b.width, paddle_b.height))

        pygame.display.flip()
        clock.tick(60)

if __name__ == "__main__":
    main()

Explanation

  1. Initialization: We start by initializing Pygame and creating the screen.
  2. Classes: Ball and Paddle classes encapsulate the behavior and state of the game objects.
  3. Game Loop: The main loop updates the positions of the objects based on user input and game rules, then redraws the screen.
  4. Event Handling: Keyboard inputs are used to move the paddles.
  5. Drawing: Pygame functions are used to draw the game objects on the screen.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment