irpas技术客

Python游戏编程_植物大战讲僵尸_贪吃蛇_python风控模型_python游戏编程

irpas 4596

曾经我也沉迷游戏,连续玩十几个小时竞技游戏可以不吃饭。游戏只是别人写好程序,与其沉迷于他人设计好程序,为何不自己设计游戏,至少学会编程后,我们的月收入上万不是问题。于是便有了录制这部[pygame菜鸟游戏编程教程]动力。不要沉迷于游戏,我们人生还有许多事情要做,旅游,找到另一个伴侣,成立一个家庭,照顾父母。。。。

编程正在逐步改变世界,程序员不是搬砖的。但传统计算教育程面临枯燥乏味课程,让学生感到乏味。 兴趣是学习最好老师!此课程目的是激发大家对编程兴趣,给广大Python入门初学者带来无穷乐趣。 游戏涉及童年玩过经典小游戏,包括贪吃蛇,消消乐,俄罗斯方块,植物大战僵尸,扫雷等等。视频的参考资料可下载脚本。脚本已经编译好,可直接运行! Have Fun!

欢迎各位同学学习《Python菜鸟快乐游戏编程_pygame》

Python菜鸟快乐游戏编程

下图展示视频中几个经典童年游戏,植物大战僵尸

《植物大战僵尸》是由PopCap Games开发的一款益智策略类单机游戏,于2009年5月5日发售。玩家通过武装多种植物切换不同的功能,快速有效地把僵尸阻挡在入侵的道路上。不同的敌人,不同的玩法构成五种不同的游戏模式,加之黑夜、浓雾以及泳池之类的障碍增加了游戏挑战性。

《植物大战僵尸》是一款极富策略性的小游戏。可怕的僵尸即将入侵,每种僵尸都有不同的特点,例如铁桶僵尸拥有极强的抗击打能力,矿工僵尸可以挖地道绕过种植在土壤表面的植物等。玩家防御僵尸的方式就是栽种植物。49种植物每种都有不同的功能,例如樱桃炸弹可以和周围一定范围内的所有僵尸同归于尽,而食人花可以吃掉最靠近自己的一只僵尸。玩家可以针对不同僵尸的弱点来合理地种植植物,这也是胜利的诀窍。游戏根据玩法不同分为五种游戏模式:冒险、生存、花瓶破碎者、小游戏、花园。加之黑夜、屋顶、浓雾以及泳池之类的障碍增加了其挑战性该游戏近乎永无止境。

奥赛罗棋,人工智能AI范畴,英国每年有比赛

黑白棋,又叫反棋(Reversi)、奥赛罗棋(Othello)、苹果棋或翻转棋。黑白棋在西方和日本很流行。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。它的游戏规则简单,因此上手很容易,但是它的变化又非常复杂。有一种说法是:只需要几分钟学会它,却需要一生的时间去精通它。黑白棋的棋盘是一个有8*8方格的棋盘。把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的全部翻转会成为自己的棋子。并且,只有在可以翻转棋子的地方才可以下子。黑白棋是19世纪末英国人发明的。直到上个世纪70年代一个日本人将其发展,借用莎士比亚名剧奥赛罗(othello)为这个游戏重新命名,也就是现在大家玩的黑白棋。为何借用莎士比亚名剧呢?是因为奥赛罗是莎士比亚一个名剧的男主角。他是一个黑人,妻子是白人,因受小人挑拨,怀疑妻子不忠一直情海翻波,最终亲手把妻子杀死。后来真相大白,奥赛罗懊悔不已,自杀而死。黑白棋就是借用这个黑人白人斗争的故事而命名。

游戏期间,位置比点数更重要!要着眼于长远利益,因为点数的领先很可能是暂时的。四个角上是必须争取占据的好位置,因为无法移开这些位置上的棋子。 设法使自己走一步时,牵制着对手无法移动任何棋子!这样可以在一排中连续走两步。

当然我们没有必要用一生的时间去精通它。用python程序可以去模拟游戏,找到最佳下棋算法。

开心消消乐,手机玩的很high的爆款游戏

《开心消消乐》是一款乐元素研发的一款三消类休闲游戏。一天晚上,天空中掉下一颗神奇的豌豆种子,正好落在了梦之森林的村长屋附近,种子落地后吸收了池塘的水分,迅速成长,一夜之间变成参天大藤蔓…… 第二天早上,村民们醒来后看到巨大的藤蔓都惊呆了,聚在一起议论纷纷。有人说他似乎看到村长的房子在高耸入云的藤蔓上,房子似乎还在上升,有人号召说应该爬上去救村长,玩家需要爬到藤曼顶部救出村长。

色彩拼图游戏,AI范畴,基于算法,在规定时间内完成色彩统一,这需要一定思考能力哟。

游戏编程模块pygame介绍

pygame是一组旨在编写视频游戏的Python模块。Pygame可以使用python语言创建功能齐全的游戏和多媒体程序。Pygame具有高度的可移植性,几乎可以在所有平台和操作系统上运行。Pygame至今已被下载了数百万次。Pygame非常流行的一个原因是免费。 根据LGPL许可证发布的内容,您可以使用它创建开源,免费软件,共享软件和商业游戏。Pygame历史开始于2000年10月。六个月后发布了pygame 1.0版。pygame的目标是使可视化游戏编程变得简单。 pygame是Python和SDL混合的产物。 SDL由Sam Lantinga创建,与DirectX相比,SDL是用于控制多媒体的跨平台C库。它已用于数百种商业和开源游戏。

迫不及待的想用pygame写一个自己的游戏了吗?用pip install pygame安装此模块吧。

pygame官网

学员查询pygame模块基础语法最好方法是访问官方文档,网址为

https://·/course/3065512 for event in pygame.event.get(): if event.type==QUIT: if event.type==pygame.locals.KEYDOWN: #change the keyboard variables键盘键设置上下左右 if event.key==pygame.locals.K_LEFT or event.key==ord('a'): moveRight=False moveLeft=True if event.key==pygame.locals.K_RIGHT or event.key==ord('d'): moveLeft=False moveRight=True if event.key==pygame.locals.K_UP or event.key==ord('w'): moveDown=False moveUp=True if event.key==pygame.locals.K_DOWN or event.key==ord('s'): moveDown=True moveUp=False #有KEYDOWN就一定有KEYUP,否则游戏对象会持续移动 if event.type==pygame.locals.KEYUP: if event.key==pygame.locals.K_ESCAPE: pygame.quit() sys.exit() if event.key==pygame.locals.K_LEFT or event.key==ord('a'): moveLeft=False if event.key==K_RIGHT or event.key==ord('d'): moveRight=False if event.key==K_UP or event.key==ord('w'): moveUp=False if event.key==K_DOWN or event.key==ord('s'): moveDown=False # move the bouncer data structure if moveDown and p_destination.bottom<WINDOWHEIGHT: p_destination.top+=PLAYERMOVERATE if moveUp and p_destination.top>0: p_destination.top-=PLAYERMOVERATE if moveLeft and p_destination.left>0: p_destination.left-=PLAYERMOVERATE if moveRight and p_destination.right<WINDOWWIDTH: p_destination.right+=PLAYERMOVERATE # Draw the game world on the window. windowSurface.blit(rescaledBackground, (0, 0)) windowSurface.blit(zombieStretchedImage,z_destination) windowSurface.blit(playerImage,p_destination) 发射子弹代码 playerRect = playerImage.get_rect() playerRect.topleft = (50, WINDOWHEIGHT /2) bulletImage = pygame.image.load('SnowPeashooterBullet.gif') bulletRect = bulletImage.get_rect() #连续发射两个子弹的距离,值越小,子弹距离越近,反之亦然 b['rect'].move_ip(1 * BULLETSPEED, 0) import pygame, random, sys, time from pygame.locals import * #连续发射两个子弹的距离,值越小,子弹距离越近,反之亦然 # set up pygame, the window, and the mouse cursor #reate an object to help track time mainClock = pygame.time.Clock() windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))#, pygame.FULLSCREEN) pygame.display.set_caption('Zombie Defence') #pygame.mouse.set_visible(False) font = pygame.font.SysFont(None, 48) pygame.mixer.music.load('grasswalk.mp3') playerImage = pygame.image.load('SnowPea.gif') #get the rectangular area of the Surface #Returns a new rectangle covering the entire surface. This rectangle will #always start at (0, 0) with a width and height the same size as the image. playerRect = playerImage.get_rect() bulletImage = pygame.image.load('SnowPeashooterBullet.gif') bulletRect = bulletImage.get_rect() zombieImage=pygame.image.load("zombie.png") zombieStretchedImage=pygame.transform.scale(zombieImage,(80,80)) z_destination=pygame.Rect(944, 300, 80, 80) backgroundImage = pygame.image.load('background.png') #对背景图片尺寸重新调整大小,宽为WINDOWWIDTH,长为WINDOWHEIGHT rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT)) playerRect.topleft = (50, WINDOWHEIGHT /2) moveLeft = moveRight = False pygame.mixer.music.play(-1, 0.0) while True: # the game loop runs while the game part is playing for event in pygame.event.get(): if event.type == QUIT: if event.type == KEYDOWN: if event.key == K_UP or event.key == ord('w'): moveDown = False moveUp = True if event.key == K_DOWN or event.key == ord('s'): moveUp = False moveDown = True if event.key == K_SPACE: shoot = True if event.type == KEYUP: if event.key == K_ESCAPE: terminate() if event.key == K_UP or event.key == ord('w'): moveUp = False if event.key == K_DOWN or event.key == ord('s'): moveDown = False if event.key == K_SPACE: shoot = False bulletAddCounter += 1 print("bulletAddCounter:",bulletAddCounter) #当while循环次数大于ADDNEWBULLETRATE时,才能发射子弹 if bulletAddCounter >= ADDNEWBULLETRATE and shoot == True: #if shoot == True: #如果不限制,子弹会连发,没有距离感 bulletAddCounter = 0 #playerRect.centery-25是为了让子弹上升一点,与植物炮口平行,centerx+10是为了子弹在前面点出现 newBullet = {'rect':pygame.Rect(playerRect.centerx+10, playerRect.centery-30, bulletRect.width, bulletRect.height), 'surface':pygame.transform.scale(bulletImage, (bulletRect.width, bulletRect.height)), } bullets.append(newBullet) # Move the player around. if moveUp and playerRect.top > 30: playerRect.move_ip(0,-1 * PLAYERMOVERATE) if moveDown and playerRect.bottom < WINDOWHEIGHT-10: playerRect.move_ip(0,PLAYERMOVERATE) # move the bullet移动子弹 b['rect'].move_ip(1 * BULLETSPEED, 0) #print(b['rect']) # Draw the game world on the window. windowSurface.blit(rescaledBackground, (0, 0)) # Draw the player's rectangle, rails windowSurface.blit(playerImage, playerRect) windowSurface.blit(zombieStretchedImage,z_destination) # draw each bullet将所有子弹绘制到游戏界面上 windowSurface.blit(b['surface'], b['rect']) pygame.display.update() #tick(framerate=0) -> milliseconds #帧速率参数,该功能将延迟以使游戏的运行速度低 #print("bullets",bullets) random.randint(a,b)随机生成若干移动僵尸

在python中的random.randint(a,b)用于生成一bai个指定范围内的整数。du其中参数zhia是下限,参数b是上限,生成的随机dao数n: a <= n <= b。

import pygame, random, sys, time from pygame.locals import * #连续发射两个子弹的距离,值越小,子弹距离越近,反之亦然 ZOMBIESIZE = 100 #includes newKindZombies ADDNEWKINDZOMBIE = ADDNEWZOMBIERATE NEWKINDZOMBIESPEED = NORMALZOMBIESPEED / 2 # set up the colors颜色值设置,可用于背景颜色 # set up pygame, the window, and the mouse cursor #reate an object to help track time mainClock = pygame.time.Clock() windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))#, pygame.FULLSCREEN) pygame.display.set_caption('Zombie Defence') #pygame.mouse.set_visible(False) font = pygame.font.SysFont(None, 48) pygame.mixer.music.load('grasswalk.mp3') playerImage = pygame.image.load('SnowPea.gif') #get the rectangular area of the Surface #Returns a new rectangle covering the entire surface. This rectangle will #always start at (0, 0) with a width and height the same size as the image. playerRect = playerImage.get_rect() bulletImage = pygame.image.load('SnowPeashooterBullet.gif') bulletRect = bulletImage.get_rect() zombieImage=pygame.image.load("zombie.png") zombieStretchedImage=pygame.transform.scale(zombieImage,(80,80)) z_destination=pygame.Rect(944, 300, 80, 80) backgroundImage = pygame.image.load('background.png') #对背景图片尺寸重新调整大小,宽为WINDOWWIDTH,长为WINDOWHEIGHT rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT)) playerRect.topleft = (50, WINDOWHEIGHT /2) moveLeft = moveRight = False pygame.mixer.music.play(-1, 0.0) while True: # the game loop runs while the game part is playing for event in pygame.event.get(): if event.type == QUIT: if event.type == KEYDOWN: if event.key == K_UP or event.key == ord('w'): moveDown = False moveUp = True if event.key == K_DOWN or event.key == ord('s'): moveUp = False moveDown = True if event.key == K_SPACE: shoot = True if event.type == KEYUP: if event.key == K_ESCAPE: terminate() if event.key == K_UP or event.key == ord('w'): moveUp = False if event.key == K_DOWN or event.key == ord('s'): moveDown = False if event.key == K_SPACE: shoot = False # Add new zombies at the top of the screen, if needed.增加上市 zombieAddCounter += 1 if zombieAddCounter == ADDNEWKINDZOMBIE: zombieAddCounter = 0 zombieSize = ZOMBIESIZE newZombie = {'rect': pygame.Rect(WINDOWWIDTH, random.randint(10,WINDOWHEIGHT-zombieSize-10), zombieSize, zombieSize), 'surface':pygame.transform.scale(zombieImage, (zombieSize, zombieSize)), } zombies.append(newZombie) bulletAddCounter += 1 print("bulletAddCounter:",bulletAddCounter) #当while循环次数大于ADDNEWBULLETRATE时,才能发射子弹 if bulletAddCounter >= ADDNEWBULLETRATE and shoot == True: #if shoot == True: #如果不限制,子弹会连发,没有距离感 bulletAddCounter = 0 #playerRect.centery-25是为了让子弹上升一点,与植物炮口平行,centerx+10是为了子弹在前面点出现 newBullet = {'rect':pygame.Rect(playerRect.centerx+10, playerRect.centery-30, bulletRect.width, bulletRect.height), 'surface':pygame.transform.scale(bulletImage, (bulletRect.width, bulletRect.height)), } bullets.append(newBullet) # Move the player around. if moveUp and playerRect.top > 30: playerRect.move_ip(0,-1 * PLAYERMOVERATE) if moveDown and playerRect.bottom < WINDOWHEIGHT-10: playerRect.move_ip(0,PLAYERMOVERATE) # move the bullet移动子弹 b['rect'].move_ip(1 * BULLETSPEED, 0) #print(b['rect']) z['rect'].move_ip(-1*NORMALZOMBIESPEED, 0) # Draw the game world on the window. windowSurface.blit(rescaledBackground, (0, 0)) # Draw the player's rectangle, rails windowSurface.blit(playerImage, playerRect) windowSurface.blit(zombieStretchedImage,z_destination) # draw each bullet将所有子弹绘制到游戏界面上 windowSurface.blit(b['surface'], b['rect']) windowSurface.blit(z['surface'], z['rect']) pygame.display.update() #tick(framerate=0) -> milliseconds #帧速率参数,该功能将延迟以使游戏的运行速度低 #print("bullets",bullets)

完整的植物大战僵尸游戏代码如下。你们可以根据自己偏好来改变人物图片,背景,配音。

import pygame, random, sys, time from pygame.locals import * ZOMBIESIZE = 100 #includes newKindZombies ADDNEWKINDZOMBIE = ADDNEWZOMBIERATE NEWKINDZOMBIESPEED = NORMALZOMBIESPEED / 2 TEXTCOLOR = (255, 255, 255) def waitForPlayerToPressKey(): for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYDOWN: if event.key == K_ESCAPE: # pressing escape quits terminate() if event.key == K_RETURN: return def playerHasHitZombie(playerRect, zombies): #test if two rectangles overlap测试是否触碰到僵尸 if playerRect.colliderect(z['rect']): def bulletHasHitZombie(bullets, zombies): if b['rect'].colliderect(z['rect']): bullets.remove(b) #测试子弹是否触碰到新僵尸。如果是,就把该僵尸移除 def bulletHasHitCrawler(bullets, newKindZombies): if b['rect'].colliderect(c['rect']): bullets.remove(b) def drawText(text, font, surface, x, y): textobj = font.render(text, 1, TEXTCOLOR) textrect = textobj.get_rect() textrect.topleft = (x, y) #draw one image onto another surface.blit(textobj, textrect) # set up pygame, the window, and the mouse cursor #reate an object to help track time mainClock = pygame.time.Clock() windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))#, pygame.FULLSCREEN) pygame.display.set_caption('Zombie VS Plants') pygame.mouse.set_visible(False) font = pygame.font.SysFont(None, 48) gameOverSound = pygame.mixer.Sound('gameover.wav') pygame.mixer.music.load('background.mp3') playerImage = pygame.image.load('plant.gif') #get the rectangular area of the Surface #Returns a new rectangle covering the entire surface. This rectangle will #always start at (0, 0) with a width and height the same size as the image. playerRect = playerImage.get_rect() bulletImage = pygame.image.load('bullet.gif') bulletRect = bulletImage.get_rect() zombieImage = pygame.image.load('zombie.png') newKindZombieImage = pygame.image.load('ConeheadZombie.gif') #newKindZombieImage = pygame.image.load('trump31.png') backgroundImage = pygame.image.load('background.png') #对背景图片尺寸重新调整大小,宽为WINDOWWIDTH,长为WINDOWHEIGHT rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT)) # show the "Start" screen windowSurface.blit(rescaledBackground, (0, 0)) windowSurface.blit(playerImage, (WINDOWWIDTH / 2, WINDOWHEIGHT - 70)) drawText('Zombie VS Plants', font, windowSurface, (WINDOWWIDTH / 4), (WINDOWHEIGHT / 4)) drawText('Press Enter to start', font, windowSurface, (WINDOWWIDTH / 3) - 10, (WINDOWHEIGHT / 3) + 50) waitForPlayerToPressKey() # set up the start of the game zombiesGottenPast = 0 playerRect.topleft = (50, WINDOWHEIGHT /2) moveLeft = moveRight = False moveUp=moveDown = False newKindZombieAddCounter = 0 bulletAddCounter = 40 pygame.mixer.music.play(-1, 0.0) while True: # the game loop runs while the game part is playing for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYDOWN: if event.key == K_UP or event.key == ord('w'): moveDown = False moveUp = True if event.key == K_DOWN or event.key == ord('s'): moveUp = False moveDown = True if event.key == K_SPACE: shoot = True if event.type == KEYUP: if event.key == K_ESCAPE: terminate() if event.key == K_UP or event.key == ord('w'): moveUp = False if event.key == K_DOWN or event.key == ord('s'): moveDown = False if event.key == K_SPACE: shoot = False # Add new zombies at the top of the screen, if needed.增加上市 zombieAddCounter += 1 if zombieAddCounter == ADDNEWKINDZOMBIE: zombieAddCounter = 0 zombieSize = ZOMBIESIZE newZombie = {'rect': pygame.Rect(WINDOWWIDTH, random.randint(10,WINDOWHEIGHT-zombieSize-10), zombieSize, zombieSize), 'surface':pygame.transform.scale(zombieImage, (zombieSize, zombieSize)), } zombies.append(newZombie) # Add new newKindZombies at the top of the screen, if needed. newKindZombieAddCounter += 1 if newKindZombieAddCounter == ADDNEWZOMBIERATE: newKindZombieAddCounter = 0 newKindZombiesize = ZOMBIESIZE newCrawler = {'rect': pygame.Rect(WINDOWWIDTH, random.randint(10,WINDOWHEIGHT-newKindZombiesize-10), newKindZombiesize, newKindZombiesize), 'surface':pygame.transform.scale(newKindZombieImage, (newKindZombiesize, newKindZombiesize)), } newKindZombies.append(newCrawler) bulletAddCounter += 1 if bulletAddCounter >= ADDNEWBULLETRATE and shoot == True: bulletAddCounter = 0 newBullet = {'rect':pygame.Rect(playerRect.centerx+10, playerRect.centery-25, bulletRect.width, bulletRect.height), 'surface':pygame.transform.scale(bulletImage, (bulletRect.width, bulletRect.height)), } bullets.append(newBullet) # Move the player around. if moveUp and playerRect.top > 30: playerRect.move_ip(0,-1 * PLAYERMOVERATE) if moveDown and playerRect.bottom < WINDOWHEIGHT-10: playerRect.move_ip(0,PLAYERMOVERATE) # Move the zombies down. for z in zombies: z['rect'].move_ip(-1*NORMALZOMBIESPEED, 0) # Move the newKindZombies down. for c in newKindZombies: c['rect'].move_ip(-1*NEWKINDZOMBIESPEED,0) # move the bullet for b in bullets: b['rect'].move_ip(1 * BULLETSPEED, 0) # Delete zombies that have fallen past the bottom. for z in zombies[:]: if z['rect'].left < 0: zombies.remove(z) zombiesGottenPast += 1 # Delete newKindZombies that have fallen past the bottom. for c in newKindZombies[:]: if c['rect'].left <0: newKindZombies.remove(c) zombiesGottenPast += 1 for b in bullets[:]: if b['rect'].right>WINDOWWIDTH: bullets.remove(b) # check if the bullet has hit the zombie 检查子弹是否触碰到僵尸 for z in zombies: if bulletHasHitZombie(bullets, zombies): score += 1 zombies.remove(z) for c in newKindZombies: if bulletHasHitCrawler(bullets, newKindZombies): score += 1 newKindZombies.remove(c) # Draw the game world on the window. windowSurface.blit(rescaledBackground, (0, 0)) # Draw the player's rectangle, rails windowSurface.blit(playerImage, playerRect) # Draw each baddie for z in zombies: windowSurface.blit(z['surface'], z['rect']) for c in newKindZombies: windowSurface.blit(c['surface'], c['rect']) # draw each bullet for b in bullets: windowSurface.blit(b['surface'], b['rect']) # Draw the score and how many zombies got past drawText('zombies gotten past: %s' % (zombiesGottenPast), font, windowSurface, 10, 20) drawText('score: %s' % (score), font, windowSurface, 10, 50) # update the display pygame.display.update() # Check if any of the zombies has hit the player. if playerHasHitZombie(playerRect, zombies): if playerHasHitZombie(playerRect, newKindZombies): # check if score is over MAXGOTTENPASS which means game over if zombiesGottenPast >= MAXGOTTENPASS: #tick(framerate=0) -> milliseconds #帧速率参数,该功能将延迟以使游戏的运行速度低 mainClock.tick(FPS) # Stop the game and show the "Game Over" screen.游戏结束执行命令 pygame.mixer.music.stop() gameOverSound.play() #播放游戏结束时声音 if zombiesGottenPast >= MAXGOTTENPASS: windowSurface.blit(rescaledBackground, (0, 0)) windowSurface.blit(playerImage, (WINDOWWIDTH / 2, WINDOWHEIGHT - 70)) drawText('score: %s' % (score), font, windowSurface, 10, 30) drawText('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3)) drawText('YOUR COUNTRY HAS BEEN DESTROIED', font, windowSurface, (WINDOWWIDTH / 4)- 80, (WINDOWHEIGHT / 3) + 100) drawText('Press enter to play again or escape to exit', font, windowSurface, (WINDOWWIDTH / 4) - 80, (WINDOWHEIGHT / 3) + 150) pygame.display.update() waitForPlayerToPressKey() if playerHasHitZombie(playerRect, zombies): windowSurface.blit(rescaledBackground, (0, 0)) windowSurface.blit(playerImage, (WINDOWWIDTH / 2, WINDOWHEIGHT - 70)) drawText('score: %s' % (score), font, windowSurface, 10, 30) drawText('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3)) drawText('YOU HAVE BEEN KISSED BY THE ZOMMBIE', font, windowSurface, (WINDOWWIDTH / 4) - 80, (WINDOWHEIGHT / 3) +100) drawText('Press enter to play again or escape to exit', font, windowSurface, (WINDOWWIDTH / 4) - 80, (WINDOWHEIGHT / 3) + 150) pygame.display.update() waitForPlayerToPressKey() gameOverSound.stop() #游戏结束声音停止

这一章我们学习了python的pygame模块,了解了pygame模块的基础知识和如何编写植物大战僵尸的DIY游戏。总之pygame在编写视频游戏上方便,高效,且具有高度的可移植性,几乎可以在所有平台和操作系统上运行。不要等了,赶紧去安装pygame模块,开始第一个游戏编程之旅吧。

论文涉及机器学习建模解决方案

版权声明:文章来自公众号(python风控模型),未经许可,不得抄袭。遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #python游戏编程