You are on page 1of 2

Week 2:

import pygame

import sys

# Initialize Pygame

pygame.init()

# Set up the game window

window_size = (800, 600)

screen = pygame.display.set_mode(window_size)

pygame.display.set_caption("Simple Game")

# Set up the player character

player_size = 50

player_color = (255, 0, 0) # Red

player_pos = [window_size[0] // 2, window_size[1] // 2]

# Set up the game clock

clock = pygame.time.Clock()

# Main game loop

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()
# Update the game logic here (e.g., move the player)

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT]:

player_pos[0] -= 5

if keys[pygame.K_RIGHT]:

player_pos[0] += 5

if keys[pygame.K_UP]:

player_pos[1] -= 5

if keys[pygame.K_DOWN]:

player_pos[1] += 5

# Draw the player and update the display

screen.fill((255, 255, 255)) # White background

pygame.draw.rect(screen, player_color, (player_pos[0], player_pos[1], player_size, player_size))

pygame.display.flip()

# Set the frames per second (FPS)

clock.tick(30) # Adjust the value to control the speed

You might also like