69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
# Code is taken from https://realpython.com/pygame-a-primer/
|
|
|
|
# Import and initialize the pygame library
|
|
import pygame
|
|
from player import Player
|
|
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
|
|
|
from pygame.locals import (
|
|
K_UP,
|
|
K_DOWN,
|
|
K_LEFT,
|
|
K_RIGHT,
|
|
K_ESCAPE,
|
|
KEYDOWN,
|
|
QUIT,
|
|
)
|
|
|
|
pygame.init()
|
|
|
|
|
|
# Create the screen object
|
|
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
|
|
|
|
# Variable to keep the main loop running
|
|
running = True
|
|
|
|
player = Player()
|
|
|
|
# Create groups to hold enemy sprites and all sprites
|
|
# - enemies is used for collision detection and position updates
|
|
# - all_sprites is used for rendering
|
|
enemies = pygame.sprite.Group()
|
|
all_sprites = pygame.sprite.Group()
|
|
all_sprites.add(player)
|
|
|
|
# Main loop
|
|
while running:
|
|
# Look at every event in the queue
|
|
for event in pygame.event.get():
|
|
# Did the user hit a key?
|
|
if event.type == KEYDOWN:
|
|
# Was it the Escape key? If so, stop the loop.
|
|
if event.key == K_ESCAPE:
|
|
running = False
|
|
|
|
# Did the user click the window close button? If so, stop the loop.
|
|
elif event.type == QUIT:
|
|
running = False
|
|
|
|
# Fill the background with white
|
|
screen.fill((255, 255, 255))
|
|
|
|
# Get all the keys currently pressed
|
|
pressed_keys = pygame.key.get_pressed()
|
|
|
|
# Update the player sprite based on user keypresses
|
|
player.update(pressed_keys)
|
|
|
|
# Draw all sprites
|
|
for entity in all_sprites:
|
|
screen.blit(entity.surf, entity.rect)
|
|
|
|
# Flip the display
|
|
pygame.display.flip()
|
|
|
|
# Done! Time to quit.
|
|
pygame.quit() |