pygame安装 pip install pygame

在CMD中输入python -m pygame.examples.aliens可以看到一个自带的pygame的演示小游戏

学了几天pygame,那就实践一下吧,如图实现玩家操控小鸟躲避小球的游戏,当碰撞到小球游戏结束,关闭游戏

所以我们需要实现以下几个功能:

1.操控小鸟

2.小球移动

3.碰撞检测

4.结束游戏

一. 控制小鸟我们用pygame.KEYDOWN来检测,当用户敲击下键盘上下左右时,改变鸟的位置坐标,并消除之前的图片

1 #键盘敲击事件

2 elif event.type ==pygame.KEYDOWN:3 #上

4 if event.key ==pygame.K_UP:5 if imageRect.top !=0:6 pygame.draw.rect(screen,THECOLORS['white'],imageRect)7 imageRect =imageRect.move(0,birdspeed[0])8 count = count + 1

9 #下

10 elif event.key ==pygame.K_DOWN:11 if imageRect.bottom != 360:12 pygame.draw.rect(screen, THECOLORS['white'], imageRect)13 imageRect = imageRect.move(0,birdspeed[1])14 count = count + 1

15 #左

16 elif event.key ==pygame.K_LEFT:17 if imageRect.left !=0:18 pygame.draw.rect(screen, THECOLORS['white'], imageRect)19 imageRect = imageRect.move(birdspeed[3],0)20 #右

21 elif event.key ==pygame.K_RIGHT:22 if imageRect.right != 540:23 pygame.draw.rect(screen, THECOLORS['white'], imageRect)24 imageRect = imageRect.move(birdspeed[2],0)25 #按ESC退出游戏

26 elif event.key ==pygame.K_ESCAPE:27 sys.exit()

二.我们先设置小球的初始位置,然后让小球从右向左移动,并用draw.rect画掉之前的图像,当小球到达屏幕最左端的时候,我们让它重新出现,并且修改纵坐标为鸟的同一行

1 replace_rect = pygame.draw.rect(screen, THECOLORS['white'], ballRect)2 #小球自右向左移动

3 if ballLocation[0] > -60:4 if count > 10 and count < 8:5 ballLocation[0] = ballLocation[0] + ballspeed[count//10]6 else:7 ballLocation[0] = ballLocation[0] +ballspeed[0]8 else:9 ballLocation[0] = 540

10 #小球移动到最左端时改变出现的位置

11 if ballLocation[0] == -60:12 ballLocation[1] =imageRect.top13 #纵坐标超出范围时修改

14 if ballLocation[1] > 359:15 ballLocation[1] = ballLocation_change[random.randint(1,6)]

三.碰撞检测和结束

我将鸟的图像创建一个Rect对象,相当于这个rect对象帮我获取的鸟这个图像的上下左右的坐标,这样我们就可以用来判断它的位置信息,这里我用ballLocation来存储球的位置

1 #碰撞检测的实现

2 if imageRect.bottom == ballLocation[1]+60 and imageRect.top == ballLocation[1] :3 if imageRect.left-ballLocation[0] < 60 and imageRect.left-ballLocation[0] >0 :4 #如果碰撞就结束游戏

5 print("Game Over")6 pygame.mixer.music.load(gameOver_musci)7 pygame.mixer.music.play()8 screen.blit(game_over_image,[0,0])9 pygame.display.update()10 pygame.time.delay(1500)11 sys.exit()

完整代码如下,注释还挺多的,图片和音乐资源网上自己都能找到,用自己喜欢的图片就行了,最好是透明的图片,如果和我一样图片不是透明的而是白色背景,就得加这个了不然就不用

将透明色设置为白色

control_image = pygame.image.load('资源/img/bird.png').convert()

control_image.convert_alpha()

control_image.set_colorkey(ALPHA)

1 #导入和常量设置

2 importpygame,sys3 from pygame.color importTHECOLORS4 importrandom5 size = wdith , height = 540 ,360#设置屏幕尺寸

6 birdspeed = [-60,60,60,-60]7 ballspeed = [-8,-9,-10,-11,-12,-13,-14,-15,-16]8 ballLocation = [540,60]9 ballLocation_change = [0,60,120,180,240,300,360]10 ALPHA = (255,255,255)#设置透明色11 fps = 120#屏幕刷新率

12 count = 1#用于统计操控次数来改变游戏难度

13 #初始化界面

14 pygame.init()15 fclock =pygame.time.Clock()16 screen =pygame.display.set_mode(size)17 pygame.display.set_caption('躲小球1.0')18 #音乐载入

19 music = '资源/music/letter.mp3'

20 gameOver_musci = '资源/music/gameover2.mp3'

21 pygame.mixer.music.load(music)22 pygame.mixer.music.play()23 #图片载入

24 imag = '资源/img/cat1.png'

25 imag =pygame.image.load(imag)26 pygame.display.set_icon(imag)27 #将透明色设置为白色

28 control_image = pygame.image.load('资源/img/bird.png').convert()29 control_image.convert_alpha()30 control_image.set_colorkey(ALPHA)31

32 game_over_image = pygame.image.load('资源/img/gameover.jpg')33 game_over_image = pygame.transform.scale(game_over_image,[540,360])#设置为指定大小的图片34 ball_image = pygame.image.load('资源/img/ball.png')35 background = pygame.image.load('资源/img/sky.png')36 background = pygame.transform.scale(background,[540,360])37 control_image = pygame.transform.scale(control_image,[60,60])#设定为指定大小的图片38 ball_image = pygame.transform.scale(ball_image,[60,60])39 ballRect =ball_image.get_rect()40 imageRect =control_image.get_rect()41 #背景色和贴图片

42 screen.fill(THECOLORS['white'])43 screen.blit(control_image,imageRect)44 screen.blit(ball_image,ballLocation)45 #事件循环响应队列

46 whileTrue:47 for event inpygame.event.get():48 if event.type ==pygame.QUIT:49 sys.exit()50 #键盘敲击事件

51 elif event.type ==pygame.KEYDOWN:52 #上

53 if event.key ==pygame.K_UP:54 if imageRect.top !=0:55 pygame.draw.rect(screen,THECOLORS['white'],imageRect)#将前一个图像覆盖掉56 imageRect =imageRect.move(0,birdspeed[0])57 count = count + 1

58 #下

59 elif event.key ==pygame.K_DOWN:60 if imageRect.bottom != 360:61 pygame.draw.rect(screen, THECOLORS['white'], imageRect)62 imageRect = imageRect.move(0,birdspeed[1])63 count = count + 1

64 #左

65 elif event.key ==pygame.K_LEFT:66 if imageRect.left !=0:67 pygame.draw.rect(screen, THECOLORS['white'], imageRect)68 imageRect = imageRect.move(birdspeed[3],0)69 #右

70 elif event.key ==pygame.K_RIGHT:71 if imageRect.right != 540:72 pygame.draw.rect(screen, THECOLORS['white'], imageRect)73 imageRect = imageRect.move(birdspeed[2],0)74 #按ESC退出游戏

75 elif event.key ==pygame.K_ESCAPE:76 sys.exit()77 #抹除操作前的图像

78 replace_rect = pygame.draw.rect(screen, THECOLORS['white'], ballRect)79 #小球自右向左移动

80 if ballLocation[0] > -60:81 if count > 10 and count < 8:82 ballLocation[0] = ballLocation[0] + ballspeed[count//10]83 else:84 ballLocation[0] = ballLocation[0] +ballspeed[0]85 else:86 ballLocation[0] = 540

87 #小球移动到最左端时改变出现的位置

88 if ballLocation[0] == -60:89 ballLocation[1] =imageRect.top90 #纵坐标超出范围时修改

91 if ballLocation[1] > 359:92 ballLocation[1] = ballLocation_change[random.randint(1,6)]93 #碰撞检测的实现

94 if imageRect.bottom == ballLocation[1]+60 and imageRect.top == ballLocation[1] :95 if imageRect.left-ballLocation[0] < 60 and imageRect.left-ballLocation[0] >0 :96 #如果碰撞就结束游戏

97 print("Game Over")98 pygame.mixer.music.load(gameOver_musci)99 pygame.mixer.music.play()100 screen.blit(game_over_image,[0,0])101 pygame.display.update()102 pygame.time.delay(1500)103 sys.exit()104 #背景

105 screen.blit(background,(0,0))106 #小球

107 screen.blit(ball_image,ballLocation)108 #控制对象

109 screen.blit(control_image,imageRect)110

111 pygame.display.update()#刷新112 fclock.tick(fps)

python躲方块_pygame实现的《躲小球》相关推荐

  1. csgo躲猫猫模式显示服务器已满,CSGO有躲猫猫模式吗 CSGO躲猫猫模式进入方法一览...

    CSGO有躲猫猫模式吗 CSGO躲猫猫模式进入方法一览 从CS1.6开始,到CS:S,然后是最近的<反恐精英:全球攻势>(CSGO),虽然一直是电子竞技游戏,但有许多高手也为这款游戏创建了 ...

  2. 猫咪藏在哪个房间python作业_猫咪生气躲进房间,众人找到后,猫咪一脸疑问:听说你们在找我...

    以前,养猫的人不多,而且养猫的方式也不一样,人们每天忙得都顾不上和猫玩耍,观察它们.所以就说猫咪养不熟,高冷什么的,这就是因为不了解所以产生的误解. 如今,养猫的人越来越多,而且人们的空闲时间也多了, ...

  3. 这些 Python 不为人知的「坑」,躲都躲不开

    作者:王易诺,人工智能算法工程师,Python/C++ 程序员,推理/科幻小说作者.曾于高中获得全国青少年信息学奥林匹克联赛一等奖. 现已取得复旦大学计算机科学技术学士学位,以及爱丁堡大学计算机科学硕 ...

  4. python编程方块_趣味Python编程之经典俄罗斯方块

    本帖最后由 柠檬守护 于 2016-10-23 14:14 编辑 转载:http://www.linuxidc.com/Linux/2016-10/136205.htm 用python把经典俄罗斯方块 ...

  5. python设计抽奖游戏 球_python3实现小球转动抽奖小游戏

    最近老师在讲 tkinter,所以我做了一个抽奖小游戏. 一.效果图 先上效果图.红色的小球会围绕蓝色小球做环形运动.我设置的四个角是奖品,其余的都是再接再厉. 二.方法 基于tkinter中的but ...

  6. python 控制系统音量_pygame学习笔记(4):声音控制

    pygame.mixer是一个用来处理声音的模块,其含义为"混音器".游戏中对声音的处理一般包括制造声音和播放声音两部分,这里仅学习了播放声音部分. 1.pygame.mixer启 ...

  7. 高并发之存储篇:关注下索引原理和优化吧!躲得过实践,躲不过面试官!

    以MySQL的InnoDB索引为主,看完需要5分钟 本文内容预览: 为什么Kafka不需要我们关心索引,而Mysql却需要? Mysql数据怎么被组织 2.1 数据记录最小单位:行 2.2 与磁盘最小 ...

  8. python代码雨_pygame实现烟雨蒙蒙下彩虹雨

    学习了一天的深度学习,略有疲惫,我们用pygame搞个小游戏放松放松吧.今天我们的游戏主体是烟雨蒙蒙下彩虹雨,仿佛置身江南水乡. 游戏描述 我们希望看到江南水乡下起彩虹雨.这里背景是江南水乡,烟雨蒙蒙 ...

  9. python程序填空_pygame实现成语填空游戏

    最近看到很多人玩成语填字游戏,那么先用pygame来做一个吧,花了大半天终于完成了,附下效果图. 偷了下懒程序没有拆分,所有程序写在一个文件里,主要代码如下: # -*- coding=utf-8 - ...

  10. python动画精灵_pygame学习笔记(5):游戏精灵

    据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽.魂斗罗,却对精灵一点也不知.pygame.sprite.Sprite就是Pygame里面用来实现精灵的一个类,使用时,并不需要 ...

最新文章

  1. mac镜像cdr格式_设计常用文件格式!萌新必备
  2. linux系统编程需要什么,若想成为一名Linux下编程高手,必须能对各种系统调用有透彻的了解...
  3. PHP中try{}catch{}是异常处理.
  4. Python跨平台文件夹分割方法os.sep
  5. c修改datatable单元格的值_神奇的VBA编程:批量拆分单元格数据
  6. 经过几天的Scala回归Java的10个最烦人的事情
  7. python3实用编程技巧_6.python3实用编程技巧进阶(一)
  8. 醉了!吃着火锅哼着歌,男朋友强行给我科普什么是补码!
  9. flutter Radio 单选框
  10. 亚太数学建模竞赛优秀论文_全国大学生数学建模竞赛介绍
  11. 01-09 Linux三剑客-awk
  12. thinkphp5的Illegal string offset 'id'错误
  13. 51nod 1428 活动安排问题 (贪心+优先队列)
  14. 2021 美国硅谷程序员调查:后端人才最吃香 !
  15. 企业信息安全管理制度
  16. 去了一趟少林寺 竟然发现...
  17. jvm学习——jvm内存区域
  18. 最前线|库克:苹果正在考虑调整iPhone定价策略
  19. unity获取麦克风音量_Unity调取移动端的麦克风进行录音并播放
  20. 美团首席科学家的成长史

热门文章

  1. 利用C语言实现wol网络远程唤醒
  2. 【Git】Conventional Commit 约定式提交规范
  3. PS非主流照片文字合成表现手法
  4. 怎样做具有视觉冲击感的非主流照片
  5. 学习笔记-SNN用STDP法的MNIST数据集识别代码深入阅读
  6. 计算机网络实验:802.3协议分析和以太网
  7. LCC谐振变换器部分参数设计过程
  8. vue 自动打开浏览器
  9. python转换整数_在Python中将数字转换为整数列表
  10. PMP考试 工作绩效数据 工作绩效信息 工作绩效报告 区别与联系