def __init__(self, enemies, tower_type, image, shot_frequency, shot_range, shot_strength, cost): self.tower_type = tower_type self.pos = Vector(0, 0) self.radius = 35 self.nozzle_original = Vector(0, self.radius - 5) # sorry, but has to be hardcoded self.nozzle = self.nozzle_original self.image = image self.image = pg.transform.scale(self.image, (self.radius * 2, ) * 2) # scale the image to size self.alive = True # if attackable towers are implemented in the future self.idle = True self.placeable = False self.edge = self.setEdgePoints() self.target = None self.angle = 0 self.shot_range = shot_range self.shot_frequency = shot_frequency self.shot_strength = shot_strength self.shot_dt = 0 self.shooting = False self.cost = cost self.enemies = enemies
def checkIdleTower(self): self.idle_tower.placeable = True # check if player has enough money if self.idle_tower.cost > self.money: self.idle_tower.placeable = False return # check if it's on the gamemap for point in self.idle_tower.edge: if not self.gamemap.collidepoint(point): self.idle_tower.placeable = False return # check if it's on the paths for subpath in self.gamemap.path.subpaths: for point in self.idle_tower.edge: if subpath.collidepoint(point): self.idle_tower.placeable = False return # check if it's on other towers for other in self.towers: if Vector.norm(other.pos - self.idle_tower.pos ) <= other.radius + self.idle_tower.radius: self.idle_tower.placeable = False # idle_tower is on another tower though return
def randomWalk(): # 8 1 2 # 7 0 3 # 6 5 4 x = 0 y = 0 choice = random.randint(1, 8) # print(choice) if choice == 1: y -= 1 elif choice == 2: x += 1 y -= 1 elif choice == 3: x += 1 elif choice == 4: x += 1 y += 1 elif choice == 5: y += 1 elif choice == 6: y += 1 x -= 1 elif choice == 7: x -= 1 elif choice == 8: x -= 1 y -= 1 return Vector(x, y)
def draw(self, surface): # render enemy rad2 = Vector(self.radius, self.radius) render_image = pg.transform.rotozoom(self.image, -self.angle, 1) # rotozoom for a bit AA draw_center = self.pos - Vector(*render_image.get_size()) / 2 surface.blit(render_image, draw_center) # render healthbar if self.health < self.health_start: self.draw_bar(surface, self.pos - rad2 - Vector(0, 5), (self.radius * 2, 5), (0, 0, 0)) self.draw_bar( surface, self.pos - rad2 - Vector(0, 5), (self.radius * 2 / self.health_start * self.health, 5), (255, 0, 0))
def start(self): self.start_time = time.time() pygame.init() pygame.mouse.set_visible(False) pygame.key.set_repeat(True) self.display_info = pygame.display.Info() self.screen = pygame.display.set_mode((GAME_WIDTH, GAME_HEIGHT)) # pylint: disable=I0011,E1121 self.background = pygame.Surface(self.screen.get_size()).convert() self.background.fill(BACKGROUND_COLOR) # pylint: disable=I0011,I0012; enable=E1121 self.font_mgr = FontManager() self.size = Vector(self.screen.get_size()[0], self.screen.get_size()[1]) self.clock = pygame.time.Clock() self.player = Player(self.size.div(2)) self.spawn_enemy() self.loop() pygame.quit()
def __init__(self,pos,target,hitpoints ,image_bullet): self.radius = 7 self.image = image_bullet self.image = pg.transform.scale(self.image, (self.radius*2,)*2) # scale the image to size self.pos = Vector(pos[0],pos[1]) self.vel = 0.5 # perhaps later implement as 'tower-specific' variable? self.target = target self.hitpoints = hitpoints self.alive = True
class AccelRectangle(Rectangle): def __init__(self, pos, size): super().__init__(pos, size) self.velocity = Vector(0) def accelerate(self, direction): if direction == Direction.UP: self.velocity.y -= ACCEL_SPEED elif direction == Direction.DOWN: self.velocity.y += ACCEL_SPEED elif direction == Direction.LEFT: self.velocity.x -= ACCEL_SPEED elif direction == Direction.RIGHT: self.velocity.x += ACCEL_SPEED def check_bounds(self, bounds): if self.pos.x < 0: self.pos.x = 0 if self.velocity.x < 0: self.velocity.x = 0 elif self.pos.x > bounds.x - self.size.x: self.pos.x = bounds.x - self.size.x if self.velocity.x > 0: self.velocity.x = 0 if self.pos.y < 0: self.pos.y = 0 if self.velocity.y < 0: self.velocity.y = 0 elif self.pos.y > bounds.y - self.size.y: self.pos.y = bounds.y - self.size.y if self.velocity.y > 0: self.velocity.y = 0 def decelerate(self): self.velocity.slow(ACCEL_SPEED / 2) def move(self): self.pos.x += self.velocity.x self.pos.y += self.velocity.y
def draw(self, surface): rad2 = Vector(self.radius, self.radius) if self.idle: # Range-circle surf = pg.Surface((self.shot_range * 2, ) * 2, pg.SRCALPHA) pg.draw.circle(surf, (0, 0, 255, 50), (self.shot_range, ) * 2, self.shot_range) surface.blit(surf, self.pos - (self.shot_range, ) * 2) pg.draw.circle(surface, (0, 0, 255), self.pos, self.shot_range, 3) # Tower itself render_image = pg.transform.rotozoom(self.image, -self.angle, 1) # rotozoom for a bit AA draw_center = self.pos - Vector(*render_image.get_size()) / 2 surface.blit(render_image, draw_center) if not self.placeable: # Red circle, if not allowed to place aSurf = pg.Surface((self.radius * 2, ) * 2, pg.SRCALPHA) aSurf.convert_alpha() radius = int(self.radius) pg.draw.circle(aSurf, (255, 0, 0, 50), rad2, radius) pg.draw.circle(aSurf, (255, 0, 0, 250), rad2, radius, 3) surface.blit(aSurf, draw_center)
def __init__(self, subpath, image, speed=10, health_start=100, value=10, angle=90, radius=20): self.pos = Vector(subpath.start[0], subpath.start[1]) self.pos = self.pos + (random.randint(-10, 10), random.randint( -10, 10)) self.radius = radius self.image = image self.image = pg.transform.scale(self.image, (self.radius * 2, ) * 2) self.waypoint = Vector(subpath.end[0], subpath.end[1]) self.angle = (self.waypoint - self.pos).angle("deg") self.varyPath(20) self.current = 0 self.alive = True self.health_start = health_start self.health = self.health_start self.speed = speed / 100 self.value = value
def render(self): self.screen.blit(self.background, (0, 0)) if self.is_ended: self.draw_text(FONT_END, "You have lost!", Vector(0, 256), COLOR_BLUE) return now = time.time() for enemy in self.enemies: enemy.render(self) self.player.render(self.screen) acceleration_text = "Acceleration: {}".format(self.player.velocity) position_text = "Position: {}".format(self.player.pos) score_text = "Score: {}".format(self.score) hps_text = "Hits / Second: {}".format(str(self.score / (now - self.start_time))[0:6]) end_text = "Press X to end the game." self.draw_text(FONT_NORMAL, acceleration_text, Vector(0, 0), COLOR_WHITE) self.draw_text(FONT_NORMAL, position_text, Vector(0, 16), COLOR_WHITE) self.draw_text(FONT_NORMAL, score_text, Vector(0, 32), COLOR_WHITE) self.draw_text(FONT_NORMAL, hps_text, Vector(0, 48), COLOR_WHITE) self.draw_text(FONT_NORMAL, end_text, Vector(0, 64), COLOR_WHITE)
def update(self, dt): if self.idle: self.edge = self.setEdgePoints() self.pos = Vector(pg.mouse.get_pos()[0], pg.mouse.get_pos()[1]) else: self.aim(dt) # doesn't aim, if not placed down
def setEdgePoints(self): l = list(self.pos + (Vector(self.radius - 4, 0).rotate(a)) for a in range(0, 360, int(360 / 8))) return l
class Tower: # attributes: # tower_type # pos # nozzle_original # nossle (point of exit for the bullets) (relative to pos) # radius (size of tower-image) # image # alive # idle # palceable # edge (points that make up hitbox) # target # angle # shot_frequency # shot_dt (time since last shot) # shooting # shot_range # references: # enemies (from gamemap) # subpaths (from gamemap) def __init__(self, enemies, tower_type, image, shot_frequency, shot_range, shot_strength, cost): self.tower_type = tower_type self.pos = Vector(0, 0) self.radius = 35 self.nozzle_original = Vector(0, self.radius - 5) # sorry, but has to be hardcoded self.nozzle = self.nozzle_original self.image = image self.image = pg.transform.scale(self.image, (self.radius * 2, ) * 2) # scale the image to size self.alive = True # if attackable towers are implemented in the future self.idle = True self.placeable = False self.edge = self.setEdgePoints() self.target = None self.angle = 0 self.shot_range = shot_range self.shot_frequency = shot_frequency self.shot_strength = shot_strength self.shot_dt = 0 self.shooting = False self.cost = cost self.enemies = enemies def setEdgePoints(self): l = list(self.pos + (Vector(self.radius - 4, 0).rotate(a)) for a in range(0, 360, int(360 / 8))) return l def update(self, dt): if self.idle: self.edge = self.setEdgePoints() self.pos = Vector(pg.mouse.get_pos()[0], pg.mouse.get_pos()[1]) else: self.aim(dt) # doesn't aim, if not placed down # aims at enemy, furthest down the path def aim(self, dt): if self.shooting: self.shooting = False for enemy in self.enemies: if (enemy.pos - self.pos).norm() < self.shot_range: self.target = enemy # aquire target self.angle = (self.pos - enemy.pos).angle( "deg") # "aim at enemy" self.nozzle = self.nozzle_original.rotate(self.angle) if self.shot_dt >= 1000 / self.shot_frequency: # "may I soot?" self.shooting = True # fire!! self.shot_dt = 0 else: self.shot_dt += dt # count time since last shot break # aims at nearest enemy def aimnear(self, dt): # used at all? if self.shooting: self.shooting = False # Find all enemies, that are in range inRange = [] for enemy in self.enemies: if (enemy.pos - self.pos).norm() < self.shot_range: inRange.append(enemy) # Find closest enemy of those if len(inRange) > 0: closest = inRange[0] for enemy in inRange: if (enemy.pos - self.pos).norm() < (closest.pos - self.pos).norm(): closest = enemy # Set closest as target self.target = closest # aquire target self.angle = (self.pos - self.target.pos).angle( "deg") # "aim at enemy" self.nozzle = self.nozzle_original.rotate(self.angle) if self.shot_dt >= 1000 / self.shot_frequency: # "may I shoot?" self.shooting = True # fire!! self.shot_dt = 0 else: self.shot_dt += dt # count time since last shot def draw(self, surface): rad2 = Vector(self.radius, self.radius) if self.idle: # Range-circle surf = pg.Surface((self.shot_range * 2, ) * 2, pg.SRCALPHA) pg.draw.circle(surf, (0, 0, 255, 50), (self.shot_range, ) * 2, self.shot_range) surface.blit(surf, self.pos - (self.shot_range, ) * 2) pg.draw.circle(surface, (0, 0, 255), self.pos, self.shot_range, 3) # Tower itself render_image = pg.transform.rotozoom(self.image, -self.angle, 1) # rotozoom for a bit AA draw_center = self.pos - Vector(*render_image.get_size()) / 2 surface.blit(render_image, draw_center) if not self.placeable: # Red circle, if not allowed to place aSurf = pg.Surface((self.radius * 2, ) * 2, pg.SRCALPHA) aSurf.convert_alpha() radius = int(self.radius) pg.draw.circle(aSurf, (255, 0, 0, 50), rad2, radius) pg.draw.circle(aSurf, (255, 0, 0, 250), rad2, radius, 3) surface.blit(aSurf, draw_center)
projectName = 'NoC Intro Random Walker' COLOR_WHITE = (255, 255, 255) COLOR_BLACK = (0, 0, 0) GAME_WINDOW = display.set_mode((900, 400)) #Create Window display.set_caption(projectName) GAME_WINDOW.fill(COLOR_WHITE) display.update() game_running = True WIDTH, HEIGHT = pygame.display.get_surface().get_size() pos = Vector(int(WIDTH / 2), int(HEIGHT / 2)) posVel = Vector(1, 3.3) def edgeCheck(v): if (v.x > WIDTH): v.x = 0 if (v.x < 0): v.x = WIDTH if (v.y > HEIGHT): v.y = 0 if (v.y < 0): v.y = HEIGHT def randomWalk():
def show(self,surface): rad2 = Vector(self.radius,self.radius) draw_center = self.pos - rad2 surface.blit(self.image, draw_center)
def render(self, game, color=None): super().render(game.screen, self.get_color()) text = str(time.time() - self.creation_time)[0:4] pos = Vector(self.pos.x + (ENTITY_SIZE / 4), self.pos.y + (ENTITY_SIZE / 2) - 5) game.draw_text(FONT_NORMAL, text, pos, COLOR_BLACK)
class Game(object): def __init__(self): self.coords = [] self.player = None self.enemies = [] self.last_spawn_time = 0 self.start_time = 0 self.score = 0 self.screen = None self.display_info = None self.clock = None self.font_mgr = None self.is_ended = False self.should_stop = False self.size = None self.background = None def start(self): self.start_time = time.time() pygame.init() pygame.mouse.set_visible(False) pygame.key.set_repeat(True) self.display_info = pygame.display.Info() self.screen = pygame.display.set_mode((GAME_WIDTH, GAME_HEIGHT)) # pylint: disable=I0011,E1121 self.background = pygame.Surface(self.screen.get_size()).convert() self.background.fill(BACKGROUND_COLOR) # pylint: disable=I0011,I0012; enable=E1121 self.font_mgr = FontManager() self.size = Vector(self.screen.get_size()[0], self.screen.get_size()[1]) self.clock = pygame.time.Clock() self.player = Player(self.size.div(2)) self.spawn_enemy() self.loop() pygame.quit() def loop(self): while not self.should_stop: self.clock.tick(60) self.render() pygame.display.flip() self.update() for event in pygame.event.get(): if event.type == Pygame.QUIT: self.should_stop = True def render(self): self.screen.blit(self.background, (0, 0)) if self.is_ended: self.draw_text(FONT_END, "You have lost!", Vector(0, 256), COLOR_BLUE) return now = time.time() for enemy in self.enemies: enemy.render(self) self.player.render(self.screen) acceleration_text = "Acceleration: {}".format(self.player.velocity) position_text = "Position: {}".format(self.player.pos) score_text = "Score: {}".format(self.score) hps_text = "Hits / Second: {}".format(str(self.score / (now - self.start_time))[0:6]) end_text = "Press X to end the game." self.draw_text(FONT_NORMAL, acceleration_text, Vector(0, 0), COLOR_WHITE) self.draw_text(FONT_NORMAL, position_text, Vector(0, 16), COLOR_WHITE) self.draw_text(FONT_NORMAL, score_text, Vector(0, 32), COLOR_WHITE) self.draw_text(FONT_NORMAL, hps_text, Vector(0, 48), COLOR_WHITE) self.draw_text(FONT_NORMAL, end_text, Vector(0, 64), COLOR_WHITE) def update(self): now = time.time() if now - self.last_spawn_time > 0.5 and len(self.enemies) < MAX_ENEMY_COUNT: self.spawn_enemy() keys = pygame.key.get_pressed() if keys[Pygame.K_ESCAPE]: self.end() return if keys[Pygame.K_r] or keys[Pygame.K_z] or keys[Pygame.K_b]: self.player.is_bot = True self.player.update(self) for index in range(0, len(self.enemies)): if self.player.collides(self.enemies[index]): if self.player.target is not None and index < self.player.target: self.player.target -= 1 if self.enemies[index].is_target: self.player.is_target_destroyed = True self.enemies.pop(index) self.score += 1 break time_since_creation = now - self.enemies[index].creation_time if time_since_creation > TIME_TO_LOSE: self.end() return def end(self): print("ended: {}".format(self.score)) def draw_text(self, size, text, pos, color): rendered = self.font_mgr.get(size).render(text, True, color) self.screen.blit(rendered, (pos.x, pos.y)) def spawn_enemy(self): size = Vector(ENTITY_SIZE) pos = Vector.rand(size, self.size.sub(ENTITY_SIZE)) enemy = Enemy(pos) self.enemies.append(enemy) self.last_spawn_time = time.time()
def spawn_enemy(self): size = Vector(ENTITY_SIZE) pos = Vector.rand(size, self.size.sub(ENTITY_SIZE)) enemy = Enemy(pos) self.enemies.append(enemy) self.last_spawn_time = time.time()
probability = r1*r1; # Pick a second random value. r2 = random.randint(0,max*max); # Does it qualify? If so, we’re done! if r2 < probability: return random.randint(-r1, r1) else: print("rejecting", r1, "less than", r2) r = 1 #dot.set_alpha(10) # alpha level pos = Vector(int(WIDTH/2),int(HEIGHT/2)) #--------------------------------------------------------# # Game Loop while game_running: # Check for events xstep = weightedRandom() ystep = weightedRandom() step = Vector(xstep,ystep) lastPos = pos.copy() pos.add(step) pygame.draw.line(GAME_WINDOW, COLOR_BLACK, lastPos.coords(), pos.coords(), r) display.update()g edgeCheck(pos)
def __init__(self, pos): super().__init__(pos, Vector(ENTITY_SIZE))
def readWaypoints(self,file): file = open(file) for line in file: coords = line.strip().split(",") self.points.append( Vector(coords[0],coords[1]).asInt() )
def __init__(self, pos, size): super().__init__(pos, size) self.velocity = Vector(0)
print("This walker prioritizes white squares over black") COLOR_WHITE = (255, 255, 255) COLOR_BLACK = (0, 0, 0) GAME_WINDOW = display.set_mode((900, 400)) #Create Window display.set_caption(projectName) GAME_WINDOW.fill(COLOR_WHITE) display.update() game_running = True WIDTH, HEIGHT = pygame.display.get_surface().get_size() pos = Vector(int(WIDTH / 2), int(HEIGHT / 2)) posVel = Vector(1, 3.3) def edgeCheck(v): if (v.x > WIDTH): v.x = 0 if (v.x < 0): v.x = WIDTH if (v.y > HEIGHT): v.y = 0 if (v.y < 0): v.y = HEIGHT def randomWalk():