Mastering Pygame: Crafting a Dynamic 2D Game Experience

In this classic arcade-style game powered by Pygame, players guide a character to evade obstacles while collecting points. With intuitive controls, it offers an engaging experience reminiscent of timeless arcade adventures.

#Start to write the code for 2D game

import pygame
import random

#Initialize Pygame

pygame.init()

#Set up the screen

screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(“Simple 2D Game”)

#Colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

#Player

player_width = 50
player_height = 50
player_x = screen_width // 2 – player_width // 2
player_y = screen_height – player_height – 20
player_speed = 5

#Obstacles

obstacle_width = 50
obstacle_height = 50
obstacle_speed = 5
obstacle_frequency = 25
obstacles = []

#Points

points = 0
font = pygame.font.SysFont(None, 36)

#Game Over

game_over_font = pygame.font.SysFont(None, 72)

def draw_player(x, y):
pygame.draw.rect(screen, WHITE, [x, y, player_width, player_height])

def draw_obstacles(obstacles):
for obstacle in obstacles:
pygame.draw.rect(screen, WHITE, obstacle)

def draw_text(text, font, color, x, y):
surface = font.render(text, True, color)
screen.blit(surface, (x, y))

def game_over():
text = game_over_font.render(“Game Over”, True, WHITE)
screen.blit(text, (screen_width // 2 – 150, screen_height // 2 – 50))

#Game loop

running = True
clock = pygame.time.Clock()

while running:
screen.fill(BLACK)

for event in pygame.event.get():

if event.type==pygame.QUIT:

running=False

#player Movement

keys=pygame.key.get_pressed()

if keys[pygame.K_LEFT] and player_x>0:

player_x-=player_speed

if keys[pygame.K_RIGHT] and player_x<screen_width-player_width:

player_x+=player_speed

#Spawn obstacles

if random.randint(1,obstacle_frequency)==1:

obstacle_x=random.randrange(0,screen_width-obstacle_width)

obstacle_y=-obstacle_height

obstacles.append(pygame.Rect(obstacle_x,obstacle_y,obstacle_width,obstacle_height))

#Move Obstacles

for obstacle in obstacles:

obstacle.y+=obstacle_speed

if obstacle.y>screen_height:

obstacles.remove(obstacle)

points +=1

#Collision Detection

player_rect=pygame.Rect(player_x,player_y, player_width, player_height)

for obstacle in obstacles:

if player_rect.colliderect(obstacle):

running=False

#Draw everything

draw_player(player_x, payer_y)

draw_obstacles (obstacles)

draw_text(“Your Points: “+ str(points), font, WHITE, 15,15)

#Check for game over

if not running:

game_over()

pygame.display.update()

clock.tick(60)

pygame.quit()

Game Workflow

  1. Initializing Pygame: First, we import the Pygame library and initialize it. Pygame is a set of Python modules designed for writing video games.
  2. Setting up the Screen: We create a window with a width of 800 pixels and a height of 600 pixels. This is where the game will be displayed.
  3. Defining Colors: We define some colors using RGB values. In this case, we have white (255, 255, 255) and black (0, 0, 0).
  4. Player Setup: We set up the player’s character. The player is represented by a rectangle with a width and height of 50 pixels. Initially, the player is positioned near the bottom center of the screen.
  5. Obstacle Setup: We set up the falling obstacles. These obstacles also consist of rectangles, but they fall from the top of the screen at random positions. The obstacles have a set width and height, and they fall at a constant speed.
  6. Points Setup: We initialize a variable to keep track of the player’s points. Points are earned by avoiding obstacles.
  7. Game Over Setup: We set up a font for displaying “Game Over” text when the game ends.
  8. Drawing Functions: We define functions to draw the player, obstacles, and text on the screen. These functions use Pygame’s drawing capabilities.
  9. Game Loop: The main part of the game is the game loop, which runs continuously until the game is over. Within the loop, we handle user input, update the game state, and draw everything on the screen.
  10. Player Movement: We check for user input to move the player left or right using the arrow keys.
  11. Obstacle Spawning: We randomly spawn obstacles at the top of the screen. The frequency of spawning is determined by the obstacle_frequency variable.
  12. Obstacle Movement: We move each obstacle downward on the screen. If an obstacle goes off the bottom of the screen, it is removed from the list of obstacles, and the player earns a point.
  13. Collision Detection: We check for collisions between the player and obstacles. If a collision occurs, the game ends.
  14. Drawing Everything: We draw the player, obstacles, and points on the screen during each iteration of the game loop.
  15. Game Over: If the game ends, we display “Game Over” text on the screen.
  16. Updating the Display: Finally, we update the display and regulate the frame rate to ensure smooth gameplay.

Overall, this code creates a simple game where the player controls a character to avoid falling obstacles and collect points.

Leave a Reply

Your email address will not be published. Required fields are marked *