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 门的位置
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