Installiere PyGame. Achtung: In der Vergangenheit habe ich die Erfahrung gemacht, dass PyGame manchmal nicht mit der neusten Versionen von Python läuft. Die neuste Version, mit der ich es getestet habe, ist Python 3.9.0, wahrscheinlich läuft es aber auch mit neueren Versionen. Es ist aber kein Problem, mehrere Python-Installationen parallel zu haben.
Siehe dazu die Seite Python Setup
Für unterschiedliche Programmiersprachen gibt es unterschiedliche Konventionen dazu, wie man Variablen, Klassen usw. benennt. Zum Beispiel sollten Klassen im UpperCamelCase
Stil und Funktionen & Variablen im snake_case
Stil notiert werden. Hier findest du mehr Infos zu den Programmier-Konventionen.
Pfade von Ordner und Dokumente werden in Windows (myfolder\\myfile.txt
) anders angegeben als in MacOS (myfolder/myfile.txt
). Damit dein Code auf sämtlichen Plattformen funktioniert, musst du also solche Pfadangaben vermeiden. Stattdessen sollst du das os Modul verwenden:
import os.path
# KORREKT:
path = os.path.join("myfolder", "myfile.txt")
# falsch 1:
path = "myfolder/myfile.txt")
# falsch 2:
path = "myfolder\\myfile.txt")
Für dein/euer PyGame-Projekt ist es Pflicht, dieses objektorientiert zu programmieren. Verwende dafür das Template unten. Theoretisch kann man das ganze Game in einem einzigen File programmieren. Gerade für grössere Projekte und wenn man zusammen am gleichen Code arbeitet, so lohnt es sich, das Projekt auf mehrere Files aufzuteilen. Wir verfolgen hier den Grundsatz: für jede Klasse ein eigenes File.
Template herunterladen
Template anzeigen
- main.py
from settings import *
from player import *
from game import *
"""
MAIN FILE
- execute this file
- for VSC: template includes launch.json file which includes line '"program": "main.py",' -> always launch this file
"""
if __name__ == "__main__":
Game().play()
- settings.py
"""
file contains all settings
remember: use CAPITAL letters for CONSTANTS (variables that don't change)
"""
# WINDOWS SIZE
WIN_HEIGHT = 800
WIN_WIDTH = 500
# DEFINE SOME COLORS
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BG_COLOR = WHITE
# FRAMERATE
FPS = 30 # frames per second
TIME_DELAY = int(1000 / FPS) # calculate delay time between two frames
# GAME CONSTANTS
SPEED = 12
- game.py
import pygame
import sys
from settings import *
from player import *
from ball import *
class Game:
"""
Main GAME class
"""
def __init__(self):
pygame.init()
pygame.font.init()
self.time_delay = TIME_DELAY
self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) # create screen which will display everything
self.win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Bouncing Balls") # Game title
def quit(self):
pygame.quit()
sys.exit(0)
def play(self):
# CREATE GAME OBJECTS
# PLAYER:
player = Player(os.path.join("data","battleship.png"),(150,550))
# OTHER OBJECTS:
"""
if you have multiple objects of the same class (e.g. enemies), use a SPRITE GROUP:
"""
balls = pygame.sprite.Group()
balls.add(Ball(os.path.join("data","ball_blue.png"),[100,200]))
balls.add(Ball(os.path.join("data","ball_black.png"),[200,400]))
balls.add(Ball(os.path.join("data","ball_green.png"),[300,600]))
# GAME PERMANENT LOOP
while True:
pygame.time.delay(TIME_DELAY)
# KEY EVENTS
for event in pygame.event.get():
# Exit app if click quit button
if event.type == pygame.QUIT:
self.quit()
# Naviation of player
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player.rect.top -= SPEED
# and so on ...
# COLLISION DETECTION
"""
see manual for all types of collisions: https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide
"""
for ball in balls: # use loop to iterate through sprite group easily
pass
# UPDATE ALL GAME OBJECTS
balls.update() # can update all members of a group with a single command
player.update()
# DRAW
self.screen.fill(BG_COLOR) # draw empty screen
balls.draw(self.screen) # draw all group members
self.screen.blit(player.image, player.rect) # draw single object
# UPDATE DISPLAY
pygame.display.update()
pygame.quit()
- player.py
import pygame
import os
from settings import *
class Player(pygame.sprite.Sprite):
def __init__(self, img_path, xy_center):
super().__init__() # call __init__ of parent class (i.e. of pygame.sprite.Sprite)
# ASSIGN CLASS ATTRIBUTES
if not os.path.exists(img_path):
raise Exception("THE FOLLOWING FILE DOES NOT EXIST: {0}".format(img_path))
self.image = pygame.image.load(str(img_path)) # load image
self.rect = self.image.get_rect() # create rectangle containing ball image
self.rect.center = xy_center # set center coords of player
self.mask = pygame.mask.from_surface(self.image) # creates a mask, used for collision detection (see manual about pygame.sprite.collide_mask())
def update(self):
"""
- update function gets executed in every step
- determines motion of player
"""
pass
def collide(self,ball):
"""
player collides with ball, given as argument
this method updates velocities of BOTH player and ball
"""
pass
- ball.py
import pygame
import os
class Ball(pygame.sprite.Sprite):
"""
Class for balls, derive from pygame's sprite class
-> makes your life easier since you can use e.g. the collision detection of the sprite class
"""
def __init__(self, img_path, xy_center):
super().__init__() # call __init__ of parent class (i.e. of pygame.sprite.Sprite)
# ASSIGN CLASS ATTRIBUTES
if not os.path.exists(img_path): # check if folder of image exists
raise Exception("THE FOLLOWING FILE DOES NOT EXIST: {0}".format(img_path))
self.image = pygame.image.load(str(img_path)) # load image
self.rect = self.image.get_rect() # create rectangle containing ball image
self.rect.center = xy_center # set center coords of ball
self.mask = pygame.mask.from_surface(self.image) # creates a mask, used for collision detection (see manual about pygame.sprite.collide_mask())
def update(self):
"""
- update function gets executed in every step
- determines motion of ball
"""
pass
def collide(self,ball):
"""
ball self collides with other ball, given as argument
this method updates velocities of BOTH balls
"""
pass