Pygame Development - Part 1

Dieser und die folgenden Artikel werden sich mit der Entwicklung eines Videospiels mit der Python Bibliothek pygame befassen. Ich erhoffe mir dadurch, einiges über die Entwicklung eines Spieles zu lernen und letztendlich mein Traumspiel zu entwickeln.

Installation

Die Installation von pygame ist so einfach, wie die Installation jedes anderen Python Modules:

python3 -V
> Python 3.8.5

# Generate virtualenv
virtualenv -p `which python3` pygame
source pygame/bin/activate

# Instlall pygame
pip install pygame
python -c "import pygame; pygame.version"
> pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)
> Hello from the pygame community. https://www.pygame.org/contribute.html

Ein erstes Fenster

Für den Anfang soll ein einfaches schwarzes Fenster erzeugt werden. Das Programm soll beendet werden wenn das Fenster geschlossen wird oder wenn man die ESC Taste drückt.

#!/usr/bin/env python3


# Import and init pygame
import pygame
pygame.init()


# Define some constants
SIZE = (800, 600)
BLACK = (0, 0, 0)
FPS = 60


# Create main window
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Hello Pygame")
running = True


# Enter main game loop
while running:
    # Handle Events
    for event in pygame.event.get():
        print(str(event))

        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False

        # Check for KEYDOWN
        if event.type == pygame.KEYDOWN:
            # Check for ESC
            if event.key == pygame.K_ESCAPE:
                running = False

    # Update game
    pass

    # Draw onto the screen
    screen.fill(BLACK)
    pygame.display.flip()


# Exit pygame
pygame.quit()
April 20, 2021