def __init__(self, name='', polygon=robody, projectile_cb=None, scanner_cb=None, laser_cb=None): super(Robot, self).__init__(polygon=polygon) self.name = name self.color = None self.position = Vector(0, 0) self.velocity = Vector(0, 0) #self.projectile_cb = projectile_cb self.alive = True self.hull = 100 self.kills = [] self.scanner = ArcScanner(self, scanner_cb) self.cannon = ATCannon(source=self, projectile_cb=projectile_cb) self.shield = EnergyShield() self.laser = LaserEmitter(sourcebot=self, laser_cb=laser_cb) self.cannon_iface = self.cannon.gen_interface() self.modules = [] self.modules.append(self.scanner) self.modules.append(self.cannon) self.modules.append(self.shield) self.modules.append(self.laser)
def bound(self, tiles): player_box, collisions = move(self.box, self.vel, [tile.box for tile in tiles]) self.box = player_box self.pos = Vector(player_box.x, player_box.y) if collisions["bottom"]: self.vel.y = 0 # if this isn't included I go up like a ramp self.ground = True
def __init__(self, name='', polygon=robody): super(Robot, self).__init__(polygon=polygon) self.name = name self.color = None self.position = Vector(0, 0) self.velocity = Vector(0, 0) self.modules = [] self.alive = True self.hull = 100 self.kills = []
class Player(Object): moving_right = False moving_left = False ground = False def __init__(self, sprite_path, x, y): super().__init__(sprite_path, x, y) def apply(self, v): self.acc.add(v) def render(self, ctx): ctx.blit(self.sprite, self.pos.repr()) def bound(self, tiles): player_box, collisions = move(self.box, self.vel, [tile.box for tile in tiles]) self.box = player_box self.pos = Vector(player_box.x, player_box.y) if collisions["bottom"]: self.vel.y = 0 # if this isn't included I go up like a ramp self.ground = True # do I add ground = False here since there aren't collisions? # it seems to glitch out :( def move(self, keys): if keys[K_RIGHT]: self.apply(Vector(SPEED, 0)) if keys[K_LEFT]: self.apply(Vector(-SPEED, 0)) def update(self): drag = self.vel.clone() drag_magnitude = (drag.mag() ** 2) * AIR_CONSTANT drag.set_mag(-drag_magnitude) self.apply(drag) self.vel.add(self.acc) self.vel.x_limit(MAX_SPEED_X) self.vel.y_limit(MAX_SPEED_Y) self.pos.add(self.vel) self.acc.mult(0) if not self.ground: self.apply(GRAVITY) self.box.x = self.pos.x self.box.y = self.pos.y
def execute(self): # to be initialized as a thread rval = random.randint(-3, 3) s_angle = random.randint(0, 360) self.scanner.set_arc_width(30) while True: if self.alive: time.sleep(.03) self.forward(1) if random.randint(0, 4) == 0: while rval >= 3 and rval <= -3: rval += random.randint(-3,3) if random.randint(0, 50) == 0: rval *= -1 self.turn(rval) tvals = self.scan(s_angle) if tvals is not False: s_angle += 30 s_angle %=360 if len(tvals) > 0: for t in tvals: d = None if d is None: d = t elif self.position.distance(t) < self.position.distance(d): d = t angle = Vector(d).angle_between(self.position) #self.fire(angle) self.cannon_iface.fire(angle) self.laser.fire(angle) s_angle = angle self.shield.modulate(random.randint(0,2)) else: return
def forward(self, v=2): if v > self.speed: v = self.speed if v < 0: v = 0 vx = math.cos(math.radians(self.source.heading)) * v vy = math.sin(math.radians(self.source.heading)) * v self.source.velocity = Vector(vx, vy)
def bot_overlap(self, rob): r = int(rob.body.radius) rob.position = Vector(random.randint(r + 100, self.arena[0] - r - 100), random.randint(r + 100, self.arena[1] - r - 100)) for b in self.robs: if b is not rob: if b.intersects(rob): return True return False
def initialize_bots(self): for rob in self.robs: r = int(rob.body.radius) while self.bot_overlap(rob): rob.position = Vector( random.randint(r + 100, self.arena[0] - r - 100), random.randint(r + 100, self.arena[1] - r - 100)) rob.heading = random.randint(0, 360) while rob.color is None or sum(rob.color) < 1: rob.color = random.random(), random.random(), random.random()
def scanner_cb(self, source, angle_degrees, position, arc_degrees, arc_length, color): start_angle = (angle_degrees - arc_degrees / 2) % 360 end_angle = (start_angle + arc_degrees) % 360 rvals = [] p = Vector(position) for rob in self.robs: if rob is not source: if p.distance(rob.position) < arc_length: angle = rob.position.angle_between(p) % 360 if start_angle + arc_degrees > 360: if angle > start_angle or angle < end_angle: rvals.append(rob.position) else: if start_angle < angle < end_angle: rvals.append(rob.position) if len(rvals) > 0: color = [1, 0, 0] self.scanner_draw(p, angle_degrees, arc_degrees, arc_length, color) return rvals
def calc_velocity(self): radians = math.radians(self.heading) dx = math.cos(radians) * self.speed dy = math.sin(radians) * self.speed return Vector(dx, dy)
def move(self, keys): if keys[K_RIGHT]: self.apply(Vector(SPEED, 0)) if keys[K_LEFT]: self.apply(Vector(-SPEED, 0))
import pygame, math from pygame.locals import * from engine import Object, Vector from config import * SPEED = 2 MAX_SPEED_X = 4 MAX_SPEED_Y = 15 JUMP_SPEED = 15 AIR_CONSTANT = 0.1 GRAVITY = Vector(0, 1) def collision_test(rect, tiles): hit_list = [] for tile in tiles: if rect.colliderect(tile): hit_list.append(tile) return hit_list def move(rect, vel, tiles): collision_types = {"top": False, "bottom": False, "right": False, "left": False} hit_list = collision_test(rect, tiles) testv = vel.clone() testv.normalize() if (testv.mag() == 0): return rect, collision_types while len(hit_list) > 0: rect.y -= testv.y
def forward(self, val): vx = math.cos(math.radians(self.heading)) * val vy = math.sin(math.radians(self.heading)) * val self.velocity = Vector(vx, vy)
class Robot(Entity): def __init__(self, name='', polygon=robody, projectile_cb=None, scanner_cb=None, laser_cb=None): super(Robot, self).__init__(polygon=polygon) self.name = name self.color = None self.position = Vector(0, 0) self.velocity = Vector(0, 0) #self.projectile_cb = projectile_cb self.alive = True self.hull = 100 self.kills = [] self.scanner = ArcScanner(self, scanner_cb) self.cannon = ATCannon(source=self, projectile_cb=projectile_cb) self.shield = EnergyShield() self.laser = LaserEmitter(sourcebot=self, laser_cb=laser_cb) self.cannon_iface = self.cannon.gen_interface() self.modules = [] self.modules.append(self.scanner) self.modules.append(self.cannon) self.modules.append(self.shield) self.modules.append(self.laser) def get_position(self): return self.position def get_heading(self): return self.heading def get_hull(self): return self.hull def get_velocity(self): return self.velocity def scan(self, angle): rvals = self.scanner.scan(angle, self.position, self.color) return rvals def forward(self, val): vx = math.cos(math.radians(self.heading)) * val vy = math.sin(math.radians(self.heading)) * val self.velocity = Vector(vx, vy) def turn(self, val): self.rotation = val def execute(self): # to be initialized as a thread rval = random.randint(-3, 3) s_angle = random.randint(0, 360) self.scanner.set_arc_width(30) while True: if self.alive: time.sleep(.03) self.forward(1) if random.randint(0, 4) == 0: while rval >= 3 and rval <= -3: rval += random.randint(-3,3) if random.randint(0, 50) == 0: rval *= -1 self.turn(rval) tvals = self.scan(s_angle) if tvals is not False: s_angle += 30 s_angle %=360 if len(tvals) > 0: for t in tvals: d = None if d is None: d = t elif self.position.distance(t) < self.position.distance(d): d = t angle = Vector(d).angle_between(self.position) #self.fire(angle) self.cannon_iface.fire(angle) self.laser.fire(angle) s_angle = angle self.shield.modulate(random.randint(0,2)) else: return def draw(self, ctx): #body ctx.set_operator(cairo.OPERATOR_ADD) ctx.set_line_width(1) col = [*self.color] + [0.7] ctx.set_source_rgba(*col) ctx.move_to(self.body.points[0].x, self.body.points[0].y) for point in self.body.points: ctx.line_to(point[0], point[1]) ctx.line_to(self.body.points[0].x, self.body.points[0].y) ctx.fill() ctx.set_source_rgb(*self.color) ctx.move_to(self.body.points[0].x, self.body.points[0].y) for point in self.body.points: ctx.line_to(point[0], point[1]) ctx.line_to(self.body.points[0].x, self.body.points[0].y) ctx.stroke() # energy shield shield_color = self.shield.color + [.3] ctx.set_source_rgba(*shield_color) ctx.arc(self.position[0], self.position[1], self.body.radius+1, 0, math.pi*2) ctx.fill() self.draw_data(ctx) def draw_data(self, ctx): #circle ctx.set_line_width(1) ctx.set_source_rgb(0, 1, 0) ctx.arc(self.position[0], self.position[1], self.body.radius+1, 0, math.pi*2) ctx.stroke() #data self.draw_text(ctx) def draw_text(self, ctx): ctx.set_source_rgb(*self.color) ctx.select_font_face("Courier", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(12) (x, y, width, height, dx, dy) = ctx.text_extents(self.name) ctx.move_to(self.position[0]-width/2, self.position[1]+self.body.radius + height + 2) ctx.show_text(self.name) hull_str = str(self.hull) r = 1-.01*self.hull g = .01*self.hull ctx.set_source_rgb(r, g, 0) (x, y, width, height, dx, dy) = ctx.text_extents(hull_str) ctx.move_to(self.position[0] - width/2, self.position[1]- self.body.radius - height + 2) ctx.show_text(hull_str)