Pygame Flappy Bird?
Ich habe den folgenden Python-Code mit Pygame programmiert. Bin aber noch ein absoluter Anfänger, daher ist zwischen "#copy start" und "#copy end" der Code von ChatGPT generiert. Die Pipes werden trotzdem nicht angezeigt, weiß jemad woran das liegt?
import pygame
import os
import random
pygame.init()
width = 1280
height = 720
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption("Flappy Bird")
PIPE_WIDTH = 80
PIPE_GAP = 150
PIPE_SPEED = 5
flappyY = 360
score = 0
highscore = 0
pipes = []
pipe_timer = 1
if not os.path.exists("highscore.txt"):
with open("highscore.txt", "w") as file:
file.write(str(highscore))
with open("highscore.txt", "r") as rfile:
highscore = int(rfile.read())
#copy start
class Pipe:
def __init__(self):
self.x = width
self.height = random.randint(100, height - PIPE_GAP - 100)
self.passed = False
def move(self):
self.x -= PIPE_SPEED
def draw(self, screen):
# Draw upper pipe
pygame.draw.rect(screen, (0, 255, 0), (self.x, 0, PIPE_WIDTH, self.height))
# Draw lower pipe
pygame.draw.rect(screen, (0, 255, 0), (self.x, self.height + PIPE_GAP, PIPE_WIDTH, height - self.height - PIPE_GAP))
def off_screen(self):
return self.x + PIPE_WIDTH < 0
#copy end
running = True
while running:
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if event.type == pygame.QUIT:
running = False
if keys[pygame.K_SPACE]:
if flappyY > -16:
flappyY -= 1
if flappyY < 704:
flappyY += 0.4
if score > highscore:
highscore = score
#copy start
if pipe_timer > 100:
pipes.append(Pipe())
pipe_timer = 0
for pipe in pipes:
pipe.move()
pipe.draw(screen)
pipes = [pipe for pipe in pipes if not pipe.off_screen()]
pipe_timer += 1
#copy end
pygame.display.set_caption(f"Flappy Bird (Highscore: {highscore:.0f})")
rect = pygame.Rect(640, flappyY, 32, 32)
#print(flappyY)
screen.fill((0, 180, 255))
pygame.draw.rect(screen, "red" , rect)
pygame.display.update()
pygame.quit()
Vielen Dank und LG