标签:

Python既有脚本语言的灵活性,又有丰富的类库与面向对象的特点,开发起来很方便,

游戏的规则和乒乓球一样,如果妙蛙种子掉地上了就算输,你可以用蓝色的跷跷板弹它,使他不落到地面上,Game Over后可按任意键继续游戏或选择退出;

把下面这段代码贴到你的IDLE中,找到下面这两行替换成你机器中的图片路径,按F5就可以运行了。

img_player = "C:\Users\dswu\Desktop\player.png"

img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"

代码如下:

1 importsys, pygame2 from random import *

3 from pygame.locals import *

4 from pygame.font import *

5 classMyPlayer(pygame.sprite.Sprite):6 def __init__(self, image_file, location, speed):7 pygame.sprite.Sprite.__init__(self)8 self.image =pygame.image.load(image_file)9 self.rect =self.image.get_rect()10 self.rect.left, self.rect.top =location11 self.speed =speed12 self.status =True13 defmove(self):14 self.rect =self.rect.move(self.speed)15 if self.rect.left < 0 or self.rect.right >width:16 self.speed[0] = -self.speed[0]17 if self.rect.top <0:18 self.speed[1] = -self.speed[1]19 if self.rect.bottom >height:20 #Game over

21 self.status =False22 classReflector(pygame.sprite.Sprite):23 def __init__(self, image_file, location, speed):24 pygame.sprite.Sprite.__init__(self)25 self.image =pygame.image.load(image_file)26 self.rect =self.image.get_rect()27 self.rect.left, self.rect.top =location28 self.speed =speed29 defmove(self):30 self.rect =self.rect.move(self.speed)31 if self.rect.left < 0 or self.rect.right >width:32 self.speed[0] = -self.speed[0]33 if self.rect.top < 0 or self.rect.bottom >height:34 self.speed[1] = -self.speed[1]35 defanimate(players):36 screen.fill([255,255,255])37 for player inplayers:38 player.move()39 for player inplayers:40 players.remove(player)41 ifpygame.sprite.spritecollide(player,players,False):42 player.speed[0] = -player.speed[0]43 player.speed[1] = -player.speed[1]44 players.add(player)45 player.move()46 screen.blit(player.image,player.rect)47 pygame.display.flip()48 pygame.time.delay(10)49

50 pygame.init()51 size = width,height = 640,480

52 screen =pygame.display.set_mode(size)53 screen.fill([255,255,255])54 pygame.display.set_caption("MiaoWa Game")55 defplay():56 img_player = "C:\Users\dswu\Desktop\player.png"

57 players =pygame.sprite.Group()58 for row in range(0,1):59 for column in range(0,1):60 playerLocation = [column*250+10,row*250+10]61 playerSpeed = [choice([-2,2]), choice([-2,2])]62 player =MyPlayer(img_player, playerLocation, playerSpeed)63 players.add(player)64 img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"

65 ref_pos = [0,464]66 ref_speed =[0,0]67 reflector =Reflector(img_ref_path, ref_pos, ref_speed)68 players.add(reflector)69 running =True70 whilerunning:71 key_pressed =pygame.key.get_pressed()72 for event inpygame.event.get():73 if event.type ==pygame.QUIT:74 game.quit()75 if event.type ==KEYDOWN:76 if event.key ==K_LEFT:77 ref_speed[0] = -2

78 elif event.key ==K_RIGHT:79 ref_speed[0] = +2

80 animate(players)81 if player.status ==False:82 running =False83 final_text = "Game Over!"

84 ft_font = pygame.font.Font(None, 100)85 ft_surf = ft_font.render(final_text, 1, (0,0,0))86 screen.blit(ft_surf, [screen.get_width()/2 - ft_surf.get_width()/2, 100])87 tip_text = "Type any key to continue"

88 tip_font = pygame.font.Font(None, 50)89 tip_surf = tip_font.render(tip_text, 1, (0,0,0))90 screen.blit(tip_surf, [screen.get_width()/2 - tip_surf.get_width()/2, 200])91 pygame.display.flip()92 keepOn =True93 whilekeepOn:94 key_pressed =pygame.key.get_pressed()95 for event inpygame.event.get():96 if event.type ==pygame.QUIT:97 pygame.quit()98 if event.type ==KEYDOWN:99 play()100 play()

主要是通过pygame.sprite.Sprite类实现碰撞的监控,通过事件的捕捉及判断实现这种弹力球类的游戏,其中涉及到文字在界面上的显示,游戏中的循环控制,以及重新开始游戏等。

代码运行一下应该就差不多明白了,如果有不明白的地方请给我留言,方便一起学习与进步。

标签:

python 乒乓球_Python中实现乒乓球效果相关推荐

  1. python字符集_PYTHON 中的字符集

    Python中的字符编码是个老生常谈的话题,今天来梳理一下相关知识,希望给其他人些许帮助. Python2的 默认编码 是ASCII,不能识别中文字符,需要显式指定字符编码:Python3的 默认编码 ...

  2. python参数化_Python 中如何实现参数化测试的方法示例

    之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest.nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架. 本文想针对测试中一种很常见的测试场景,即参 ...

  3. kafka python框架_Python中如何使用Apache Avro——Apache的数据序列化系统

    了解如何创建和使用基于Apache Avro的数据,以实现更好,更有效的传输. 在这篇文章中,我将讨论Apache Avro,这是一种开源数据序列化系统,Spark,Kafka等工具正在使用该工具进行 ...

  4. python标准化_python中标准化

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! sdk 3.0 实现了统一化,各个语言版本的 sdk具备使用方法相同.接口调用方 ...

  5. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  6. 支持向量机python代码_Python中的支持向量机SVM的使用(有实例)

    除了在Matlab中使用PRTools工具箱中的svm算法,Python中一样可以使用支持向量机做分类.因为Python中的sklearn库也集成了SVM算法,本文的运行环境是Pycharm. 一.导 ...

  7. python程序启动其他python程序_python中使用fork创建新的进程

    python中如果要创建新进程的话,可以使用os模块中的fork方法.为了了解其中工作原理,笔者结合linux的查看进程ps命令,对这个方法做了一些测试. python运行时进程 python一开始运 ...

  8. 数学建模可以用python吗_Python中常用的数学建模Scipy

    Python中常用的数学建模Scipy 发布时间:2020-09-10 16:56:48 来源:亿速云 阅读:116 本篇文章为大家展示了Python中常用的数学建模Scipy,代码简明扼要并且容易理 ...

  9. python 图像分割_Python中的图像分割(第2部分)

    python 图像分割 Stuck behind the paywall? Click here to read the full article with my friend link. 卡在收费墙 ...

最新文章

  1. TCP/IP反码求和校验
  2. Oracle入门(十二B)之表创建
  3. python解析html xml最好的模块_Python HTML/XML解析器BeautifulSoup(爬虫解析器)
  4. R语言-时间日期函数
  5. 龘(若出身在新中国我的65047777
  6. 大一计算机专业学生如何在寒假充电?
  7. 现身说法“好奇心害死人啊”
  8. 循环训练_特殊时期别出门,一套居家循环训练简单有趣又有效
  9. 理解PPAPI的设计
  10. DateFormat的使用
  11. c语言装b程序,C语言打造表白和装逼利器:亲爱的让我们相爱在一起,酷炫的梦幻...
  12. oracle wip 资源吸收,Oracle ERP系统借贷关系
  13. 简单写一下未来10年的职业规划
  14. 《SteamVR2.2.0开发指南》(Yanlz+Unity+XR+SteamVR+OpenXR+OpenVR+Valve+VIVE+Oculus+Interaction+VR+立钻哥哥++ok++)
  15. 图像算法原理与实践——图像修复之 全变分模型
  16. 智能点餐小程序有哪些基本功能
  17. 乔布斯的简历17.4万拍卖,HR看了想打人
  18. 计算机每次关机需要配置,电脑关机,详细教您怎么设置电脑定时关机
  19. shiro框架基础--shiro框架概念及原理
  20. 【综述】近年来NLP在法律领域的相关研究工作

热门文章

  1. OpenGL超级宝典(第五版)环境配置【转】
  2. 前端笔记35——label标签
  3. BGW协议(算数共享)
  4. ios设备解锁---iToolab UnlockGo mac
  5. 原生php开发留言板源码
  6. Tornado的部署
  7. 辐射3游戏登录是提示计算机丢失xlive.dll文件,辐射3提示丢失xlive.dll
  8. 虽迟但到,我的2022年终总结
  9. Kerberos 使用小记
  10. git的初次使用及查看vue源码