早爆娱闻 玩出花儿了!一文教你用 Python 制作吃豆人游戏!| 附代,Python( 三 )
3、游戏创建:
在通过关卡定义墙等位置后以及精灵自身属性怪物运动、食物定义等后 , 通过调用已经创建好的类达到搭建游戏的目的 。
具体如下可见:
importosimportsysimportpygameimportLevels'''定义一些必要的参数'''BLACK=(0,0,0)WHITE=(255,255,255)BLUE=(0,0,255)GREEN=(0,255,0)RED=(255,0,0)YELLOW=(255,255,0)PURPLE=(255,0,255)SKYBLUE=(0,191,255)BGMPATH=os.path.join(os.getcwd,'resources/sounds/bg.mp3')ICONPATH=os.path.join(os.getcwd,'resources/images/icon.png')FONTPATH=os.path.join(os.getcwd,'resources/font/ALGER.TTF')HEROPATH=os.path.join(os.getcwd,'resources/images/pacman.png')BlinkyPATH=os.path.join(os.getcwd,'resources/images/Blinky.png')ClydePATH=os.path.join(os.getcwd,'resources/images/Clyde.png')InkyPATH=os.path.join(os.getcwd,'resources/images/Inky.png')PinkyPATH=os.path.join(os.getcwd,'resources/images/Pinky.png')'''开始某一关游戏'''defstartLevelGame(level,screen,font):clock=pygame.time.ClockSCORE=0wall_sprites=level.setupWalls(SKYBLUE)gate_sprites=level.setupGate(WHITE)hero_sprites,ghost_sprites=level.setupPlayers(HEROPATH,[BlinkyPATH,ClydePATH,InkyPATH,PinkyPATH])food_sprites=level.setupFood(YELLOW,WHITE)is_clearance=FalsewhileTrue:foreventinpygame.event.get:ifevent.type==pygame.QUIT:sys.exit(-1)pygame.quitifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_LEFT:forheroinhero_sprites:hero.changeSpeed([-1,0])hero.is_move=Trueelifevent.key==pygame.K_RIGHT:forheroinhero_sprites:hero.changeSpeed([1,0])hero.is_move=Trueelifevent.key==pygame.K_UP:forheroinhero_sprites:hero.changeSpeed([0,-1])hero.is_move=Trueelifevent.key==pygame.K_DOWN:forheroinhero_sprites:hero.changeSpeed([0,1])hero.is_move=Trueifevent.type==pygame.KEYUP:if(event.key==pygame.K_LEFT)or(event.key==pygame.K_RIGHT)or(event.key==pygame.K_UP)or(event.key==pygame.K_DOWN):hero.is_move=Falsescreen.fill(BLACK)forheroinhero_sprites:hero.update(wall_sprites,gate_sprites)hero_sprites.draw(screen)forheroinhero_sprites:food_eaten=pygame.sprite.spritecollide(hero,food_sprites,True)SCORE+=len(food_eaten)wall_sprites.draw(screen)gate_sprites.draw(screen)food_sprites.draw(screen)forghostinghost_sprites:#幽灵随机运动(效果不好且有BUG)'''res=ghost.update(wall_sprites,None)whilenotres:ghost.changeSpeed(ghost.randomDirection)res=ghost.update(wall_sprites,None)'''#指定幽灵运动路径ifghost.tracks_loc[1]<ghost.tracks[ghost.tracks_loc[0]][2]:ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0:2])ghost.tracks_loc[1]+=1else:ifghost.tracks_loc[0]<len(ghost.tracks)-1:ghost.tracks_loc[0]+=1elifghost.role_name=='Clyde':ghost.tracks_loc[0]=2else:ghost.tracks_loc[0]=0ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0:2])ghost.tracks_loc[1]=0ifghost.tracks_loc[1]<ghost.tracks[ghost.tracks_loc[0]][2]:ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0:2])else:ifghost.tracks_loc[0]<len(ghost.tracks)-1:loc0=ghost.tracks_loc[0]+1elifghost.role_name=='Clyde':loc0=2else:loc0=0ghost.changeSpeed(ghost.tracks[loc0][0:2])ghost.update(wall_sprites,None)ghost_sprites.draw(screen)score_text=font.render(''Score:%s''%SCORE,True,RED)screen.blit(score_text,[10,10])iflen(food_sprites)==0:is_clearance=Truebreakifpygame.sprite.groupcollide(hero_sprites,ghost_sprites,False,False):is_clearance=Falsebreakpygame.display.flipclock.tick(10)returnis_clearance'''显示文字'''defshowText(screen,font,is_clearance,flag=False):clock=pygame.time.Clockmsg='GameOver!'ifnotis_clearanceelse'Congratulations,youwon!'positions=[[235,233],[65,303],[170,333]]ifnotis_clearanceelse[[145,233],[65,303],[170,333]]surface=pygame.Surface((400,200))surface.set_alpha(10)surface.fill((128,128,128))screen.blit(surface,(100,200))texts=[font.render(msg,True,WHITE),font.render('PressENTERtocontinueorplayagain.',True,WHITE),font.render('PressESCAPEtoquit.',True,WHITE)]whileTrue:foreventinpygame.event.get:ifevent.type==pygame.QUIT:sys.exitpygame.quitifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_RETURN:ifis_clearance:ifnotflag:returnelse:main(initialize)else:main(initialize)elifevent.key==pygame.K_ESCAPE:sys.exitpygame.quitforidx,(text,position)inenumerate(zip(texts,positions)):screen.blit(text,position)pygame.display.flipclock.tick(10)'''初始化'''definitialize:pygame.initicon_image=pygame.image.load(ICONPATH)pygame.display.set_icon(icon_image)screen=pygame.display.set_mode([606,606])pygame.display.set_caption('吃豆人')returnscreen'''主函数'''defmain(screen):pygame.mixer.initpygame.mixer.music.load(BGMPATH)pygame.mixer.music.play(-1,0.0)pygame.font.initfont_small=pygame.font.Font(FONTPATH,18)font_big=pygame.font.Font(FONTPATH,24)fornum_levelinrange(1,Levels.NUMLEVELS+1):ifnum_level==1:level=Levels.Level1is_clearance=startLevelGame(level,screen,font_small)ifnum_level==Levels.NUMLEVELS:showText(screen,font_big,is_clearance,True)else:showText(screen,font_big,is_clearance)
- “龙吟”也就算了,散布“恐怖假信息”玩出格了
- 秀水镇|“龙吟”也就算了,散布“恐怖假信息”玩出格了
- 早爆娱闻 17分钟充满4000mAh,网友:就这?,小米的100W快充或将量产
- 早爆娱闻 国产“芯”即将到来,成功打破技术垄断:国产22nm光刻机研发成功
- 小鸡游戏官方|玩个游戏,玩出“帕金森”老人病?
- 小匪娱闻|出手阔绰!TVB“御用宫女”36岁生日送自己绝版手表,价值超70万
- 早爆娱闻|你说了算,做汽车服务加盟连锁还是自己干?看完以后
- 618 Battle名场面,好爸爸居然以一波"捣蛋"玩出圈了!
- 娱闻坊|虞书欣疑似参加《中餐厅4》,被老板推掉,粉丝气愤
- 幸福一箩筐|去年被吐槽全是竖排镜头设计,小米今年火力全开,后盖设计玩出花