From c9413c4affab0b6fe21b74fa65018fb522e94f99 Mon Sep 17 00:00:00 2001 From: Megamichi Date: Mon, 29 Jan 2024 18:46:36 +0100 Subject: [PATCH] / --- main.py | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..498f58c --- /dev/null +++ b/main.py @@ -0,0 +1,106 @@ +#!/usr/bin/python3 +import pygame +from random import randint +pygame.init() +## funktionen +def get_debug_text(): + def text(text,line_counter): + debug_text = default_font.render(str(text), True, (0,0,0)) + screen.blit(debug_text,(0,line_counter*default_font.get_height())) + + line_counter = 1 + globalvars = globals() + for a in globalvars: + text(f"{str(a)} = {str(globalvars[a])}",line_counter) + line_counter += 1 +## klassen +class Player(): + def __init__(self): + self.pos = 80,0 + self.gravity = 0.7 + self.gravityspeed = 0 + self.jumping = False + + def move(self): + self.gravityspeed += self.gravity + x,y = self.pos + y += self.gravityspeed + if y > boden: + y =boden + self.jumping = False + else: + self.jumping = True + self.pos = x,y + def jump(self): + if not self.jumping: + self.gravityspeed = -14 + + def render(self): + pygame.draw.rect(screen,(0,255,0),(self.pos[0]-50, + self.pos[1]-60, + 50, + 60)) + +class Kaktus(): + def __init__(self): + self.pos = screensize[0],boden + def move(self,steps): + x,y = self.pos + x -= steps + self.pos = x,y + def render(self): + pygame.draw.rect(screen,(255,0,0),(self.pos[0]-50, + self.pos[1]-60, + 50, + 60)) + +## variablen +screen = pygame.display.set_mode((800,500),pygame.RESIZABLE) +pygame.display.set_caption("FLAPPY BIRD") +screensize = pygame.display.get_window_size() +clock = pygame.time.Clock() +default_font = pygame.font.SysFont("sans", 14) +boden = (screensize[1]//3)*2 + +player = Player() +Kaktuse = [] +for a in range(3): + Kaktuse.append(Kaktus()) +kaktusspeed = 5 +show_debug = False +running = True +kaktus_ticker = 0 +if __name__ == "__main__": + while running: + ## key managment + pressed_keys = pygame.key.get_pressed() + + if pressed_keys[pygame.K_ESCAPE]: + exit() + if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]: + show_debug = not show_debug + if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]: + player.jump() + Kaktuse.append(Kaktus()) + + preview_pressed_keys = pressed_keys + ## event managment + for event in pygame.event.get(): + if event.type == pygame.QUIT: + exit() + if event.type == pygame.WINDOWRESIZED: + screensize = pygame.display.get_window_size() + + pygame.draw.line(screen,(0,0,0),(0,boden),(screensize[0],boden)) + player.move() + player.render() + for a in Kaktuse: + a.move(kaktusspeed) + a.render() + kaktus_ticker += 1 + if show_debug: + get_debug_text() + ## bildschirm aktuallisierung + pygame.display.flip() + clock.tick(60) + screen.fill((255,255,255)) \ No newline at end of file