def __init__(self, screen, config, startClock): super(RobotRunScene, self).__init__(screen, config, startClock) self.w, self.h = self.screen.get_width(), self.screen.get_height() self.__As_MAP = AStarArea(Rectangle(0, 0, 800, 600), 80, 60) self.caption = 'Actor与A*结合测试场景' self.__containers = [] for i in range(0, 6): x = int(random.randint(100, self.w - 150) / 10) * 10 y = int(random.randint(100, self.h - 150) / 10) * 10 w = int(random.randint(50, 150) / 10) * 10 h = int(random.randint(50, 150) / 10) * 10 self.__containers.append(Container(Rectangle(x, y, w, h))) _x, _y = int(x / self.__As_MAP.unit_w), int(y / self.__As_MAP.unit_h) _w, _h = int(w / self.__As_MAP.unit_w), int(h / self.__As_MAP.unit_h) self.__As_MAP.addObstacleArea(_x, _w, _y, _h) self.__A_Robot = RobotActor(Rectangle(0, 0, 100, 100)) self.__E_Msg = TextElement((600, 0, 200, 60), 'Robotlocal:(0, 0)\nMouselocal:(0, 0)', gl_Font, 12, (255, 255, 255), 1) self.__E_Msg2 = TextElement((600, 40, 200, 600), 'Astart:\n', gl_Font, 10, (255, 255, 255), 1) self.__pathList = [] self.__normalLis = [] self.__build_As_Map() self.__i = 0 self.__end = (0, 0)
def __init__(self, image, rect=None, isScale=True): super().__init__() self.image = image self.rect = rect if isinstance(rect, list) or isinstance(rect, tuple): self.rect = pygame.Rect(rect[0], rect[1], rect[2], rect[3]) elif isinstance(rect, Rectangle): self.rect = pygame.Rect(rect.x, rect.y, rect.w, rect.h) if isinstance(self.image, str): self.image = pygame.image.load(image) if self.rect is None: self.rect = self.image.get_rect() elif isScale: self.image = pygame.transform.scale(self.image, (self.rect.w, self.rect.h)) self.Events = IOEvent3() self.EventsHadDo = ElementHadDoEvent() self.visual = True self.active = True self.zIndex = 0 self.mouseLastPos = (0, 0) self.mousePos = (0, 0) self.mouseButtons = (0, 0, 0) self.mouseRel = (0, 0) self.physicalBodyType = None if isinstance(rect, tuple): self.collidedArea = Rectangle(self.rect[0], self.rect[1], self.rect[2], self.rect[3]) else: self.collidedArea = Rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h)
class Actor: def __init__(self, texture, area=None, physicalBody=None, visual=True, zIndex=0, active=False): self.texture = texture self.visual = visual self.zIndex = zIndex self.physicalBody = physicalBody self.active = active self.area = area if self.area is None: w = self.texture.get_rect().w h = self.texture.get_rect().h self.area = Rectangle(0, 0, w, h) else: self.texture = pygame.transform.scale(self.texture, (self.area.w, self.area.h)) self.initArea = self.area def collided(self, othActor): if not isinstance(othActor, Actor): raise Exception( "Class '{}' must is a subclass of 'Actor'".format(othActor)) return self.area.intersects(othActor.area) def update(self, *args): pass def draw(self, screen): screen.blit(self.texture, self.area.local())
def setRect(self, rec): if (isinstance(rec, tuple) or isinstance(rec, list)) and len(rec) >= 4: self.rect = pygame.Rect(rec[0], rec[1], rec[2], rec[3]) else: self.rect = rec self.image = pygame.transform.scale(self.image, (self.rect.w, self.rect.h)) self.collidedArea = Rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h)
def init(self, length, width, show_length, show_width, pathfinding_unit): self.width, self.length = width, length self.show_length, self.show_width = show_length, show_width self.A_Map = AStarArea(Rectangle(0, 0, self.show_length, self.show_width), self.show_length / pathfinding_unit, self.show_length / pathfinding_unit) self.__surface = pygame.Surface((self.length, self.width)).convert() self.isInit = True
def doClockEvent(self, NowClock): # if self.FPS != 0: # self.m.update(0.01) self.m.applyForce(vec2(0, 9.8)) if self.mousePressed: self.m.applyForce(vec2(4, 0)) self.m.update(0.1) self.m.edges(Rectangle(0, 0, self.width, self.height))
def __evn_downsizeTex(self, typ): if typ: self.texture = pygame.transform.scale( self.__bkupTex, (int(self.__texture_w - 0.1 * self.__texture_w), int(self.__texture_h - 0.1 * self.__texture_h))) else: self.texture = pygame.transform.scale( self.__bkupTex, (int(self.__texture_w + 0.1 * self.__texture_w), int(self.__texture_h + 0.1 * self.__texture_h))) self.area = Rectangle(self.area.x, self.area.y, self.texture.get_rect().w, self.texture.get_rect().h) self.__texture_w = self.area.w self.__texture_h = self.area.h self.res_surface = self.texture
def __init__(self, texture, area=None, physicalBody=None, visual=True, zIndex=0, active=False): self.texture = texture self.visual = visual self.zIndex = zIndex self.physicalBody = physicalBody self.active = active self.area = area if self.area is None: w = self.texture.get_rect().w h = self.texture.get_rect().h self.area = Rectangle(0, 0, w, h) else: self.texture = pygame.transform.scale(self.texture, (self.area.w, self.area.h)) self.initArea = self.area
def __init__(self, screen, config, clock): super(ActorScene, self).__init__(screen, config, clock) rect_container = Rectangle(600, 200, 100, 100) rect_container2 = Rectangle(600, 0, 100, 100) rect_container3 = Rectangle(0, 0, 100, 100) rect_container4 = Rectangle(400, 200, 100, 100) rect_wall = Rectangle(0, 200, 400, 400) self.__A_wall = wallActor( pygame.image.load( 'F:/练习/PyCharm/PygameTest/resource/Test/wall.jpg'), rect_wall) self.__A_container = containerActor( pygame.image.load( 'F:/练习/PyCharm/PygameTest/resource/Test/container1.jpg'), rect_container) self.__A_container2 = containerActor( pygame.image.load( 'F:/练习/PyCharm/PygameTest/resource/Test/container2.jpg'), rect_container2) self.__A_container3 = containerActor( pygame.image.load( 'F:/练习/PyCharm/PygameTest/resource/Test/container3.jpg'), rect_container3) self.__A_container4 = containerActor( pygame.image.load( 'F:/练习/PyCharm/PygameTest/resource/Test/container4.jpg'), rect_container4) self.__A_container2.physicalBody.vel = vec2(0, 3) self.__A_container3.physicalBody.vel = vec2(0, 3) self.ps = physicalScene(Rectangle(0, 0, 800, 600), vec2(0, 0.098), self.startClock) self.ps.add(self.__A_container.physicalBody) self.ps.add(self.__A_container2.physicalBody) self.ps.add(self.__A_container3.physicalBody) self.ps.add(self.__A_container4.physicalBody)
def __init__(self, screen, config, startClock): super(TestPainterScene, self).__init__(screen, config, startClock) self.painter = Painter(self.screen) self.points = [] for i in range(0, 6): x = random.randint(0, 800) y = random.randint(0, 400) self.points.append(vec2(x, y)) self.rect = Rectangle(400, 100, 200, 100) self.line = Ray(vec2(200, 200), math.radians(30), 400) self.white = (255, 255, 255) self.tra = Triangle(point2(100, 100), point2(60, 180), point2(140, 180)) self.circle = Circle(400, 300, 100) self.ellipse = Ellipse(400, 300, 160, 70)
def __init__(self, area, msgQueue=None, Events=None): _area = None if isinstance(area, Shape): _area = area else: _area = Rectangle(area[0], area[1], area[2], area[3]) self.area = _area self.msgQueue = msgQueue self.Events = Events if self.Events is None: self.Events = IOEvent3() self.EventsHadDo = ElementHadDoEvent() self.active = True self.zIndex = 0 self.visual = True self.mouseLastPos = (0, 0) self.mousePos = (0, 0) self.mouseButtons = (0, 0, 0) self.mouseRel = (0, 0)
def __init__(self, name, bgWidth, bgHeight, _id): self.texture = pygame.image.load(name) self.__texture_w = self.texture.get_rect().w self.__texture_h = self.texture.get_rect().h area = Rectangle(centeredXPos(bgWidth, self.__texture_w), centeredYPos(bgHeight, self.__texture_h), self.__texture_w, self.__texture_h) super(Operation, self).__init__(area, name) self.id = _id self.Interpolation = None self.InterpData = dict() self.__bkupTex = self.texture self.__AnimeSequence = list() self.__AnimeFrames = list() self.Events.appendEvent(ioEvent3Enum.mouseRollDown, lambda: self.__evn_downsizeTex(1), 0) self.Events.appendEvent(ioEvent3Enum.mouseRollUp, lambda: self.__evn_downsizeTex(0), 0) self.__index = 0
def buildSpriteMap(self): rowMarginStep, colMarginStep = 4, 4 rowMargin, colMargin = rowMarginStep, colMarginStep while self.__col < 9: rowMarginStep = 8 if ((self.__row + 1) % 3 == 0) else 2 colMarginStep = 8 if ((self.__col + 1) % 3 == 0) else 2 ret = Rectangle(100 + (self.__row * self.__length + rowMargin), 10 + (self.__col * self.__length + colMargin), self.__length, self.__length) number = self.__data["puzzle"][self.__index] self.__index += 1 if number != 0: resName = self.__resImgPath + self.__resImgName + self.__resStaticImgName + str( number) + self.__resStaticImgExt else: resName = self.__resImgPath + self.__resImgName + "0" + self.__resImgExt element = SudokuSprite(ret, resName) self.__indexDict[element] = (self.__row, self.__col) element.setIndex((self.__row, self.__col)) element.setValue(number) element.setIsStatic(True) element.Events.appendEvent(ioEvent3Enum.mouseLeftKeyDown, lambda: self.__resSoundClick.play(), 1) self.__elements.append(element) self.__row += 1 rowMargin = rowMargin + rowMarginStep if self.__row == 9: self.__row = 0 rowMargin = 4 self.__col += 1 colMargin = colMargin + colMarginStep return self.__elements
def __init__(self, screen, config, clock): super(PhysicsScene, self).__init__(screen, config, clock) self.m = Mover(vec2(500, 50), Rectangle(0, 0, 800, 600), 1) self.a = Attractor(vec2(400, 300))
class Operation(ImgElement): def __init__(self, name, bgWidth, bgHeight, _id): self.texture = pygame.image.load(name) self.__texture_w = self.texture.get_rect().w self.__texture_h = self.texture.get_rect().h area = Rectangle(centeredXPos(bgWidth, self.__texture_w), centeredYPos(bgHeight, self.__texture_h), self.__texture_w, self.__texture_h) super(Operation, self).__init__(area, name) self.id = _id self.Interpolation = None self.InterpData = dict() self.__bkupTex = self.texture self.__AnimeSequence = list() self.__AnimeFrames = list() self.Events.appendEvent(ioEvent3Enum.mouseRollDown, lambda: self.__evn_downsizeTex(1), 0) self.Events.appendEvent(ioEvent3Enum.mouseRollUp, lambda: self.__evn_downsizeTex(0), 0) self.__index = 0 # def showOperate(self): # def __evn_downsizeTex(self, typ): if typ: self.texture = pygame.transform.scale( self.__bkupTex, (int(self.__texture_w - 0.1 * self.__texture_w), int(self.__texture_h - 0.1 * self.__texture_h))) else: self.texture = pygame.transform.scale( self.__bkupTex, (int(self.__texture_w + 0.1 * self.__texture_w), int(self.__texture_h + 0.1 * self.__texture_h))) self.area = Rectangle(self.area.x, self.area.y, self.texture.get_rect().w, self.texture.get_rect().h) self.__texture_w = self.area.w self.__texture_h = self.area.h self.res_surface = self.texture def getInitTexture(self): return self.__bkupTex def clearAnimeData(self): self.__AnimeFrames.clear() def record(self, time): _animeFrame = AnimeFrame(time, vec4(self.area.local(), self.area.size())) self.__AnimeFrames.append(_animeFrame) def showAnime(self): if len(self.__AnimeFrames) > 0: _frame = self.__AnimeFrames[self.__index] self.area.x = _frame.local.x self.area.y = _frame.local.y self.res_surface = pygame.transform.scale( self.__bkupTex, (int(_frame.local.z), int(_frame.local.w))) self.__index += 1 if self.__index >= len(self.__AnimeFrames): self.__index = 0 def getAnimeFrames(self): return self.__AnimeFrames
def __init__(self, *args): super(verletSceneRotate, self).__init__(*args) self.caption = 'VerletSceneRotateTest' self.m = square(100, Rectangle(300, 250, 200, 100))
def update(self, *args): self.highLight = False self.rect = pygame.Rect(self.__x + random.uniform(-1, 1), self.__y + random.uniform(-1, 1), self.rect.w, self.rect.h) self.collidedArea = Rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h)
class Sprite(pygame.sprite.Sprite): """游戏中所有精灵的父类 框架:Syclight Framework with pygame 实现自pygame.sprite.sprite(*groups): 在本框架中的声明精灵时要求继承该类,否则将会导致出错 当继承该类时,要求使用该类的变量和实现必要的方法,用不到的方法可以忽略 """ def __init__(self, image, rect=None, isScale=True): super().__init__() self.image = image self.rect = rect if isinstance(rect, list) or isinstance(rect, tuple): self.rect = pygame.Rect(rect[0], rect[1], rect[2], rect[3]) elif isinstance(rect, Rectangle): self.rect = pygame.Rect(rect.x, rect.y, rect.w, rect.h) if isinstance(self.image, str): self.image = pygame.image.load(image) if self.rect is None: self.rect = self.image.get_rect() elif isScale: self.image = pygame.transform.scale(self.image, (self.rect.w, self.rect.h)) self.Events = IOEvent3() self.EventsHadDo = ElementHadDoEvent() self.visual = True self.active = True self.zIndex = 0 self.mouseLastPos = (0, 0) self.mousePos = (0, 0) self.mouseButtons = (0, 0, 0) self.mouseRel = (0, 0) self.physicalBodyType = None if isinstance(rect, tuple): self.collidedArea = Rectangle(self.rect[0], self.rect[1], self.rect[2], self.rect[3]) else: self.collidedArea = Rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h) def setRect(self, rec): if (isinstance(rec, tuple) or isinstance(rec, list)) and len(rec) >= 4: self.rect = pygame.Rect(rec[0], rec[1], rec[2], rec[3]) else: self.rect = rec self.image = pygame.transform.scale(self.image, (self.rect.w, self.rect.h)) self.collidedArea = Rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h) def setImg(self, arg): if isinstance(arg, str): self.image = pygame.image.load(arg) if isinstance(arg, pygame.Surface): self.image = arg def draw(self, surface): if self.image: surface.blit(self.image, self.rect) def collided(self, oth): if not isinstance(oth, Sprite): raise Exception( "Param '{}' must is a subclass of 'sprite'".format(oth)) if self.zIndex != oth.zIndex: return False return self.collidedArea.intersects(oth.collidedArea)