supermario!
资源内容介绍
supermario! import numpy as npimport random, sysimport pgzrunimport timedef newgame(): mario.pos=(200,HEIGHT-120) mario.vy=0 mario.time=0 mario.dir="right" mario.dead=False mario.small=True mario.s="s" mario.points=0 mario.win=False mario.start_time = time.time() # 记录游戏开始时间 for i in range(len(objs)): objs.remove(objs[0]) file = open(sys.path[0] + "\\level1.dat") i = 0 for line in file: for j in range(len(line)): if line[j]=="O": objs.append(Brick("brick.png",(j*32,32*i))) elif line[j]=="B": objs.append(Brick("brick2.png",(j*32,32*i))) elif line[j]=="D": objs.append(Block("block.png",(j*32,32*i))) elif line[j]=="Q": objs.append(Question("question.png",(j*32,32*i))) elif line[j]=="c": objs.append(Cloud("cloud.png",(j*32,32*i))) elif line[j]=="h": objs.append(Bush("hill.png",(j*32,32*i-22))) elif line[j]=="b": objs.append(Bush("bush.png",(j*32,32*i-12))) elif line[j]=="E": objs.append(Monster("enemy1.png",(j*32,32*i))) objs[-1].dir = 1 elif line[j]=="T": objs.append(Monster("turtle.png",(j*32,32*i))) objs[-1].dir = 1 elif line[j]=="p": objs.append(Coin("coin.png",(j*32,32*i))) elif line[j]=="A": objs.append(Bush("castle.png",(j*32,32*i-12))) i = i + 1''' 例: np.abs() 函数计算马里奥的中心点(mario.center)+其高度的一半(mario.size[1] / 2) -砖块的中心点(self.center)+其高度的一半(self.size[1] / 2)之间的距离。 如果这个距离小于15,则认为发生了顶部碰撞。 如果发生碰撞,那么马里奥的垂直速度(mario.vy)被设置为0,表示停止下落, 并且马里奥的底部位置(mario.bottom)被设置为砖块的顶部位置(self.top)。 '''class Brick(Actor): # 定义一个砖块的类,继承自Actor def react(self): if np.abs(mario.center[1] + mario.size[1] / 2 - self.center[1] + self.size[1] / 2) < 15: # 顶部碰撞 mario.vy = 0 # mairo垂直速度设为0,停止下落 mario.bottom = self.top # mario的底部位置(mario.bottom)被设置为砖块的顶部位置(self.top) elif np.abs(mario.center[1] - mario.size[1] / 2 - self.center[1] - self.size[1] / 2) < 15: # 底部碰撞 mario.vy = 0 mario.top = self.bottom # mario的顶部位置(mario.top)被设置为砖块的底部位置(self.bottom) elif np.abs(mario.center[0] + mario.size[0] / 2 - self.center[0] + self.size[0] / 2) < 15: # 右侧碰撞 moveall(6) # 所有物体向左移动6个单位 elif np.abs(mario.center[0] - mario.size[0] / 2 - self.center[0] - self.size[0] / 2) < 15: # 左侧碰撞 moveall(-6) # 所有单位向右移动6个单位 def move(self): passclass Block(Actor): # 可以撞击的砖块 def react(self): if np.abs(mario.center[1] + mario.size[1] / 2 - self.center[1] + self.size[1] / 2) < 15: # 顶部碰撞 mario.vy = 0 mario.bottom = self.top elif np.abs(mario.center[1] - mario.size[1] / 2 - self.center[1] - self.size[1] / 2) < 15: # 底部碰撞 mario.vy = 0 mario.top = self.bottom animate(self, pos=(self.center[0], -10000)) elif np.abs(mario.center[0] + mario.size[0] / 2 - self.center[0] + self.size[0] / 2) < 15: # 右侧碰撞 moveall(6) elif np.abs(mario.center[0] - mario.size[0] / 2 - self.center[0] - self.size[0] / 2) < 15: # 左侧碰撞 moveall(-6) def move(self): passclass Coin(Actor): # 金币 def react(self): if mario.colliderect(self): objs.remove(self) mario.points=mario.points+1 # 马里奥获得硬币时加分 def move(self): passclass Mushroom(Actor): # 定义蘑菇 def react(self): if self.colliderect(mario): mario.small = False objs.remove(self) def move(self): for obj in objs: if obj != self and self.colliderect(obj) and not obj.image in ["bush.png","brick.png","hill.png","coin.png"]: self.dir = -self.dir self.x = self.x + self.dir uy = self.vy self.vy = self.vy+2000.0*0.015 self.y = self.y+(uy+self.vy)*0.5*0.015 for obj in objs: if self.colliderect(obj) and np.abs(self.center[1]+self.size[1]/2-obj.center[1]+obj.size[1]/2)<15: self.vy = 0 self.bottom = obj.topclass Question(Actor): # 带“?”的盒子 def react(self): if np.abs(mario.center[1] + mario.size[1] / 2 - self.center[1] + self.size[1] / 2) < 15: # 顶部碰撞 mario.vy = 0 mario.bottom = self.top elif np.abs(mario.center[1] - mario.size[1] / 2 - self.center[1] - self.size[1] / 2) < 15: # 底部碰撞 mario.vy = 0 mario.top = self.bottom if self.image == "question.png": self.image = "question2.png" objs.append(Mushroom("mushroom.png", (self.center[0], self.center[1] - 50))) objs[-1].dir = 1 objs[-1].vy = 0 animate(objs[-1], pos=(self.center[0], self.center[1] - objs[-1].size[1] + 2)) elif np.abs(mario.center[0] + mario.size[0] / 2 - self.center[0] + self.size[0] / 2) < 15: # 右侧碰撞 moveall(6) elif np.abs(mario.center[0] - mario.size[0] / 2 - self.center[0] - self.size[0] / 2) < 15: # 左侧碰撞 moveall(-6) def move(self): passclass Cloud(Actor): # 定义云朵的移动 def react(self): pass def move(self): self.x = (self.x-1) % 7000class Monster(Actor): # 定义怪物 def react(self): if np.abs(mario.center[1] + mario.size[1] / 2 - self.center[1] + self.size[1] / 2) < 15: # 顶部碰撞 mario.vy = 0 mario.bottom = self.top mario.points=mario.points+1 # 击杀怪物时增加分数 animate(self, pos=(self.right + 50, HEIGHT + 50)) elif np.abs(mario.center[1] - mario.size[1] / 2 - self.center[1] - self.size[1] / 2) < 15: # 底部碰撞 mario.vy = 0 mario.top = self.bottom elif np.abs(mario.center[0] + mario.size[0] / 2 - self.center[0] + self.size[0] / 2) < 15: # 右侧碰撞 if mario.small: mario.dead = True newgame() else: animate(self, pos=(self.right + 50, HEIGHT + 50)) mario.small = True elif np.abs(mario.center[0] - mario.size[0] / 2 - self.center[0] - self.size[0] / 2) < 15: # 左侧碰撞 if mario.small: mario.dead = True newgame() else: animate(self, pos=(self.right + 50, HEIGHT + 50)) mario.small = True def move(self): for obj in objs: if obj!=self and self.colliderect(obj) and not obj.image in ["bush.png","brick.png","hill.png"]: self.dir=-self.dir if self.dir==1 and self.image in ["turtle.png","turtleleft.png"]: self.image = "turtle.png" elif self.dir==-1 and self.image in ["turtle.png","turtleleft.png"]: self.i