def generate(busters_cnt, ghosts_cnt): list_busters_pos = [[1176, 2024], [2024, 1176], [14824, 6976], [13976, 7824]] list_busters_types = [0, 0, 1, 1] list_ghosts_lives = [5, 15, 40] busters = { i: Entity( f"{i} {list_busters_pos[i][0]} {list_busters_pos[i][1]} {list_busters_types[i]} 5 0" ) for i in range(busters_cnt * 2) } ghosts = {i: Entity(f"{i} 8000 4500 -1 5 0") for i in range(ghosts_cnt)} lives = list_ghosts_lives[round(random.uniform(0, 16000)) % 3] ghosts[0].state = lives for i in range(1, ghosts_cnt, 2): lives = list_ghosts_lives[round(random.uniform(0, 16000)) % 3] ghosts[i].state = lives ghosts[i + 1].state = lives x = round(random.uniform(0, 16000)) ghosts[i].x = x ghosts[i + 1].x = 16000 - x y = round(random.uniform(0, 9000)) ghosts[i].y = y ghosts[i + 1].y = 9000 - y return busters, ghosts
def __init__(self, tier=1, speed=3, angle=0, pos=(0, 0)): # Tier determines the size, and how many times it breaks apart size = (tier * 15, tier * 15) Entity.__init__(self, size, pos) self.tier = tier self.velocity.from_polar((speed, angle)) self.angle = angle self.position = Vector(pos) # The angle spread of the children when the asteroid breaks apart self.spread = 20 first = (size[0] - 1) / 3 second = first * 2 third = first * 3 octagon = [ (first, 0), # top 1 (second, 0), # top 2 (third, first), # right 1 (third, second), # right 2 (second, third), # bottom 1 (first, third), # bottom 2 (0, second), # left 1 (0, first) # left 2 ] draw_polygon(self.image, COLOR.WHITE, octagon, 2) Asteroid.group.add(self)
def __init__(self, pos=SCREEN_CENTER): Entity.__init__(self, (18, 18), pos) # Acceleration vector that will be rotated and added to velocity self.acceleration = Vector(0, -0.2) # controls how quickly the ship slows down self.slow_speed = 0.1 # controls how quickly the ship rotates and slows its rotation self.rotate_increment = 0.5 self.slow_rotation = self.rotate_increment / 2 # 10 frame cooldown, start at 0 self.fire_cooldown = 0 # Group of all lasers for collision purposes self.lasers = SpriteGroup() # Draws an arrow facing in the direction of angle to serve as the ship size = self.image.get_size() arrow_points = [ (0, size[1] - 1), # bottom left ((size[0] - 1) / 2, 0), # top middle (size[0] - 1, size[1] - 1) # bottom right ] draw_lines(self.image, COLOR.WHITE, False, arrow_points, 2)
def kill(self): if self.tier > 1: # Spawn two new asteroids of a tier lower speed = self.velocity.length() new_tier = self.tier - 1 angle1 = self.angle - self.spread angle2 = self.angle + self.spread asteroid1 = Asteroid(new_tier, speed, angle1, self.position) asteroid2 = Asteroid(new_tier, speed, angle2, self.position) Asteroid.number_destroyed += 1 Entity.kill(self)
def __init__(self, pos=(0, 0), angle=0): Entity.__init__(self, (5, 2), pos) # 60 frames = 1 second self.lifetime = 60 self.orig_img.fill(COLOR.WHITE) # angle 0 points right, so subtract 90 degrees angle = angle - 90 self.velocity.from_polar((self.max_velocity, angle)) self.rotate(angle)
def update(self): keys = get_pressed() if keys[K_LEFT]: self.rotation_speed -= self.rotate_increment if keys[K_RIGHT]: self.rotation_speed += self.rotate_increment if keys[K_UP]: self.velocity += self.acceleration if keys[K_z]: self.fire() Entity.update(self)
def __init__(self, *args): """Create a viewport entity that displays contents of a camera inside the world. Arguments are either (Camera, int, int) for a new camera or (Camera) for an existing one """ Entity.__init__(self) if len(args) == 1: self.camera = args[0] elif len(args) == 3: scene = args[0] width = args[1] height = args[2] self.camera = Camera(scene, width, height) else: raise TypeError, "Viewport's constructor takes either 4 or 2 arguments, %i given" % len(args)+1
def __init__(self, scr_w: int, scr_h: int) -> None: self.console = tcod.console_new(scr_w, scr_h) self.stage: Stage = Stage(scr_w, scr_h) self.entities: List[Entity] = [] self.player: Entity = Entity() self.entities.append(self.player) self._construct()
def __init__(self, entity: Optional[Entity] = None) -> None: if entity is None: self.entity: Entity = Entity() else: self.entity: Entity = entity self.entity.body = Body(self.entity, Pos(0, 0))
def calc_rotation(self): """Calculates the next angle in the rotation""" # Slow the rotation if self.rotation_speed > 0: self.rotation_speed -= self.slow_rotation elif self.rotation_speed < 0: self.rotation_speed += self.slow_rotation return Entity.calc_rotation(self)
def calc_position(self): # simulate friction to slow the ship friction = self.velocity.length() - self.slow_speed if friction <= 0: friction = 0 try: self.velocity.scale_to_length(friction) except ValueError: # Fixes vector scaling issues self.velocity = Vector() return Entity.calc_position(self)
def test_engine_file(self): blocks = [] current_block = None file_name_new = '/home/miron/work/cg-CodeBusters/tests/game_v2_4.txt' block_starters = ['INIT:\n', 'INPUT:\n', 'OUTPUT:\n'] block_stoppers = ['INIT:\n', 'INPUT:\n', 'OUTPUT:\n', '\n'] with open(file_name_new, 'r') as f: lines = f.readlines() for line in lines: if line in block_stoppers and current_block is not None: current_block = None if line in block_starters: current_block = [] blocks.append(current_block) if current_block is not None and line not in block_stoppers and line not in block_starters: current_block.append(line[:-1]) busters_count, ghosts_count = map(int, blocks[0][:2]) del blocks[0], blocks[2] steps1 = list(zip(blocks[0::4], blocks[1::4])) steps2 = list(zip(blocks[2::4], blocks[3::4])) busters, ghosts = {}, {} for step_ in zip(steps1, steps2): for j in range(2): for i in step_[j][0]: id_, x, y, type_, state, value = i.split() if type_ == '-1' and int(id_) not in ghosts: ghosts[int(id_)] = Entity(i) if type_ != '-1' and int(id_) not in busters: busters[int(id_)] = Entity(i) g = Engine(busters_count, ghosts_count, busters, ghosts) for i in range(len(steps1)): a = g.get_info(0) b = g.get_info(1) self.assertEqual('\n'.join(steps1[i][0]), a[:-1]) self.assertEqual('\n'.join(steps2[i][0]), b[:-1]) if i < len(steps1) - 1: g.do(steps1[i][1], steps2[i][1])
def init(self): self.text_font = pygame.font.Font('freesansbold.ttf', 13) self.text_font2 = pygame.font.Font('freesansbold.ttf', 20) self.ball = Ball(np.array([280, 454]), 10.0, Material(THECOLORS['green'])) wall_material = Material(THECOLORS['black']) wall_width = 5.0 self.walls = [ Wall(np.array([0, 0]), float(self.width), wall_width, wall_material, "UpperWall"), Wall(np.array([0, 0]), wall_width, float(self.height), wall_material, "LeftWall"), Wall(np.array([self.width - wall_width, 0]), wall_width, float(self.height), wall_material, "RightWall"), Wall(np.array([self.width - 25.0, self.height - 35.0]), 10.0, 35.0, wall_material, "Plunger"), Entity(ConvexPolygon( np.array([[self.width - 40, 0], [self.width, 0], [self.width, 50], [self.width - 40, 20]]), Transform(np.array([self.width, self.height]))), wall_material, 100, name="TopRight"), Entity(ConvexPolygon( np.array([[5, self.height - 69], [5, self.height - 97], [57.91117863822124, 434.5569177391425], [46.08882136177876, 455.4430822608575]]), Transform(np.array([5, self.height - 69]))), wall_material, 100, name="LeftArm"), Entity(ConvexPolygon( np.array([[self.width - 40, self.height - 97], [self.width - 40, self.height - 69], [215.91117863822123, 455.4430822608575], [204.08882136177877, 434.5569177391425]]), Transform(np.array([self.width - 40, self.height - 97]))), wall_material, 100, name="RightArm"), ] self.valve = Valve( Rectangle(5.0, self.height - 100.0, Transform(np.array([self.width - 40, 100]))), Rectangle(5.0, float(self.height), Transform(np.array([self.width - 40, 0]))), wall_material, "MiddleWall") bumper_material = Material(THECOLORS['red'], restitution=1.2) p = np.array([60, 100]) p2 = np.array([200, 150]) w = np.array([15, 0]) h = np.array([0, 30]) self.bumpers = [ Bumper(ConvexPolygon(np.array([p - w, p + h, p + w, p - h]), Transform(p)), material=bumper_material, name="Bumper1", points=20), Bumper(ConvexPolygon(np.array([p2 - w, p2 + h, p2 + w, p2 - h]), Transform(p)), material=bumper_material, name="Bumper2", points=20), # Bumper(Circle(10.0, Transform(np.array(np.array([70, 320])))), material=bumper_material, name="Bumper3"), # Bumper(Circle(10.0, Transform(np.array(np.array([190, 320])))), material=bumper_material, name="Bumper4"), Bumper(Circle(20.0, Transform((p + p2) / 2 + 3 * h)), material=bumper_material, name="Bumper5", points=5), ] flipper_material = Material(THECOLORS['red'], restitution=0.7) self.left_flipper = Flipper(np.array([52, self.height - 55]), np.array([102, self.height - 25]), -math.pi / 4, flipper_material, 'LeftFlipper') self.right_flipper = Flipper( np.array([self.width - 90, self.height - 55]), np.array([self.width - 140, self.height - 25]), math.pi / 4, flipper_material, 'RightFlipper')
def get_local_boids(self, boid: Entity): return [ other_boid for other_boid in self.boids if boid.distance_to(other_boid) < boid.local_radius and boid != other_boid ]
def rotate(self, angle=0): self.acceleration.rotate_ip(self.rotation_speed) Entity.rotate(self, angle)
def kill(self): self.lasers.empty() Entity.kill(self)
current_block = [] blocks.append(current_block) if current_block is not None and line not in block_stoppers and line not in block_starters: current_block.append(line[:-1]) busters_count, ghosts_count = map(int, blocks[0][:2]) player0, player1 = Game('\n'.join(blocks[0])), Game('\n'.join(blocks[3])) del blocks[0], blocks[2] steps1 = list(zip(blocks[0::4], blocks[1::4])) steps2 = list(zip(blocks[2::4], blocks[3::4])) busters, ghosts = {}, {} for step_ in zip(steps1, steps2): for j in range(2): for i in step_[j][0]: id_, x, y, type_, state, value = i.split() if type_ == '-1' and int(id_) not in ghosts: ghosts[int(id_)] = Entity(i) if type_ != '-1' and int(id_) not in busters: busters[int(id_)] = Entity(i) states = [] #busters_count = 2 #ghosts_count = 501 #busters, ghosts = generate(busters_count, ghosts_count) #player0, player1 = Game(str(busters_count)+"\n"+str(ghosts_count)+"\n0"), Game(str(busters_count)+"\n"+str(ghosts_count)+"\n1") engine = Engine(busters_count, ghosts_count, busters, ghosts) player0.update(engine.get_info(0)[:-1]) player1.update(engine.get_info(1)[:-1]) mind = Mind(player0)
def update(self): Entity.update(self) self.lifetime -= 1 if self.lifetime <= 0: self.kill()
playertile = (0, 0) playerposition = (0, 0) pygame.init() pygame.display.set_caption('~ R I K \' S G A M E ~') pygame.display.set_icon(pygame.image.load('icon.png')) clock = pygame.time.Clock() w = World(size=1024, tilesize=32) s = Screen(800, 800) gs = GraphicsSystem() cs = ControlSystem() ss = SpriteSystem() p = Entity(w) p.camera = Camera(0, 0, 800, 800, track=True, entitytotrack=p) p.position = Position(0, 0, 32, 32, 2) p.control = Control(pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT) p.state = State() p.sprite = Sprite() p.sprite.sprites['idle'] = [ pygame.image.load('images/player/idle.png'), pygame.image.load('images/player/idle2.png'), pygame.image.load('images/player/idle3.png') ] p.sprite.animate = True running = True