|
这篇文章主要介绍了python游戏编程入门游戏代码,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。哈喽铁子们表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势不对?比方说,可以通过打游戏来学编程!今天给大家分享100个Python小游戏,一定要收藏!1、简易飞机大战飞机大战相信大家都玩过吧,非常简单有意思的游戏,咱们通过Python给它复刻出来,回味童年。素材文件全部源码:importsysimportcfgimportpygamefrommodulesimport*'''游戏界面'''defGamingInterface(num_player,screen):#初始化pygame.mixer.music.load(cfg.SOUNDPATHS['CoolSpaceMusic'])pygame.mixer.music.set_volume(0.4)pygame.mixer.music.play(-1)explosion_sound=pygame.mixer.Sound(cfg.SOUNDPATHS['boom'])fire_sound=pygame.mixer.Sound(cfg.SOUNDPATHS['shot'])font=pygame.font.Font(cfg.FONTPATH,20)#游戏背景图bg_imgs=[cfg.IMAGEPATHS['bg_big'],cfg.IMAGEPATHS['seamless_space'],cfg.IMAGEPATHS['space3']]bg_move_dis=0bg_1=pygame.image.load(bg_imgs[0]).convert()bg_2=pygame.image.load(bg_imgs[1]).convert()bg_3=pygame.image.load(bg_imgs[2]).convert()#玩家,子弹和小行星精灵组player_group=pygame.sprite.Group()bullet_group=pygame.sprite.Group()asteroid_group=pygame.sprite.Group()#产生小行星的时间间隔asteroid_ticks=90foriinrange(num_player):player_group.add(Ship(i+1,cfg))clock=pygame.time.Clock()#分数score_1,score_2=0,0#游戏主循环whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()#--玩家一:↑↓←→控制,j射击;玩家二:wsad控制,空格射击pressed_keys=pygame.key.get_pressed()foridx,playerinenumerate(player_group):direction=Noneifidx==0:ifpressed_keys[pygame.K_UP]:direction='up'elifpressed_keys[pygame.K_DOWN]:direction='down'elifpressed_keys[pygame.K_LEFT]:direction='left'elifpressed_keys[pygame.K_RIGHT]:direction='right'ifdirection:player.move(direction)ifpressed_keys[pygame.K_j]:ifplayer.cooling_time==0:fire_sound.play()bullet_group.add(player.shot())player.cooling_time=20elifidx==1:ifpressed_keys[pygame.K_w]:direction='up'elifpressed_keys[pygame.K_s]:direction='down'elifpressed_keys[pygame.K_a]:direction='left'elifpressed_keys[pygame.K_d]:direction='right'ifdirection:player.move(direction)ifpressed_keys[pygame.K_SPACE]:ifplayer.cooling_time==0:fire_sound.play()bullet_group.add(player.shot())player.cooling_time=20ifplayer.cooling_time>0:player.cooling_time-=1if(score_1+score_2)0:ifplayer.explode_step>3:player_group.remove(player)iflen(player_group)==0:returnelse:player.explode(screen)else:player.draw(screen)#--画子弹forbulletinbullet_group:bullet.move()ifpygame.sprite.spritecollide(bullet,asteroid_group,True,None):bullet_group.remove(bullet)ifbullet.player_idx==1:score_1+=1else:score_2+=1else:bullet.draw(screen)#--画小行星forasteroidinasteroid_group:asteroid.move()asteroid.rotate()asteroid.draw(screen)#--显示分数score_1_text='玩家一得分:%s'%score_1score_2_text='玩家二得分:%s'%score_2text_1=font.render(score_1_text,True,(0,0,255))text_2=font.render(score_2_text,True,(255,0,0))screen.blit(text_1,(2,5))screen.blit(text_2,(2,35))#--屏幕刷新pygame.display.update()clock.tick(60)'''主函数'''defmain():pygame.init()pygame.font.init()pygame.mixer.init()screen=pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('简易版——飞机大战')num_player=StartInterface(screen,cfg)ifnum_player==1:whileTrue:GamingInterface(num_player=1,screen=screen)EndInterface(screen,cfg)else:whileTrue:GamingInterface(num_player=2,screen=screen)EndInterface(screen,cfg)'''run'''if__name__=='__main__':main()2、宝石消消乐都是一些大家耳熟能详的小游戏素材全部源码: importosimportsysimportcfgimportpygamefrommodulesimport*'''游戏主程序'''defmain():pygame.init()screen=pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('开心消消乐🎃')#加载背景音乐pygame.mixer.init()pygame.mixer.music.load(os.path.join(cfg.ROOTDIR,"resources/audios/bg.mp3"))pygame.mixer.music.set_volume(0.6)pygame.mixer.music.play(-1)#加载音效sounds={}sounds['mismatch']=pygame.mixer.Sound(os.path.join(cfg.ROOTDIR,'resources/audios/badswap.wav'))sounds['match']=[]foriinrange(6):sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR,'resources/audios/match%s.wav'%i)))#加载字体font=pygame.font.Font(os.path.join(cfg.ROOTDIR,'resources/font/font.TTF'),25)#图片加载gem_imgs=[]foriinrange(1,8):gem_imgs.append(os.path.join(cfg.ROOTDIR,'resources/images/gem%s.png'%i))#主循环game=gemGame(screen,sounds,font,gem_imgs,cfg)whileTrue:score=game.start()flag=False#一轮游戏结束后玩家选择重玩或者退出whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUITor(event.type==pygame.KEYUPandevent.key==pygame.K_ESCAPE):pygame.quit()sys.exit()elifevent.type==pygame.KEYUPandevent.key==pygame.K_r:flag=Trueifflag:breakscreen.fill((135,206,235))text0='Finalscore:%s'%scoretext1='Presstorestartthegame.'text2='Presstoquitthegame.'y=150foridx,textinenumerate([text0,text1,text2]):text_render=font.render(text,1,(85,65,0))rect=text_render.get_rect()ifidx==0:rect.left,rect.top=(212,y)elifidx==1:rect.left,rect.top=(122.5,y)else:rect.left,rect.top=(126.5,y)y+=100screen.blit(text_render,rect)pygame.display.update()game.reset()'''run'''if__name__=='__main__':main()还是直接上目录吧~ 免子八音符外星人入侵推箱子俄罗斯方块飞机大战接金币月饼塔防小恐龙像素鸟坦克大战滑雪拼图走迷宫炸弹人打砖块乒乓球24点游戏宝石版本消消乐吃豆豆斗地主大富翁21点游戏我的世界魂斗罗记忆愤怒小鸟扫雷贪吃蛇2048游戏五子棋打地康2048下一百层切水果数独游戏空洞机甲打地鼠PythonAngryBirdschess象棋彩色版本飞机大战嗷大喵快跑代码Tom猫SokobanPythonPlantsVsZombiesMine-game-mastergluttonous-masterFlappyBirdEatBeansAlD1904-game2048-master2048--master21点-python-practice-master贪吃蛇AI中国急棋冒险岛黄金矿工原版飞机大战2飞船大战蔡徐坤债怒的小鸟拼图游戏XiaoXiaoLe超级玛丽幸运大转盘智慧校园考试系统植物大战黑白飞机大战一只单身狗小狗快跑象棋五子棋贪吃蛇忍者奔跑愤怒的小鸟天天跑酷五子棋AI超级马里奥连连看植物大战僵尸走四棋儿末世文字版本游戏FlyFight宇宙激战泡泡躲障碍打飞鸟齿据气球吃水果吃掉一切射击游戏无敌狙击手消灭病毒小游戏扫雷小游戏病毒小游戏01简易版推箱子简易版塔防游戏彩色版贪吃蛇躲避小球鱿鱼游戏猜教字游戏升级版界面自动玩俄罗斯方块 好了,大家快去拿源码熟悉一下,下周就可以开始愉快的摸鱼啦!文章知识点与官方知识档案匹配,可进一步学习相关知识Python入门技能树人工智能机器学习工具包Scikit-learn444032人正在系统学习中,哈喽铁子们表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势不对?比方说,可以通过打游戏来学编程!今天给大家分享100个Python小游戏,一定要收藏!1、简易飞机大战飞机大战相信大家都玩过吧,非常简单有意思的游戏,咱们通过Python给它复刻出来,回味童年。素材文件全部源码:importsysimportcfgimportpygamefrommodulesimport*'''游戏界面'''defGamingInterface(num_player,screen):#初始化pygame.mixer.music.load(cfg.SOUNDPATHS['CoolSpaceMusic'])pygame.mixer.music.set_volume(0.4)pygame.mixer.music.play(-1)explosion_sound=pygame.mixer.Sound(cfg.SOUNDPATHS['boom'])fire_sound=pygame.mixer.Sound(cfg.SOUNDPATHS['shot'])font=pygame.font.Font(cfg.FONTPATH,20)#游戏背景图bg_imgs=[cfg.IMAGEPATHS['bg_big'],cfg.IMAGEPATHS['seamless_space'],cfg.IMAGEPATHS['space3']]bg_move_dis=0bg_1=pygame.image.load(bg_imgs[0]).convert()bg_2=pygame.image.load(bg_imgs[1]).convert()bg_3=pygame.image.load(bg_imgs[2]).convert()#玩家,子弹和小行星精灵组player_group=pygame.sprite.Group()bullet_group=pygame.sprite.Group()asteroid_group=pygame.sprite.Group()#产生小行星的时间间隔asteroid_ticks=90foriinrange(num_player):player_group.add(Ship(i+1,cfg))clock=pygame.time.Clock()#分数score_1,score_2=0,0#游戏主循环whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()#--玩家一:↑↓←→控制,j射击;玩家二:wsad控制,空格射击pressed_keys=pygame.key.get_pressed()foridx,playerinenumerate(player_group):direction=Noneifidx==0:ifpressed_keys[pygame.K_UP]:direction='up'elifpressed_keys[pygame.K_DOWN]:direction='down'elifpressed_keys[pygame.K_LEFT]:direction='left'elifpressed_keys[pygame.K_RIGHT]:direction='right'ifdirection:player.move(direction)ifpressed_keys[pygame.K_j]:ifplayer.cooling_time==0:fire_sound.play()bullet_group.add(player.shot())player.cooling_time=20elifidx==1:ifpressed_keys[pygame.K_w]:direction='up'elifpressed_keys[pygame.K_s]:direction='down'elifpressed_keys[pygame.K_a]:direction='left'elifpressed_keys[pygame.K_d]:direction='right'ifdirection:player.move(direction)ifpressed_keys[pygame.K_SPACE]:ifplayer.cooling_time==0:fire_sound.play()bullet_group.add(player.shot())player.cooling_time=20ifplayer.cooling_time>0:player.cooling_time-=1if(score_1+score_2)0:ifplayer.explode_step>3:player_group.remove(player)iflen(player_group)==0:returnelse:player.explode(screen)else:player.draw(screen)#--画子弹forbulletinbullet_group:bullet.move()ifpygame.sprite.spritecollide(bullet,asteroid_group,True,None):bullet_group.remove(bullet)ifbullet.player_idx==1:score_1+=1else:score_2+=1else:bullet.draw(screen)#--画小行星forasteroidinasteroid_group:asteroid.move()asteroid.rotate()asteroid.draw(screen)#--显示分数score_1_text='玩家一得分:%s'%score_1score_2_text='玩家二得分:%s'%score_2text_1=font.render(score_1_text,True,(0,0,255))text_2=font.render(score_2_text,True,(255,0,0))screen.blit(text_1,(2,5))screen.blit(text_2,(2,35))#--屏幕刷新pygame.display.update()clock.tick(60)'''主函数'''defmain():pygame.init()pygame.font.init()pygame.mixer.init()screen=pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('简易版——飞机大战')num_player=StartInterface(screen,cfg)ifnum_player==1:whileTrue:GamingInterface(num_player=1,screen=screen)EndInterface(screen,cfg)else:whileTrue:GamingInterface(num_player=2,screen=screen)EndInterface(screen,cfg)'''run'''if__name__=='__main__':main()2、宝石消消乐都是一些大家耳熟能详的小游戏素材全部源码: importosimportsysimportcfgimportpygamefrommodulesimport*'''游戏主程序'''defmain():pygame.init()screen=pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('开心消消乐🎃')#加载背景音乐pygame.mixer.init()pygame.mixer.music.load(os.path.join(cfg.ROOTDIR,"resources/audios/bg.mp3"))pygame.mixer.music.set_volume(0.6)pygame.mixer.music.play(-1)#加载音效sounds={}sounds['mismatch']=pygame.mixer.Sound(os.path.join(cfg.ROOTDIR,'resources/audios/badswap.wav'))sounds['match']=[]foriinrange(6):sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR,'resources/audios/match%s.wav'%i)))#加载字体font=pygame.font.Font(os.path.join(cfg.ROOTDIR,'resources/font/font.TTF'),25)#图片加载gem_imgs=[]foriinrange(1,8):gem_imgs.append(os.path.join(cfg.ROOTDIR,'resources/images/gem%s.png'%i))#主循环game=gemGame(screen,sounds,font,gem_imgs,cfg)whileTrue:score=game.start()flag=False#一轮游戏结束后玩家选择重玩或者退出whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUITor(event.type==pygame.KEYUPandevent.key==pygame.K_ESCAPE):pygame.quit()sys.exit()elifevent.type==pygame.KEYUPandevent.key==pygame.K_r:flag=Trueifflag:breakscreen.fill((135,206,235))text0='Finalscore:%s'%scoretext1='Presstorestartthegame.'text2='Presstoquitthegame.'y=150foridx,textinenumerate([text0,text1,text2]):text_render=font.render(text,1,(85,65,0))rect=text_render.get_rect()ifidx==0:rect.left,rect.top=(212,y)elifidx==1:rect.left,rect.top=(122.5,y)else:rect.left,rect.top=(126.5,y)y+=100screen.blit(text_render,rect)pygame.display.update()game.reset()'''run'''if__name__=='__main__':main()还是直接上目录吧~ 免子八音符外星人入侵推箱子俄罗斯方块飞机大战接金币月饼塔防小恐龙像素鸟坦克大战滑雪拼图走迷宫炸弹人打砖块乒乓球24点游戏宝石版本消消乐吃豆豆斗地主大富翁21点游戏我的世界魂斗罗记忆愤怒小鸟扫雷贪吃蛇2048游戏五子棋打地康2048下一百层切水果数独游戏空洞机甲打地鼠PythonAngryBirdschess象棋彩色版本飞机大战嗷大喵快跑代码Tom猫SokobanPythonPlantsVsZombiesMine-game-mastergluttonous-masterFlappyBirdEatBeansAlD1904-game2048-master2048--master21点-python-practice-master贪吃蛇AI中国急棋冒险岛黄金矿工原版飞机大战2飞船大战蔡徐坤债怒的小鸟拼图游戏XiaoXiaoLe超级玛丽幸运大转盘智慧校园考试系统植物大战黑白飞机大战一只单身狗小狗快跑象棋五子棋贪吃蛇忍者奔跑愤怒的小鸟天天跑酷五子棋AI超级马里奥连连看植物大战僵尸走四棋儿末世文字版本游戏FlyFight宇宙激战泡泡躲障碍打飞鸟齿据气球吃水果吃掉一切射击游戏无敌狙击手消灭病毒小游戏扫雷小游戏病毒小游戏01简易版推箱子简易版塔防游戏彩色版贪吃蛇躲避小球鱿鱼游戏猜教字游戏升级版界面自动玩俄罗斯方块 好了,大家快去拿源码熟悉一下,下周就可以开始愉快的摸鱼啦!文章知识点与官方知识档案匹配,可进一步学习相关知识Python入门技能树人工智能机器学习工具包Scikit-learn444032人正在系统学习中
|
|