Curriculum
Course: Python Game Development : Build 5 Fun Pr...
Login

Curriculum

Python Game Development : Build 5 Fun Projects with Pygame

Text lesson

Intial setup: Project 4 : Catch the same color ball

Initial Setup

These lines import the necessary modules: pygame for game development and random for generating random numbers.

import pygame

import random

This initializes the pygame library.

# Initialize pygame

pygame.init()

These are constants defining the dimensions of the game window (WIDTH and HEIGHT), frames per second (FPS), colors (BLACK and WHITE), and dimensions/speed of the player character and falling balls.

# Constants

WIDTH, HEIGHT = 800, 600

FPS = 60

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

BALL_RADIUS = 10

PLAYER_WIDTH = 100

PLAYER_HEIGHT = 20

PLAYER_SPEED = 5

This is a list of colors that will be randomly assigned to the player character and falling balls.

# Define colors

COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0), (128, 0, 128), (255, 192, 203), (0, 128, 0)]

This creates the game window with the specified width and height, and sets the window caption.

# Create the display window

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption(“Catch The Color Game”)