本文实例为大家分享了python实现飞扬的小鸟游戏的具体代码,供大家参考,具体内容如下
flappygamemain.py
# - * -编码:utf - 8 - * 从__future__进口unicode_literals ”“” 创建: record.append ([int (line.strip () .split (", ") [0]), line.strip () .split (", ") [1]]) 行=f.readline () f.close () record.append([得分,str (datetime.datetime.now ()))) record.sort(关键=operator.itemgetter(0),反向=True) 打印记录 而len(记录)在10: record.pop () file_writer=开放(“记录。txt”,“w”) file_writer.writelines(“时间”+”、“+“年级”+“\ n”) 因为我在范围(len(记录): file_writer.writelines (str(记录[我][0])+”、“+ str(记录[我][1])+“\ n”) 如果int(记录[我][0])==int(分数): 指数=我 file_writer.close () 如果指数在3: 指数=3 返回记录[0][0],指数 而不是运行: clock.tick (60) screen.fill (0) 屏幕上。位块传输(get_start_image (0, 0)) pygame.display.update () x, y=pygame.mouse.get_pos () 在pygame.event.get事件(): 如果事件。类型==pygame.QUIT: pygame.quit () 退出() elif事件。类型==pygame.MOUSEBUTTONDOWN: 如果y<380年和300年y> x> 180和x & lt; 210: 运行=True 而真正的: clock.tick (60) pilar_frequency +=1 #生成柱子 如果pilar_frequency % 100==0: pilar_pos=[384年,随机的。randint (130、250)] new_pilar=皮拉尔(pilar_image_up pilar_image_down pilar_pos) pilar_set.add (new_pilar) 如果pilar_frequency祝辞=1000: pilar_frequency=0 国旗=(1)*旗帜 #移动柱子 皮拉尔在pilar_set: pilar.move () 如果collide_circle(皮拉尔,mybird): #碰撞检测代码 mybird.is_hit=True 皮拉尔在pilar_set: pilar.stop () 如果pilar.pilar1_rect。正确的& lt; 0: pilar_set.remove(皮拉尔) #小鸟降落 mybird.SelfMoveDown () #绘制背景 screen.fill (0) 如果标志==1: 屏幕上。位块传输(background_day (0, 0)) 其他: 屏幕上。位块传输(background_night (0, 0)) #绘制柱子 皮拉尔在pilar_set: screen.blit (pilar.pilar1_image pilar.pilar1_rect) screen.blit (pilar.pilar2_image pilar.pilar2_rect) #绘制地面 screen.blit(地面、(0384)) #绘制玩家小鸟 bird_frequency +=1 如果不是mybird。is_hit: #未发生碰撞 +=10分 screen.blit (mybird.image mybird.image_index, mybird.rect) mybird。image_index=bird_frequency % 3 其他:#发生碰撞 运行=False mybird.SelfDiedDown () mybird.rect screen.blit (mybird.image [0]) has_log=False 虽然不是跑步和mybird。is_downtoground: #画面切换到结束界面 screen.blit (gameover_image, (64, 30)) screen.blit (medal_blackball_image (42100)) screen.blit (restart_image (122270)) 如果不是has_log: bestscore指数=get_history_record(得分/100)#获取历史记录情况 打印分/100 打印bestscore、索引 has_log=True screen.blit (medal_images(指数),(75160)#要读取之前保存的信息, x, y=pygame.mouse.get_pos () #目前得分 score_font=pygame.font。字体(没有,36) score_text=score_font.render (str(得分/100),真的,(255255255)) text_rect=score_text.get_rect () text_rect。midtop=(290、145) 屏幕上。位块传输(score_text text_rect) #历史最佳得分 score_font=pygame.font。字体(没有,36) score_text=score_font.render (str (bestscore),真的,(255255255)) text_rect=score_text.get_rect () text_rect。midtop=[290200] 屏幕上。位块传输(score_text text_rect) 在pygame.event.get事件(): 如果事件。类型==pygame.QUIT: pygame.quit () 退出() elif事件。类型==pygame.MOUSEBUTTONDOWN: 如果y<360年和280年y> x> 150和x & lt; 240: #重新开始 pilar_set=pygame.sprite.Group () mybird=鸟(bird_images bird_pos) 分数=0 运行=True pygame.display.update () #绘制得的分 score_font=pygame.font。字体(没有,36) score_text=score_font.render (str(得分/100),真的,(255255255)) text_rect=score_text.get_rect () text_rect。midtop=(185, 30) 屏幕上。位块传输(score_text text_rect) #屏幕更新 pygame.display.update () #绘制按键执行代码 key_pressed=pygame.key.get_pressed () 如果不是mybird.is_hit: 如果key_pressed [K_w]或key_pressed [K_UP]: mybird.moveUp () 如果key_pressed [K_s]或key_pressed [K_DOWN]: mybird.moveDown () 在pygame.event.get事件(): 如果事件。类型==pygame.QUIT: pygame.quit () 退出()python实现飞扬的鸟小游戏