def __init__(self): """ Starts with speed zero on the upper left corner """ self.pos = tools.Vector2(constants.DEF_Px, constants.DEF_Py) self.direction = constants.STOP self.sprite = theme.sprite('default/hero.png', 0.4)
def random_pos(): """ Calculates a valid random position for the ball :return: a valid random position """ return tools.Vector2( random.randint(1 + int(0.1 * constants.SCREEN_SIZE[0]), int(0.9 * constants.SCREEN_SIZE[0])), random.randint(int(1 + 0.1 * constants.SCREEN_SIZE[1]), int(0.9 * constants.SCREEN_SIZE[1])))
def update(self, grid): """ given the actual game state, calculates the next position of the ball and its next direction (if the ball collide with the wall). And checks if the ball killed the hero. :param grid: game state :return: returns if the ball killed or not the hero """ nextPos = tools.Vector2( self.pos.x + constants.BALL_RADIUS * tools.sign(self.speed.x), self.pos.y + constants.BALL_RADIUS * tools.sign(self.speed.y)) actualGrid = tools.discretize(self.pos) nextGrid = tools.discretize(nextPos) #horizontal reflection on walls if tools.valid(nextGrid[0], actualGrid[1], grid) and grid[nextGrid[0]][ actualGrid[1]] == constants.CONQUERED or not tools.valid_x( nextGrid[0], grid): self.speed.x *= -1 nextPos.x = self.pos.x + self.speed.x nextGrid[0] = tools.conv(nextPos.x, 0) #vertical reflectio on walls if tools.valid( actualGrid[0], nextGrid[1], grid) and grid[actualGrid[0]][ nextGrid[1]] == constants.CONQUERED or not tools.valid_y( nextGrid[1], grid): self.speed.y *= -1 nextPos.y = self.pos.y + self.speed.y nextGrid[1] = tools.conv(nextPos.y, 1) #verify if player path was hit if tools.valid( nextGrid[0], nextGrid[1], grid) and grid[nextGrid[0]][nextGrid[1]] == constants.PROCESS: return constants.LOSE #new position self.pos = tools.Vector2(self.pos.x + self.speed.x, self.pos.y + self.speed.y) #may return 'LOSE' or 'UNDEFINED' states return constants.UNDEFINED
def random_spd(): """ Calculates a random direction for the ball :return: a random direction """ speed = tools.Vector2(constants.DEF_Vx, constants.DEF_Vy) if random.randint(-1, 1) < 0: speed.x *= -1 if random.randint(-1, 1) < 0: speed.y *= -1 return speed
def update(self, grid): """ This ball is capable of destroying the wall, so this function updates the ball position, destroys the wall and check if the ball killed the hero. :param grid: current state of the game :return: returns if the ball killed or not the hero """ self.destructedBlocks = 0 nextPos = tools.Vector2( self.pos.x + constants.BALL_RADIUS * (self.speed.x / abs(self.speed.x)), self.pos.y + constants.BALL_RADIUS * self.speed.y / abs(self.speed.y)) actualGrid = tools.discretize(self.pos) nextGrid = tools.discretize(nextPos) if grid[nextGrid[0]][actualGrid[1]] == constants.CONQUERED: if nextGrid[0] > 0 and nextGrid[0] < len(grid) - 1: grid[nextGrid[0]][actualGrid[1]] = constants.NOTHING self.destructedBlocks -= 1 self.speed.x *= -1 nextPos.x = self.pos.x + self.speed.x nextGrid[0] = tools.conv(nextPos.x, 0) if grid[actualGrid[0]][nextGrid[1]] == constants.CONQUERED: if nextGrid[1] > 0 and nextGrid[1] < len(grid[actualGrid[0]]) - 1: grid[actualGrid[0]][nextGrid[1]] = constants.NOTHING self.destructedBlocks -= 1 self.speed.y *= -1 nextPos.y = self.pos.y + self.speed.y nextGrid[1] = tools.conv(nextPos.y, 1) if grid[nextGrid[0]][nextGrid[1]] == constants.PROCESS: return constants.LOSE self.pos = tools.Vector2(self.pos.x + self.speed.x, self.pos.y + self.speed.y) return constants.UNDEFINED
def __init__(self, x, y, width, maxValue, action, value=0, share=None): """ :param x: horizontal position :param y: vertical position :param width: width of the slider :param maxValue: maximum value of the slider :param action: function that will be called when the user clicks the slider :param value: current value of the slider """ self.action = action self.width = width self.height = 50 self.maxValue = maxValue self.value = value self.old_value = value self.shortcut = [constants.NOKEY] Elements.__init__(self, x, y) self.pointerPos = tools.Vector2( self.x + int(self.value * self.width / self.maxValue), self.y) self.backgroundColor = theme.sliderBackColor self.icon = share self.turn_off()
def __init__(self): ball.Ball.__init__(self) self.pos = tools.Vector2(400, 300) self.destructedBlocks = 0
def setValue(self, value): self.value = value self.pointerPos = tools.Vector2( self.x + int(self.value * self.width / self.maxValue), self.y)