游戏介绍:一开始玩家有100的生命值,10的攻击力,10的防御力,玩家可以通过获得相应颜色钥匙来打开相应颜色得门,随后通过打开门来攻击鬼怪来获取生命值和金币,药水提升生命值,获得蓝色宝石来提升预防力,获取红色宝石来获取攻击力,直至将所有妖怪,宝石等获得通过第一层。
运行结果:
游戏开发制作流程 :
1.收集素材(背景,玩家,敌人,宝石)
2.创建攻击函数
3.创建魔塔函数
一,收集素材
百度搜索所需的素材,并通过ps来进行简单的处理
二,创建攻击函数
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)) 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.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墙壁以及门在没有对应钥匙时得不可通过机制写在移动函数得判定里
if str=="UP": FORBID=0 rect_new=self.image_rect.copy() rect_new.move_ip(0,-50)
3.11-1判断是否撞墙
for wall_ in wallL[:]: if rect_new.colliderect(wall_): FORBID=1
3.11-2是否在没有钥匙得时候通过门
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-3打不过怪时不可通过
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-4判断是否出界
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_temp8: screen.blit(blue_door,k) for k in List_temp10: screen.blit(red_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_temp9: screen.blit(bkey,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.35对于列表中的元素,碰撞以后
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.36红血瓶
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.37蓝血瓶
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.38战斗事件
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.39侧边栏背景
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.40任务栏
screen.blit(ykey,(0,400)) screen.blit(bkey,(0,450)) screen.blit(rkey,(0,500))
3.41属性栏
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.42钥匙数量
Yellow_key=CAIXUKUN.property['Yellow_key'] Blue_key=CAIXUKUN.property['Blue_key'] Red_key=CAIXUKUN.property['Red_key']
3.43物品栏
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.44如果获得电梯,则在右侧边栏加上该宝物
if get_lift: screen.blit(pygame.transform.scale(pygame.image.load('life.png').convert_alpha(),(40,40)),(770,100))
3.45属性栏
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_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) 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["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 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 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_temp8: screen.blit(blue_door,k) for k in List_temp10: screen.blit(red_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_temp9: screen.blit(bkey,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]) #黄门 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 #红血瓶 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] #蓝血瓶 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] for r_gem in rgemL[:]: if CAIXUKUN.image_rect.colliderect(r_gem): sound1.play() i=rgemL.index(r_gem) rgemL.remove(r_gem) CAIXUKUN.attack +=2 del List_temp5[i] for b_gem in bgemL[:]: if CAIXUKUN.image_rect.colliderect(b_gem): sound1.play() i=bgemL.index(b_gem) bgemL.remove(b_gem) CAIXUKUN.defend +=2 del List_temp6[i] for y_key in ykeyL[:]: if CAIXUKUN.image_rect.colliderect(y_key): i=ykeyL.index(y_key) ykeyL.remove(y_key) CAIXUKUN.property['Yellow_key']+=1 del List_temp7[i] for b_key in bkeyL[:]: if CAIXUKUN.image_rect.colliderect(b_key): i=bkeyL.index(b_key) bkeyL.remove(b_key) CAIXUKUN.property['Blue_key']+=1 del List_temp9[i] for r_key in rkeyL[:]: if CAIXUKUN.image_rect.colliderect(r_key): i=rkeyL.index(r_key) rkeyL.remove(r_key) CAIXUKUN.property['Red_key']+=1 del List_temp11[i] #战斗事件 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 for lift_ in liftL[:]: if CAIXUKUN.image_rect.colliderect(lift_): sound6.play() liftL.remove(lift_) get_lift=1 Lift[0]=(-100,-100) #侧边栏背景 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) #任务栏 screen.blit(ykey,(0,400)) screen.blit(bkey,(0,450)) screen.blit(rkey,(0,500)) #属性栏 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)) #钥匙数量 Yellow_key=CAIXUKUN.property['Yellow_key'] Blue_key=CAIXUKUN.property['Blue_key'] Red_key=CAIXUKUN.property['Red_key'] 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)) if get_lift: screen.blit(pygame.transform.scale(pygame.image.load('life.png').convert_alpha(),(40,40)),(770,100)) screen.blit(CAIXUKUN.image,CAIXUKUN.image_rect) #属性栏 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)) pygame.display.update()
来源:oschina
链接:https://my.oschina.net/u/4463382/blog/3221756