dino/main.py
2024-01-29 19:19:45 +01:00

112 lines
No EOL
3.4 KiB
Python

#!/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 = []
kaktus_ticker = 0
next_kaktus = randint(100,500)
for a in range(3):
Kaktuse.append(Kaktus())
kaktusspeed = 5
show_debug = False
running = True
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()
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()
if a.pos[0] < 0:
Kaktuse.pop(0)
if kaktus_ticker > next_kaktus:
next_kaktus = randint(80,180)
kaktus_ticker =0
Kaktuse.append(Kaktus())
kaktus_ticker += kaktusspeed*0.3
if show_debug:
get_debug_text()
## bildschirm aktuallisierung
pygame.display.flip()
clock.tick(60)
screen.fill((255,255,255))