class Mover(): def __init__(self): self.radius = 16 # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-2, 2) v_y = random.randrange(-2, 2) self.velocity = PVector(v_x, v_y) # Farbe self.color = (239, 242, 63) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): self.position.add(self.velocity) if (self.position.x >= WIDTH + self.radius): self.position.x = -self.radius elif (self.position.x <= -self.radius): self.position.x = WIDTH - self.radius if (self.position.y >= HEIGHT + self.radius): self.position.y = -self.radius elif (self.position.y <= -self.radius): self.position.y = HEIGHT - self.radius
class Being(object): def __init__(self, canvas, width, height): self.canvas = canvas self.color = (0, 0, 200) self.speed = 4 self.sight_dist = 10 self.location = PVector(randint(0, width), randint(0, height)) self.velocity = PVector(randint(-self.speed, self.speed), randint(-self.speed, self.speed)) def update(self): self.location.add(self.velocity) def draw(self): pygame.draw.rect(self.canvas, self.color, [self.location.x, self.location.y, 4, 4]) def check_edges(self, width, height): if self.location.x > width: self.location.x = 0 elif self.location.x < 0: self.location.x = width - 4 if self.location.y > height: self.location.y = 0 elif self.location.y < 0: self.location.y = height - 4 def know_beings(self, humans, zombies): self.humans = humans self.zombies = zombies
def tend_to_place(agents, current): target = PVector(0, -14) position = PVector(current.xyz[0], current.xyz[1]) target.subVector(position) target.divScalar(50) target.normalize() return target.return_as_vector()
class Ball(): def __init__(self): self.radius = random.randrange(10, 20) # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Farbe self.color = random.choice(colorlist) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): self.position.add(self.velocity) if (self.position.x >= WIDTH - self.radius) or (self.position.x <= self.radius): self.velocity.x *= -1 if (self.position.y >= HEIGHT - self.radius) or (self.position.y <= self.radius): self.velocity.y *= -1
class Mover(object): def __init__(self, x, y, r): self.location = PVector(x, y) self.velocity = PVector(random.uniform(-5, 5), random.uniform(-5, 5)) self.radius = r def display(self): screen.draw.filled_circle((self.location.x, self.location.y), self.radius, (255, 0, 0)) screen.draw.circle((self.location.x, self.location.y), self.radius, (0, 0, 0)) def update(self): self.location.add(self.velocity) def check_edges(self): if (self.location.x > WIDTH - RADIUS): self.location.x = WIDTH - RADIUS self.velocity.x *= -1 elif (self.location.x < RADIUS): self.location.x = RADIUS self.velocity.x *= -1 if (self.location.y > HEIGHT - RADIUS): self.location.y = HEIGHT - RADIUS self.velocity.y *= -1 elif (self.location.y < RADIUS): self.location.y = RADIUS self.velocity.y *= -1
def __init__(self, origin, direction, speed, btype): pygame.sprite.Sprite.__init__(self, self.containers) self.btype = btype if btype == "fast": self.image = Bullet.fastimage self.rect = self.image.get_rect() elif btype == "strong": self.image = Bullet.strongimage self.rect = Bullet.strongrect elif btype == "tenseconds": self.image = Bullet.tensecondsimage self.rect = Bullet.tensecondsrect else: self.image = Bullet.fastimage self.rect = Bullet.fastrect self.rect = self.image.get_rect() self.origin = PVector(origin[0], origin[1]) self.aim = PVector(direction[0], direction[1]) self.speed = speed self.direction = self.aim - self.origin self.direction.mag = speed self.pos = [0, 0] self.pos[0] = self.origin[0] self.pos[1] = self.origin[1] self.rect = self.image.get_rect() self.mask = pygame.mask.from_surface(self.image) self.rect.center = (self.pos[0], self.pos[1])
class Particle(t.Turtle): def __init__(self, tshape, tcolor, x, y): t.Turtle.__init__(self) self.penup() self.shape(tshape) self.color(tcolor) self.speed = 1 self.max_speed = 10 self.location = PVector(x, y) self.setpos(self.location.x, self.location.y) self.acceleration = PVector(0, 0.05) self.velocity = PVector(r.uniform(-1.0, 1.0), r.uniform(-2.0, 0.0)) self.lifespan = 255 def update(self): self.velocity.add(self.acceleration) self.location.add(self.velocity) self.lifespan -= r.uniform(1.5, 3.0) if self.lifespan <= 0: self.lifespan = 0 self.color((round(self.lifespan), 0, 0)) self.setpos(self.location.x, self.location.y) def isDead(self): if self.lifespan <= 0: return True else: return False
def update(self): if not self.zombified: if not self.chased_by: # if not being chased, just make a random movement r = random() if r >= 0.50: self.velocity = PVector(randint(-self.speed, self.speed), randint(-self.speed, self.speed)) else: # move away from the zombie chasing this human change_x = 0 change_y = 0 if self.chased_by.location.x > self.location.x: change_x = -self.speed elif self.chased_by.location.x < self.location.x: change_x = self.speed if self.chased_by.location.y > self.location.y: change_y = -self.speed elif self.chased_by.location.y < self.location.y: change_y = self.speed self.velocity = PVector(change_x, change_y) Being.update(self)
def __init__(self, x, y, image_path=""): super(Sprite, self).__init__(x, y) # Placeholder for an image for the character # TODO: Replace this with an animation object that returns an image # every frame self.width = 32 self.height = 42 self.image = pygame.Surface([self.width, self.height]) self.image.fill(pygame.Color('cyan')) # Collider self.collider_t = Collider(self, 10, 10, xoff=0, yoff=-25) self.collider_b = Collider(self, 10, 10, xoff=0, yoff=25, color='blue') self.collider_rt = Collider(self, 10, 10, xoff=20, yoff=-15, color='red') self.collider_rb = Collider(self, 10, 10, xoff=20, yoff=15, color='red') self.collider_lt = Collider(self, 10, 10, xoff=-20, yoff=-15, color='yellow') self.collider_lb = Collider(self, 10, 10, xoff=-20, yoff=15, color='yellow') self.colliders = [ self.collider_t, self.collider_b, self.collider_rt, self.collider_rb, self.collider_lt, self.collider_lb ] # Possible states of the character. self.movingleft = False self.movingright = False self.jumping = False # Constant values that determine the movement of the character self.acc_force_val = 1.5 self.fri_force_val = .25 self.grv_force_val = .2 self.dec_force_val = 3 self.jmp_force_val = 10 self.jmp_force_val_end = 4 self.acc_force = PVector(self.acc_force_val, 0) self.dec_force = PVector(self.dec_force_val, 0) self.max_velo = 6
def __init__(self, canvas, width, height): self.canvas = canvas self.color = (0, 0, 200) self.speed = 4 self.sight_dist = 10 self.location = PVector(randint(0, width), randint(0, height)) self.velocity = PVector(randint(-self.speed, self.speed), randint(-self.speed, self.speed))
def __init__(self, im): super().__init__(im) # self.pos = (x, y) self.im = im self.rotspeed = randint(-5, 5) self.angle = 90 self.location = PVector(randint(0, HEIGHT), randint(0, WIDTH)) self.velocity = PVector(0, 0) self.topspeed = randint(6, 12)
def __init__(self): self.radius = 16 # Position und Velocity self.position = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = (239, 242, 63)
def __init__(self): super().__init__(WIDTH, HEIGHT, TITLE) arcade.set_background_color(((149, 224, 245))) self.movers = [] for i in range(NO_MOVERS): self.movers.append(Mover(random.uniform(0.5, 2.5), 15, HEIGHT - 15)) m = self.movers[i].mass self.gravity = PVector(0, -0.1*m) self.wind = PVector(0.005, 0.0)
def __init__(self, x, y, im, rotspeed): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.rotspeed = rotspeed self.angle = 90 self.location = PVector(x, y) self.velocity = PVector(0, 0) self.topspeed = 10
def __init__(self, location=PVector(0, 0), velocity=PVector(0, 0), acceleration=PVector(0, 0), topspeed=10): self.location = location self.velocity = velocity self.acceleration = acceleration self.topspeed = topspeed
def __init__(self): super().__init__(WIDTH, HEIGHT, TITLE) arcade.set_background_color((49, 197, 244)) self.movers = [] for _ in range(NO_MOVERS): self.movers.append(Mover(random.uniform(0.5, 2.5), 15, HEIGHT - 15)) self.wind = PVector(0.01, 0.0) self.gravity = PVector(0, -0.1)
def __init__(self, x, y): self.acceleration = PVector(0, 0) self.velocity = PVector(0, 0) self.location = PVector(x, y) self.color = (98, 199, 119) self.r = 8.0 self.maxspeed = 5 self.maxforce = 0.1 self.d = 25
def __init__(self): self.radius = 16 # Position, Velocity, and Acceleration self.position = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) self.acceleration = PVector(0.001, -0.01) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = (239, 242, 63)
def __init__(self): # Position and Velocity self.location = PVector(WIDTH / 2, HEIGHT / 2) self.prev_location = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) # Farbe self.color = (255, 255, 178) self.count = 0 self.paused = False self.stepsize = 4
def __init__(self, x, y, im, rotspeed): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.rotspeed = rotspeed self.angle = 90 self.location = PVector(x, y) self.velocity = PVector(random.uniform(-2, 2), random.uniform(-2, 2)) # self.acceleration = PVector(-0.001, 0.01) self.topspeed = 10
def apply_force_from_coords(ox, oy, position, current_drone): """Apply a simple short range repulsive force from a particle at the given coordinates on this particle.""" ax = 0 ay = 0 radius = 4 empty = PVector(0, 0) dx = ox - position.x dy = oy - position.y if dx == dy == 0: return empty.return_as_vector( ) # no directional force from particle at same location r2 = max(dx * dx + dy * dy, current_drone.min_r2) if r2 > radius: return empty.return_as_vector() # out of force range r = math.sqrt(r2) # Very simple short range repulsive force coef = (1 - current_drone.cutoff / r) / r2 / current_drone.mass ax += coef * dx ay += coef * dy direction = PVector(ax, ay) direction.normalize() return direction.return_as_vector()
def __init__(self): self.radius = random.randrange(10, 20) # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Farbe self.color = random.choice(colorlist)
def update(self): mouse = PVector(mouseX, mouseY) dir = mouse.sub(self.location) dir = dir.normalize() dir = dir.mult(0.5) self.acceleration = dir self.velocity = self.velocity.add(self.acceleration).limit( self.topspeed) self.location = self.location.add(self.velocity)
def __init__(self): self.radius = 16 # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-2, 2) v_y = random.randrange(-2, 2) self.velocity = PVector(v_x, v_y) # Farbe self.color = (239, 242, 63)
def __init__(self, tshape, tcolor, x, y): t.Turtle.__init__(self) self.penup() self.shape(tshape) self.color(tcolor) self.speed = 1 self.max_speed = 10 self.location = PVector(x, y) self.setpos(self.location.x, self.location.y) self.acceleration = PVector(0, 0.05) self.velocity = PVector(r.uniform(-1.0, 1.0), r.uniform(-2.0, 0.0)) self.lifespan = 255
def randomWalkb(x, y): new = numpy.random.randint(1, 4) if new == 1: x += 1 elif new == 2: y += 1 elif new == 3: x += -1 else: y += -1 new_position = PVector(x, y) new_position.normalize() return new_position.return_as_vector()
class Mover(): def __init__(self, m, x, y): self.mass = m self.radius = m*5 self.color = random.choice(colorlist) self.location = PVector(x, y) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0) def draw(self): arcade.draw_circle_filled(self.location.x, self.location.y, self.radius, self.color) arcade.draw_circle_outline(self.location.x, self.location.y, self.radius, arcade.color.BLACK) def apply_force(self, force): f = PVector.sdiv(force, self.mass) self.acceleration.add(f) def update(self): self.velocity.add(self.acceleration) self.location.add(self.velocity) self.acceleration.mult(0) if (self.location.x >= WIDTH - self.radius): self.location.x = WIDTH - self.radius self.velocity.x *= -1 elif (self.location.x <= self.radius): self.location.x = self.radius self.velocity.x *= -1 if (self.location.y >= HEIGHT - self.radius): self.location.y = HEIGHT - self.radius self.velocity.y *= -1 elif (self.location.y <= self.radius): self.location.y = self.radius self.velocity.y *= -1
def __init__(self): self.radius = random.randrange(10, 20) self.mouse = PVector(200, 200) # Position und Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = random.choice(colorlist)
class Mover(object): def __init__(self, x, y, r): self.location = PVector(x, y) self.radius = r self.dir = PVector(self.location.x + self.radius, self.location.y) def display(self): screen.draw.filled_circle((self.location.x, self.location.y), self.radius, (255, 0, 0)) screen.draw.circle((self.location.x, self.location.y), self.radius, (0, 0, 0)) screen.draw.line((self.location.x, self.location.y), (self.dir.x, self.dir.y), (0, 0, 0)) def update(self): mouse_x, mouse_y = pygame.mouse.get_pos() self.dir = PVector(mouse_x, mouse_y) self.dir.mag()
def velavg(uav_name, blockListDict): alt_d = 8 position = get_position(uav_name, blockListDict) close_drones = find_neighbours_in_radius(uav_name, blockListDict, 20) if len(close_drones) == 0: empty = PVector(0, 0) #velocity=PVector(current.v_ned_d[0],current.v_ned_d[1]) #current.set_v_2D_alt_lya(velocity.return_as_vector(),-alt_d) return empty.return_as_vector() velx = sum_all_vel_x(uav_name, blockListDict) velx = (velx / len(close_drones)) vely = sum_all_vel_x(uav_name, blockListDict) vely = (vely / len(blockListDict)) vel_vector = numpy.array([velx, vely]) vel_vector = normalize(vel_vector) return vel_vector
def move(self, player_pos): mouse_pos = pygame.mouse.get_pos() angle = math.atan2(player_pos[0] - self.pos[0], player_pos[1] - self.pos[1]) + math.pi self.angle = angle * 57.2957795 # Radians to degrees if Bounds.outside(self.pos[0], self.pos[1]) or self.chaser: self.direction = PVector(player_pos[0], player_pos[1]) - PVector(self.pos[0], self.pos[1]) else: self.direction = PVector(random.randint(0,640), random.randint(0,480)) - PVector(self.pos[0], self.pos[1]) self.direction.normalize() self.direction.mag = self.speed self.pos[0] = self.pos[0] + self.direction.x * self.direction.mag self.pos[1] = self.pos[1] + self.direction.y * self.direction.mag
class Enemy(pygame.sprite.Sprite): respawn = 600 limit = 50 animation_cycle = 120 image_data = {} images = [0,0] rects = {} def __init__(self, player_pos, visual, close, mover, chaser, fastie): pygame.sprite.Sprite.__init__(self, self.containers) self.pos = [0, 0] if close: self.pos[0] = player_pos[0] * random_direction() + random.randint(70, 100) self.pos[1] = player_pos[1] * random_direction() + random.randint(70, 100) else: self.pos[0] = player_pos[0] * random_direction() + random.randint(300, 500) self.pos[1] = player_pos[1] * random_direction() + random.randint(300, 500) if visual == "blob": self.images = Enemy.image_data['blob'] elif visual == "butterfly": self.images = Enemy.image_data['butterfly'] elif visual == "octopus": self.images = Enemy.image_data['octopus'] else: self.images = Enemy.image_data['blob'] self.image = self.images[0] # if visual == "blob": # self.image = Enemy.images['blob'] # elif visual == "butterfly": # self.image = Enemy.images['butterfly'] # elif visual == "octopus": # self.image = Enemy.images['octopus'] # else: # self.image = Enemy.images['blob'] self.rect = self.image.get_rect() self.rect.move_ip(Constant.SCREEN_RECT.width/2, Constant.SCREEN_RECT.bottom) self.mask = pygame.mask.from_surface(self.image) self.frame = 0 # self.image = pygame.Surface([20, 20]) # pygame.draw.circle(self.image, Color("red"), (10, 10), 10, 0) self.rect.center = (self.pos[0], self.pos[1]) self.close = close self.mover = mover self.chaser = chaser self.fastie = fastie if fastie: self.speed = random.randint(2,4) else: self.speed = 1 self.direction = None GameState.enemies_spawned += 1 def move(self, player_pos): mouse_pos = pygame.mouse.get_pos() angle = math.atan2(player_pos[0] - self.pos[0], player_pos[1] - self.pos[1]) + math.pi self.angle = angle * 57.2957795 # Radians to degrees if Bounds.outside(self.pos[0], self.pos[1]) or self.chaser: self.direction = PVector(player_pos[0], player_pos[1]) - PVector(self.pos[0], self.pos[1]) else: self.direction = PVector(random.randint(0,640), random.randint(0,480)) - PVector(self.pos[0], self.pos[1]) self.direction.normalize() self.direction.mag = self.speed self.pos[0] = self.pos[0] + self.direction.x * self.direction.mag self.pos[1] = self.pos[1] + self.direction.y * self.direction.mag def update(self): self.frame = pygame.time.get_ticks() self.rect.center = (self.pos[0], self.pos[1]) self.image = self.images[self.frame//self.animation_cycle % 2] self.image = pygame.transform.rotate(self.image, self.angle) @property def value(self): value = 1 if self.close: value += 1 if self.mover: value += 2 if self.fastie: value *= 2 if self.chaser: value += 3 return value