def test_opacity_max_boundry(self): """Ensure opacity is not set above maximum allowable level.""" a = Actor('alien') a.opacity = 1.1 self.assertEqual(a.opacity, 1.0)
def test_opacity_min_boundry(self): """Ensure opacity is not set below minimum allowable level.""" a = Actor('alien') a.opacity = -0.1 self.assertEqual(a.opacity, 0.0)
def fire(self, key): if key == keys.SPACE: bullet = Actor('bullet') bullet.x = self.actor.x bullet.y = self.actor.y - 100 self.bullets.append(bullet) sounds.shoot.play()
def __init__(self, ship_type, grid, grid_pos, direction, hidden=False): Actor.__init__(self, ship_type, (10, 10)) self.ship_type = ship_type self.grid = grid self.image = ship_type self.grid_pos = grid_pos self.topleft = self.grid.grid_pos_to_screen_pos((grid_pos)) # Set the actor anchor position to centre of the first square self.anchor = (38 / 2, 38 / 2) self.direction = direction if (direction == 'vertical'): self.angle = -90 self.hidden = hidden if (ship_type == "destroyer"): self.ship_size = 2 self.hits = [False, False] elif (ship_type == "cruiser"): self.ship_size = 3 self.hits = [False, False, False] elif (ship_type == "submarine"): self.ship_size = 3 self.hits = [False, False, False] elif (ship_type == "battleship"): self.ship_size = 4 self.hits = [False, False, False, False] elif (ship_type == "carrier"): self.ship_size = 5 self.hits = [False, False, False, False, False]
def fire(self): bullet = Actor('bullet', pos=self.pos) ang = math.radians(self.angle) bullet.exact_pos = bullet.start_pos = Vector2(self.pos) bullet.velocity = Vector2(math.sin(ang), math.cos(ang)).normalize() * 1000.0 return bullet
def __init__(self, image, pos, enemy_type, path_index=0, anchor=None, **kwargs): Actor.__init__(self, image, pos, anchor, **kwargs) self.path1 = [(1000, 325), (925, 325), (925, 475), (775, 475), (775, 225), (675, 225), (675, 350), (600, 350), (600, 275)] self.path2 = [(700, 575), (700, 525), (425, 525), (425, 375), (450, 375), (450, 275), (400, 275), (400, 125), (600, 125), (600, 275)] self.path3 = [(200, 75), (325, 75), (325, 525), (400, 525), (400, 375), (450, 375), (450, 275), (400, 275), (400, 125), (600, 125), (600, 275)] self.path4 = [(200, 75), (325, 75), (325, 525), (400, 525), (400, 375), (450, 375), (450, 275), (400, 275), (400, 125), (600, 125), (600, 275), (600, 350), (675, 350), (675, 225), (775, 225), (775, 475), (925, 475), (925, 325), (1000, 325)] self.path_index = path_index self.type = enemy_type self.speed_list = [0.2, 0.5, 1, 0.2, 1] self.speed = self.speed_list[self.type - 1] self.health_list = [20, 50, 50, 100, 100] self.health = self.health_list[self.type - 1] self.appear_time = time.time() self.boss_born_time = 0 self.frozen = False self.frozen_time = 0
def __init__(self, image=None, pos=None, speed=None, orbit_center=None, stage=None, center_drawing_color=None, pos_drawing_color=None, rect_drawing_color=None, **kwargs): """Create a game object with ``image`` and ``center`` position. In contrast with ``Actor`` all parameters are optional in order to support client-side initialization by setting attributes. When an ``image`` is missing, we use a 1x1 pixel transparent image instead. If ``center_drawing_color``, ``pos_drawing_color``, or ``rect_drawing_color`` are given, then this class will draw a center point, a coordinate tuple, or a bounding rectangle respectively. """ Actor.__init__(self, image, pos=pos, **kwargs) if speed is None: speed = self.DEFAULT_SPEED self.speed = speed self.orbit_center = orbit_center self.total_orbit_angle = 0 self.stage = stage self.rect_drawing_color = rect_drawing_color self.center_drawing_color = center_drawing_color self.pos_drawing_color = pos_drawing_color
def __init__(self, name, back_image, card_image): Actor.__init__(self, back_image, (0, 0)) self.name = name self.back_image = back_image self.card_image = card_image # Status can be 'back' (turned over) 'front' (turned up) or 'hidden' (already used) self.status = 'back'
def __init__(self, x, y, image): self.x = x self.y = y self.image = image self.actor = Actor(image) self.actor.x = x self.actor.y = y
class Pipes: GAP = 130 SPEED = 3 def __init__(self, game): self.game = game self.pipe_top = Actor('top', anchor=('left', 'bottom')) self.pipe_bottom = Actor('bottom', anchor=('left', 'top')) def reset(self): pipe_gap_y = random.randint(200, self.game.scene.height - 200) self.pipe_top.pos = (self.game.scene.width, pipe_gap_y - self.GAP // 2) self.pipe_bottom.pos = (self.game.scene.width, pipe_gap_y + self.GAP // 2) def checkcollision(self, sprite): return sprite.colliderect(self.pipe_top) or sprite.colliderect( self.pipe_bottom) def draw(self): self.pipe_top.draw() self.pipe_bottom.draw() def update(self, con): self.pipe_top.left -= self.SPEED self.pipe_bottom.left -= self.SPEED if self.pipe_top.right < 0: self.reset() if not con.dead: con.score += 1
class Background: image = None x = None y = None bg1 = None bg2 = None scroll_speed = 1 def __init__(self, x, y, image, x2, y2, image2): self.x = x self.y = y self.image = image self.image2 = image2 self.bg1 = Actor(image) self.bg1.x = x self.bg1.y = y self.bg2 = Actor(image2) self.bg2.x = x2 self.bg2.y = y2 def draw(self): self.bg1.draw() self.bg2.draw() def update(self): if self.bg1.y > 1854: self.bg1.y = -2642 if self.bg2.y > 1854: self.bg2.y = -2642 self.bg1.y += 1 self.bg2.y += 1
def test_opacity_value(self): """Ensure opacity gives the value it was set to.""" a = Actor('alien') expected_opacity = 0.54321 a.opacity = expected_opacity self.assertEqual(a.opacity, expected_opacity)
def __init__(self, x, y, obrot, obraz): Actor.__init__(self, obraz) self.pos = (x, y) self.x = x self.y = y self.obraz = obraz self.size = (10, 10) self.obrot = obrot
def draw(self): Actor.draw(self) # 绘制爆炸效果 if self.type == 3 and self.booming: boom1 = Actor("boom", (self.x + 25, self.y - 30)) boom1.draw() boom2 = Actor("boom", (self.x - 25, self.y - 30)) boom2.draw()
def __init__(self, x, y, image, speed, life): self.x = x self.y = y self.speed = speed self.image = image self.actor = Actor(image) self.actor.x = x self.actor.y = y self.life = life
def __init__(self, theme, theme_num, player_image_format, screen_width, screen_height): self.theme = theme self.theme_num = theme_num self.player_image_format = player_image_format self.screen_width = screen_width self.screen_height = screen_height # Call Actor constructor (center the Actor) Actor.__init__(self, self.getImage(), (self.screen_width / 2, self.screen_height / 2))
def __init__(self, image, pos, tower_type, anchor=None, **kwargs): Actor.__init__(self, image, pos, anchor, **kwargs) self.type = tower_type self.range = 150 self.in_range = False self.cost_list = [50, 100, 200] self.cost = self.cost_list[self.type - 1] self.shoots = [] self.last_attack_time = time.time() self.booming = False
def __init__(self, name, back_image, card_image): Actor.__init__(self, back_image, (0, 0)) self.name = name self.back_image = back_image self.card_image = card_image # Status can be 'back' (turned over) 'front' (turned up) or 'hidden' (already used) self.status = 'back' # Number is unique number based on position # count left to right, top to bottom # updated after dealt self.number = None
def __init__(self, y): self.y = y self.x1 = 0 self.x2 = self.WIDTH self.baseimage = Actor('base2', anchor=(0, 0)) self.baseimage2 = Actor('base2', anchor=(0, 0)) self.baseimage.pos = (self.x1, self.y) self.baseimage2.pos = (self.x2, self.y)
def __init__(self, x, y, image, x2, y2, image2): self.x = x self.y = y self.image = image self.image2 = image2 self.bg1 = Actor(image) self.bg1.x = x self.bg1.y = y self.bg2 = Actor(image2) self.bg2.x = x2 self.bg2.y = y2
def __init__(self, x, y, cell_width, cell_height, parent): self.status = 0 self._next = 0 self.x = x self.y = y self.cell_width = cell_width self.cell_height = cell_height self.parent = parent self._actor = Actor(self.get_image(), self.get_pos(), anchor=('left', 'top'))
def draw_players_hands(): for p, player in enumerate(game.game.players): color = 'red' if player == game.game.current_player else 'black' text = 'P{} {}'.format(p, 'wins' if game.game.winner == player else '') screen.draw.text(text, (0, 300 + p * 130), fontsize=100, color=color) for c, card in enumerate(player.hand): if player == game.player: sprite = card.sprite else: sprite = Actor('back') sprite.pos = (130 + c * 80, 330 + p * 130) sprite.draw()
def fire (self, pos): # Is this a hit for this_ship in self.ships: if (this_ship.fire(pos)): # Hit self.shots.append(Actor("hit",topleft=self.grid.grid_pos_to_screen_pos(pos))) #check if this ship sunk if this_ship.is_sunk(): # Ship sunk so make it visible this_ship.hidden = False return True self.shots.append(Actor("miss",topleft=self.grid.grid_pos_to_screen_pos(pos))) return False
def __init__(self, game, number, *args, **kwargs): self.number = number self.game = game image_name = 'players/p{}/p{}_stand'.format(self.number, self.number) super().__init__(image_name, *args, **kwargs) self.speed = [0, 0] self.facing_left = False self.last_x = self.last_y = 0 self.move_distance = 0 self.carrying = False self.putting_down = False self.spawn() self.highlight = Actor('players/highlight', anchor=(0, 70)) self.direction = Actor('players/direction_1', anchor=(0, 70))
def attack(self, enemies): tamp = time.time() min_dis = 100000 min_dis_enemy = 0 self.in_range = False # 判断病毒是否在范围内,并找到距离最短的病毒 for enemy in enemies: x = enemy.x y = enemy.y dis = math.sqrt((x - self.x)**2 + (y - self.y)**2) if dis < self.range: self.in_range = True if dis < min_dis: min_dis = dis min_dis_enemy = enemy # 发射子弹 if self.in_range and tamp - self.last_attack_time > 1: self.shoots.append(Actor("shoot", (self.x, self.y))) self.shoots[-1].speed_x = (min_dis_enemy.x - self.x) / 20 self.shoots[-1].speed_y = (min_dis_enemy.y - self.y) / 20 self.last_attack_time = tamp # 子弹运动 for shoot in self.shoots: shoot.x += shoot.speed_x shoot.y += shoot.speed_y
def __new__(typ, *args, **kwargs): result = Actor.__new__(typ) # Actor's image is mandatory, because it bases # all _rect-operations on it. # So we need to initialize a default image: GameObj.__init__(result) return result
def setUp(self): # the Alien should be 66 x 92 px self.actor = Actor('alien', pos=(100, 150), anchor=('left', 'top')) self.separate_rect = Rect((0, 20), (20, 300)) self.overlapping_rect = Rect((120, 100), (100, 100)) self.enclosed_rect = Rect((110, 160), (10, 10)) self.enclosing_rect = Rect((0, 0), (500, 500))
def updateMain(self, keyboard): if (self.game_controls.isPressed(keyboard,'up')): self.selected_row = 0 self.selected_col = self.checkColPos(self.selected_col, self.selected_row) if (self.game_controls.isPressed(keyboard,'down')): self.selected_row = 1 self.selected_col = self.checkColPos(self.selected_col, self.selected_row) if (self.game_controls.isPressed(keyboard,'right')): self.selected_col = self.checkColPos(self.selected_col + 1, self.selected_row) if (self.game_controls.isPressed(keyboard,'left')): self.selected_col = self.checkColPos(self.selected_col - 1, self.selected_row) if (self.game_controls.isOrPressed(keyboard,['jump','duck'])): # If pressed on top row then update theme if (self.selected_row == 0): (self.theme, self.theme_num) = self.current_themes[self.selected_col] # If pressed on second row then customize theme elif (self.selected_row == 1): # Reset position self.selected_row_custom = 0 self.selected_colour_custom = -1 self.customize_theme = self.available_themes[self.selected_col] self.status = STATUS_CUSTOM self.preview = Actor (self.img_file_format.format(self.customize_theme, "00", "down", "01"), (700,150)) self.theme_config.loadConfig(self.customize_theme) self.pause_timer.startCountDown() return 'character' return 'menu' self.pause_timer.startCountDown()
def test_setting_absolute_initial_pos(self): a = Actor("alien", pos=(100, 200), anchor=("right", "bottom")) self.assertEqual( a.topleft, (100 - a.width, 200 - a.height), )
def draw(self): """Draw image and additional artifacts. This additionally draws a center point, a coordinate tuple, or a bounding rectangle, if the respective attributes have been set to a color. """ Actor.draw(self) if getattr(self, "center_drawing_color", None) is not None: _PGZ.screen.draw.filled_circle(self.center, 5, self.center_drawing_color) if getattr(self, "rect_drawing_color", None) is not None: _PGZ.screen.draw.rect(self.rect, self.rect_drawing_color) if getattr(self, "pos_drawing_color", None) is not None: _PGZ.screen.draw.text(("(%d,%d)" % (round(self.x), round(self.y))), midtop=(self.center), color=self.pos_drawing_color)
def test_set_pos_relative_to_anchor(self): a = Actor("alien", anchor=(10, 10)) a.pos = (100, 100) self.assertEqual(a.topleft, (90, 90))