pygame_code_explanation
pygame_code_explanation
import pygame
import random
- import pygame: Imports the Pygame library, which is used to create games and handle graphics.
- import random: Imports the random module, which allows for random number generation, useful
pygame.init()
- pygame.init(): Initializes all the Pygame modules that are required for the game (e.g., the display,
clock).
2. Screen Setup:
- screen_width, screen_height: Defines the width and height of the game window (800x600 pixels).
- pygame.display.set_mode(): Creates the window for the game, setting the dimensions to the
3. Colors:
black = (0, 0, 0)
red = (255, 0, 0)
- Defines color variables in RGB format: white, black, red, and green.
clock = pygame.time.Clock()
- pygame.time.Clock(): Creates a clock object that controls the game's frame rate.
paddle_speed = 10
- paddle_width, paddle_height: Dimensions of the paddle (60 pixels wide, 20 pixels tall).
- paddle_y: Initial vertical position of the paddle, placed near the bottom of the screen.
- pygame.Rect(): Defines the paddle as a rectangle that will be rendered on the screen.
paddle_lengths = [paddle_height]
size_increase = 5
obstacle_speed = 5
obstacles = []
score = 0
- paddle_lengths: A list that stores the length of the paddle (initially the paddle's height).
- size_increase: The amount by which the paddle increases when a green obstacle is hit.
6. Event Handling:
def handle_events():
if event.type == pygame.QUIT:
pygame.quit()
- handle_events(): Checks for game events like quitting the game. If the quit event is triggered (e.g.,
7. Paddle Movement:
def move_paddle(direction):
global paddle_x
paddle_x -= paddle_speed
paddle_x += paddle_speed
- move_paddle(direction): Moves the paddle left or right. It checks if the paddle is within screen
def increase_size():
paddle_lengths.append(new_length)
print(paddle_lengths)
def size_decrease():
paddle_lengths.pop()
print(paddle_lengths)
if len(paddle_lengths) < 1:
pygame.quit()
- increase_size(): Increases the paddle's size by adding the new size to paddle_lengths each time
- size_decrease(): Decreases the paddle size by removing the last added length when the player
hits a red obstacle. If the paddle size becomes 0, the game quits.
9. Creating Obstacles:
def create_obstacle():
obstacle_y = 0
- create_obstacle(): Creates an obstacle at a random horizontal position and at the top of the
screen. The color is randomly chosen to be green or red, and the obstacle is added to the obstacles
list.
def draw_paddle():
global paddle
paddle_lengths[-1])
def draw_obstacles():
- draw_paddle(): Updates the paddle size and draws it on the screen using pygame.draw.rect().
- draw_obstacles(): Draws each obstacle in the obstacles list on the screen with its respective color
and position.
def check_collision():
global score
if obstacle["rect"].colliderect(paddle):
if obstacle["color"] == green:
increase_size()
score += 1
size_decrease()
score -= 1
obstacles.remove(obstacle)
obstacles.remove(obstacle)
- check_collision(): Checks if an obstacle collides with the paddle using colliderect(). If it does, the
paddle size and score are updated depending on the color of the obstacle. If an obstacle goes
def display_score():
- display_score(): Displays the current score at the top-left corner of the screen.
while True:
handle_events()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
move_paddle("left")
if keys[pygame.K_RIGHT]:
move_paddle("right")
if random.randint(1, 10) == 1:
create_obstacle()
for obstacle in obstacles:
obstacle["rect"].y += obstacle_speed
check_collision()
screen.fill(black)
draw_paddle()
draw_obstacles()
display_score()
pygame.display.flip()
clock.tick(30)
2. Checks for key presses (left and right arrow keys) and moves the paddle.
5. Clears the screen and redraws all objects (paddle, obstacles, score).
6. Updates the display and ensures the game runs at 30 frames per second (clock.tick(30)).