def __init__(self, speed, rect_width, rect_height, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.speed = speed self.pos = vec2d(pos_init) self.vel = vec2d((0, 0)) self.rect_height = rect_height self.rect_width = rect_width self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((rect_width, rect_height)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect( image, (255, 255, 255), (0, 0, rect_width, rect_height), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def init(self): """ Starts/Resets the game to its inital state """ self.creep_counts = {"GOOD": 0, "BAD": 0} if self.player is None: self.player = Player(self.AGENT_RADIUS, self.AGENT_COLOR, self.AGENT_SPEED, self.AGENT_INIT_POS, self.width, self.height) else: self.player.pos = vec2d(self.AGENT_INIT_POS) self.player.vel = vec2d((0.0, 0.0)) if self.creeps is None: self.creeps = pygame.sprite.Group() else: self.creeps.empty() for i in range(int(self.N_CREEPS / 2)): self._add_creep(type=1) for i in range(int(self.N_CREEPS / 2)): self._add_creep(0) self.score = 0 self.ticks = 0 self.lives = -1
def __init__(self, radius, speed, rng, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.rng = rng self.radius = radius self.speed = speed self.pos = vec2d(pos_init) self.pos_before = vec2d(pos_init) self.vel = vec2d((speed, -1.0 * speed)) self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((radius * 2, radius * 2)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.circle( image, (255, 255, 255), (radius, radius), radius, 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, square_width, speed, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.square_width = square_width self.speed = speed self.pos = vec2d(pos_init) self.pos_before = vec2d(pos_init) self.vel = vec2d((0, 0)) self.is_shoot = False self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((square_width, square_width)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect(image, (255, 255, 255), (0, 0, square_width, square_width), 0) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, radius, color, speed, screen_width, screen_height): Agent.__init__(self, radius, color, speed, screen_width, screen_height) image = pygame.Surface([radius * 2, radius * 2]) image.set_colorkey((0, 0, 0)) pygame.draw.circle(image, color, (radius, radius), radius, 0) self.image = image.convert() self.rect = self.image.get_rect() self.direction = vec2d((random() - 0.5, random() - 0.5)) self.direction.normalize()
def _rand_start(self, agents): pos = [] for agent in agents: pos_x = uniform(agent.radius, self.width - agent.radius) pos_y = uniform(agent.radius, self.height - agent.radius) pos.append(vec2d((pos_x, pos_y))) for i in range(len(agents)): for j in range(i + 1, len(agents)): dist = math.sqrt((pos[i].x - pos[j].x)**2 + (pos[i].y - pos[j].y)**2) while dist <= (agents[i].radius + agents[j].radius): pos[i].x = uniform(agents[i].radius, self.width - agents[i].radius) pos[i].y = uniform(agents[i].radius, self.height - agents[i].radius) dist = math.sqrt((pos[i].x - pos[j].x)**2 + (pos[i].y - pos[j].y)**2) return pos
def __init__(self, radius, color, speed, screen_width, screen_height): Agent.__init__(self, radius, color, speed, screen_width, screen_height) self.out_radius = radius * 4 image = pygame.Surface([self.out_radius * 2, self.out_radius * 2]) image.set_colorkey((0, 0, 0)) pygame.draw.circle(image, (150, 95, 95), (self.out_radius, self.out_radius), self.out_radius, 2) image.set_alpha(int(255 * 0.75)) pygame.draw.circle(image, color, (self.out_radius, self.out_radius), radius, 0) self.image = image.convert() self.rect = self.image.get_rect() self.direction = vec2d((0, 0))
def move(self, dt, other_agents): flag = True for i in range(0, len(other_agents)): if pygame.sprite.collide_circle(self, other_agents[i]): x = self.rect.center[0] - other_agents[i].rect.center[0] y = self.rect.center[1] - other_agents[i].rect.center[1] self.direction = vec2d((x, y)) flag = False if flag: self.direction.x += self.dx self.direction.y += self.dy new_x = self.pos.x + self.direction.x * dt new_y = self.pos.y + self.direction.y * dt if new_x >= self.SCREEN_WIDTH - self.radius: self.pos.x = self.SCREEN_WIDTH - self.radius self.direction.x = 0 elif new_x < self.radius: self.pos.x = self.radius self.direction.x = 0 else: self.pos.x = new_x if new_y > self.SCREEN_HEIGHT - self.radius: self.pos.y = self.SCREEN_HEIGHT - self.radius self.direction.y = 0 elif new_y < self.radius: self.pos.y = self.radius self.direction.y = 0 else: self.pos.y = new_y self.rect.center = (self.pos.x, self.pos.y)