python:pgzero:projekt4
Dies ist eine alte Version des Dokuments!
Projekt 4 - Catch the Grinch
Quelltext
# Catch the Grinch import random FONT_COLOUR = (255, 255, 255) WIDTH = 800 HEIGHT = 600 CENTRE_X = WIDTH // 2 CENTRE_Y = HEIGHT // 2 CENTRE = (CENTRE_X, CENTRE_Y) FINAL_LEVEL = 6 START_SPEED = 10 PLAYFIGURES = ["genie", "homer", "jerry", "kermit", "ninja-turtle"] game_over = False game_complete = False current_level = 1 figures = [] animations = [] def draw(): global figures, current_level, game_over, game_complete screen.clear() screen.blit("background", (0, 0)) if game_over: display_message("GAME OVER!", "Versuche es erneut.") elif game_complete: display_message("DU HAST GEWONNEN!", "Gut gemacht.") else: for figur in figures: figur.draw() def update(): global figures if len(figures) == 0: figures = make_figures(current_level) def make_figures(number_of_extra_figures): playfigures_to_create = get_playfigures_to_create(number_of_extra_figures) new_figures = create_figures(playfigures_to_create) layout_figures(new_figures) animate_figures(new_figures) return new_figures def get_playfigures_to_create(number_of_extra_figures): playfigures_to_create = ["grinch"] for i in range(0, number_of_extra_figures): random_playfigur = random.choice(PLAYFIGURES) playfigures_to_create.append(random_playfigur) return playfigures_to_create def create_figures(playfigures_to_create): new_figures = [] for playfigur in playfigures_to_create: figur = Actor(playfigur) new_figures.append(figur) return new_figures def layout_figures(figures_to_layout): number_of_gaps = len(figures_to_layout) + 1 gap_size = WIDTH // number_of_gaps random.shuffle(figures_to_layout) for index, figur in enumerate(figures_to_layout): new_x_pos = (index + 1) * gap_size figur.x = new_x_pos def animate_figures(figures_to_animate): for figur in figures_to_animate: duration = START_SPEED - current_level figur.anchor = ("center", "bottom") animation = animate(figur, duration=duration, on_finished = handle_game_over, y=HEIGHT) animations.append(animation) def handle_game_over(): global game_over game_over = True def on_mouse_down(pos): global figures, current_level for figur in figures: if figur.collidepoint(pos): if "grinch" in figur.image: grinch_figur_click() else: handle_game_over() def grinch_figur_click(): global current_level, figures, animations, game_complete stop_animations(animations) if current_level == FINAL_LEVEL: game_complete = True else: current_level = current_level + 1 figures = [] animations = [] def stop_animations(animations_to_stop): for animation in animations_to_stop: if animation.running: animation.stop() def display_message(heading_text, sub_heading_text): screen.draw.text(heading_text, fontsize=60, center=CENTRE, color=FONT_COLOUR) screen.draw.text(sub_heading_text, fontsize=30, center=(CENTRE_X, CENTRE_Y + 30), color=FONT_COLOUR)
python/pgzero/projekt4.1622367124.txt.gz · Zuletzt geändert: 2021/05/30 11:32 von lutz