|
经常听到有朋友说,学习编程是一件非常枯燥无味的事情。其实,大家有没有认真想过,可能是我们的学习方法不对?比方说,你有没有想过,可以通过打游戏来学编程?今天我想跟大家分享几个Python小游戏,教你如何通过边打游戏边学编程!今天给大家带来15个Py小游戏,一定要收藏!飞扬的小鸟Python简易时钟Python中国象棋Python吃豆豆小游戏Python幸运大转盘Python简易植物大战僵尸Python2048小游戏Python俄罗斯方块Python烟花Python贪吃蛇Python数字游戏拼图游戏滑雪小游戏数独游戏飞机大战1.飞扬的小鸟①游戏介绍:《flappybird》是一款由来自越南的独立游戏开发者DongNguyen所开发的作品,游戏于2013年5月24日上线,并在2014年2月突然暴红。游戏规则:游戏玩法非常简单,通过点击屏幕,使小鸟一直飞并穿过水管的空隙。虽然玩法简单,但是却具有一定的难度,因为要一直控制小鸟飞在适合的高度,以避开障碍。这篇文章呢,就来分析这个游戏的原理,以及用python做一个简易版的FlappyBird。②源码分享:#itbaizhanimportpygameimportsysimportrandomclassBird(object):"""定义一个鸟类"""def__init__(self):"""定义初始化方法"""self.birdRect=pygame.Rect(65,50,50,50)#鸟的矩形#定义鸟的3种状态列表self.birdStatus=[pygame.image.load("images/0.png"),pygame.image.load("images/2.png"),pygame.image.load("images/dead.png")]self.status=0#默认飞行状态self.birdX=120#鸟所在X轴坐标,即是向右飞行的速度self.birdY=350#鸟所在Y轴坐标,即上下飞行高度self.jump=False#默认情况小鸟自动降落self.jumpSpeed=10#跳跃高度self.gravity=5#重力self.dead=False#默认小鸟生命状态为活着defbirdUpdate(self):ifself.jump:#小鸟跳跃self.jumpSpeed-=1#速度递减,上升越来越慢self.birdY-=self.jumpSpeed#鸟Y轴坐标减小,小鸟上升else:#小鸟坠落self.gravity+=0.1#重力递增,下降越来越快self.birdY+=self.gravity#鸟Y轴坐标增加,小鸟下降self.birdRect[1]=self.birdY#更改Y轴位置classPipeline(object):"""定义一个管道类"""def__init__(self):"""定义初始化方法"""self.wallx=400#管道所在X轴坐标self.pineUp=pygame.image.load("images/top.png")self.pineDown=pygame.image.load("images/bottom.png")defupdatePipeline(self):""""管道移动方法"""self.wallx-=5#管道X轴坐标递减,即管道向左移动#当管道运行到一定位置,即小鸟飞越管道,分数加1,并且重置管道ifself.wallxMainGame.Start_X-MainGame.Line_Span/2andmouse_xMainGame.Start_Y-MainGame.Line_Span/2andmouse_y=5andabs(click_mod_y-MainGame.Line_Span/2)>=5:#print("有效点:x="+str(click_x)+"y="+str(click_y))#有效点击点self.PutdownPieces(MainGame.player1Color,click_x,click_y)else:print("out")ifMainGame.button_go.is_click():#self.restart()print("button_goclick")else:print("button_goclickout")defPutdownPieces(self,t,x,y):selectfilter=list(filter(lambdacm:cm.x==xandcm.y==yandcm.player==MainGame.player1Color,MainGame.piecesList))iflen(selectfilter):MainGame.piecesSelected=selectfilter[0]returnifMainGame.piecesSelected:#print("1111")arr=pieces.listPiecestoArr(MainGame.piecesList)ifMainGame.piecesSelected.canmove(arr,x,y):self.PiecesMove(MainGame.piecesSelected,x,y)MainGame.Putdownflag=MainGame.player2Colorelse:fi=filter(lambdap:p.x==xandp.y==y,MainGame.piecesList)listfi=list(fi)iflen(listfi)!=0:MainGame.piecesSelected=listfi[0]defPiecesMove(self,pieces,x,y):foriteminMainGame.piecesList:ifitem.x==xanditem.y==y:MainGame.piecesList.remove(item)pieces.x=xpieces.y=yprint("moveto"+str(x)+""+str(y))returnTruedefComputerplay(self):ifMainGame.Putdownflag==MainGame.player2Color:print("轮到电脑了")computermove=computer.getPlayInfo(MainGame.piecesList)#ifcomputer==None:#returnpiecemove=NoneforiteminMainGame.piecesList:ifitem.x==computermove[0]anditem.y==computermove[1]:piecemove=itemself.PiecesMove(piecemove,computermove[2],computermove[3])MainGame.Putdownflag=MainGame.player1Color#判断游戏胜利defVictoryOrDefeat(self):txt=""result=[MainGame.player1Color,MainGame.player2Color]foriteminMainGame.piecesList:iftype(item)==pieces.King:ifitem.player==MainGame.player1Color:result.remove(MainGame.player1Color)ifitem.player==MainGame.player2Color:result.remove(MainGame.player2Color)iflen(result)==0:returnifresult[0]==MainGame.player1Color:txt="失败!"else:txt="胜利!"MainGame.window.blit(self.getTextSuface("%s"%txt),(constants.SCREEN_WIDTH-100,200))MainGame.Putdownflag=constants.overColordefgetTextSuface(self,text):pygame.font.init()#print(pygame.font.get_fonts())font=pygame.font.SysFont('kaiti',18)txt=font.render(text,True,constants.TEXT_COLOR)returntxtdefendGame(self):print("exit")exit()if__name__=='__main__':MainGame().start_game()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262274.Python吃豆豆小游戏源码分享(部分源码):'''itbaizhan'''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.Clock()SCORE=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.quit()ifevent.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:#幽灵随机运动()'''res=ghost.update(wall_sprites,None)whilenotres:ghost.changeSpeed(ghost.randomDirection())res=ghost.update(wall_sprites,None)'''#指定幽灵运动路径ifghost.tracks_loc[1]500:k=rewardFun()end(k)breaktick.tick(fps)pygame.display.flip()#刷新窗口defend(k):textFont=pygame.font.Font("./font.ttf",50)print("恭喜你,你抽中了"+k)textSurface=textFont.render("yourawardsis:%s"%k,True,(110,55,155))screen.fill((155,155,0))screen.blit(textSurface,(30,230))if__name__=='__main__':start()middle()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495由于文章篇幅有限,文档资料内容较多,需要这些文档的朋友,可以加小助手微信免费获取,【保证100%免费】,中国人不骗中国人。(扫码立即免费领取)其他实战案例光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
|
|