导语 哈喽哈喽!我是木木子!新的一天要继续努力呀
时至今日,全国疫情仍然是个严峻的话题,希望大家防范疫情要坚持要做好防控!
正确戴口罩、洗手防疫情哦~今天的话还是给大家准备了一个小游戏:
《消灭病毒保卫城市》!你懂的~话不多说,我们来看看吧!
【程序员合集|【Pygame实战】疫情期间给不能出门的你推荐一款爽游 《消灭病毒保卫城市》【强推】愿早日结束】
正文
游戏规则: 本游戏属于策略游戏,通过设定城市人数(决定收入),可购买道具:
包括医护人员(决定收留病人的数量,一个医护人员可医治5人)、研发药物(可提升治愈率,降低致死率)、限制出
行(可降低人员移动的幅度,同时减少资金的产生)、全民防护(可阻止病毒传染)。
每一天可利用收入进行道具的购买,并开始防护,等所有的感染人员均已治愈或者死亡,游戏胜利or失败。
文章图片
环境准备:
1)素材(图片音乐等)
文章图片
2)运行环境
本文用到的运行环境:Python3.7、Pycharm社区版2020、Pygame、numpy、tkinter模块部分自带模块直接导入不
需要安装。(如果需要安装包软件、激活码或者遇到问题的话可以私信我哈!)
模块安装:pip install -i https://pypi.douban.com/simple/ +模块名
文章图片
效果展示: 1)游戏界面
文章图片
2)开始游戏
购买相应的物品比如:医护人员、药品等会相应的减少金钱的。右边的感染人数之后会显示在地图
上面。医院的图上面有治愈人数 治疗区,死亡人物等等。
每一天会布置新的部署 购买什么东西自己选择哈~下面这是第三天,买了一个医生跟治疗药物!
文章图片
?3)这是我随机玩的截图
文章图片
代码演示:
代码的话超级多的撒,还挺不好展示的,主要是分为三个py,这里给大家简单的展示一个主程序的
代码吧!完整的大家都直接找我,很多代码都是显示了注释的哈,不懂的找我即可!
最好是有一定基础的小伙伴儿找我,不然感觉新手小伙伴儿 要重头来 我讲也讲不清楚,请大家谅
解,需要时间研究跟写文章,可能时间不够!偶尔解答问题随机哈!
主程序代码700多行:
import pygame
import sys
import traceback
import os
from pygame.locals import *
import numpy as np
import linecache
from random import *
import people
import easygui as g
from tkinter import *pygame.init()# 游戏初始化
pygame.mixer.init()# 音效初始化bg_size = width, height = 1200, 700# 屏幕大小
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("消灭病毒保卫城市小游戏")# 标题
mycity = pygame.image.load("source/城市.jpg").convert()
hospital_image = pygame.image.load("source/医院.jpg").convert_alpha()
# massage_image = pygame.image.load("source/信息区.png").convert_alpha()good_man_image = pygame.image.load("source/健康人.png").convert_alpha()
infect_man_image = pygame.image.load("source/感染人.png").convert_alpha()
ill_man_image = pygame.image.load("source/重症人.png").convert_alpha()
dead_man_image = pygame.image.load("source/死亡人.png").convert_alpha()
cure_man_image = pygame.image.load("source/治愈人.png").convert_alpha()BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GRAY = (192, 192, 192)
ORANGE = (255, 165, 0)# 游戏音乐
pygame.mixer.music.load("source/背景音乐.mp3")
pygame.mixer.music.set_volume(0.2)
success_sound = pygame.mixer.Sound("source/正确.wav")
success_sound.set_volume(0.2)
lost_sound = pygame.mixer.Sound("source/失败.wav")
lost_sound.set_volume(0.2)
win_sound = pygame.mixer.Sound("source/胜利.wav")
win_sound.set_volume(0.2)def ShowBack():
# 背景图片
# mycity = pygame.image.load("source/城市.jpg").convert()
# hospital_image = pygame.image.load("source/医院.jpg").convert_alpha()
doctor_image = pygame.image.load("source/医护支持.png").convert_alpha()
pill_image = pygame.image.load("source/药物研究.png").convert_alpha()
limit_image = pygame.image.load("source/限制出行.png").convert_alpha()
protect_image = pygame.image.load("source/全民防护.png").convert_alpha()
doctor_t_image = pygame.image.load("source/医护支持_t.png").convert_alpha()
pill_t_image = pygame.image.load("source/药物研究_t.png").convert_alpha()
limit_t_image = pygame.image.load("source/限制出行_t.png").convert_alpha()
protect_t_image = pygame.image.load("source/全民防护_t.png").convert_alpha()
massage_image = pygame.image.load("source/信息区.png").convert_alpha()
money_image = pygame.image.load("source/人民币.png").convert_alpha()
# screen.blit(mycity, (500, 0))# 载入背景图片
# screen.blit(hospital_image, (100, 200))# 载入医院图片
screen.blit(massage_image, (0, 0))# 载入信息图片
screen.blit(doctor_image, (300, 0))# 载入医生图片
screen.blit(doctor_image, (0, 200))# 载入医生图片
screen.blit(doctor_t_image, (0, 300))# 载入医生文字图片
screen.blit(pill_image, (400, 0))# 载入药物图片
screen.blit(pill_image, (0, 325))# 载入药物图片
screen.blit(pill_t_image, (0, 425))# 载入药物图片
screen.blit(limit_image, (300, 100))# 载入限制出行图片
screen.blit(limit_image, (0, 450))# 载入限制出行图片
screen.blit(limit_t_image, (0, 550))# 载入限制出行图片
screen.blit(protect_image, (400, 100))# 载入保护图片
screen.blit(protect_image, (0, 575))# 载入保护图片
screen.blit(protect_t_image, (0, 675))# 载入保护图片
screen.blit(money_image, (300, 2))# 载入人民币图片
screen.blit(money_image, (400, 2))# 载入人民币图片
screen.blit(money_image, (300, 102))# 载入人民币图片
screen.blit(money_image, (400, 102))# 载入人民币图片# 初始化城市人数
def InitCity(group, myimage, num):
for i in range(num):
gm = people.City(myimage)
screen.blit(gm.image, gm.rect)
group.add(gm)# 初始化医院人数
def InitHospital(group, myimage, num):
for i in range(num):
gm = people.Hospital(myimage)
screen.blit(gm.image, gm.rect)
group.add(gm)# 住院治疗
def AddtoHospital(group, image):
hm = people.Hospital(image)
group.add(hm)
screen.blit(hm.image, hm.rect)# 死亡
def AddtoDead(group, num):
for i in range(num):
dm = people.DeadErea()
group.add(dm)
screen.blit(dm.image, dm.rect)# 治愈
def AddtoCure(group, num):
for i in range(num):
cm = people.CureErea()
group.add(cm)
screen.blit(cm.image, cm.rect)
# 写入记录文件
def PutRecord(file, record):
f = open(file, mode='w', encoding='utf-8')
for i in range(len(record)):
f.write(str(record[i]))
f.write("\n")
f.close()
# 读取记录
def GetRecord(file):
record = []
f = open(file, mode='r', encoding='utf-8')
for each in f.readlines():
record.append(int(each))
f.close()
# print(record)
return record
# 清空组
def Clear(group):
for each in group:
group.remove(each)# 主函数
def main():
pygame.mixer.music.play(-1)# 播放背景音乐# 生成健康人组、感染人组、重症人组、治愈人组、病亡人组
goodmans = pygame.sprite.Group()
infectmans = pygame.sprite.Group()
illmans = pygame.sprite.Group()
curemans = pygame.sprite.Group()
deadmans = pygame.sprite.Group()
# 医院感染人和重症人
hos_infectmans = pygame.sprite.Group()
hos_illmans = pygame.sprite.Group()clock = pygame.time.Clock()# 时钟
delay = 100
historyfile = "source/record.txt"# 记录文件running = True# 判断运行状态
active = False# 开始游戏
waited = False# 是否等待
tolnum = 0# 总人数
goodnum = 0# 初始化健康人数
infectnum = 0#初始化感染人数
city_infectnum = 0# 初始化城市感染人数
hos_infectnum = 0 # 初始化医院感染人数
illnum = 0# 初始化重症人数
city_illnum = 0# 初始化城市重症人数
hos_illnum = 0# 初始化医院重症人数
curenum = 0# 初始化治愈人数
deadnum = 0# 初始化死亡人数
money = 0# 初始钱
doc_price = 1000# 医护售价
doc_num = 0# 医护数量
doc_cure = 5# 每个医生医治患者数量
pill_price = 2000# 药物售价
pill_level = 0# 药物等级
limit_price = 3000# 限制价格
limit_level = 0# 限制等级
protect_price = 4000# 防护价格
protect_num = 0# 全民保护天数
mlen = 20# 移动步长
getmoney = 6# 每人每天赚钱数
speed = 50# 游戏速度
protect_status = False# 保护状态,默认没保护
# goodtofm = [1, 2]# 轻症感染概率
# goodtoill = [1, 1]# 重症感染概率
fmtohos = [1, 50]# 轻症患者住院概率,1/5
fmtoill = [1, 100]# 轻症患者转重症概率,1/10
fmtocure = [1, 20]# 轻症治愈概率,1/10
illtohos = [1, 5]# 重症住院概率,1/5
illtodead = [1, 100]# 重症转死亡,1/5
illtofm = [1, 20]# 重症转轻症,1/5
record = []day = 1# 天数
time = 3# 每天移动次数
move = 0# 移动计数myfont = pygame.font.Font("source/楷体_GB2312.ttf", 20)# 使用楷体字体
myfont_big = pygame.font.Font("source/楷体_GB2312.ttf", 36)# 使用大字体
myfont_small = pygame.font.Font("source/楷体_GB2312.ttf", 12)# 使用小字体
# 标志是否暂停游戏
# paused = False
# paused_image = pygame.image.load("source/暂停.png").convert_alpha()
# resume_image = pygame.image.load("source/播放.png").convert_alpha()
# paused_rect = paused_image.get_rect()
# paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
# paused_show_image = paused_image# 主页
mained = False# 主页标志
main_image = pygame.image.load("source/主页.png").convert_alpha()
main_rect = main_image.get_rect()
main_rect.left, main_rect.top = width - main_rect.width - 10, 10# 主页面
start_image = pygame.image.load("source/开始游戏.png").convert_alpha()
start_rect = start_image.get_rect()
start_rect.left, start_rect.top = (width - start_rect.width) // 2, 380
gameover_image = pygame.image.load("source/结束游戏.png").convert_alpha()
gameover_rect = gameover_image.get_rect()
gameover_rect.left, gameover_rect.top = (width - gameover_rect.width) // 2, 500
goon_image = pygame.image.load("source/继续游戏.png").convert_alpha()
goon_rect = goon_image.get_rect()
goon_rect.left, goon_rect.top = (width - goon_rect.width) // 2, 380
restart_image = pygame.image.load("source/重新开始.png").convert_alpha()
restart_rect = restart_image.get_rect()
restart_rect.left, restart_rect.top = (width - restart_rect.width) // 2, 440
tolnum_add_image = pygame.image.load("source/增加.png").convert_alpha()
tolnum_add_rect = tolnum_add_image.get_rect()
tolnum_add_rect.left, tolnum_add_rect.top = 710, 300
tolnum_dec_image = pygame.image.load("source/减少.png").convert_alpha()
tolnum_dec_rect = tolnum_dec_image.get_rect()
tolnum_dec_rect.left, tolnum_dec_rect.top = 735, 300protect_start_image = pygame.image.load("source/开始保卫.png").convert_alpha()
protect_start_rect = protect_start_image.get_rect()
protect_start_rect.left, protect_start_rect.top = 180, 150
doc_buy_image = pygame.image.load("source/购买.png").convert_alpha()
doc_buy_rect = doc_buy_image.get_rect()
doc_buy_rect.left, doc_buy_rect.top = 310, 70
pill_buy_image = pygame.image.load("source/购买.png").convert_alpha()
pill_buy_rect = pill_buy_image.get_rect()
pill_buy_rect.left, pill_buy_rect.top = 410, 70
limit_buy_image = pygame.image.load("source/购买.png").convert_alpha()
limit_buy_rect = limit_buy_image.get_rect()
limit_buy_rect.left, limit_buy_rect.top = 310, 170
protect_buy_image = pygame.image.load("source/购买.png").convert_alpha()
protect_buy_rect = protect_buy_image.get_rect()
protect_buy_rect.left, protect_buy_rect.top = 410, 170
tip_text = myfont.render("对不起!您的资金不够!!", True, RED)
doc_dec_image = pygame.image.load("source/减少.png").convert_alpha()
doc_dec_rect = doc_dec_image.get_rect()
doc_dec_rect.left, doc_dec_rect.top = 75, 205
limit_dec_image = pygame.image.load("source/减少.png").convert_alpha()
limit_dec_rect = limit_dec_image.get_rect()
limit_dec_rect.left, limit_dec_rect.top = 75, 455
protect_dec_image = pygame.image.load("source/减少.png").convert_alpha()
protect_dec_rect = protect_dec_image.get_rect()
protect_dec_rect.left, protect_dec_rect.top = 75, 580winflag = False# 胜利标志
recorded = False# 是否有记录
lostflag = False# 失败标志
# 自定义计时事件
COUNT = pygame.USEREVENT
# 每隔1秒发送一次自定义事件
pygame.time.set_timer(COUNT, 1000)
goon = False# 默认新游戏
# 计时器,当前历时时间
counts = 0
# 计时标志
timer = False
if os.path.exists(historyfile):# 如果有记录
recorded = True
record = GetRecord(historyfile)
day = record[0]
tolnum = record[1]
goodnum = record[2]# 初始化健康人数
city_infectnum = record[3]# 初始化城市感染人数
hos_infectnum = record[4]# 初始化医院感染人数
city_illnum = record[5]# 初始化城市重症人数
hos_illnum = record[6]# 初始化医院重症人数
curenum = record[7]# 初始化治愈人数
deadnum = record[8]# 初始化死亡人数
money = record[9]# 初始钱
doc_num = record[10]# 医护数量
pill_level = record[11]# 药物等级
limit_level = record[12]# 限制等级
protect_num = record[13]# 全民保护天数
infectnum = city_infectnum + hos_infectnum# 初始化感染人数
illnum = city_illnum + hos_illnum# 初始化重症人数
else:# 没有记录时
recorded = False
ShowBack()# 显示背景
screen.blit(mycity, (500, 0))# 载入城市图片
screen.blit(hospital_image, (100, 200))# 载入医院图片
# 初始化组
InitCity(goodmans, good_man_image, goodnum)# 初始化健康人数
InitCity(infectmans, infect_man_image, city_infectnum)# 初始化城市感染轻症人数
InitCity(illmans, ill_man_image, city_illnum)# 初始化城市感染重症人数
InitHospital(hos_infectmans, infect_man_image, hos_infectnum)# 初始化医院感染轻症人数
InitHospital(hos_illmans, ill_man_image, hos_illnum)# 初始化医院感染轻症人数
AddtoCure(curemans, curenum)# 初始化治愈患者
AddtoDead(deadmans, deadnum)# 初始化病亡患者
if tolnum == 0:
tolnum =500
# 记录文件
while running:
for event in pygame.event.get():
if event.type == QUIT:# 退出
# 写入记录文件
record = [day, tolnum, goodnum, city_infectnum, hos_infectnum, city_illnum, hos_illnum,\
curenum, deadnum, money, doc_num, pill_level, limit_level, protect_num]
# 确保建立文件
with open(historyfile, mode='w', encoding='utf-8') as f:
f.close()
PutRecord(historyfile, record)
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 1:# 鼠标左键按下
# 按下主页键
if main_rect.collidepoint(event.pos):# 是否主页
mained = True
active = False
waited = True
if mained:
pygame.mixer.music.pause()# 背景音乐暂停
pygame.mixer.pause()# 音效暂停
elif start_rect.collidepoint(event.pos) and not goon:# 开始游戏
# print(goon)
active = True# 开始游戏
winflag = False# 重置
lostflag = False# 重置
mained = False# 重置
waited = False
paused = False
pygame.mixer.music.unpause()# 背景音乐取消暂停
pygame.mixer.unpause()# 音效取消暂停
# 初始化游戏
day = 1
goodnum = tolnum - 1
city_infectnum = 1
InitCity(goodmans, good_man_image, goodnum)# 初始化健康人数
InitCity(infectmans, infect_man_image, city_infectnum)# 初始化城市感染轻症人数
InitCity(illmans, ill_man_image, city_illnum)# 初始化城市感染重症人数
InitHospital(hos_infectmans, infect_man_image, hos_infectnum)# 初始化医院感染轻症人数
InitHospital(hos_illmans, ill_man_image, hos_illnum)# 初始化医院感染轻症人数
AddtoCure(curemans, curenum)# 初始化治愈患者
AddtoDead(deadmans, deadnum)# 初始化病亡患者
elif goon_rect.collidepoint(event.pos) and goon:# 继续游戏
active = True
waited = False
mained = False
elif restart_rect.collidepoint(event.pos):# 重新开始
recorded = False
active = False
mained = False
# 重置
day = 0# 天数
tolnum = 500# 总人数
goodnum = 0# 初始化健康人数
city_infectnum = 0# 初始化城市感染人数
hos_infectnum = 0# 初始化医院感染人数
city_illnum = 0# 初始化城市重症人数
hos_illnum = 0# 初始化医院重症人数
curenum = 0# 初始化治愈人数
deadnum = 0# 初始化死亡人数
money = 0# 初始钱
doc_num = 0# 医护数量
pill_level = 0# 药物等级
limit_level = 0# 限制等级
protect_num = 0# 全民保护天数
Clear(goodmans)
Clear(infectmans)
Clear(illmans)
Clear(curemans)
Clear(deadmans)
Clear(hos_infectmans)
Clear(hos_illmans)
elif gameover_rect.collidepoint(event.pos):# 结束游戏游戏
# 写入记录文件
record = [day, tolnum, goodnum, city_infectnum, hos_infectnum, city_illnum, hos_illnum, \
curenum, deadnum, money, doc_num, pill_level, limit_level, protect_num]
# 确保建立文件
with open(historyfile, mode='w', encoding='utf-8') as f:
f.close()
PutRecord(historyfile, record)
pygame.quit()
sys.exit()
# 开始保卫
elif protect_start_rect.collidepoint(event.pos) and waited:
waited = False# 运动
move = 0# 重置
# 购买医护
elif doc_buy_rect.collidepoint(event.pos) and waited:
if money >= doc_price:
doc_num += 1
money -= doc_price
else:
screen.blit(tip_text, (510, 10))
#g.msgbox("对不起,您的资金不够!")# 购买药物研究
elif pill_buy_rect.collidepoint(event.pos) and waited:
if money >= pill_price:
pill_level += 1
money -= pill_price
illtodead[1] += int(illtodead[1] / 10)# 死亡率降低
fmtoill[1] += int(fmtoill[1] / 10)# 重症率降低
if illtofm[0] < illtofm[1]:
illtofm[0] += int(fmtoill[1] / 10)# 转轻率提高
if fmtocure[0] < fmtocure[1]:
fmtocure[0] += int(fmtocure[1] / 10)# 治愈率提高
# print(illtodead, fmtoill, illtofm, fmtocure)
else:
# g.msgbox("对不起,您的资金不够!")
screen.blit(tip_text, (510, 10))
# 购买限制投入
elif limit_buy_rect.collidepoint(event.pos) and waited:
if money >= limit_price and getmoney > 0 and mlen > 0:
getmoney -= 1# 收入降低
limit_level += 1# 限制等级提高
mlen -= 5
money -= limit_price
else:
#g.msgbox("对不起,您的资金不够!")
screen.blit(tip_text, (510, 10))
# 购买保护
elif protect_buy_rect.collidepoint(event.pos) and waited:
if money >= protect_price:
protect_num += 1
money -= protect_price
protect_status = True
else:
#g.msgbox("对不起,您的资金不够!")
screen.blit(tip_text, (510, 10))
# 减少医护
elif doc_dec_rect.collidepoint(event.pos) and waited:
if doc_num > 0:
doc_num -= 1
money += doc_price
# 减少限制
elif limit_dec_rect.collidepoint(event.pos) and waited:
if limit_level > 0:
limit_level -= 1
mlen += 5
getmoney += 1
# 减少保护
elif protect_dec_rect.collidepoint(event.pos) and waited:
if protect_num > 0:
protect_num -= 1
money += protect_price
# 增加总人数
elif tolnum_add_rect.collidepoint(event.pos):
if tolnum < 1000:
tolnum += 100
# 减少总人数
elif tolnum_dec_rect.collidepoint(event.pos):
if tolnum > 0:
tolnum -= 100if not (delay % speed) and move < time and active and not waited:# 每5次移动一下
screen.blit(mycity, (500, 0))# 载入城市图片
screen.blit(hospital_image, (100, 200))# 载入医院图片
move += 1
for gm in goodmans:# 显示健康人
gm.move(mlen)
screen.blit(gm.image, gm.rect)
for fm in infectmans:# 显示感染人
fm.move(mlen)
screen.blit(fm.image, fm.rect)
# 是否感染健康人
isinfect = pygame.sprite.spritecollide(fm, goodmans, False, pygame.sprite.collide_mask)
if isinfect and not protect_status:# and randint(0, goodtofm[1]) < goodtofm[0]:# 感染且没保护
for each in isinfect:
goodmans.remove(each)
each.image = infect_man_image
infectmans.add(each)
screen.blit(each.image, each.rect)
# 是否重症
if randint(0, fmtoill[1]) < fmtoill[0]:
infectmans.remove(fm)
fm.image = ill_man_image
illmans.add(fm)
screen.blit(fm.image, fm.rect)
# 是否进医院,每个医护可医治4个患者
if randint(0, fmtohos[1]) < fmtohos[0] and (len(hos_infectmans) + len(hos_illmans) < doc_num * doc_cure):
infectmans.remove(fm)
AddtoHospital(hos_infectmans, infect_man_image)for im in illmans:# 显示重症人
im.move(mlen)
screen.blit(im.image, im.rect)
# 是否感染健康人
isinfect = pygame.sprite.spritecollide(im, goodmans, False, pygame.sprite.collide_mask)
if isinfect and not protect_status:# and randint(0, goodtoill[1]) < goodtoill[0]:# 感染且没保护
for each in isinfect:
goodmans.remove(each)
each.image = infect_man_image
infectmans.add(each)
screen.blit(each.image, each.rect)
# 是否住院,每个医护医治4个患者
if randint(0, illtohos[1]) < illtohos[0] and (len(hos_infectmans) + len(hos_illmans) < doc_num * doc_cure):
illmans.remove(im)
AddtoHospital(hos_illmans, ill_man_image)
continue
# 是否死亡
rate = randint(0, illtodead[1])
if rate < illtodead[0]:
# print(rate, illtodead[0])
illmans.remove(im)
AddtoDead(deadmans, 1)for hfm in hos_infectmans:# 医院轻症患者
screen.blit(hfm.image, hfm.rect)
# 是否治愈
if randint(0, fmtocure[1]) < fmtocure[0]:
hos_infectmans.remove(hfm)
AddtoCure(curemans, 1)
money += 100# 治愈一人赚100元
continue
# 是否转重症
if randint(0, fmtoill[1]) < fmtoill[0]:
hos_infectmans.remove(hfm)
hfm.image = ill_man_image
hos_illmans.add(hfm)
screen.blit(hfm.image, hfm.rect)for him in hos_illmans:# 医院重症患者
screen.blit(him.image, him.rect)
# 是否转轻症
if randint(0, illtofm[1]) < illtofm[0]:
hos_illmans.remove(him)
him.image = infect_man_image
hos_infectmans.add(him)
screen.blit(him.image, him.rect)
continue
# 是否死亡
rate = randint(0, illtodead[1])
if rate < illtodead[0]:
# print(rate)
hos_illmans.remove(him)
AddtoDead(deadmans, 1)for cm in curemans:# 治愈患者
screen.blit(cm.image, cm.rect)for dm in deadmans:# 死亡患者
screen.blit(dm.image, dm.rect)elif move == time:
success_sound.play()
move = 0# 重新计数
day += 1# 增加一天
waited = True
# 未入院人员每人每天赚5元
money += (len(goodmans) + len(infectmans) + len(illmans)) * getmoney
if protect_num > 0:
protect_num -= 1
protect_status = True
else:
protect_status = False
if (infectnum + illnum) == 0 and goodnum > 0:
win_sound.play()
active = False
mained = True
winflag = True
elif (infectnum + illnum) == 0 and goodnum == 0:
lostflag = True# 显示得分情况
# screen.blit(massage_image, (0, 0))# 载入信息图片
ShowBack()# 显示背景
goodnum = len(goodmans)# 健康人数
infectnum = len(infectmans) + len(hos_infectmans)# 轻症患者
illnum = len(illmans) + len(hos_illmans)# 重症患者
curenum = len(curemans)# 治愈人数
deadnum = len(deadmans)# 死亡人数
day_text = myfont_big.render(f"{str(day)}", True, RED)
screen.blit(day_text, (75, 10))
tolnum_text = myfont.render(f"{str(tolnum)}", True, BLACK)
screen.blit(tolnum_text, (65, 60))
goodnum_text = myfont.render(f"{str(goodnum)}", True, BLACK)
screen.blit(goodnum_text, (75, 83))
infectnum_text = myfont.render(f"{str(infectnum)}", True, BLACK)
screen.blit(infectnum_text, (75, 106))
illnum_text = myfont.render(f"{str(illnum)}", True, BLACK)
screen.blit(illnum_text, (75, 129))
curenum_text = myfont.render(f"{str(curenum)}", True, BLACK)
screen.blit(curenum_text, (75, 152))
deadnum_text = myfont.render(f"{str(deadnum)}", True, BLACK)
screen.blit(deadnum_text, (75, 175))
money_text = myfont.render(f"{str(money)}", True, RED)
screen.blit(money_text, (210, 18))
screen.blit(protect_start_image, protect_start_rect)# 开始按钮
# 显示道具价格
doc_price_text = myfont.render(f"{str(doc_price)}", True, ORANGE)
screen.blit(doc_price_text, (320, 3))
pill_price_text = myfont.render(f"{str(pill_price)}", True, ORANGE)
screen.blit(pill_price_text, (420, 3))
limit_price_text = myfont.render(f"{str(limit_price)}", True, ORANGE)
screen.blit(limit_price_text, (320, 103))
protect_price_text = myfont.render(f"{str(protect_price)}", True, ORANGE)
screen.blit(protect_price_text, (420, 103))
# 显示购买
screen.blit(doc_buy_image, doc_buy_rect)
screen.blit(pill_buy_image, pill_buy_rect)
screen.blit(limit_buy_image, limit_buy_rect)
screen.blit(protect_buy_image, protect_buy_rect)
# 显示道具数量
doc_num_text = myfont.render(f"{str(doc_num)}", True, RED)
screen.blit(doc_num_text, (5, 205))
pill_level_text = myfont.render(f"{str(pill_level)}", True, RED)
screen.blit(pill_level_text, (5, 330))
limit_level_text = myfont.render(f"{str(limit_level)}", True, RED)
screen.blit(limit_level_text, (5, 455))
protect_num_text = myfont.render(f"{str(protect_num)}", True, RED)
screen.blit(protect_num_text, (5, 580))
screen.blit(doc_dec_image, doc_dec_rect)
screen.blit(limit_dec_image, limit_dec_rect)
screen.blit(protect_dec_image, protect_dec_rect)
if waited:
wait_text = myfont.render("请部署您的防护!", True, RED)
screen.blit(wait_text, (140, 85))# 暂停/播放
# screen.blit(paused_show_image, paused_rect)# 暂停图片
# 主页
screen.blit(main_image, main_rect)# 主页图片if not active:# 刚启动时
# 主页
pygame.draw.rect(screen, GRAY, (400, 100, 400, 500), 0)# 画矩形,位置400,100,长400,高500
# 显示最快纪录和当前纪录
tolnum_record_text = myfont.render(f"您的城市人口为:{tolnum} 人", True, RED)
day_record_text = myfont.render(f"您已守护:{day} 天", True, RED)
goodnum_record_text = myfont.render(f"健康人数为:{goodnum} 人", True, RED)
infectnum_record_text = myfont.render(f"轻症患者有:{infectnum} 人", True, RED)
illnum_record_text = myfont.render(f"重症患者有:{illnum} 人", True, RED)
curenum_record_text = myfont.render(f"治愈人数有:{curenum} 人", True, RED)
deadnum_record_text = myfont.render(f"病亡人数有:{deadnum} 人", True, RED)
record_text = myfont.render("", True, RED)
if not recorded and not mained:# 没有记录,初始进入
goon = False
wel_text = myfont_big.render("欢迎进入保卫城市游戏!", True, RED)
screen.blit(wel_text, (410, 200))
screen.blit(tolnum_record_text, (480, 300))
# 增加和减少
screen.blit(tolnum_add_image, tolnum_add_rect)
screen.blit(tolnum_dec_image, tolnum_dec_rect)
# 开始游戏
screen.blit(start_image, start_rect)
elif recorded and not mained:# 读取记录
goon = True
record_text = myfont.render("您上次的游戏进度为:", True, RED)
screen.blit(record_text, (480, 140))
screen.blit(tolnum_record_text, (480, 170))
screen.blit(day_record_text, (480, 200))
screen.blit(goodnum_record_text, (480, 230))
screen.blit(infectnum_record_text, (480, 260))
screen.blit(illnum_record_text, (480, 290))
screen.blit(curenum_record_text, (480, 320))
screen.blit(deadnum_record_text, (480, 350))
# 继续游戏
screen.blit(goon_image, goon_rect)
# 重新开始
screen.blit(restart_image, restart_rect)
elif mained:
if not winflag and not lostflag:# 点击主页
record_text = myfont.render("您当前的游戏进度为:", True, RED)
# 继续游戏
screen.blit(goon_image, goon_rect)
elif winflag:# 胜利
record_text = myfont.render("恭喜你!消灭所有病毒!!", True, RED)
elif lostflag:# 失败
record_text = myfont.render("很遗憾!未能消灭病毒!!", True, RED)
goon = True
screen.blit(record_text, (480, 140))
screen.blit(tolnum_record_text, (480, 170))
screen.blit(day_record_text, (480, 200))
screen.blit(goodnum_record_text, (480, 230))
screen.blit(infectnum_record_text, (480, 260))
screen.blit(illnum_record_text, (480, 290))
screen.blit(curenum_record_text, (480, 320))
screen.blit(deadnum_record_text, (480, 350))
# 重新开始
screen.blit(restart_image, restart_rect)# 结束游戏
screen.blit(gameover_image, gameover_rect)# 时间间隔
delay -= 1
if not delay:
delay = 50
pygame.display.flip()# 页面刷新clock.tick(60)if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
总结 这款《消灭病毒保卫城市》小游戏界面还是写挺好,然后效果也展示的很清楚,大家可以自己试
试!看看你能坚持多久?!
完整的免费源码领取处:找我吖!文末可得自行领取,滴滴我也可!
往期推荐阅读——
项目6.0中国象棋游戏
Pygame实战:中国象棋人机对抗赛今开战、谁占上风?要不要来一盘试试?
项目5.3 《鱿鱼游戏》之《一二三木头人》小游戏
当《鱿鱼游戏》强势来袭:“一、二、三,木头人,思密达。”你还在恐惧嘛?
项目4.9天天酷跑小游戏
《天天酷跑》是不是真的不火了?Python发布“酷跑+”计划。
项目1.1扫雷
Pygame实战:据说这是史上最难扫雷游戏,没有之一,你们感受下......
文章汇总—— 项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了
(更多内容+源码都在文章汇总哦!!欢迎阅读~)
文章图片
推荐阅读
- Pygame合集|Pygame实战(中国象棋人机对抗赛今开战、谁占上风(要不要来一盘试试?))
- 程序员合集|【Pygame实战】如果你是赛车爱好者(这款新赛车游戏分分钟让你上瘾(超跑又是谁的梦想()))
- Pygame合集|【普通玩家VS高手玩家】一带传奇游戏《俄罗斯方块儿》新作—实现AI自动玩游戏~
- python合集|Pygame实战(BOOM 这有一款超刺激的扎气球游戏等你来玩~)
- 界面化小程序|【免费测试】姓名测算性别—仅需这几步即可(来看程序员是如何解决的(爱了爱了~))
- python合集|当《鱿鱼游戏》强势来袭(“一、二、三,木头人,思密达。”你还在恐惧嘛())
- 程序员合集|【Pygame实战】经典的坦克大战游戏,勾起童年无限回忆《坦克大战小霸王版》