像素鸟(笨鸟先飞)
guide_y))SCREEN.blit(IMAGE["birds"][frames[idx]], FLOOR_Y))show_score(score)SCREEN.blit(bird.image, "pipe_group": pipe_group。
self.rotate)def go_die(self):"""判断失败后是否在地面之上"""if self.rect.y FLOOR_Y:self.rect.y += self.max_y_valself.rotate = -90self.image = self.images[self.frames[self.dix]]self.image = pygame.transform.rotate(self.image, "night"])]color = random.choice(["red", extension = os.path.splitext(image)path = os.path.join(IMAGE_PATH, self.rotate)else:self.dying = Falseclass Pipe(pygame.sprite.Sprite):"""水管类"""def __init__(self, (0, (免费下载) [188 次下载] import osimport randomimport pygameW, pipe_group):AUDIO["hit"].play()AUDIO["die"].play()bird.dying = Trueresult = {"bird": bird, extension = os.path.splitext(audio)path = os.path.join(AUDIO_PATH, H * 0.4)pipe_group = pygame.sprite.Group()distance = 150n_pairs = 4pipe_gap = 100for i in range(n_pairs):pipe_y = random.randint(int(H*0.3), True)]# 显示窗口menu_window()result = game_window()end_window(result)def menu_window():"""控制游戏菜单窗口函数"""floor_x = 0floor_gap = W - IMAGE["floor"].get_width()guide_x = (W - IMAGE["guide"].get_width()) / 2guide_y = (H - IMAGE["guide"].get_height()) / 2bird_x = W * 0.2bird_y = (H - IMAGE["birds"][0].get_width()) / 2 + 53bird_y_val = 1bird_y_range = [bird_y - 8, flap=False):"""更新小鸟飞行状态"""if flap:self.y_val = self.y_val_after_flapself.rotate = self.rotate_after_flapself.y_val = min(self.y_val + self.gravity, pipe_y)pipe_down = Pipe(W + i * distance, "score": score}return resultif bird.rect.left + first_pipe_up.x_val first_pipe_up.rect.centerx bird.rect.left:AUDIO["score"].play()score += 1SCREEN.blit(IMAGE["bgpic"], gameover_y))SCREEN.blit(bird.image, FLOOR_Y))SCREEN.blit(IMAGE["guide"], int(H * 0.75))new_pipe_up = Pipe(first_pipe_up.rect.x + n_pairs * distance, (floor_x。
False。
bird.rect)pygame.display.update()CLOCK.tick(FPS)def end_window(result):"""控制游戏结束窗口函数"""gameover_x = (W - IMAGE["gameover"].get_width()) / 2gameover_y = (FLOOR_Y - IMAGE["gameover"].get_height()) / 2bird = result["bird"]pipe_group = result["pipe_group"]while True:if bird.dying:bird.go_die()else:for event in pygame.event.get():if event.type == pygame.QUIT:quit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:quit()if event.key == pygame.K_SPACE:returnSCREEN.blit(IMAGE["bgpic"], "red-pipe"])]IMAGE["pipes"] = [pipe, image)IMAGE[name] = pygame.image.load(path)FLOOR_Y = H - IMAGE["floor"].get_height()AUDIO = {}for audio in os.listdir(AUDIO_PATH):name, pipe_y - pipe_gap, pygame.transform.flip(pipe。
512FPS = 30IMAGE_PATH = "./assets/sprites/"AUDIO_PATH = "./assets/audio/"pygame.init()pygame.mixer.init()SCREEN = pygame.display.set_mode((W, (0, (bird_x。
IMAGE[color+"-mid"], upwards=True):"""初始化"""pygame.sprite.Sprite.__init__(self) # 初始化精灵组if upwards:self.image = IMAGE["pipes"][0]self.rect = self.image.get_rect()self.rect.x = xself.rect.top = yelse:self.image = IMAGE["pipes"][1]self.rect = self.image.get_rect()self.rect.x = xself.rect.bottom = yself.x_val = -4def update(self):"""更新水管移动"""self.rect.x += self.x_valif __name__ == '__main__':main()[源代码打包下载] (免费下载) [188 次下载] , y):"""初始化小鸟信息"""self.dix = 0self.frames = [0] * 5 + [1] * 5 + [2] * 5 + [1] * 5self.images = IMAGE["birds"]self.image = self.images[self.frames[self.dix]]self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.y_val = -10self.max_y_val = 10self.gravity = 1self.rotate = 45self.max_rotate = -20self.rotate_val = -3self.y_val_after_flap = -10self.rotate_after_flap = 45self.dying = Falsedef update(self, False)pipe_group.add(pipe_up)pipe_group.add(pipe_down)while True:flap = Falsefor event in pygame.event.get():if event.type == pygame.QUIT:quit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:quit()if event.key == pygame.K_SPACE:AUDIO["flap"].play()flap = True # 向上飞floor_x -= 4if floor_x = floor_gap: # 不让地面移出屏幕floor_x = 0first_pipe_up = pipe_group.sprites()[0]first_pipe_down = pipe_group.sprites()[1]if first_pipe_up.rect.right 0:pipe_y = random.randint(int(H * 0.3)。
(floor_x, audio)AUDIO[name] = pygame.mixer.Sound(path)def main():"""游戏主函数"""# 播放开始音效AUDIO["start"].play()IMAGE["bgpic"] = IMAGE[random.choice(["day", H = 288, self.max_y_val)self.rect.y += self.y_valself.rotate = max(self.rotate + self.rotate_val, (gameover_x。
x, 0))pipe_group.draw(SCREEN)SCREEN.blit(IMAGE["floor"], int(H*0.7))pipe_up = Pipe(W + i * distance, IMAGE[color+"-down"]]pipe = IMAGE[random.choice(["green-pipe", pipe_y)new_pipe_down = Pipe(first_pipe_down.rect.x + n_pairs * distance, pipe_y - pipe_gap, bird_y))pygame.display.update()CLOCK.tick(FPS)def game_window():"""控制游戏进行函数"""AUDIO["flap"].play()score = 0floor_x = 0floor_gap = W - IMAGE["floor"].get_width()bird = Bird(W * 0.2。
y, (0, (x, False)pipe_group.add(new_pipe_up)pipe_group.add(new_pipe_down)first_pipe_up.kill()first_pipe_down.kill()# 更新角色状态bird.update(flap) # 小鸟飞行动作切换pipe_group.update() # 水管移动# 碰撞检测if bird.rect.y FLOOR_Y or bird.rect.y 0 or \pygame.sprite.spritecollideany(bird, FLOOR_Y))SCREEN.blit(IMAGE["gameover"], H))pygame.display.set_caption("Flappy Bird")CLOCK = pygame.time.Clock()IMAGE = {}for image in os.listdir(IMAGE_PATH):name, y))x += wclass Bird:"""小鸟类"""def __init__(self, x, bird.rect)show_score(result["score"])pygame.display.update()CLOCK.tick(FPS)def show_score(score):"""显示得分"""score_str = str(score)n = len(score_str)w = IMAGE["0"].get_width() * 1.1x = (W - n * w) / 2y = H * 0.1for num in score_str:SCREEN.blit(IMAGE[num], "blue", self.max_rotate)self.dix += 1self.dix %= len(self.frames)self.image = self.images[self.frames[self.dix]]self.image = pygame.transform.rotate(self.image, 0))SCREEN.blit(IMAGE["floor"], bird_y + 8]idx = 0repeat = 5frames = [0] * repeat + [1] * repeat + [2] * repeat + [1] * repeatwhile True:for event in pygame.event.get():if event.type == pygame.QUIT:quit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:quit()if event.key == pygame.K_SPACE:returnfloor_x -= 4if floor_x = floor_gap:floor_x = 0bird_y += bird_y_valif bird_y bird_y_range[0] or bird_y bird_y_range[1]: # 不让小鸟移出规定范围bird_y_val *= -1idx += 1idx %= len(frames)SCREEN.blit(IMAGE["bgpic"], (0。
(guide_x, "yellow"])IMAGE["birds"] = [IMAGE[color+"-up"], 0))pipe_group.draw(SCREEN)SCREEN.blit(IMAGE["floor"],。
评论列表