你是否还记得有一款游戏叫做“魔塔”?

Posted YF云飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了你是否还记得有一款游戏叫做“魔塔”?相关的知识,希望对你有一定的参考价值。

目录

前言

正文

游戏介绍:

游戏开发制作流程 

1.收集素材

2.创建攻击函数

2.1 定义两个对象(主角和怪物)

2.2 函数输出为【0】表示打不过,胜利输出受损生命值,设置 cancel 可以撤销本次战斗即回到之前状态

2.3 进行战斗结果

2.4 设置主函数,使一些人物属性,怪物等提示信息显示

2.5 显示人物与怪物得初始位置

3.创建魔塔函数

3.1 初始化设置

3.2 加载素材

3.3 背景音乐

3.4 一些音效:打怪,开门,通过等

3.5 等待 1 秒让 mixer 完成初始化

3.6 加载字体

3.7 最上面一格

3.8 加载属性框,包括矩形的四个参数 left,top,width,height

3.9 获得钥匙图片所在得位置得矩形对象

3.10 人物类

3.11 一些其它判断机制

3.12 怪物类

3.13 铺地板:范围从(200,50)到(700,550)

3.14 墙壁的位置(矩形左上角(left,top))

3.15 门的位置

3.16 宝物的位置

3.17 怪物的位置

3.18 门,楼梯,宝物

3.19 创建矩形对象的列表——用于判断所遇到的事件

3.20 将怪物放入列表

3.21 键盘控制

3.22 设置一个简单外挂,鼠标点击左键钥匙 + 1 ,右键钥匙 - 1,中键归 0

3.23 电梯功能

3.24 开始画地图

3.25 对于列表中碰撞以后的元素

3.26 红血瓶

3.27 蓝血瓶

3.28 战斗事件

3.29 侧边栏背景

3.30 任务栏

3.31 属性栏

3.32 钥匙数量

3.33 物品栏

3.34 如果获得则在右侧边栏加上该宝物

3.35 属性栏

代码参考

fight.py

TowerTest.py

总结

一点回忆

“作者”说

是的,时间已经过去


14天学习训练营导师课程:
李宁《Python Pygame游戏开发入门与实战》
李宁《计算机视觉OpenCV Python项目实战》1
李宁《计算机视觉OpenCV Python项目实战》2
李宁《计算机视觉OpenCV Python项目实战》3


前言

        近期看到好几篇DOS系统的技术分享博客,我也顺路去搜集了一些相关资料。突发奇想地,我也去搜集了一些DOS游戏。在收集游戏的过程中,当我在 RetroGame 网上看到题图的那一瞬间,我仿佛回到了小学,那段手握学习机,痴迷的计算自己数值路线,思索自己到第能不能打赢面前这只可恶的红蝙蝠的快乐时光。

        我终于回想起了它的名字——《魔塔》。

        大多数人最初接触的魔塔游戏都是胖老鼠——基于Flash技术将原作改编为21/24层的汉化魔塔——上传到在线游戏网站的那一版,没错,就是那款被各路厂商盗用后广为流传的经典魔塔。我第一次接触到的也是这个版本。

        作为第一部(仅)有中文的魔塔,胖老鼠魔塔可以说创造了一代的经典——固定的数值设计,简单明了的道具系统,可操作性极佳的成长方法,都令玩家轻易的沉迷其中。是的,这就是这个游戏与其后续衍生作品中最独特的地方:固定数据的RPG游戏。

        而最近又恰巧没有什么好的pygame练手的项目,索性这一次就把这一款童年经典尝试做一个Demo出来吧!


正文

游戏介绍:

        一开始名为cxk的玩家角色有 100 的生命值,10 的攻击力,10 的防御力。玩家可以通过获得相应颜色钥匙来打开相应颜色的门,随后攻击鬼怪来获取生命值和金币。药水可以提升生命值,蓝色宝石可以提升防御力,红色宝石可以提升攻击力,直至通过第一层。


游戏开发制作流程 

本着“自顶向下”的设计思路,我将其大致分为了三个流程:

1. 收集素材(背景,玩家,敌人,宝石)

2. 创建攻击函数

3. 创建魔塔函数


1.收集素材

        百度搜索所需的素材,并通过 ps 来进行简单的处理,此处略。


2.创建攻击函数

        作为一款RPG游戏,最直观且重要的肯定是游戏的逻辑对象。

        虽然魔塔的界面让我们感觉它是一款简单的地牢游戏,貌似随便打打杀杀就可以过关,但事实上玩这个游戏需要动很多脑筋,任何一个轻率的选择都可能导致游戏的失败,该游戏有属性攻击、防御、生命、金币、经验。对怪物的伤害次数计算公式,是敌人的生命/(自己的攻击-敌人的防御);而伤害的数目计算是怪物伤害次数(敌人的攻击-自己的防御)。

        我们先来实现它!

2.1 定义两个对象(主角和怪物)

def fight(A,B,cancel=0):
    a=A.attack-B.defend
    b=B.attack-A.defend

2.2 函数输出为【0】表示打不过,胜利输出受损生命值,设置 cancel 可以撤销本次战斗即回到之前状态

if a<=0:
    return [0]
if a>0 and b<=0:
    if(cancel==0):
        A.gold+=B.gold
    if(cancel!=0):
        A.life=alife
        B.life=blife
    return 0

2.3 进行战斗结果

for i in range(1,100):
    B.life=B.life-a
    if B.life<0:
        if(cancel==0):
            B.life=blife
            A.gold+=B.gold
        if(cancel!=0):
            A.life=alife
            B.life=blife
        return alife-A.life
    A.life=A.life-b
    if A.life<0:
        A.life=alife
        B.life=blife
        return [0]

2.4 设置主函数,使一些人物属性,怪物等提示信息显示

if __name__=="__main__":
    class character:
        def __init__(self,tuple):
            self.attack=tuple[0]
            self.defend=tuple[1]
            self.life=tuple[2]
            self.gold=0
        def __str__(self):
            return u"攻击力:%d ,防御力:%d, 生命值:%d,金币:  %d"%(self.attack,self.defend,self.life,self.gold)


    class monster:
        def __init__(self,tuple):
            self.attack=tuple[0]
            self.defend=tuple[1]
            self.life=tuple[2]
            self.gold=tuple[3]

        def __str__(self):
            return u"攻击力:%d ,防御力:%d, 生命值:%d,"%(self.attack,self.defend,self.life)

2.5 显示人物与怪物得初始位置

CAIXUKUN=character((10,10,100)) //不是小黑子doge
gSlime_=monster((11,5,10,1))
rSlime_=monster((12,5,10,2))
sBat_=monster((13,5,10,3))
bWizard_=monster((15,5,10,5))
Skeleton_=monster((18,5,10,8))
scSkeleton_=monster((25,5,10,10))

BOSS=monster((100,5,10,10))

3.创建魔塔函数

        此外,我们辛苦白嫖(剽窃doge)来的魔塔素材也得派上用场啊!接下来完成魔塔函数。

3.1 初始化设置

pygame.init()
pygame.mixer.init()

3.2 加载素材

#环境
wall=pygame.transform.scale(pygame.image.load('w1.png').convert_alpha(),(50,50))
ground=pygame.transform.scale(pygame.image.load('w2.png').convert_alpha(),(50,50))
yellow_door=pygame.transform.scale(pygame.image.load('d1.png').convert_alpha(),(50,50))
blue_door=pygame.transform.scale(pygame.image.load('d2.png').convert_alpha(),(50,50))
red_door=pygame.transform.scale(pygame.image.load('d3.png').convert_alpha(),(50,50))
special_door=pygame.transform.scale(pygame.image.load('d4.png').convert_alpha(),(50,50))
ykey=pygame.transform.scale(pygame.image.load('ykey.png').convert_alpha(),(50,50))
bkey=pygame.transform.scale(pygame.image.load('bkey.png').convert_alpha(),(50,50))
rkey=pygame.transform.scale(pygame.image.load('rkey.png').convert_alpha(),(50,50))
rbottle=pygame.transform.scale(pygame.image.load('rbottle.png').convert_alpha(),(50,50))
bbottle=pygame.transform.scale(pygame.image.load('bbottle.png').convert_alpha(),(50,50))
rgem=pygame.transform.scale(pygame.image.load('rgem.png').convert_alpha(),(50,50))
bgem=pygame.transform.scale(pygame.image.load('bgem.png').convert_alpha(),(50,50))
upstair=pygame.transform.scale(pygame.image.load('upstair.png').convert_alpha(),(50,50))
downstair=pygame.transform.scale(pygame.image.load('downstair.png').convert_alpha(),(50,50))

#怪物
rSlime=pygame.transform.scale(pygame.image.load('rSlime.png').convert_alpha(),(50,50))
gSlime=pygame.transform.scale(pygame.image.load('gSlime.png').convert_alpha(),(50,50))
bSlime=pygame.transform.scale(pygame.image.load('bSlime.png').convert_alpha(),(50,50))
kSlime=pygame.transform.scale(pygame.image.load('kSlime.png').convert_alpha(),(50,50))
sBat=pygame.transform.scale(pygame.image.load('sBat.png').convert_alpha(),(50,50))
bBat=pygame.transform.scale(pygame.image.load('bBat.png').convert_alpha(),(50,50))
rBat=pygame.transform.scale(pygame.image.load('rBat.png').convert_alpha(),(50,50))
bWizard=pygame.transform.scale(pygame.image.load('bWizard.png').convert_alpha(),(50,50))
rWizard=pygame.transform.scale(pygame.image.load('rWizard.png').convert_alpha(),(50,50))
Skeleton=pygame.transform.scale(pygame.image.load('Skeleton.png').convert_alpha(),(50,50))
scSkeleton=pygame.transform.scale(pygame.image.load('scSkeleton.png').convert_alpha(),(50,50))
bcSkeleton=pygame.transform.scale(pygame.image.load('bcSkeleton.png').convert_alpha(),(50,50))
ghost=pygame.transform.scale(pygame.image.load('ghost.png').convert_alpha(),(50,50))
lGuard=pygame.transform.scale(pygame.image.load('lGuard.png').convert_alpha(),(50,50))
mGuard=pygame.transform.scale(pygame.image.load('mGuard.png').convert_alpha(),(50,50))
hGuard=pygame.transform.scale(pygame.image.load('hGuard.png').convert_alpha(),(50,50))
lWitch=pygame.transform.scale(pygame.image.load('lWitch.png').convert_alpha(),(50,50))
hWitch=pygame.transform.scale(pygame.image.load('hWitch.png').convert_alpha(),(50,50))
Orc=pygame.transform.scale(pygame.image.load('Orc.png').convert_alpha(),(50,50))
wOrc=pygame.transform.scale(pygame.image.load('wOrc.png').convert_alpha(),(50,50))
Stone=pygame.transform.scale(pygame.image.load('Stone.png').convert_alpha(),(50,50))
Swordsman=pygame.transform.scale(pygame.image.load('Swordsman.png').convert_alpha(),(50,50))
wGhost=pygame.transform.scale(pygame.image.load('wGhost.png').convert_alpha(),(50,50))
Vampier=pygame.transform.scale(pygame.image.load('Vampier.png').convert_alpha(),(50,50))

#神秘人和宝物
guide=pygame.transform.scale(pygame.image.load('guide.png').convert_alpha(),(50,50))
merchant=pygame.transform.scale(pygame.image.load('merchant.png').convert_alpha(),(50,50))
princess=pygame.transform.scale(pygame.image.load('princess.png').convert_alpha(),(50,50))
lift=pygame.transform.scale(pygame.image.load('lift.png').convert_alpha(),(50,50))

#商店
shop=pygame.transform.scale(pygame.image.load('shop.png').convert_alpha(),(150,50))

3.3 背景音乐

pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play()
musicPlaying=False

3.4 一些音效:打怪,开门,通过等

sound0=pygame.mixer.Sound('0.wav')
sound1=pygame.mixer.Sound('1.wav')
sound2=pygame.mixer.Sound('2.wav')
sound3=pygame.mixer.Sound('3.wav')
sound4=pygame.mixer.Sound('4.wav')
sound5=pygame.mixer.Sound('5.wav')
sound6=pygame.mixer.Sound('6.wav')

3.5 等待 1 秒让 mixer 完成初始化

pygame.time.delay(1000)

3.6 加载字体

        这些个都可以自己选哈。

my_FontCH=pygame.font.Font('【春田】小火锅安卓.ttf',30)
my_FontCHs=pygame.font.Font('方正正中黑简体.TTF',30)
my_FontEN=pygame.font.Font('KeeponTruckin.ttf',30)

3.7 最上面一格

text_surface0=my_FontCH.render('小游戏',True,(0,0,0),(191,191,191))

3.8 加载属性框,包括矩形的四个参数 left,top,width,height

att_rect1=pygame.Rect(0,50,200,600)
att_rect2=pygame.Rect(750,50,200,600)
att_rect3=pygame.Rect(200,600,550,50)
att_rect4=pygame.Rect(0,0,950,50)

3.9 获得钥匙图片所在得位置得矩形对象

ykey_rect=ykey.get_rect()
ykey_rect.left,ykey_rect.top=0,400
bkey_rect=bkey.get_rect()
bkey_rect.left,bkey_rect.top=0,450
rkey_rect=rkey.get_rect()
rkey_rect.left,rkey_rect.top=0,500

3.10 人物类

class character:
    def __init__(self,tuple):
        self.floor=1
        self.attack=tuple[0]
        self.defend=tuple[1]
        self.life=tuple[2]
        self.gold=0
        #物品栏
        self.property='Yellow_key':0,'Blue_key':0,'Red_key':0
        #图像
        self.image=pygame.transform.scale(pygame.image.load('character.png').convert_alpha(),(50,50))
        self.bg=pygame.transform.scale(pygame.image.load('w2.png').convert_alpha(),(50,50))
        self.image_rect=self.image.get_rect()
        self.image_rect.left=450
        self.image_rect.top=550

3.11 一些其它判断机制

3.11-1 门在没有对应钥匙时不可通过

if str=="UP":
    FORBID=0
    rect_new=self.image_rect.copy()
    rect_new.move_ip(0,-50)

3.11-2 判断是否撞墙

for wall_ in wallL[:]:
    if rect_new.colliderect(wall_):
        FORBID=1

3.11-3 检测是否在没有钥匙时通过门

for y_door in ydoorL[:]:
    if rect_new.colliderect(y_door) and self.property["Yellow_key"]==0:
        FORBID=1
for b_door in bdoorL[:]:
    if rect_new.colliderect(b_door) and self.property["Blue_key"]==0:
        FORBID=1
for r_door in rdoorL[:]:
    if rect_new.colliderect(r_door) and self.property["Red_key"]==0:
        FORBID=1

3.11-4 打不过怪时不可通过

for monster_ in list_morect[:]:
    if rect_new.colliderect(monster_):
        x=list_morect.index(monster_)
        if(fight(CAIXUKUN,eval(list_moname[x]+"_"),1))==[0]:
            sound5.play()
            FORBID=1
if FORBID==1:
    pass

3.11-5 判断是否出界

else:
    if self.image_rect.top<100:
        pass
    else:
        self.image_rect.move_ip(0,-50)

3.12 怪物类

class monster:
    def __init__(self,tuple):
        self.attack=tuple[0]
        self.defend=tuple[1]
        self.life=tuple[2]
        self.gold=tuple[3]

3.13 铺地板:范围从(200,50)到(700,550)

ground_L=[]
for i in range(4,14+1):
    for j in range(1,11+1):
        ground_L.append((50*i,50*j))

3.14 墙壁的位置(矩形左上角(left,top))

wall_L=[(200,100),(250,100),(300,100),(350,100),(400,100),(450,100),(500,100),(550,100),(600,100),(650,100),(450,150),(450,200),(450,250),(450,350),
        (500,250),(550,250),(650,250),(650,150),(650,200),(600,350),(550,350),(500,350),(650,350),(650,300),(350,200),(350,250),(350,300),(350,350),
        (350,400),(350,450),(350,500),(350,550),(300,250),(200,250),(300,400),(200,400),(400,450),(550,450),(600,450),
        (500,450),(550,500),(550,550),(700,450)]

3.15 门的位置

dict_door=(350,150):'yellow',(250,250):'blue',(600,250):'yellow',(450,300):'yellow',(250,400):'yellow',(450,450):'yellow',(450,450):'yellow',
           (650,450):'red',(600,250):'red'

3.16 宝物的位置

dict_trea=(550,150):'ykey',(200,350):'ykey',(300,500):'ykey',(400,500):'ykey',(400,550):'bkey',(400,250):'rkey',(600,150):'rkey',(400,200):'ykey',(200,150):'ykey',(300,550):'ykey',(200,150):'rbottle',(100,350):'rbottle',(500,150):'rgem',(500,200):'bgem',(550,200):'rbottle',(200,550):'rbottle',(200,500):'bbottle',(650,550):'bbottle',(650,500):'bgem',
#楼梯

3.17 怪物的位置

dict_monster=(300,50):'gSlime',(400,50):'gSlime',(600,550):'sBat',(700,550):'sBat',(350,50):'gSlime',(500,300):'rSlime',(500,300):'rSlime',
(250,300):'hWitch',(250,450):'ghost',(550,300):'ghost',(600,300):'rSlime'

3.18 门,楼梯,宝物

List_temp1=[k for k,v in dict_door.items() if v=='yellow']
List_temp2=[k for k,v in dict_stair.items() if v=='up']
List_temp3=[k for k,v in dict_trea.items() if v=='rbottle']
List_temp4=[k for k,v in dict_trea.items() if v=='bbottle']
List_temp5=[k for k,v in dict_trea.items() if v=='rgem']
List_temp6=[k for k,v in dict_trea.items() if v=='bgem']
List_temp7=[k for k,v in dict_trea.items() if v=='ykey']
List_temp8=[k for k,v in dict_door.items() if v=='blue']
List_temp9=[k for k,v in dict_trea.items() if v=='bkey']
List_temp10=[k for k,v in dict_door.items() if v=='red']
List_temp11=[k for k,v in dict_trea.items() if v=='rk']

3.19 创建矩形对象的列表——用于判断所遇到的事件

def build_List(LIST1,LIST2):
    for i in range(1,len(LIST2)+1):
        LIST1.append(pygame.Rect(LIST2[i-1][0],LIST2[i-1][1],50,50))
wallL=[]
build_List(wallL,wall_L)

ydoorL=[]
build_List(ydoorL,List_temp1)

bdoorL=[]
build_List(bdoorL,List_temp8)

rdoorL=[]
build_List(rdoorL,List_temp10)

stairupL=[]
build_List(stairupL,List_temp2)

rbottleL=[]
build_List(rbottleL,List_temp3)

bbottleL=[]
build_List(bbottleL,List_temp4)

rgemL=[]
build_List(rgemL,List_temp5)

bgemL=[]
build_List(bgemL,List_temp6)

ykeyL=[]
build_List(ykeyL,List_temp7)

bkeyL=[]
build_List(bkeyL,List_temp9)

rkeyL=[]
build_List(rkeyL,List_temp11)

3.20 将怪物放入列表

guideL=[]
guideL.append(pygame.Rect(Guide[0][0],Guide[0][1],50,50))

liftL=[]
liftL.append(pygame.Rect(Lift[0][0],Lift[0][1],50,50))

3.21 键盘控制

for event in pygame.event.get():
    if event.type==pygame.QUIT:
        sys.exit()
    elif event.type==pygame.KEYDOWN:
        if event.key==pygame.K_w:
            CAIXUKUN.move("UP")
        if event.key == pygame.K_s:
            CAIXUKUN.move("DOWN")
        if event.key==pygame.K_a:
            CAIXUKUN.move("LEFT")
        if event.key==pygame.K_d:
            CAIXUKUN.move("RIGHT")

3.22 设置一个简单外挂,鼠标点击左键钥匙 + 1 ,右键钥匙 - 1,中键归 0

elif event.type==pygame.MOUSEBUTTONDOWN:
    if ykey_rect.collidepoint(event.pos):
        if event.button==1:
            CAIXUKUN.property["Yellow_key"]+=1
            sound4.play()
        elif event.button==2:
            CAIXUKUN.property["Yellow_key"]=0
        elif event.button==3:
            CAIXUKUN.property["Yellow_key"]-=1
    elif bkey_rect.collidepoint(event.pos):
        if event.button==1:
            CAIXUKUN.property["Blue_key"]+=1
            sound4.play()
        elif event.button==2:
            CAIXUKUN.property["Blue_key"]=0
        elif event.button==3:
            CAIXUKUN.property["Blue_key"]-=1
    elif rkey_rect.collidepoint(event.pos):
        if event.button==1:
            CAIXUKUN.property["Red_key"]+=1
            sound4.play()
        elif event.button==2:
            CAIXUKUN.property["Red_key"]=0
        elif event.button==3:
            CAIXUKUN.property["Red_key"]-=1

3.23 电梯功能

elif up_lift_rect.collidepoint(event.pos):
    if event.button==1 and get_lift and CAIXUKUN.floor<66 and CAIXUKUN.floor!=42:
        CAIXUKUN.floor+=1
elif down_lift_rect.collidepoint(event.pos):
    if event.button==1 and get_lift and CAIXUKUN.floor>1 and CAIXUKUN.floor!=44:
        CAIXUKUN.floor-=1

3.24 开始画地图

for i in ground_L:
    screen.blit(ground,i)
for j in wall_L:
    screen.blit(wall,j)

for k in List_temp1:
    screen.blit(yellow_door,k)

for k in List_temp2:
    screen.blit(upstair,k)

for k in List_temp3:
    screen.blit(rbottle,k)

for k in List_temp4:
    screen.blit(bbottle,k)

for k in List_temp5:
    screen.blit(rgem,k)

for k in List_temp6:
    screen.blit(bgem,k)

for k in List_temp7:
    screen.blit(ykey,k)

for k in List_temp8:
    screen.blit(blue_door,k)

for k in List_temp9:
    screen.blit(bkey,k)

for k in List_temp10:
    screen.blit(red_door,k)

for k in List_temp11:
    screen.blit(rkey,k)

for k in list_molocation:
    n=list_molocation.index(k)
    k_name=list_moname[n]
    screen.blit(eval(k_name),k)
screen.blit(lift,Lift[0])
screen.blit(guide,Guide[0])

3.25 对于列表中碰撞以后的元素

for y_door in ydoorL[:]:
    if CAIXUKUN.image_rect.colliderect(y_door):
        if CAIXUKUN.property["Yellow_key"]>0:
            i=ydoorL.index(y_door)
            ydoorL.remove(y_door)
            CAIXUKUN.property["Yellow_key"]-=1
            del List_temp1[i]
        else:
            pass

for b_door in bdoorL[:]:
    if CAIXUKUN.image_rect.colliderect(b_door):
        if CAIXUKUN.property["Blue_key"]>0:
            i=bdoorL.index(b_door)
            bdoorL.remove(b_door)
            CAIXUKUN.property["Blue_key"]-=1
            del List_temp8[i]
        else:
            pass

for r_door in rdoorL[:]:
    if CAIXUKUN.image_rect.colliderect(r_door):
        if CAIXUKUN.property["Red_key"]>0:
            i=rdoorL.index(r_door)
            rdoorL.remove(r_door)
            CAIXUKUN.property["Red_key"]-=1
            del List_temp10[i]
        else:
            pass

3.26 红血瓶

for r_bottle in rbottleL[:]:
    if CAIXUKUN.image_rect.colliderect(r_bottle):
        sound1.play()
        i=rbottleL.index(r_bottle)
        rbottleL.remove(r_bottle)
        CAIXUKUN.life+=20
        del List_temp3[i]

3.27 蓝血瓶

for b_bottle in bbottleL[:]:
    if CAIXUKUN.image_rect.colliderect(b_bottle):
        sound1.play()
        i=bbottleL.index(b_bottle)
        bbottleL.remove(b_bottle)
        CAIXUKUN.life+=10
        del List_temp4[i]

3.28 战斗事件

for monster_ in list_morect[:]:
    if CAIXUKUN.image_rect.colliderect(monster_):
        x=list_morect.index(monster_)
        oops=eval(list_moname[x]+'_')
        if(fight(CAIXUKUN,oops)!=[0]):
            sound2.play()
            list_morect[x]=pygame.Rect(-100,-100,50,50)
            list_molocation[x]=(-100,-100)
        else:
            pass

3.29 侧边栏背景

screen.fill((191,191,191),rect=att_rect1,special_flags=0)
screen.fill((191,191,191),rect=att_rect2,special_flags=0)
screen.fill((191,191,191),rect=att_rect3,special_flags=0)
screen.fill((191,191,191),rect=att_rect4,special_flags=0)

3.30 任务栏

screen.blit(ykey,(0,400))
screen.blit(bkey,(0,450))
screen.blit(rkey,(0,500))

3.31 属性栏

text_surface1=my_FontCHs.render("第%s层"%str(CAIXUKUN.floor),True,(0,0,0),(191,191,191))
text_surface2=my_FontCHs.render("攻击力:%s"%str(CAIXUKUN.attack),True,(0,0,0),(191,191,191))
text_surface3=my_FontCHs.render("防御力:%s"%str(CAIXUKUN.defend),True,(0,0,0),(191,191,191))
text_surface4=my_FontCHs.render("生命值:%s"%str(CAIXUKUN.life),True,(0,0,0),(191,191,191))
text_surface5=my_FontCHs.render("金币:%s"%str(CAIXUKUN.gold),True,(0,0,0),(191,191,191))

3.32 钥匙数量

Yellow_key=CAIXUKUN.property['Yellow_key']
Blue_key=CAIXUKUN.property['Blue_key']
Red_key=CAIXUKUN.property['Red_key']

3.33 物品栏

text_surface6=my_FontCHs.render("%s把"%str(Yellow_key),True,(0,0,0),(191,191,191))
text_surface7=my_FontCHs.render("%s把"%str(Blue_key),True,(0,0,0),(191,191,191))
text_surface8=my_FontCHs.render("%s把"%str(Red_key),True,(0,0,0),(191,191,191))
text_surface9=my_FontCHs.render("永久使用宝物",True,(0,0,0),(191,191,191))
text_surface10=my_FontCHs.render("一次使用宝物",True,(0,0,0),(191,191,191))
text_surface11=my_FontCHs.render("自动使用宝物",True,(0,0,0),(191,191,191))

3.34 如果获得则在右侧边栏加上该宝物

if get_lift:
    screen.blit(pygame.transform.scale(pygame.image.load('life.png').convert_alpha(),(40,40)),(770,100))

3.35 属性栏

screen.blit(text_surface0,(280,0))
screen.blit(text_surface1,(0,100))
screen.blit(text_surface2,(0,150))
screen.blit(text_surface3,(0,200))
screen.blit(text_surface4,(0,250))
screen.blit(text_surface5,(0,300))

screen.blit(text_surface6,(100,400))
screen.blit(text_surface7,(100,450))
screen.blit(text_surface8,(100,500))
screen.blit(text_surface9,(758,50))
screen.blit(text_surface10,(758,280))
screen.blit(text_surface11,(758,510))

代码参考

fight.py

def fight(A,B,cancel=0):
    a=A.attack-B.defend
    b=B.attack-A.defend

    alife=A.life
    blife=B.life

    if a<=0:
        return [0]
    if a>0 and b<=0:
        if(cancel==0):
            A.gold+=B.gold
        if(cancel!=0):
            A.life=alife
            B.life=blife
        return 0
    else:
        for i in range(1,100):
            B.life=B.life-a
            if B.life<0:
                if(cancel==0):
                    B.life=blife
                    A.gold+=B.gold
                if(cancel!=0):
                    A.life=alife
                    B.life=blife
                return alife-A.life
            A.life=A.life-b
            if A.life<0:
                A.life=alife
                B.life=blife
                return [0]

if __name__=="__main__":
    class character:
        def __init__(self,tuple):
            self.attack=tuple[0]
            self.defend=tuple[1]
            self.life=tuple[2]
            self.gold=0
        def __str__(self):
            return u"攻击力:%d ,防御力:%d, 生命值:%d,金币:  %d"%(self.attack,self.defend,self.life,self.gold)


    class monster:
        def __init__(self,tuple):
            self.attack=tuple[0]
            self.defend=tuple[1]
            self.life=tuple[2]
            self.gold=tuple[3]

        def __str__(self):
            return u"攻击力:%d ,防御力:%d, 生命值:%d,"%(self.attack,self.defend,self.life)
    CAIXUKUN=character((10,10,100))
    gSlime_=monster((11,5,10,1))
    rSlime_=monster((12,5,10,2))
    sBat_=monster((13,5,10,3))
    bWizard_=monster((15,5,10,5))
    Skeleton_=monster((18,5,10,8))
    scSkeleton_=monster((25,5,10,10))

    BOSS=monster((100,5,10,10))


    fight(CAIXUKUN,gSlime_)
    print(CAIXUKUN)
    fight(CAIXUKUN,gSlime_,cancel=1)
    print(CAIXUKUN,rSlime_)
    print(CAIXUKUN)

    if (fight(CAIXUKUN,BOSS)==0):
        print(CAIXUKUN)
        print(BOSS)

TowerTest.py

import pygame,sys

from Fight import fight

pygame.init()
pygame.mixer.init()

size=width,height=950,650
screen=pygame.display.set_mode(size)
pygame.display.set_caption('Tower')

#环境
wall=pygame.transform.scale(pygame.image.load('w1.png').convert_alpha(),(50,50))
ground=pygame.transform.scale(pygame.image.load('w2.png').convert_alpha(),(50,50))
yellow_door=pygame.transform.scale(pygame.image.load('d1.png').convert_alpha(),(50,50))
blue_door=pygame.transform.scale(pygame.image.load('d2.png').convert_alpha(),(50,50))
red_door=pygame.transform.scale(pygame.image.load('d3.png').convert_alpha(),(50,50))
special_door=pygame.transform.scale(pygame.image.load('d4.png').convert_alpha(),(50,50))
ykey=pygame.transform.scale(pygame.image.load('ykey.png').convert_alpha(),(50,50))
bkey=pygame.transform.scale(pygame.image.load('bkey.png').convert_alpha(),(50,50))
rkey=pygame.transform.scale(pygame.image.load('rkey.png').convert_alpha(),(50,50))
rbottle=pygame.transform.scale(pygame.image.load('rbottle.png').convert_alpha(),(50,50))
bbottle=pygame.transform.scale(pygame.image.load('bbottle.png').convert_alpha(),(50,50))
rgem=pygame.transform.scale(pygame.image.load('rgem.png').convert_alpha(),(50,50))
bgem=pygame.transform.scale(pygame.image.load('bgem.png').convert_alpha(),(50,50))
upstair=pygame.transform.scale(pygame.image.load('upstair.png').convert_alpha(),(50,50))
downstair=pygame.transform.scale(pygame.image.load('downstair.png').convert_alpha(),(50,50))

#怪物
rSlime=pygame.transform.scale(pygame.image.load('rSlime.png').convert_alpha(),(50,50))
gSlime=pygame.transform.scale(pygame.image.load('gSlime.png').convert_alpha(),(50,50))
bSlime=pygame.transform.scale(pygame.image.load('bSlime.png').convert_alpha(),(50,50))
kSlime=pygame.transform.scale(pygame.image.load('kSlime.png').convert_alpha(),(50,50))
sBat=pygame.transform.scale(pygame.image.load('sBat.png').convert_alpha(),(50,50))
bBat=pygame.transform.scale(pygame.image.load('bBat.png').convert_alpha(),(50,50))
rBat=pygame.transform.scale(pygame.image.load('rBat.png').convert_alpha(),(50,50))
bWizard=pygame.transform.scale(pygame.image.load('bWizard.png').convert_alpha(),(50,50))
rWizard=pygame.transform.scale(pygame.image.load('rWizard.png').convert_alpha(),(50,50))
Skeleton=pygame.transform.scale(pygame.image.load('Skeleton.png').convert_alpha(),(50,50))
scSkeleton=pygame.transform.scale(pygame.image.load('scSkeleton.png').convert_alpha(),(50,50))
bcSkeleton=pygame.transform.scale(pygame.image.load('bcSkeleton.png').convert_alpha(),(50,50))
ghost=pygame.transform.scale(pygame.image.load('ghost.png').convert_alpha(),(50,50))
lGuard=pygame.transform.scale(pygame.image.load('lGuard.png').convert_alpha(),(50,50))
mGuard=pygame.transform.scale(pygame.image.load('mGuard.png').convert_alpha(),(50,50))
hGuard=pygame.transform.scale(pygame.image.load('hGuard.png').convert_alpha(),(50,50))
lWitch=pygame.transform.scale(pygame.image.load('lWitch.png').convert_alpha(),(50,50))
hWitch=pygame.transform.scale(pygame.image.load('hWitch.png').convert_alpha(),(50,50))
Orc=pygame.transform.scale(pygame.image.load('Orc.png').convert_alpha(),(50,50))
wOrc=pygame.transform.scale(pygame.image.load('wOrc.png').convert_alpha(),(50,50))
Stone=pygame.transform.scale(pygame.image.load('Stone.png').convert_alpha(),(50,50))
Swordsman=pygame.transform.scale(pygame.image.load('Swordsman.png').convert_alpha(),(50,50))
wGhost=pygame.transform.scale(pygame.image.load('wGhost.png').convert_alpha(),(50,50))
Vampier=pygame.transform.scale(pygame.image.load('Vampier.png').convert_alpha(),(50,50))

#神秘人和宝物
guide=pygame.transform.scale(pygame.image.load('guide.png').convert_alpha(),(50,50))
merchant=pygame.transform.scale(pygame.image.load('merchant.png').convert_alpha(),(50,50))
princess=pygame.transform.scale(pygame.image.load('princess.png').convert_alpha(),(50,50))
lift=pygame.transform.scale(pygame.image.load('lift.png').convert_alpha(),(50,50))

#商店
shop=pygame.transform.scale(pygame.image.load('shop.png').convert_alpha(),(150,50))

#背景音乐
pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play()
musicPlaying=False

sound0=pygame.mixer.Sound('0.wav')
sound1=pygame.mixer.Sound('1.wav')
sound2=pygame.mixer.Sound('2.wav')
#sound3=pygame.mixer.Sound('3.wav')
sound4=pygame.mixer.Sound('4.wav')
sound5=pygame.mixer.Sound('5.wav')
sound6=pygame.mixer.Sound('6.wav')

pygame.time.delay(1000)

sound0.play()

#加载字体
my_FontCH=pygame.font.Font('【春田】小火锅安卓.ttf',30)
my_FontCHs=pygame.font.Font('方正正中黑简体.TTF',30)
my_FontEN=pygame.font.Font('KeeponTruckin.ttf',30)

#最上面一格
text_surface0=my_FontCH.render('小游戏',True,(0,0,0),(191,191,191))

#矩形的四个值
att_rect1=pygame.Rect(0,50,200,600)
att_rect2=pygame.Rect(750,50,200,600)
att_rect3=pygame.Rect(200,600,550,50)
att_rect4=pygame.Rect(0,0,950,50)

#获取钥匙图片所在的位置
ykey_rect=ykey.get_rect()
ykey_rect.left,ykey_rect.top=0,400
bkey_rect=bkey.get_rect()
bkey_rect.left,bkey_rect.top=0,450
rkey_rect=rkey.get_rect()
rkey_rect.left,rkey_rect.top=0,500
#电梯位置
up_lift_rect=pygame.Rect(770,100,40,20)
down_lift_rect=pygame.Rect(770,120,40,20)


#人物类
class character:
    def __init__(self,tuple):
        self.floor=1
        self.attack=tuple[0]
        self.defend=tuple[1]
        self.life=tuple[2]
        self.gold=0
        #物品栏
        self.property='Yellow_key':0,'Blue_key':0,'Red_key':0
        #图像
        self.image=pygame.transform.scale(pygame.image.load('character.png').convert_alpha(),(50,50))
        self.bg=pygame.transform.scale(pygame.image.load('w2.png').convert_alpha(),(50,50))
        self.image_rect=self.image.get_rect()
        self.image_rect.left=450
        self.image_rect.top=550
    

    def move(self,str):
        if str=="UP":
            FORBID=0
            rect_new=self.image_rect.copy()
            rect_new.move_ip(0,-50)
            for wall_ in wallL[:]:
                if rect_new.colliderect(wall_):
                    FORBID=1
            for y_door in ydoorL[:]:
                if rect_new.colliderect(y_door) and self.property["Yellow_key"]==0:
                    FORBID=1
            for b_door in bdoorL[:]:
                if rect_new.colliderect(b_door) and self.property["Blue_key"]==0:
                    FORBID=1
            for r_door in rdoorL[:]:
                if rect_new.colliderect(r_door) and self.property["Red_key"]==0:
                    FORBID=1
            for monster_ in list_morect[:]:
                if rect_new.colliderect(monster_):
                    x=list_morect.index(monster_)
                    if(fight(CAIXUKUN,eval(list_moname[x]+"_"),1))==[0]:
                        sound5.play()
                        FORBID=1
            if FORBID==1:
                pass
            else:
                if self.image_rect.top<100:
                    pass
                else:
                    self.image_rect.move_ip(0,-50)

        if str=="DOWN":
            FORBID=0
            rect_new=self.image_rect.copy()
            rect_new.move_ip(0,50)

            for wall_ in wallL[:]:
                if rect_new.colliderect(wall_):
                    FORBID=1
            for y_door in ydoorL[:]:
                if rect_new.colliderect(y_door)and self.property["Yellow_key"]==0:
                    FORBID=1
            for b_door in bdoorL[:]:
                if rect_new.colliderect(b_door) and self.property["Blue_key"]==0:
                    FORBID=1
            for r_door in rdoorL[:]:
                if rect_new.colliderect(r_door) and self.property["Red_key"]==0:
                    FORBID=1
            for monster_ in list_morect[:]:
                if rect_new.colliderect(monster_):
                     x = list_morect.index(monster_)
                     if (fight(CAIXUKUN, eval(list_moname[x] + "_"), 1)) == [0]:
                         sound5.play()
                         FORBID = 1
            if FORBID == 1:
                pass
            else:
                if self.image_rect.bottom>550:
                    pass
                else:
                    self.image_rect.move_ip(0, 50)

        if str == "LEFT":
            FORBID = 0
            rect_new = self.image_rect.copy()
            rect_new.move_ip(-50, 0)

            for wall_ in wallL[:]:
                if rect_new.colliderect(wall_):
                    FORBID = 1
            for y_door in ydoorL[:]:
                if rect_new.colliderect(y_door) and self.property["Yellow_key"] == 0:
                    FORBID = 1
            for b_door in bdoorL[:]:
                if rect_new.colliderect(b_door) and self.property["Blue_key"]==0:
                    FORBID=1
            for r_door in rdoorL[:]:
                if rect_new.colliderect(r_door) and self.property["Red_key"]==0:
                    FORBID=1
            for monster_ in list_morect[:]:
                if rect_new.colliderect(monster_):
                    x = list_morect.index(monster_)
                    if (fight(CAIXUKUN, eval(list_moname[x] + "_"), 1)) == [0]:
                        sound5.play()
                        FORBID = 1
            if FORBID == 1:
                pass
            else:
                if self.image_rect.left< 250:
                    pass
                else:
                    self.image_rect.move_ip(-50,0)

        if str == "RIGHT":
            FORBID = 0
            rect_new = self.image_rect.copy()
            rect_new.move_ip(50, 0)

            for wall_ in wallL[:]:
                if rect_new.colliderect(wall_):
                    FORBID = 1
            for y_door in ydoorL[:]:
                if rect_new.colliderect(y_door) and self.property["Yellow_key"] == 0:
                    FORBID = 1
            for b_door in bdoorL[:]:
                if rect_new.colliderect(b_door) and self.property["Blue_key"]==0:
                    FORBID=1
            for r_door in rdoorL[:]:
                if rect_new.colliderect(r_door) and self.property["Red_key"]==0:
                    FORBID=1
            for monster_ in list_morect[:]:
                if rect_new.colliderect(monster_):
                    x = list_morect.index(monster_)
                    if (fight(CAIXUKUN, eval(list_moname[x] + "_"), 1)) == [0]:
                        sound5.play()
                        FORBID = 1
            if FORBID == 1:
                pass
            else:
                if self.image_rect.right > 700:
                    pass
                else:
                    self.image_rect.move_ip(50, 0)


class monster:
    def __init__(self,tuple):
        self.attack=tuple[0]
        self.defend=tuple[1]
        self.life=tuple[2]
        self.gold=tuple[3]

CAIXUKUN=character((10,10,100))
gSlime_=monster((11,5,10,1))
rSlime_=monster((12,5,10,2))
sBat_=monster((13,5,10,3))
ghost_=monster((14,5,10,4))
hWitch_=monster((15,5,10,5))
Skeleton_=monster((20,11,10,8))
scSkeleton_=monster((25,11,10,10))

ground_L=[]
for i in range(4,14+1):
    for j in range(1,11+1):
        ground_L.append((50*i,50*j))


wall_L=[(200,100),(250,100),(300,100),(350,100),(400,100),(450,100),(500,100),(550,100),(600,100),(650,100),(450,150),(450,200),(450,250),(450,350),
        (500,250),(550,250),(650,250),(650,150),(650,200),(600,350),(550,350),(500,350),(650,350),(650,300),(350,200),(350,250),(350,300),(350,350),
        (350,400),(350,450),(350,500),(350,550),(300,250),(200,250),(300,400),(200,400),(400,450),(550,450),(600,450),
        (500,450),(550,500),(550,550),(700,450)]
#门
dict_door=(350,150):'yellow',(250,250):'blue',(600,250):'yellow',(450,300):'yellow',(250,400):'yellow',(450,450):'yellow',(450,450):'yellow',
           (650,450):'red',(600,250):'red'
#宝物
dict_trea=(550,150):'ykey',(200,350):'ykey',(300,500):'ykey',(400,500):'ykey',(400,550):'bkey',(400,250):'rkey',(600,150):'rkey',(400,200):'ykey',(200,150):'ykey',(300,550):'ykey',(200,150):'rbottle',(100,350):'rbottle',(500,150):'rgem',(500,200):'bgem',(550,200):'rbottle',(200,550):'rbottle',(200,500):'bbottle',(650,550):'bbottle',(650,500):'bgem',

#楼梯
dict_stair=(200,50):'up'

#怪物
dict_monster=(300,50):'gSlime',(400,50):'gSlime',(600,550):'sBat',(700,550):'sBat',(350,50):'gSlime',(500,300):'rSlime',(500,300):'rSlime',
(250,300):'hWitch',(250,450):'ghost',(550,300):'ghost',(600,300):'rSlime'

#神秘人
Guide=[(500,500)]
Lift=[(250,550)]

get_lift=0


List_temp1=[k for k,v in dict_door.items() if v=='yellow']
List_temp2=[k for k,v in dict_stair.items() if v=='up']
List_temp3=[k for k,v in dict_trea.items() if v=='rbottle']
List_temp4=[k for k,v in dict_trea.items() if v=='bbottle']
List_temp5=[k for k,v in dict_trea.items() if v=='rgem']
List_temp6=[k for k,v in dict_trea.items() if v=='bgem']
List_temp7=[k for k,v in dict_trea.items() if v=='ykey']
List_temp8=[k for k,v in dict_door.items() if v=='blue']
List_temp9=[k for k,v in dict_trea.items() if v=='bkey']
List_temp10=[k for k,v in dict_door.items() if v=='red']
List_temp11=[k for k,v in dict_trea.items() if v=='rkey']



def build_List(LIST1,LIST2):
    for i in range(1,len(LIST2)+1):
        LIST1.append(pygame.Rect(LIST2[i-1][0],LIST2[i-1][1],50,50))

wallL=[]
build_List(wallL,wall_L)

ydoorL=[]
build_List(ydoorL,List_temp1)

bdoorL=[]
build_List(bdoorL,List_temp2)

rdoorL=[]
build_List(rdoorL,List_temp3)

stairupL=[]
build_List(stairupL,List_temp4)

rbottleL=[]
build_List(rbottleL,List_temp5)

bbottleL=[]
build_List(bbottleL,List_temp6)

rgemL=[]
build_List(rgemL,List_temp7)

bgemL=[]
build_List(bgemL,List_temp8)

ykeyL=[]
build_List(ykeyL,List_temp9)

bkeyL=[]
build_List(bkeyL,List_temp10)

rkeyL=[]
build_List(rkeyL,List_temp11)

list_molocation=list(dict_monster)
list_moname=list(dict_monster.values())
list_morect=[]
build_List(list_morect,list_molocation)


guideL=[]
guideL.append(pygame.Rect(Guide[0][0],Guide[0][1],50,50))

liftL=[]
liftL.append(pygame.Rect(Lift[0][0],Lift[0][1],50,50))

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:
            if event.key==pygame.K_w:
                CAIXUKUN.move("UP")
            if event.key == pygame.K_s:
                CAIXUKUN.move("DOWN")
            if event.key==pygame.K_a:
                CAIXUKUN.move("LEFT")
            if event.key==pygame.K_d:
                CAIXUKUN.move("RIGHT")
            if event.key==pygame.K_p:
                if musicPlaying:
                    pygame.mixer.music.stop()
                else:
                    pygame.mixer.music.play(-1)
                musicPlaying=not musicPlaying
            if event.key==pygame.K_1:
                sound0.play()
        elif event.type==pygame.MOUSEBUTTONDOWN:
            if ykey_rect.collidepoint(event.pos):
                if event.button==1:
                    CAIXUKUN.property["Yellow_key"]+=1
                    sound4.play()
                elif event.button==2:
                    CAIXUKUN.property["Yellow_key"]=0
                elif event.button==3:
                    CAIXUKUN.property["Yellow_key"]-=1
            elif bkey_rect.collidepoint(event.pos):
                if event.button==1:
                    CAIXUKUN.property[

以上是关于你是否还记得有一款游戏叫做“魔塔”?的主要内容,如果未能解决你的问题,请参考以下文章

C课程设计——魔塔游戏

bzoj2130: 魔塔

[M贪心] lcLCP30. 魔塔游戏(STL优先队列+堆+贪心)

[M贪心] lcLCP30. 魔塔游戏(STL优先队列+堆+贪心)

UnityRPG游戏战斗怪物检测

CSP-S2周末刷题班(第四场)