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

Code, Programmiersprache, Python, Python 3, Pygame, Flappy bird, VS Code, ChatGPT
Pygame hängt sich bei while True: loop auf?

Hallo,

ich programmiere gerade ein Spiel in Pygame. Auf jeden Fall möchte ich den Ninja Wurfsternen (&anderen Waffen)Ammo geben, dass man eine bestimmte Anzahl von diesen hat und diese nicht spammen kann. Wenn man Space drückt, wird der Ammo Variable eins abgezogen und wenn der Ammo (heißt es der Ammo? ) höher als 0 ist, wird der Befehl self.shoot() ausgeführt. Jetzt mein Problem: Egal, welche Nummer die Variable hat (außer Null), kann ich einmal schießen und danach nicht mehr, auch wenn ich Space drücke. Ich weiß nur nicht wieso. Wenn ich es in einen while True, loop packe, hängt sich pygame auf. Nur wie kann ich das fixen, dass ich öfter als 1 Mal schießen kann. Hier mein benötigter Code (ohne den While True loop):

class Player(pg.sprite.Sprite):

def __init__(self, game, x, y):

self.weapon = 'blowpipe'

self.shurikan = False

self.blowpipe = False

self.xp = PLAYER_XP

self.shoot_ammo = True

self.BLOWPIPE_AMMO = 5

self.SHURIKAN_AMMO = 5

def get_keys(self):

keys = pg.key.get_pressed()

if keys[pg.K_SPACE]:

if self.weapon == 'blowpipe' and self.shoot_ammo == True:

self.BLOWPIPE_AMMO -= 1

if self.BLOWPIPE_AMMO < 0:

self.BLOWPIPE_AMMO = 0

if self.BLOWPIPE_AMMO == 0:

self.shoot_ammo = False

if self.BLOWPIPE_AMMO > 0:

self.shoot()

if self.weapon == 'shurikan' and self.shoot_ammo == True:

self.SHURIKAN_AMMO -= 1

if self.SHURIKAN_AMMO < 0:

self.SHURIKAN_AMMO = 0

if self.SHURIKAN_AMMO == 0:

self.shoot_ammo = False

if self.SHURIKAN_AMMO > 0:

self.shoot()

def shoot(self):

if self.shoot_ammo == True:

now = pg.time.get_ticks()

if now - self.last_shot > WEAPONS[self.weapon]['rate']:

self.last_shot = now

dir = vec(1, 0).rotate(-self.rot)

EinegleicheListegibtesfürBlowpipe.

pos = self.pos + BARREL_OFFSET.rotate(-self.rot)

self.vel = vec(-WEAPONS[self.weapon]['rate'], 0).rotate(-self.rot)

for i in range(WEAPONS[self.weapon]['count']):

spread = uniform(-WEAPONS[self.weapon]['spread'], WEAPONS[self.weapon]['spread'])

Blowpipe(self.game, pos, dir.rotate(spread))

Außerdem gibt es eine Weapons Liste in einem anderen File:

WEAPONS['blowpipe'] = {'img': 'blowpipe.png',

'speed': 500,

'lifetime': 600,

'rate': 300,

'kickback': 0,

'spread': 5,

'damage': 3,

'size': 'blowpipe',

'count': 1}

Eine ähnliche Liste benutze ich für den Shurikan. Thx

Computer, Mac, programmieren, Informatik, Python, Python 3, Pygame, VS Code

Meistgelesene Beiträge zum Thema VS Code