def fire(self, direction, theta, x, y): self.charging = True self.cool_down = 1 bullet = Bullet(direction, theta, x, y, vec(0, 0), vec(0, 0), 2, self.frames) self.bullets.add(bullet) return bullet
def update(self, move): # sanity check commands #print("Turn:",turn) assert move in (LEFT, RIGHT, UP, DOWN, NONE) # update angle according to steer if move == LEFT: v = vec(-D_X, 0) elif move == RIGHT: v = vec(D_X, 0) elif move == UP: v = vec(0, -D_X) elif move == DOWN: v = vec(0, D_X) elif move == NONE: v = vec(0, 0) # get new position with updated velocity self.x += v # update image surface position as needed self.rect = self.image.get_rect() self.rect.center = (int(self.x.x), int(self.x.y))
def __init__(self, *groups, pos_x = 0, pos_y = 0, v_x = np.sqrt(2), v_y= np.sqrt(2), radius = BALL_RADIUS, color = WHITE, speed = BALL_SPEED, background = BLACK, window_size = WINDOW_SIZE): # create base sprite instance Sprite.__init__(self, *groups) self.window_size = WINDOW_SIZE # create visual attributes self.image = pg.Surface((2*radius,2*radius)) self.rect = self.image.fill(background) self.rect.topleft = (pos_x,pos_y) # add circle to image, make edges transparent pg.draw.circle(self.image,color,(radius,radius),radius) self.image.set_colorkey(background) # create positional attributes self.speed = speed self.x = vec(pos_x,pos_y) self.v = vec(v_x,v_y) self.v = speed * self.v.normalize()
def __init__(self, bounds): """ :param bounds: tuple """ pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface( (int(DISPLAY_SIZE[0] / 102.4), int(DISPLAY_SIZE[1] / 57.6))) self.image.fill(pygame.Color('WHITE')) self.rect = self.image.get_rect() self.rect.center = bounds[2] / 2, bounds[3] / 2 self.default_pos = bounds[2] / 2, bounds[3] / 2 self.bounds = bounds self.start_angle = 130 # Either 50 or 130 # Flag to let the game know when the ball is in play so a round can be started with SPACE self.in_play = False # Position (start in the middle of the screen) self.pos = vec(self.rect.center[0], self.rect.center[1]) # Angle (degrees) self.angle = 0.0 # Speed - in pixels self.speed = 0 self.max_speed = 15 # Velocity - I believe this is a placeholder for update() method self.vel = vec(1, 0).rotate(self.angle) * self.speed
def update(self): if self.able_to_move: self.pix_pos += self.direction * self.speed if self.pix_pos.x % self.app.cell_width == 0: if self.direction == vec(1, 0) or self.direction == vec(-1, 0): if self.stored_direction != None: self.direction = self.stored_direction self.able_to_move = self.can_move() if self.pix_pos.y % self.app.cell_height == 0: if self.direction == vec(0, 1) or self.direction == vec(0, -1): if self.stored_direction != None: self.direction = self.stored_direction self.able_to_move = self.can_move() self.grid_pos[0] = (self.pix_pos[0] - self.app.cell_width // 2) // self.app.cell_width + 1 self.grid_pos[1] = (self.pix_pos[1] - self.app.cell_height // 2) // self.app.cell_height + 1 if self.on_coin(): self.eat_coin() if self.on_food(): self.eat_food()
def can_move(self): if vec(self.grid_pos + self.direction) in self.app.walls: if vec(self.grid_pos + self.direction) in self.app.barrier and ( (not self.outside) or self.state == "Eaten"): return True else: return False return True
def can_move_certain_direction(self, position, direction): if vec(position + direction) in self.app.walls: if vec(position + direction) in self.app.barrier and ( (not self.outside) or self.state == "Eaten"): return True else: return False return True
def reset(self): self.grid_pos = vec(self.init_pos.x, self.init_pos.y) self.pix_pos = vec(self.grid_pos.x * self.app.cell_width, self.grid_pos.y * self.app.cell_height) self.color = self.get_color() self.direction = vec(0, 0) self.personality = self.set_personality() self.speed = self.get_speed() self.target = None
def time_to_move(self): if self.pix_pos.x % self.app.cell_width == 0: if self.direction == vec(1, 0) or self.direction == vec(-1, 0) or self.direction == vec(0, 0): return True if self.pix_pos.y % self.app.cell_height == 0: if self.direction == vec(0, 1) or self.direction == vec(0, -1) or self.direction == vec(0, 0): return True return False
def fire(self, direction, theta, x, y): self.frames = self.spritesheet.get_images("static", min(self.count * 10, 100)) self.cooldown = self.firerate self.count += 1 self.count = self.count % 20 return Bullet(direction, theta, x, y, vec(20, 0), vec(0, 0), 100, self.frames)
def check_intersection_near(self): if self.grid_pos + self.direction in self.app.crossroads: return True if self.grid_pos + self.direction == self.app.crossroad_L or self.grid_pos + self.direction == self.app.crossroad_R: return True if (self.grid_pos + self.direction == vec(13, 11) or self.grid_pos + self.direction == vec(14, 11)) and self.state == "Eaten": return True return False
def time_to_move(self): if int(self.pix_pos.x + TOP_BOTTOM_BUFFER // 2) % self.app.cell_width == 0: if self.direction == vec(1, 0) or self.direction == vec(-1, 0): return True if int(self.pix_pos.y + TOP_BOTTOM_BUFFER // 2) % self.app.cell_height == 0: if self.direction == vec(0, 1) or self.direction == vec(0, -1): return True
def __init__(self, file, x, y, game, speed, lane): super().__init__() self.game = game self.lane = lane self.sheet = Spritesheet(file) self.color = (0, 0, 0) self.index = 0 self.housingSpace = 1 self.x = x self.y = y self.apparantX = self.x self.apparantY = self.y self.waypoint_index = 0 self.waypoints = self.game.paths[self.lane] self.target = self.waypoints[self.waypoint_index] self.isActive = True self.pos = vec(self.x, self.y) self.imagify() self.last_update = pg.time.get_ticks() self.last_update2 = pg.time.get_ticks() self.hitSound = sounds['SlimeHit'] self.hitSound.set_volume(2) self.update_thres = 200 self.update_thres2 = 10 self.last_moved = pg.time.get_ticks() self.move_thres = 100 #self.last_switched = pg.time.get_ticks() self.animation_database = self.sheet.load_animation( 32, 32, self.color, 1) self.animation_frame = 0 self.action = 0 self.animation_framerate = 5 self.dropchance = 20 self.speed = speed self.vel = vec() self.vel.from_polar((self.speed, 0)) self.vel2 = vec() self.vel2.from_polar((self.speed, -90)) self.rect.topleft = vec(self.x, self.y) self.apparantPos = vec(self.apparantX, self.apparantY) self.menu_slime = False self.r = rd.choice(['r', 'd', 'u', 'l'])
def checkMove(self): if self.direction == vec(0,0): return True if int(self.pixel_pos.x + SPACE//2) % self.game.tile_width == 0: # making it so we're only moving between cells and not in between lines if self.direction == vec(1, 0) or self.direction == vec(-1, 0): #want to be moving on X-axis to check x direction return True if int(self.pixel_pos.y + SPACE//2) % self.game.tile_height == 0: if self.direction == (0, 1) or self.direction == vec(0, -1): return True
def frightened(self): directions = [vec(0, -1), vec(-1, 0), vec(0, 1), vec(1, 0)] starting_idx = random.randint(0, 3) actual_pos = self.grid_pos + self.direction for i in range(4): direction = directions[(starting_idx + i) % 4] if self.can_move_certain_direction( actual_pos, direction) and actual_pos + direction != self.grid_pos: self.stored_direction = direction
def __init__(self, center): super(Missile, self).__init__() self.center = center self.rect = self.image.get_rect() self.target = choice(game.mobs.sprites()) self.pos = vec(self.center) self.vel = vec(0, -Missile.MAX_SPEED / 2) self.acc = vec(0, 0) self.rect.center = self.pos self.shield = 5
def __init__(self, x, y, vel): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((10, 20)) self.image.fill(YELLOW) self.rect = self.image.get_rect() self.rect.top = y + 32 self.rect.centerx = x + 32 self.pos = vec(self.rect.x, self.rect.y) self.bullet_clock = pygame.time.get_ticks() self.vel = vec(vel)
def anticollisione(self): for car in self.groups: if car != self and car.vision_rect != None: if self.vision_rect.colliderect(car.rect): self.vel = vec(0,0) self.acc = vec(0,0) break elif self.vision_rect.colliderect(car.vision_rect): a = self.left_right(self.desired, car.desired) if a > 0: self.vel = vec(0,0)
def __init__(self, x, y, ind_images): pygame.sprite.Sprite.__init__(self, allsprites) self.x = x self.y = y self.frame = 0 self.image = ind_images[self.frame] self.rect = self.image.get_rect() self.rect.center = (self.x, self.y) self.pos = vec(self.x, self.y) self.vel = vec(0, 0) self.last_update = pygame.time.get_ticks()
def chase_pinky(self): if self.grid_pos == (13, 11) or self.grid_pos == (14, 11): self.outside = True if not self.outside: target_pos = vec(13, 11) else: target_pos = self.app.player.grid_pos + ( self.app.player.direction * 4) if self.app.player.direction == vec(0, -1): target_pos += vec(-4, 0) return target_pos
def __init__(self, app, pos): self.app = app self.grid_pos = pos self.pix_pos = vec(self.grid_pos.x * self.app.cell_width, self.grid_pos.y * self.app.cell_height) self.direction = vec(-1, 0) self.stored_direction = None self.able_to_move = True self.current_score = 0 self.speed = 2 self.god_mode_timer = 0
def get_pix_pos(self, coin=None): if coin == None: return vec( self.grid_pos.x * self.app.cell_width + self.app.cell_width // 2, self.grid_pos.y * self.app.cell_height + self.app.cell_height // 2) else: return vec( coin.x * self.app.cell_width + self.app.cell_width // 2, coin.y * self.app.cell_height + self.app.cell_height // 2)
def __init__(self, app, pos, nb): self.app = app self.grid_pos = pos self.pix_pos = vec(self.grid_pos.x * self.app.cell_width, self.grid_pos.y * self.app.cell_height) self.nb = nb self.color = self.get_color() self.direction = vec(0, 0) self.personality = self.set_personality() self.speed = self.get_speed() self.target = None self.init_pos = vec(pos.x, pos.y)
def on_coin(self): if self.grid_pos in self.app.coins: if int(self.pix_pos.x + TOP_BOTTOM_BUFFER // 2) % self.app.cell_width == 0: if self.direction == vec(1, 0) or self.direction == vec(-1, 0): return True if int(self.pix_pos.y + TOP_BOTTOM_BUFFER // 2) % self.app.cell_height == 0: if self.direction == vec(0, 1) or self.direction == vec(0, -1): return True return False
def __init__(self, center): super(MobMissile, self).__init__() self.rect = self.image.get_rect() self.pos = vec(center) self.vel = (game.player.rect.center - self.pos).normalize() * MobMissile.MAX_SPEED self.acc = vec(0, 0) self.rect.center = center self.center = center self.frame_nbr = 0 self.shield = 5 self.then = get_ticks()
def __init__(self, app, pos): self.app = app self.starting_pos = [pos[0], pos[1]] self.grid_pos = vec(pos[0], pos[1]) self.pix_pos = self.get_pix_pos() self.direction = vec(1, 0) self.stored_direction = None self.score = 0 self.able_to_move = True self.speed = 2 self.lives = 3
def chase_inky(self): if self.grid_pos == (13, 11) or self.grid_pos == (14, 11): self.outside = True if not self.outside: target_pos = vec(13, 11) else: cell_pos = self.app.player.grid_pos + self.app.player.direction * 2 if self.app.player.direction == vec(0, -1): cell_pos += vec(-2, 0) offset = cell_pos - self.app.enemies[0].grid_pos target_pos = self.app.enemies[0].grid_pos + (2 * offset) return target_pos
def set_target(self): if self.personality == "fast" or self.personality == "slow": return self.app.player.grid_pos else: if self.app.player.grid_pos[0] > COLS // 2 and self.app.player.grid_pos[1] > ROWS // 2: return vec(1, 1) if self.app.player.grid_pos[0] > COLS // 2 and self.app.player.grid_pos[1] < ROWS // 2: return vec(1, ROWS - 2) if self.app.player.grid_pos[0] < COLS // 2 and self.app.player.grid_pos[1] > ROWS // 2: return vec(COLS - 2, 1) else: return vec(COLS - 2, ROWS - 2)
def chase_blinky(self): if self.grid_pos == (13, 11) or self.grid_pos == (14, 11): self.outside = True if not self.outside: target_pos = vec(13, 11) else: if (self.grid_pos.x - self.app.player.grid_pos.x)**2 + ( self.grid_pos.x - self.app.player.grid_pos.y)**2 > 64: target_pos = self.app.player.grid_pos else: target_pos = vec(30, -1) return target_pos
def set_target(self): if self.personality == "blinky" or self.personality == "inky": return self.app.pacman.grid_pos else: if self.app.pacman.grid_pos[0] > COLS//2 and self.app.pacman.grid_pos[1] > ROWS//2: return vec(1, 1) if self.app.pacman.grid_pos[0] > COLS//2 and self.app.pacman.grid_pos[1] < ROWS//2: return vec(1, ROWS-2) if self.app.pacman.grid_pos[0] < COLS//2 and self.app.pacman.grid_pos[1] > ROWS//2: return vec(COLS-2, 1) else: return vec(COLS-2, ROWS-2)