一、实验目的

以经典的飞机大战代码为参考,实现食物在舍友之间的单向传递,共有三次失误机会,机会全部用完后则视为游戏失败。

二、游戏操作

舍友一:A:向左移动,D:向右移动

舍友二:左方向键:向左移动,右方向键:向右移动

空格键:向对面的舍友发射食物

三、游戏成品

四、游戏代码

import sys
import pygame
import time
import setting
pygame.init()#主窗口
scene_image = pygame.display.set_mode((800,600))
scene_rect = scene_image.get_rect()
scene_image.fill(setting.color_1)#标题栏
pygame.display.set_caption("双人成行")zhangjin_image = pygame.image.load('zhangjin.bmp')
zhangjin_image = pygame.transform.scale(zhangjin_image,(90,80))
zhangjin_image = pygame.transform.rotate(zhangjin_image,90)
zhangjin_rect = zhangjin_image.get_rect()
zhangjin_rect.midtop = scene_rect.midtop
zhangjin_rect.y = scene_rect.y+20liyi_image = pygame.image.load('liyi.bmp')
liyi_image = pygame.transform.scale(liyi_image,(90,80))  #更改liyi大小
liyi_rect = liyi_image.get_rect()
liyi_rect.midbottom = scene_rect.midbottom
liyi_rect.bottom = scene_rect.bottom -20liyi_moving_left = False
liyi_moving_right = Falsezhangjin_moving_left = False
zhangjin_moving_right = False#食物
foods = pygame.sprite.Group()
# foods = []
lives_isleft = 0
#文字
button_rect = pygame.Rect(0,0,200,50)
button_rect.center = scene_rect.center
play_font = pygame.font.SysFont(None,48)
play_image = play_font.render("play",True,setting.color_2,setting.color_3)
play_rect = play_image.get_rect()
play_rect.center = button_rect.center#统计信息
score = 0
food_points = 1
score_str = str(score)
score_font = pygame.font.SysFont(None,36)
score_image = score_font.render(f"recent score:{score_str}",True,setting.color_3,setting.color_1)
score_rect = score_image.get_rect()
score_rect.right = scene_rect.right - 20
score_rect.top = 20
scene_image.blit(score_image,score_rect)high_score = 0
high_score_str = str(high_score)
high_score_font = pygame.font.SysFont(None,36)
high_score_image = high_score_font.render(f"highest score:{high_score_str}",True,setting.color_3,setting.color_1)
high_score_rect = high_score_image.get_rect()
high_score_rect.midtop =score_rect.midbottom
high_score_rect.right = score_rect.right
scene_image.blit(high_score_image,high_score_rect)
pygame.display.flip()#剩余可不吃
zhuzi_group = pygame.sprite.Group()
for zhuzi_number in range(setting.zhuzi_limit - 1):zhuzi = pygame.sprite.Sprite()zhuzi.image = pygame.image.load('zhuzi.bmp')zhuzi.image = pygame.transform.scale(zhuzi.image, (45, 40))zhuzi.rect = zhuzi.image.get_rect()zhuzi.rect.x = 5+(zhuzi.rect.width + 5) * zhuzi_numberzhuzi.rect.y = 5zhuzi_group.add(zhuzi)
zhuzi_group.draw(scene_image)while True:pygame.mixer.music.play(-1)for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:liyi_moving_left = Trueif event.key == pygame.K_RIGHT:liyi_moving_right = Trueif event.key == pygame.K_a:zhangjin_moving_left = Trueif event.key == pygame.K_d:zhangjin_moving_right = Trueif event.key == pygame.K_q:sys.exit()if event.key == pygame.K_SPACE:if len(foods) < setting.foods_number:food = pygame.sprite.Sprite()f = pygame.image.load('food.bmp')f = pygame.transform.scale(f, (20, 20))food.rect = f.get_rect()food.image = ffood.rect.midbottom = liyi_rect.midtopfoods.add(food)elif event.type == pygame.KEYUP:if event.key == pygame.K_LEFT:liyi_moving_left = Falseif event.key == pygame.K_RIGHT:liyi_moving_right = Falseif event.key == pygame.K_a:zhangjin_moving_left = Falseif event.key == pygame.K_d:zhangjin_moving_right = Falseelif event.type == pygame.MOUSEBUTTONDOWN:mouse_pos = pygame.mouse.get_pos()if button_rect.collidepoint(mouse_pos):lives_isleft = setting.zhuzi_limitpygame.mouse.set_visible(False)foods.empty()if lives_isleft > 0:if liyi_moving_left and liyi_rect.left > 0:liyi_rect.x -= setting.speedif liyi_moving_right and liyi_rect.right < scene_rect.right:liyi_rect.x += setting.speedif zhangjin_moving_left and zhangjin_moving_left > 0:zhangjin_rect.x -= setting.speedif zhangjin_moving_right and zhangjin_moving_right < scene_rect.right:zhangjin_rect.x += setting.speedzhangjin_sprite = pygame.sprite.Sprite()zhangjin_sprite.rect = zhangjin_rectzhangjin_sprite.image = zhangjin_imagescene_image.fill(setting.color_1)scene_image.blit(zhangjin_image, zhangjin_rect)scene_image.blit(liyi_image, liyi_rect)# 张锦没吃上foodfor item in foods:item.rect.y -= setting.speedif item.rect.bottom < 0:lives_isleft -= 1foods.remove(item)scene_image.blit(food.image, item)if lives_isleft == 0:# time.sleep(0.5)liyi_rect.bottom = scene_rect.bottom - 20zhangjin_rect.y = scene_rect.y + 20foods.empty()# 张锦zhangjin_rect = zhangjin_image.get_rect()zhangjin_rect.midtop = scene_rect.midtopzhangjin_rect.y = scene_rect.y + 20# 李毅liyi_rect = liyi_image.get_rect()liyi_rect.midbottom = scene_rect.midbottomliyi_rect.bottom = scene_rect.bottom - 20#张锦和food碰撞if pygame.sprite.spritecollide(zhangjin_sprite,foods,True):score += 1if score > high_score:high_score = score# 统计信息score_str = str(score)score_font = pygame.font.SysFont(None, 36)score_image = score_font.render(f"recent score:{score_str}", True, setting.color_3, setting.color_1)score_rect = score_image.get_rect()score_rect.right = scene_rect.right - 20score_rect.top = 20scene_image.blit(score_image, score_rect)high_score_str = str(high_score)high_score_font = pygame.font.SysFont(None, 36)high_score_image = high_score_font.render(f"highest score:{high_score_str}", True, setting.color_3,setting.color_1)high_score_rect = high_score_image.get_rect()high_score_rect.midtop = score_rect.midbottomhigh_score_rect.right = score_rect.rightscene_image.blit(high_score_image, high_score_rect)pygame.display.flip()# 剩余可不吃zhuzi_group = pygame.sprite.Group()for zhuzi_number in range(lives_isleft - 1):zhuzi = pygame.sprite.Sprite()zhuzi.image = pygame.image.load('zhuzi.bmp')zhuzi.image = pygame.transform.scale(zhuzi.image, (45, 40))zhuzi.rect = zhuzi.image.get_rect()zhuzi.rect.x = 5 + (zhuzi.rect.width + 5) * zhuzi_numberzhuzi.rect.y = 5zhuzi_group.add(zhuzi)zhuzi_group.draw(scene_image)else:pygame.draw.rect(scene_image,setting.color_4,button_rect)scene_image.blit(play_image,play_rect)pygame.mouse.set_visible(True)score = 0pygame.display.flip()time.sleep(setting.time)

五、分段解析

1、实现长按方向键连续移动

liyi_moving_left = False
liyi_moving_right = False
zhangjin_moving_left = False
zhangjin_moving_right = Falsewhile True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:liyi_moving_left = Trueif event.key == pygame.K_RIGHT:liyi_moving_right = Trueif event.key == pygame.K_a:zhangjin_moving_left = Trueif event.key == pygame.K_d:zhangjin_moving_right = Trueif event.key == pygame.K_q:sys.exit()if event.key == pygame.K_SPACE:if len(foods) < setting.foods_number:food = pygame.sprite.Sprite()f = pygame.image.load('food.bmp')f = pygame.transform.scale(f, (20, 20))food.rect = f.get_rect()food.image = ffood.rect.midbottom = liyi_rect.midtopfoods.add(food)elif event.type == pygame.KEYUP:if event.key == pygame.K_LEFT:liyi_moving_left = Falseif event.key == pygame.K_RIGHT:liyi_moving_right = Falseif event.key == pygame.K_a:zhangjin_moving_left = Falseif event.key == pygame.K_d:zhangjin_moving_right = Falseif liyi_moving_left and liyi_rect.left > 0:liyi_rect.x -= setting.speedif liyi_moving_right and liyi_rect.right < scene_rect.right:liyi_rect.x += setting.speedif zhangjin_moving_left and zhangjin_moving_left > 0:zhangjin_rect.x -= setting.speedif zhangjin_moving_right and zhangjin_moving_right < scene_rect.right:zhangjin_rect.x += setting.speed

根据pygame中event.get()函数对键盘,鼠标等动作的捕捉,可以初步实现一令一动的目的。但是要实现连续移动,就要设置moving_right,moving_left两个阀门,当其为true的时候,x轴位置可以连续发生改变

2、按钮以及文本的显示实现

button_rect = pygame.Rect(0,0,200,50)
button_rect.center = scene_rect.center
play_font = pygame.font.SysFont(None,48)
play_image = play_font.render("play",True,setting.color_2,setting.color_3)
play_rect = play_image.get_rect()
play_rect.center = button_rect.center
#变量的赋值pygame.draw.rect(scene_image,setting.color_4,button_rect)
scene_image.blit(play_image,play_rect)
#参数的调用

pygame中的文本是以image格式实现,rect规定了大小以及位置信息,image则代表了内容(对于文字来说就是文字颜色,背景色,文本内容等信息,对于图片来说就是图片地址)

3、鼠标的隐藏和出现

pygame.mouse.set_visible(False)

set_Visible函数中参数False代表鼠标消失,True代表鼠标出现

4、统计信息的实现方法

#统计信息
score = 0
food_points = 1
score_str = str(score)
score_font = pygame.font.SysFont(None,36)
score_image = score_font.render(f"recent score:{score_str}",True,setting.color_3,setting.color_1)
score_rect = score_image.get_rect()
score_rect.right = scene_rect.right - 20
score_rect.top = 20
scene_image.blit(score_image,score_rect)high_score = 0
high_score_str = str(high_score)
high_score_font = pygame.font.SysFont(None,36)
high_score_image = high_score_font.render(f"highest score:{high_score_str}",True,setting.color_3,setting.color_1)
high_score_rect = high_score_image.get_rect()
high_score_rect.midtop =score_rect.midbottom
high_score_rect.right = score_rect.right
scene_image.blit(high_score_image,high_score_rect)
pygame.display.flip()while true:#张锦和food碰撞if pygame.sprite.spritecollide(zhangjin_sprite,foods,True):score += 1if score > high_score:high_score = score# 统计信息score_str = str(score)score_font = pygame.font.SysFont(None, 36)score_image = score_font.render(f"recent score:{score_str}",True,setting.color_3, setting.color_1)score_rect = score_image.get_rect()score_rect.right = scene_rect.right - 20score_rect.top = 20scene_image.blit(score_image, score_rect)high_score_str = str(high_score)high_score_font = pygame.font.SysFont(None, 36)high_score_image = high_score_font.render(f"highest score:{high_score_str}",True, setting.color_3,setting.color_1)high_score_rect = high_score_image.get_rect()high_score_rect.midtop = score_rect.midbottomhigh_score_rect.right = score_rect.rightscene_image.blit(high_score_image, high_score_rect)pygame.display.flip()

用image规定文本内容,用rect规定文本的出现坐标,当舍友头像与食物发生碰撞的时候,说明成功进食,所以score加1,当当前分数高于最高分时,将目前分数赋值给最高分,每次重新开始游戏前将score归零

5、当出现没有及时享用的食物到达边框

zhuzi_group = pygame.sprite.Group()for zhuzi_number in range(lives_isleft - 1):zhuzi = pygame.sprite.Sprite()zhuzi.image = pygame.image.load('zhuzi.bmp')zhuzi.image = pygame.transform.scale(zhuzi.image, (45, 40))zhuzi.rect = zhuzi.image.get_rect()zhuzi.rect.x = 5 + (zhuzi.rect.width + 5) * zhuzi_numberzhuzi.rect.y = 5zhuzi_group.add(zhuzi)zhuzi_group.draw(scene_image)

左上角生命值减一(注:左上角生命+1为总生命值)

6、屏幕中最多出现十个食物

foods_number = 10if event.key == pygame.K_SPACE:if len(foods) < foods_number:food = pygame.sprite.Sprite()f = pygame.image.load('food.bmp')f = pygame.transform.scale(f, (20, 20))food.rect = f.get_rect()food.image = ffood.rect.midbottom = liyi_rect.midtopfoods.add(food)for item in foods:item.rect.y -= setting.speedif item.rect.bottom < 0:lives_isleft -= 1foods.remove(item)scene_image.blit(food.image, item)if lives_isleft == 0:# time.sleep(0.5)liyi_rect.bottom = scene_rect.bottom - 20zhangjin_rect.y = scene_rect.y + 20foods.empty()# 张锦zhangjin_rect = zhangjin_image.get_rect()zhangjin_rect.midtop = scene_rect.midtopzhangjin_rect.y = scene_rect.y + 20# 李毅liyi_rect = liyi_image.get_rect()liyi_rect.midbottom = scene_rect.midbottomliyi_rect.bottom = scene_rect.bottom - 20

食物最终有两种状态,当被舍友享用之后score加一,反之,到达边界后,生命值减一,并且将其移出食物所在列表,保持屏幕上最多有十个食物的规定。如果生命值耗费完,将页面恢复为最初的样子,且出现可重新开始游戏的字样。

完结撒花!

python——pygame制作恶搞舍友小游戏相关推荐

  1. python +pygame 制作五子连珠小游戏

    python +pygame 制作五子连珠小游戏 学习python半年了,今天分享一个利用pygame制作的五子连珠游戏. 一.代码: 1.球类,ball.py """ ...

  2. 100行代码,使用 Pygame 制作一个贪吃蛇小游戏!

    作者 | 周萝卜 来源 | 萝卜大杂烩 相信我们大家都玩过贪吃蛇游戏,今天我们就从头一起来写一个贪吃蛇小游戏,只需要100多行的代码就完成了. 用到的 Pygame 函数 贪吃蛇小游戏用到的函数 功能 ...

  3. 又是一年中秋至|Python Pygame制作中秋兔子接月饼游戏【源码+解析】

    一年中秋又快到了,今年加入了Python的学习行列,得益于Python的开发效率和易读性,网上写文章的次数多了起来,既然是中秋节那肯定要搞个应景的游戏才行. 左思右想没有头绪时,刚好看到一篇介绍Pyg ...

  4. 基于Python实现制作的接金币小游戏

    资源下载地址:https://download.csdn.net/download/sheziqiong/85738332 资源下载地址:https://download.csdn.net/downl ...

  5. 用python+pygame写的宝石迷阵小游戏

    寒假的时候没什么意思,索性学了一个脚本语言,当初选定Python也是因为他很强大不仅可以像Ruby一样做网络,也可以像Perl做管理(可能这么比不太对哈~~),其实Python是门很简单的语言,大概一 ...

  6. 完美的Python代码制作“恐龙跳一跳“小游戏【附带源码 】

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 原理介绍 游戏规则 ...

  7. 恐龙跳一跳游戏python_【Python】Python代码制作”恐龙跳一跳”小游戏

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 相关文件 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 原理介绍 ...

  8. Maze_AI: 一款基于 Python + Pygame + AI 算法的迷宫小游戏

    (一)课题内容 实现走迷宫. 主要功能为界面显示.上下左右键的响应以及当前步数统计. 通过该课题全面熟悉数组.字符串等的使用,掌握程序设计的基本方法及友好界面的设计. (二)课题要求 1. 基本要求 ...

  9. python的pygame库使用方法_python基础教程使用Python第三方库pygame写个贪吃蛇小游戏...

    今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是基本游戏功能已经实现,下面是代码: # 导入模块 import pygame impo ...

最新文章

  1. 格式化json日期'/Date(-62135596800000)/'
  2. SEO(搜索引擎最佳化)简介
  3. 信息竞赛进阶指南--归并排序求逆序对
  4. 博客园的祥和需要大家共同努力
  5. cordova 打包vue 集成的app , router-view 默认首页白屏
  6. wifi智能门锁远程控制方案能实现哪些功能
  7. 维基百科Wikipedia镜像网站列表
  8. opencv 之 颜色通道提取
  9. CANoe中的Channel-based access和Network-based access
  10. UVA - 558 Wormholes (SPEA算法模板题)
  11. Python中面向对象(类,对象,魔法,打印)
  12. 如何使用Transformers和Tokenizers从头开始训练新的语言模型
  13. 实现《你的名字》同款滤镜,python+opencv
  14. 矢志不渝,人生会慢慢露出轮廓。
  15. chrome浏览器主页被劫持
  16. java中的repo什么意思,repo是什么意思什么梗 repo的含义及出处
  17. Image.FromStream与Image.FromFile使用区别
  18. html滤镜菜鸟教程,Style
  19. 深入理解Java中的i++、++i语句
  20. Windows下多鼠标/双鼠标技术专题

热门文章

  1. ifrme嵌入外部页面,在外部页面调用本页面方法,window.postMessage实现跨域通信
  2. AXI 总线协议学习笔记(2)
  3. 鄂维南院士:数据科学的基本内容
  4. Python-100-Days学习笔记day09
  5. php prettyprinter,SQL Pretty Printer
  6. 如何制作我的第一个Phaser.js游戏
  7. matlab里butter,matlab中butter函数
  8. 服务器CPU跑满了怎么办
  9. 惊魂,我的23个密码被泄露,快看看你的有没有泄露
  10. 【学习周报】注意力机制的工作原理和主流方法。