我需要在pygame中为我的《Flappy bird》添加一个管道刷出系统。我已经对它进行了编码,这样我就可以生成一个管道,但我需要在顶部有一个180度旋转的管道,中间有一点空间,这样我的鸟就可以在管道之间飞行。现在我只得到一个错误。(我将添加碰撞和管道列表,以便它们保留在屏幕上)。
下面是我的代码:import pygame
import os
import random
from sys import exit
pygame.init()
os.system("cls")
WIDTH = 288
HEIGHT = 512
FPS = 60
JUMP_POWER = 60
GRAVITY = 0.15
GAME_ACTIVE = 1
VOLUME = 0.15
OBSTACLE_INTERVAL = 500
OBSTACLE_SPACE = 100
AWAY_FROM_BIRD = 150
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("assets/images/player/bird.png").convert_alpha()
self.rect = self.image.get_rect(center = (WIDTH/4, HEIGHT/2))
self.gravity_store = 0
self.jump_sfx = pygame.mixer.Sound("assets/audio/jump.wav")
self.jump_sfx.set_volume(VOLUME)
self.hit_sfx = pygame.mixer.Sound("assets/audio/hit.wav")
self.hit_sfx.set_volume(VOLUME)
def player_input(self):
for event in event_list:
if event.type == pygame.KEYDOWN:
if self.rect.bottom <= 0:
pass
else:
if event.key == pygame.K_SPACE:
self.gravity_store = 0
self.rect.y -= JUMP_POWER
self.jump_sfx.play()
def gravity(self):
self.gravity_store += GRAVITY
self.rect.y += self.gravity_store
def collision(self):
global GAME_ACTIVE
if self.rect.colliderect(ground_rect):
self.hit_sfx.play()
GAME_ACTIVE = 0
def update(self):
self.player_input()
self.gravity()
self.collision()
class Obstacles(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")
def obstacle_spawn(self):
player_x = player.sprite.rect.x
obstacle_height1 = random.randint((HEIGHT/2)-150, HEIGHT-150)
self.rect = self.image.get_rect(midtop = (player_x+AWAY_FROM_BIRD, obstacle_height1))
obstacle.draw(SCREEN)
self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")
obstacle_height2 = self.rect.midtop
heightlist = list(obstacle_height2)
y = heightlist[1]
y = y - OBSTACLE_SPACE
del heightlist[1]
heightlist.append(y)
obstacle_height2 = tuple(heightlist)
self.image = pygame.transform.rotate(self.image, 180)
self.rect = self.image.get_rect(bottom = (player_x+AWAY_FROM_BIRD, obstacle_height2))
obstacle.draw(SCREEN)
def update(self):
self.obstacle_spawn()
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mama Bird")
clock = pygame.time.Clock()
background_surf = pygame.image.load("assets/images/background/background-day.png")
background_rect = background_surf.get_rect()
ground_surf = pygame.image.load("assets/images/background/base.png")
ground_rect = ground_surf.get_rect(topleft = (0, HEIGHT-112))
player = pygame.sprite.GroupSingle()
player.add(Player())
obstacle = pygame.sprite.Group()
obstacle.add(Obstacles())
OBSTACLESPAWN = pygame.USEREVENT + 1
pygame.time.set_timer(OBSTACLESPAWN, OBSTACLE_INTERVAL)
while True:
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
pygame.quit()
exit()
if GAME_ACTIVE == 1:
SCREEN.blit(background_surf, background_rect)
player.draw(SCREEN)
player.update()
for event in event_list:
if event.type == OBSTACLESPAWN and GAME_ACTIVE == 1:
obstacle.update()
#obstacle.draw(SCREEN)
SCREEN.blit(ground_surf, ground_rect)
pygame.display.update()
clock.tick(FPS)
错误如下:
Traceback (most recent call last):
File "C:Users46722DocumentsProgrammeringPythonyesmanPygameMama Birdmain.py", line 121, in <module>
obstacle.update()
File "C:Users46722AppDataLocalProgramsPythonPython310libsite-packagespygamesprite.py", line 539, in update
sprite.update(*args, **kwargs)
File "C:Users46722DocumentsProgrammeringPythonyesmanPygameMama Birdmain.py", line 86, in update
self.obstacle_spawn()
File "C:Users46722DocumentsProgrammeringPythonyesmanPygameMama Birdmain.py", line 81, in obstacle_spawn
self.rect = self.image.get_rect(bottom = (player_x+AWAY_FROM_BIRD, obstacle_height2))
TypeError: invalid rect assignment
PS C:Users46722DocumentsProgrammeringPythonyesmanPygameMama Bird>
bottom
参数只影响矩形的y-坐标。因此,您只能分配单个值,而不能分配元组。如果你想设置x和坐标,您必须使用bottomleft
,midbottom
或bottomright
(见pygame.Rect
)。此外,obstacle_height2
需要是单个值,而不是一个列表。例:self.rect.top
obstacle_height2 = self.rect.top
self.image = pygame.transform.rotate(self.image, 180)
self.rect = self.image.get_rect(midbottom = (player_x+AWAY_FROM_BIRD, obstacle_height2))
另外,你需要用pygame.transform.flip
:
self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")
self.image = pygame.transform.flip(self.image, False, True)