游戏玩法

根据神庙逃亡,实现一个人躲避僵尸的小游戏,主要的是精灵、精灵组之间相撞、相交的处理。
游戏开始随机出现一定的僵尸,随机移动,玩家在一位置上,如果僵尸靠近玩家一定距离,则玩家持续掉血。玩家通过上下左右移动躲避僵尸,屏幕会随机刷新一个加血包,玩家吃了就会加一定的血,并在此刷新血包。

property()

这个函数在类中返回新的属性

property(get,set,del,doc)

参数如上所示,get、set、del分别是获取设值删除调用的,doc是描述的。

精灵类

在原来的精灵类中添加方向和属性即可。

class MySprite(pygame.sprite.Sprite):def __init__(self, target):pygame.sprite.Sprite.__init__(self)self.master_image = Noneself.frame = 0self.old_frame = -1self.frame_width = 1self.frame_height = 1self.first_frame = 0self.last_frame = 0self.columns = 1self.last_time = 0self.direction = 0self.classification = "玩家"def load(self, filename, width, height, columns, direction, classification="玩家"):# 精灵的属性self.classification = classification# 方向self.direction = direction# 载入图片# 780 * 300self.master_image = pygame.image.load(filename).convert_alpha() # 载入图片self.frame_width = width self.frame_height = height self.rect = Rect(0, 0, width, height)self.columns = columns rect = self.master_image.get_rect() self.last_frame = (rect.width // width) * (rect.height // height) - 1 def update(self, current_time, rate=30): # current_time 更新频率 为30if current_time > self.last_time + rate: # 如果当前事件 大于 最后的时间 + 当前的节奏self.frame += 1 # 当前的帧数加一if self.frame > self.last_frame: # 当前最后一帧 则从第一帧开始self.frame = self.first_frame  # 从0开始self.last_time = current_time # 将最后帧值为30# build current frame only if it changedif self.frame != self.old_frame: # 当前帧数不等于老的一帧frame_x = (self.frame % self.columns) * self.frame_widthframe_y = (self.frame // self.columns) * self.frame_heightrect = (frame_x, frame_y, self.frame_width, self.frame_height) # 更新对应的位置self.image = self.master_image.subsurface(rect) # 循环箱已有的方向self.old_frame = self.framedef __str__(self):return str(self.frame) + "," + str(self.first_frame) + \"," + str(self.last_frame) + "," + str(self.frame_width) + \"," + str(self.frame_height) + "," + str(self.columns)

初始画面

和原来一样,先建立简单的画面。

import mySprite1
import pygame, sys, random
from pygame.locals import *def print_text(font, x, y, text, color=(255, 255, 255)):imgText = font.render(text, True, color)screen.blit(imgText, (x, y))# 设置窗口
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("勇闯后半夜")
font = pygame.font.Font(None, 30)
timer = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == QUIT:sys.exit()key = pygame.key.get_pressed()if key[K_ESCAPE]:sys.exit()screen.fill((50, 50, 100))pygame.display.update()

精灵移动函数

如果是僵尸遇到墙壁自动反方向走,根据方向改变对应的位置。

def reversal_direction(mySprite):direction = mySprite.directionif direction == 0:direction = 4elif direction == 2:direction = 6elif direction == 4:direction = 0elif direction == 6:direction = 2mySprite.direction = directiondef increment(mySprite, offset=1):# 上下左右direction = mySprite.directionrect = mySprite.rectif direction == 0:rect.y -= offsetelif direction == 2:rect.x += offsetelif direction == 4:rect.y += offsetelif direction == 6:rect.x -= offset# 超出边界的处理# 超出边界flgboundary = Falseif rect.x < 0:rect.x = 0boundary = Trueif rect.x + mySprite.frame_width > 800:rect.x = 800 - mySprite.frame_widthboundary = Trueif rect.y < 0:rect.y = 0boundary = Trueif rect.y + mySprite.frame_height > 600:rect.y = 600 - mySprite.frame_heightboundary = True# 如果超出边界而且是僵尸的话 则反转方向if boundary and mySprite.classification == "僵尸":reversal_direction(mySprite)

加载玩家

这个是素材的图,如上所示,奇数行便是对应的方向移动。文件大小是768 * 768,可以分为96 * 96的8张。

加载玩家

# 玩家
play_group = pygame.sprite.Group()play = mySprite1()
play.load("farmer walk.png", 96, 96, 8)
play.direction = -1
play_group.rect.x = 96
play_group.rect.y = 96
play_group.add(play)play_group.update(ticks, 50)screen.fill((50, 50, 100))play_group.draw(screen)pygame.display.update()


通过改变帧数修改,根据游戏的结束或是否移动,改变对应的事件

    if not game_over:play.first_frame = play.direction * play.columnsplay.last_frame = play.first_frame + play.columns - 1if play.frame < play.first_frame:play.frame = play.first_frameif not play_moving:play.frame = play.first_frame = play.last_frameelse:increment(play)

控制交互

    if key[K_ESCAPE]:sys.exit()elif key[K_UP]:play.direction = 0play_moving = Trueelif key[K_DOWN]:play.direction = 4play_moving = Trueelif key[K_LEFT]:play.direction = 6play_moving = Trueelif key[K_RIGHT]:play.direction = 2play_moving = Trueelse:play_moving = False

添加僵尸

# 随机生成20个僵尸
for n in range(0, 10):zombie = MySprite()random_direction = random.randint(0, 3) * 2zombie.load("zombie walk.png", 96, 96, 8, random_direction, "僵尸")zombie.rect.x = random.randint(0, 600)zombie.rect.y = random.randint(0, 500)print(zombie.rect)zombie_group.add(zombie)# 设置僵尸for z in zombie_group:z.first_frame = z.direction * z.columnsz.last_frame = z.first_frame + z.columns - 1if z.frame < z.first_frame:z.frame = z.first_frameincrement(z)

添加血包

血包就是单纯的一个图的展示。

health = MySprite()
health.load("health.png", 32, 32, 1)
health.rect.x = random.randint(0, 600)
health.rect.y = random.randint(0, 500)
health_group.add(health)

精灵相互碰撞事件

主要是僵尸和人 、人和血包之间的相撞事件

    # 相撞事件attack = Noneattack = pygame.sprite.spritecollideany(play, zombie_group)if attack is not None:if pygame.sprite.collide_rect_ratio(0.5)(play, attack):play_health -= 10attack.rect.x = random.randint(0, 600)attack.rect.y = random.randint(0, 500)else:attack = Noneif pygame.sprite.collide_rect_ratio(0.5)(play, health):play_health += 30if play_health > 100:play_health = 100health.rect.x = random.randint(0, 600)health.rect.y = random.randint(0, 500)if play_health <= 0:game_over = True# 显示血量pygame.draw.rect(screen, (100, 200, 100, 180), Rect(300, 575, 200, 25))pygame.draw.rect(screen, (50, 150, 150, 180), Rect(300, 575, play_health * 2, 25))

完整代码

import pygame, sys, random
from pygame.locals import *
from mySprite1 import *def print_text(font, x, y, text, color=(255, 255, 255)):imgText = font.render(text, True, color)screen.blit(imgText, (x, y))def reversal_direction(mySprite):direction = mySprite.directionif direction == 0:direction = 4elif direction == 2:direction = 6elif direction == 4:direction = 0elif direction == 6:direction = 2mySprite.direction = directiondef increment(mySprite, offset=1):# 上下左右direction = mySprite.directionrect = mySprite.rectif direction == 0:rect.y -= offsetelif direction == 2:rect.x += offsetelif direction == 4:rect.y += offsetelif direction == 6:rect.x -= offset# 超出边界的处理# 超出边界flgboundary = Falseif rect.x < 0:rect.x = 0boundary = Trueif rect.x + mySprite.frame_width > 800:rect.x = 800 - mySprite.frame_widthboundary = Trueif rect.y < 0:rect.y = 0boundary = Trueif rect.y + mySprite.frame_height > 600:rect.y = 600 - mySprite.frame_heightboundary = True# 如果超出边界而且是僵尸的话 则反转方向if boundary and mySprite.classification == "僵尸":reversal_direction(mySprite)# 设置窗口
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("勇闯后半夜")
font = pygame.font.Font(None, 30)
timer = pygame.time.Clock()
game_over = False# 玩家
play_group = pygame.sprite.Group()play = MySprite()
play.load("farmer walk.png", 96, 96, 8, 4)
play.rect.x = 96
play.rect.y = 96
play_moving = False
play_group.add(play)
play_health = 100# 僵尸
zombie_group = pygame.sprite.Group()# 随机生成20个僵尸
for n in range(0, 10):zombie = MySprite()random_direction = random.randint(0, 3) * 2zombie.load("zombie walk.png", 96, 96, 8, random_direction, "僵尸")zombie.rect.x = random.randint(0, 600)zombie.rect.y = random.randint(0, 500)print(zombie.rect)zombie_group.add(zombie)# 血包
health_group = pygame.sprite.Group()health = MySprite()
health.load("health.png", 32, 32, 1)
health.rect.x = random.randint(0, 600)
health.rect.y = random.randint(0, 500)
health_group.add(health)while True:# 设置执行的频率timer.tick(30)ticks = pygame.time.get_ticks()for event in pygame.event.get():if event.type == QUIT:sys.exit()key = pygame.key.get_pressed()if key[K_ESCAPE]:sys.exit()elif key[K_UP]:play.direction = 0play_moving = Trueelif key[K_DOWN]:play.direction = 4play_moving = Trueelif key[K_LEFT]:play.direction = 6play_moving = Trueelif key[K_RIGHT]:play.direction = 2play_moving = Trueelse:play_moving = Falseif not game_over:# 设置玩家play.first_frame = play.direction * play.columnsplay.last_frame = play.first_frame + play.columns - 1if play.frame < play.first_frame:play.frame = play.first_frame# 设置僵尸for z in zombie_group:z.first_frame = z.direction * z.columnsz.last_frame = z.first_frame + z.columns - 1if z.frame < z.first_frame:z.frame = z.first_frameincrement(z)if not play_moving:play.frame = play.first_frame = play.last_frameelse:increment(play)# 相撞事件attack = Noneattack = pygame.sprite.spritecollideany(play, zombie_group)if attack is not None:if pygame.sprite.collide_rect_ratio(0.5)(play, attack):play_health -= 10attack.rect.x = random.randint(0, 600)attack.rect.y = random.randint(0, 500)else:attack = Noneif pygame.sprite.collide_rect_ratio(0.5)(play, health):play_health += 30if play_health > 100:play_health = 100health.rect.x = random.randint(0, 600)health.rect.y = random.randint(0, 500)if play_health <= 0:game_over = Trueplay_group.update(ticks, 50)zombie_group.update(ticks, 50)health_group.update(ticks, 50)screen.fill((50, 50, 100))play_group.draw(screen)zombie_group.draw(screen)health_group.draw(screen)# 显示血量pygame.draw.rect(screen, (100, 200, 100, 180), Rect(300, 575, 200, 25))pygame.draw.rect(screen, (50, 150, 150, 180), Rect(300, 575, play_health * 2, 25))if game_over:print_text(font, 300, 100, "GAME OVER!!!")pygame.display.update()

pygame 躲避僵尸相关推荐

  1. 如何使用iPhone生存僵尸启示录

    A virus breaks out of a lab somewhere deep underground, society falls into chaos, and now the Jones' ...

  2. 玩和平精英暗夜危机僵尸模式吓尿了?这十个对抗僵尸无敌位置你都知道吗?

    和平精英暗夜危机模式怎么玩?暗夜危机模式下如何躲避僵尸?前几天和平精英更新了版本并上线了全新的暗夜危机模式僵尸玩法,虽然该模式早就在国际服上线了,但还是有不少玩家特别是女性玩家被暗夜危机恐怖的音效吓尿 ...

  3. 七日杀服务器怎么修改天数,七日杀游戏里怎么修改天数 | 手游网游页游攻略大全...

    发布时间:2016-07-21 七日杀 游戏玩法心得 玩家心得攻略,更多七日杀的相关游戏资讯尽在多游GAME! 七日杀 游戏玩法心得 玩家心得攻略 玩了两天的感慨:丧尸的叫声比战斗力更折磨人; 自杀系 ...

  4. 自创文字小游戏《人类末日·丧尸危机》

    这个游戏有很多坑哦,其他就没什么要注意的啦. 你不要以为你以为的就是对的! 有些关卡不能通关纯属运气!!!最多10次就可以通关啦!!! 代码在哪里? #include <bits/stdc++. ...

  5. 七日杀怎么修改服务器世界参数,七日杀怎么修改mod参数 | 手游网游页游攻略大全...

    发布时间:2015-11-19 修改分辨率的方法 首先要进入注册表. 点开始,然后点运行,运行Regedit,然后查找7 Days To Die 出现了一大推注册表. 然后看最下面有两个,一个是Scr ...

  6. 七日杀服务器文件翻译,七日杀items文件翻译 | 手游网游页游攻略大全

    发布时间:2015-10-24 七日杀a13.6存档在哪 a13.6存档位置详解.a13.6是近期很热门的游戏,很多玩家不知道存档在哪?a13.6新版本存档位置改变了,有些玩家可能找不到游戏的存档目录 ...

  7. 7日杀服务器抽奖系统,七日杀钱代码 | 手游网游页游攻略大全

    发布时间:2018-05-12 作为一款生存游戏确实非常优秀,各位玩家觉得有难度的,不妨试试使用控制台修改修改.这里小编给大家带来了控制台代码大全,感兴趣的玩家一起来看看吧. 开启作弊方法是在游戏中按 ...

  8. 求生之路2服务器指令比较全

    bind "b" "say !buy"  绑定 // 远程连接密码 rcon_password 256 net_graph 1 游戏显示FPS+ping的命令 ...

  9. 七日杀服务器修改采集倍数,七日杀怎么修改物资倍数 | 手游网游页游攻略大全...

    发布时间:2018-05-15 空投物资对游戏里的求生玩家来说可谓是不可或缺的资源,但仍有许多玩家都找不到空投物资.这里小编给大家介绍空投物资显形方法,一起来看看吧. 此方法只适合单机,联机的玩家暂时 ...

最新文章

  1. 发现一个工具,可以清除 xp win7 用户密码,在PE下运行
  2. 杨老师课堂之JavaScript定时器限时抢购秒杀商品案例
  3. 商人过河 java_商人过河问题(二)java实现
  4. simple css 汉化,Simple CSS(CSS文档生成器)
  5. 华为交换机重置命令(reset saved-configuration)
  6. 【错误记录】Flutter 设备连接显示 Loading... ( 断网 | 删除 flutter/bin/cache/lockfile 文件 )
  7. 【Paper】2020_含时延约束的多智能体系统二分一致性
  8. Visual Studio Code 中文下载
  9. Spring Boot和Thymeleaf:重新加载模板和静态资源,而无需重新启动应用程序
  10. ubuntu apache配置负载均衡篇(二)
  11. C++——如何理解.h文件和.cpp文件
  12. 庆大学校计算机系,张丽霞(加州大学洛杉矶分校计算机系教授)_百度百科
  13. r语言 list添加_R语言里面双层list变成长形数据框
  14. 接口测试基础与工具(一)
  15. 搭建HDFS和HBase集群
  16. stm32无源蜂鸣器定时器_【STM32H7教程】第20章 STM32H7的GPIO应用之无源蜂鸣器...
  17. 苹果手机还原网络设置会怎样_如果你的苹果手机信号不好!要记得这样设置,让你信号轻松满格...
  18. 手算KMP算法nextval数组
  19. 百度把黑科技发布会开到央视,主持人连连感叹:文科生都看懂了
  20. 440 亿美元成交!Twitter 「卖身」马斯克

热门文章

  1. node.js清除缓存命令
  2. gitlab从安装到使用到常见问题处理
  3. Android_插值器
  4. 企业微信公众号运营技巧有哪些
  5. 玩转外贸LinkedIn必备的三大特质,以及突破六度人脉技巧
  6. 电子商务购物网站的设计与实现(论文+源码)_kaic
  7. C++中struct的用法
  8. 图片上传之webuploader和qiniuUploader
  9. 根据SNP的位置从基因组提取上下游序列
  10. centos下申请阿里云泛域名证书并自动更新