def addBlocks(self, count): for i in range(count): last_block = self.blocks[ len(self.blocks) - 1] if len(self.blocks) - 1 != -1 else Block(1, Vector2(-1, 0)) self.blocks.append( Block( self.scale, Vector2(last_block.pos.x - last_block.direction.x, last_block.pos.y - last_block.direction.y), self.color)) self.blocks[len(self.blocks) - 1].last_direction = last_block.direction
def randomize(self, exclude=[]): local_w = int(self.w / self.scale) - 1 local_h = int(self.h / self.scale) - 1 new_x = randint(0, local_w) new_y = randint(0, local_h) while (True): is_new = True for base in exclude: if Vector2(new_x, new_y) == base.pos: is_new = False new_x = randint(0, local_w) new_y = randint(0, local_h) if is_new: break self.pos = Vector2(new_x, new_y)
class Movement(): UP = Vector2(0, -1) DOWN = Vector2(0, 1) LEFT = Vector2(-1, 0) RIGHT = Vector2(1, 0) def __init__(self): self.direction = self.LEFT self.last_direction = self.LEFT def updateAllDir(self, vec, prev): self.direction = vec if Movement.getOpposite( vec) != self.direction and Movement.getOpposite( vec) != prev.last_direction else self.direction self.last_direction = vec if Movement.getOpposite( vec) != self.direction and Movement.getOpposite( vec) != prev.last_direction else self.direction @staticmethod def getOpposite(vec): return vec.vecxScalar(-1)
def render(self, canvas, modifier=Vector2(0, 0)): tmp_mesh = list(self.mesh) x = True for i in range(len(tmp_mesh)): if x == True: tmp_mesh[i] = tmp_mesh[ i] * self.scale + self.pos.x * self.scale + modifier.x * self.scale x = False else: tmp_mesh[i] = tmp_mesh[ i] * self.scale + self.pos.y * self.scale + modifier.y * self.scale x = True canvas.create_polygon(tmp_mesh, fill=self.color)
def __init__(self, xr, yr): self._vec = Vector2(xr, yr)
def __init__(self, scale=1, pos=Vector2(0, 0), color="green"): Base.__init__(self, scale, pos) self.blocks = [] self.color = color
def __init__(self, w, h, scale=1, color="red"): Block.__init__(self, scale, Vector2(0, 0), color) self.w = w self.h = h self.randomize()
def __init__(self, scale=1, pos=Vector2(0, 0), color="black"): Mesh.__init__(self, scale, pos, [0, 0, 0, 1, 1, 1, 1, 0], color) Movement.__init__(self)
def __init__(self, scale=1, pos=Vector2(0, 0)): self.scale = scale self.pos = pos
def __init__(self, scale=1, pos=Vector2(0, 0), mesh=[], color="black"): Base.__init__(self, scale, pos) self.mesh = mesh self.color = color