常量
self.speed = 0
self.image_index = 0
self.image = bird_images[self.image_index]
self.rect = self.image.get_rect()
def jump(self):
self.speed = jump_speed
def update(self):
# 更新速度和位置
self.speed += gravity
self.y += self.speed
# 更新图片
self.image_index += 1
if self.image_index >= len(bird_images):
self.image_index = 0
self.image = bird_images[self.image_index]
# 更新矩形
self.rect.x = self.x
self.rect.y = self.y
def draw(self):
screen.blit(self.image, self.rect)
class Pipe:
def __init__(self, x):
self.x = x
self.height = random.randint(100, 300)
self.top_y = self.height - pipe_image.get_height()
self.bottom_y = self.height + pipe_gap
self.rect_top = pygame.Rect(self.x, self.top_y, pipe_image.get_width(), pipe_image.get_height())
self.rect_bottom = pygame.Rect(self.x, self.bottom_y, pipe_image.get_width(), pipe_image.get_height())
def update(self):
self.x += pipe_speed
self.rect_top.x = self.x
self.rect_bottom.x = self.x
def draw(self):
screen.blit(pipe_image, (self.x, self.top_y))
screen.blit(pygame.transform.flip(pipe_image, False, True), (self.x, self.bottom_y))
class Base:
def __init__(self):
self.x = 0
self.y = screen_height - base_image.get_height()
self.rect = pygame.Rect(self.x, self.y, base_image.get_width(), base_image.get_height())
def update(self):
self.x += base_speed
if self.x <= -base_image.get_width():
self.x += base_image.get_width()
self.rect.x = self.x
def draw(self):
screen.blit(base_image, (self.x, self.y))
# 创建对象
bird = Bird()
pipes = [Pipe(screen_width + i * 200) for i in range(3)]
base = Base()
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():