0% found this document useful (0 votes)
7 views2 pages

Pygame GAME?

This document is a Python script that uses Pygame to create a simple game where a player can move around the screen and score points by colliding with randomly generated enemies. The game includes basic functionalities such as player movement, enemy generation, collision detection, and score tracking. The game runs in a loop until the player decides to quit, with a frame rate capped at 60 frames per second.

Uploaded by

maneirawm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Pygame GAME?

This document is a Python script that uses Pygame to create a simple game where a player can move around the screen and score points by colliding with randomly generated enemies. The game includes basic functionalities such as player movement, enemy generation, collision detection, and score tracking. The game runs in a loop until the player decides to quit, with a frame rate capped at 60 frames per second.

Uploaded by

maneirawm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import random

Initialize Pygame
pygame.init()

Set up some constants


WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

Set up the display


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

Set up the font


font = pygame.font.Font(None, 36)

Set up the clock


clock = pygame.time.Clock()

Set up the player


player_x, player_y = WIDTH / 2, HEIGHT / 2
player_speed = 5

Set up the enemies


enemies = []
enemy_speed = 2

Set up the score


score = 0

Game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Get a list of all keys currently being pressed down


keys = pygame.key.get_pressed()

# Move the player


if keys[pygame.K_UP]:
player_y -= player_speed
if keys[pygame.K_DOWN]:
player_y += player_speed
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed

# Create new enemies


if random.random() < 0.05:
enemy_x, enemy_y = random.randint(0, WIDTH), random.randint(0, HEIGHT)
enemies.append((enemy_x, enemy_y))

# Move enemies
for i, (enemy_x, enemy_y) in enumerate(enemies):
enemy_x += enemy_speed
if enemy_x > WIDTH:
enemy_x = 0
enemies[i] = (enemy_x, enemy_y)

# Check for collisions


for enemy_x, enemy_y in enemies:
if (abs(player_x - enemy_x) < 20) and (abs(player_y - enemy_y) < 20):
score += 1

# Draw everything
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, (player_x, player_y, 20, 20))
for enemy_x, enemy_y in enemies:
pygame.draw.rect(screen, WHITE, (enemy_x, enemy_y, 20, 20))
text = font.render(f"Score: {score}", True, WHITE)
screen.blit(text, (10, 10))

# Flip the display


pygame.display.flip()

# Cap the frame rate


clock.tick(60)

Quit Pygame
pygame.quit()

You might also like