class Shooting(GameState): """Main gameplay state.""" colors = { "day": { "sky": pg.Color("skyblue"), "grass": pg.Color(125, 183, 100)}, "night": { "sky": pg.Color(1, 2, 7), "grass": pg.Color(11, 15, 8)}} def __init__(self): super(Shooting, self).__init__() self.animations = pg.sprite.Group() self.world = World(True) self.cooldown = 0 self.cooldown_duration = 250 self.mag_size = 4 self.bullets_in_mag = self.mag_size self.crosshairs_day = prepare.GFX['crosshairs_day'] self.crosshairs_night = prepare.GFX['crosshairs_night'] self.crosshairs = self.crosshairs_day self.crosshairs_rect = self.crosshairs.get_rect() self.switched = False self.bullet = pg.transform.scale(prepare.GFX['bullet'], (15,100)) self.bullet_rect = self.bullet.get_rect() self.bullet_spacer = 5 self.clay_score = 0 self.clay_scores = [] self.clay_score_timer = 0.0 def startup(self, persistent): self.persist = persistent self.score = 0 self.score_label = Label("{}".format(self.score), {"topleft": (5, 5)}, font_size=64) self.world.reset() pg.mouse.set_visible(False) def get_event(self, event): if event.type == pg.QUIT: pg.mouse.set_visible(True) self.quit = True elif event.type == pg.KEYUP: if event.key == pg.K_ESCAPE: self.quit = True elif event.key == pg.K_r: self.on_reload() elif event.type == pg.MOUSEBUTTONDOWN: if event.button == 1: self.shoot() elif event.button == 3: self.on_reload() def on_reload(self): self.bullets_in_mag = self.mag_size prepare.SFX["reload"].play() def update(self, dt): self.cooldown += dt self.animations.update(dt) self.world.update(dt) for sprite in self.world.all_sprites: self.world.all_sprites.change_layer(sprite, sprite.z * -1) self.score_label.set_text("{}".format(int(self.score))) if self.world.done: self.done = True self.persist["score"] = int(self.score) self.next_state = "HIGHSCORES" self.crosshairs_rect.center = pg.mouse.get_pos() if self.world.nighttime: self.crosshairs = self.crosshairs_night else: self.crosshairs = self.crosshairs_day if self.world.h >= 19: if not self.switched: prepare.SFX["clicker"].play() self.switched = not self.switched #elif self.world.h < 19: # self.switched = False if self.world.h >= 6 and self.world.h < 19: if self.switched: prepare.SFX["clicker"].play() self.switched = not self.switched self.remove_clay_score() def shoot(self): if self.cooldown < self.cooldown_duration: return else: self.cooldown = 0 if not self.bullets_in_mag: return else: self.bullets_in_mag -= 1 prepare.SFX["gunshot"].play() for clay in [x for x in self.world.clays if not x.shattered]: if clay.rect.collidepoint(pg.mouse.get_pos()): clay.shatter() self.add_points(clay) self.add_clay_score() def add_clay_score(self): lbl = Label("{}".format(int(self.clay_score)),{"topleft": (50, 5)}, font_size=64) lbl.rect = pg.mouse.get_pos() self.clay_scores.append(lbl) def remove_clay_score(self): if pg.time.get_ticks()-self.clay_score_timer > 3000: self.clay_score_timer = pg.time.get_ticks() if self.clay_scores: self.clay_scores.pop(0) def add_points(self, clay): modifier = clay.z / 50. score = modifier * (100. / clay.speed) self.clay_score = score self.score += score def draw(self, surface): surface.fill(self.world.sky) surface.fill(self.world.grass, self.world.ground_rect) self.world.all_sprites.draw(surface) surface.blit(self.crosshairs, self.crosshairs_rect) for rnd in range(self.bullets_in_mag): surface.blit(self.bullet, (rnd*self.bullet_rect.width + rnd*self.bullet_spacer,55)) self.score_label.draw(surface) for score in self.clay_scores: score.draw(surface)
class Hunting(GameState): """The main game state.""" def __init__(self): super(Hunting, self).__init__() self.world_surf = pg.Surface(prepare.WORLD_SIZE).convert() self.world_rect = self.world_surf.get_rect() self.background = make_background(prepare.WORLD_SIZE) self.all_sprites = pg.sprite.LayeredDirty() self.colliders = pg.sprite.Group() self.ui = pg.sprite.Group() self.noise_detector = NoiseDetector((10, 80), self.ui) self.hunter = Hunter(self.world_rect.center, 0, self.noise_detector, self.all_sprites) self.turkeys = self.add_turkeys() self.bullets = pg.sprite.Group() self.make_trees() hx, hy = self.hunter.rect.center self.ammo_crate = AmmoCrate((hx - 50, hy - 50), self.colliders, self.all_sprites) self.all_sprites.clear(self.world_surf, self.background) self.leaves = pg.sprite.Group() self.roasts = pg.sprite.Group() self.flocks = pg.sprite.Group() self.animations = pg.sprite.Group() self.rustle_sounds = [ prepare.SFX["rustle{}".format(x)] for x in range(1, 21) ] self.wind_gust() style = { "font_path": prepare.FONTS["pretzel"], "font_size": 24, "text_color": (58, 41, 18) } self.shell_label = Label("{}".format(self.hunter.shells), {"topleft": (50, 10)}, **style) self.roasts_label = Label("{}".format(self.hunter.roasts), {"topleft": (50, 50)}, **style) Icon((20, 3), "shell", self.ui) Icon((10, 45), "roast", self.ui) self.add_flock() def wind_gust(self): """Play wind sound and set up next gust.""" prepare.SFX["wind"].play() task = Task(self.wind_gust, randint(15000, 45000)) self.animations.add(task) def add_turkeys(self): """Spawn turkeys.""" turkeys = pg.sprite.Group() w, h = prepare.WORLD_SIZE for _ in range(35): pos = randint(20, w - 20), randint(20, h - 20) Turkey(pos, turkeys, self.all_sprites) return turkeys def make_trees(self): """Spawn trees.""" self.trees = pg.sprite.Group() w, h = prepare.WORLD_SIZE for _ in range(120): while True: pos = (randint(50, w - 20), randint(20, h - 20)) tree = Tree(pos) collisions = (tree.collider.colliderect(other.collider) for other in self.colliders) if not any(collisions) and not tree.collider.colliderect( self.hunter.collider): break self.trees.add(tree) self.colliders.add(tree) self.all_sprites.add(tree) def add_flock(self): """Add a Flock of birds.""" flock = Flock((self.hunter.collider.centerx, -1500), self.animations, self.all_sprites, self.flocks) next_flock = randint(45000, 150000) #next flock in 45-150 seconds task = Task(self.add_flock, next_flock) self.animations.add(task) def update(self, dt): self.animations.update(dt) keys = pg.key.get_pressed() self.hunter.update(dt, keys, self.bullets, self.turkeys, self.colliders, self.all_sprites, self.animations) self.turkeys.update(dt, self.trees) self.bullets.update(dt) for sprite in self.all_sprites: self.all_sprites.change_layer(sprite, sprite.collider.bottom) tree_hits = pg.sprite.groupcollide(self.bullets, self.trees, True, False, footprint_collide) for bullet in tree_hits: for tree in tree_hits[bullet]: choice(self.rustle_sounds).play() num = randint(3, 9) for spot_info in sample(leaf_spots[tree.trunk], num): self.add_leaf(tree, spot_info) turkey_hits = pg.sprite.groupcollide(self.bullets, self.turkeys, True, True, footprint_collide) for t_bullet in turkey_hits: for turkey in turkey_hits[t_bullet]: Roast(turkey.pos, self.roasts, self.all_sprites) if self.hunter.shells < self.hunter.max_shells: if self.hunter.collider.colliderect( self.ammo_crate.rect.inflate(16, 16)): prepare.SFX["gunload"].play() self.hunter.shells = self.hunter.max_shells if self.hunter.state == "move": self.scare_turkeys() self.noise_detector.update(dt) self.shell_label.set_text("{}".format(self.hunter.shells)) self.roasts_label.set_text("{}".format(self.hunter.roasts)) roast_collisions = pg.sprite.spritecollide(self.hunter, self.roasts, True, footprint_collide) if roast_collisions: prepare.SFX["knifesharpener"].play() self.hunter.roasts += len(roast_collisions) self.flocks.update(self.hunter) def scare_turkeys(self): """Make turkeys flee depending on distance and the player's noise level.""" size = self.noise_detector.noise_level scare_rect = self.hunter.collider.inflate(size, size) scared_turkeys = ( t for t in self.turkeys if t.collider.colliderect(scare_rect) and t.state.name != "flee") for scared in scared_turkeys: scared.flee(self.hunter) def add_leaf(self, tree, spot_info): """Add a falling leaf.""" fall_time = randint(2000, 2500) leaf = Leaf(tree, spot_info, self.leaves, self.all_sprites) y = leaf.rect.centery + leaf.fall_distance ani = Animation(centery=y, duration=fall_time, round_values=True) ani.callback = leaf.land ani.start(leaf.rect) ani2 = Animation(centery=leaf.collider.centery + leaf.fall_distance, duration=fall_time, round_values=True) ani2.start(leaf.collider) fade = Animation(img_alpha=0, duration=3000, delay=fall_time, round_values=True) fade.callback = leaf.kill fade.update_callback = leaf.set_alpha fade.start(leaf) self.animations.add(ani, ani2, fade) def get_view_rect(self): """ Return the currently visible portion of the world map centered on the player. """ view_rect = pg.Rect((0, 0), prepare.SCREEN_SIZE) view_rect.center = self.hunter.pos view_rect.clamp_ip(self.world_rect) return view_rect def draw(self, surface): self.all_sprites.draw(self.world_surf) rect = self.get_view_rect() surf = self.world_surf.subsurface(rect) surface.blit(surf, (0, 0)) self.shell_label.draw(surface) self.roasts_label.draw(surface) self.ui.draw(surface)
class MapViewer(GameState): """ This state allows the user to view the map they selected from the menu. """ def __init__(self): super(MapViewer, self).__init__() self.screen_rect = prepare.SCREEN.get_rect() def startup(self, persistent): self.persist = persistent self.map_name = self.persist["map name"] self.map = prepare.GFX[self.map_name.replace(" ", "-")] self.rect = self.map.get_rect(center=self.screen_rect.center) self.map_scale = prepare.SCALES[self.map_name] self.lines = [] self.is_path_end = False self.font = prepare.FONTS["Saniretro"] self.distance_label = None self.text_color = (255, 255, 255) self.bg_color = (0, 0, 0) info = "Left click to add a point, Right click to end path" self.info_labe = Label(self.font, 20, info, self.text_color, {"bottomleft": self.screen_rect.bottomleft}, self.bg_color) def get_event(self, event): if event.type == pg.QUIT: self.quit = True if event.type == pg.MOUSEBUTTONUP: if event.button == 1: # left click if self.lines and self.lines[-1].moving: self.set_anchor_point(event.pos) else: if self.is_path_end: self.create_new_path() self.create_new_line(event.pos) if event.button == 3: # right click self.end_path(event.pos) if event.type == pg.MOUSEMOTION: if self.lines and self.lines[-1].moving: self.lines[-1].end = event.pos def update(self, dt): if self.lines: for line in self.lines: line.update() def draw(self, surface): surface.fill(pg.Color("gray2")) surface.blit(self.map, self.rect) if self.lines: for line in self.lines: line.draw(surface) self.distance_label.draw(surface) self.info_labe.draw(surface) def create_new_line(self, pos): line = Line(pos) self.lines.append(line) text = "{:.2f} miles - {:.2f} kms".format(0.0, 0.0) self.distance_label = Label(self.font, 20, text, self.text_color, {"topleft": (0, 0)}, self.bg_color) def set_anchor_point(self, pos): self.lines[-1].set_end(pos) self.distance_label.set_text(self.distance) line = Line(pos) self.lines.append(line) def create_new_path(self): self.lines = [] def end_path(self, pos): self.lines[-1].set_end(pos) self.is_path_end = True @property def distance(self): distance = 0 for line in self.lines: distance += line.distance * self.map_scale kms = distance * 1.60934 # simple conversion text = "{:.2f} miles - {:.2f} kms".format(distance, kms) return text
class Hunting(GameState): """The main game state.""" def __init__(self): super(Hunting, self).__init__() self.world_surf = pg.Surface(prepare.WORLD_SIZE).convert() self.world_rect = self.world_surf.get_rect() self.background = make_background(prepare.WORLD_SIZE) self.all_sprites = pg.sprite.LayeredDirty() self.colliders = pg.sprite.Group() self.ui = pg.sprite.Group() self.noise_detector = NoiseDetector((10, 80), self.ui) self.hunter = Hunter(self.world_rect.center, 0, self.noise_detector, self.all_sprites) self.turkeys = self.add_turkeys() self.bullets = pg.sprite.Group() self.make_trees() hx, hy = self.hunter.rect.center self.ammo_crate = AmmoCrate((hx - 50, hy - 50), self.colliders, self.all_sprites) self.all_sprites.clear(self.world_surf, self.background) self.leaves = pg.sprite.Group() self.roasts = pg.sprite.Group() self.flocks = pg.sprite.Group() self.animations = pg.sprite.Group() self.rustle_sounds = [prepare.SFX["rustle{}".format(x)] for x in range(1, 21)] self.wind_gust() style = {"font_path": prepare.FONTS["pretzel"], "font_size": 24, "text_color": (58, 41, 18)} self.shell_label = Label("{}".format(self.hunter.shells), {"topleft": (50, 10)}, **style) self.roasts_label = Label("{}".format(self.hunter.roasts), {"topleft": (50, 50)}, **style) Icon((20, 3), "shell", self.ui) Icon((10, 45), "roast", self.ui) self.add_flock() def wind_gust(self): """Play wind sound and set up next gust.""" prepare.SFX["wind"].play() task = Task(self.wind_gust, randint(15000, 45000)) self.animations.add(task) def add_turkeys(self): """Spawn turkeys.""" turkeys = pg.sprite.Group() w, h = prepare.WORLD_SIZE for _ in range(35): pos = randint(20, w - 20), randint(20, h - 20) Turkey(pos, turkeys, self.all_sprites) return turkeys def make_trees(self): """Spawn trees.""" self.trees = pg.sprite.Group() w, h = prepare.WORLD_SIZE for _ in range(120): while True: pos = (randint(50, w - 20), randint(20, h - 20)) tree = Tree(pos) collisions = (tree.collider.colliderect(other.collider) for other in self.colliders) if not any(collisions) and not tree.collider.colliderect(self.hunter.collider): break self.trees.add(tree) self.colliders.add(tree) self.all_sprites.add(tree) def add_flock(self): """Add a Flock of birds.""" flock = Flock((self.hunter.collider.centerx, -1500), self.animations, self.all_sprites, self.flocks) next_flock = randint(45000, 150000) #next flock in 45-150 seconds task = Task(self.add_flock, next_flock) self.animations.add(task) def update(self, dt): self.animations.update(dt) keys = pg.key.get_pressed() self.hunter.update(dt, keys, self.bullets, self.turkeys, self.colliders, self.all_sprites, self.animations) self.turkeys.update(dt, self.trees) self.bullets.update(dt) for sprite in self.all_sprites: self.all_sprites.change_layer(sprite, sprite.collider.bottom) tree_hits = pg.sprite.groupcollide(self.bullets, self.trees, True, False, footprint_collide) for bullet in tree_hits: for tree in tree_hits[bullet]: choice(self.rustle_sounds).play() num = randint(3, 9) for spot_info in sample(leaf_spots[tree.trunk], num): self.add_leaf(tree, spot_info) turkey_hits = pg.sprite.groupcollide(self.bullets, self.turkeys, True, True, footprint_collide) for t_bullet in turkey_hits: for turkey in turkey_hits[t_bullet]: Roast(turkey.pos, self.roasts, self.all_sprites) if self.hunter.shells < self.hunter.max_shells: if self.hunter.collider.colliderect(self.ammo_crate.rect.inflate(16, 16)): prepare.SFX["gunload"].play() self.hunter.shells = self.hunter.max_shells if self.hunter.state == "move": self.scare_turkeys() self.noise_detector.update(dt) self.shell_label.set_text("{}".format(self.hunter.shells)) self.roasts_label.set_text("{}".format(self.hunter.roasts)) roast_collisions = pg.sprite.spritecollide(self.hunter, self.roasts, True, footprint_collide) if roast_collisions: prepare.SFX["knifesharpener"].play() self.hunter.roasts += len(roast_collisions) self.flocks.update(self.hunter) def scare_turkeys(self): """Make turkeys flee depending on distance and the player's noise level.""" size = self.noise_detector.noise_level scare_rect = self.hunter.collider.inflate(size, size) scared_turkeys = (t for t in self.turkeys if t.collider.colliderect(scare_rect) and t.state.name != "flee") for scared in scared_turkeys: scared.flee(self.hunter) def add_leaf(self, tree, spot_info): """Add a falling leaf.""" fall_time = randint(2000, 2500) leaf = Leaf(tree, spot_info, self.leaves, self.all_sprites) y = leaf.rect.centery + leaf.fall_distance ani = Animation(centery=y, duration=fall_time, round_values=True) ani.callback = leaf.land ani.start(leaf.rect) ani2 = Animation(centery=leaf.collider.centery + leaf.fall_distance, duration=fall_time, round_values=True) ani2.start(leaf.collider) fade = Animation(img_alpha=0, duration=3000, delay=fall_time, round_values=True) fade.callback = leaf.kill fade.update_callback = leaf.set_alpha fade.start(leaf) self.animations.add(ani, ani2, fade) def get_view_rect(self): """ Return the currently visible portion of the world map centered on the player. """ view_rect = pg.Rect((0, 0), prepare.SCREEN_SIZE) view_rect.center = self.hunter.pos view_rect.clamp_ip(self.world_rect) return view_rect def draw(self, surface): self.all_sprites.draw(self.world_surf) rect = self.get_view_rect() surf = self.world_surf.subsurface(rect) surface.blit(surf, (0, 0)) self.shell_label.draw(surface) self.roasts_label.draw(surface) self.ui.draw(surface)
class Shooting(GameState): """Main gameplay state.""" colors = { "day": { "sky": pg.Color("skyblue"), "grass": pg.Color(125, 183, 100)}, "night": { "sky": pg.Color(1, 2, 7), "grass": pg.Color(11, 15, 8)}} def __init__(self): super(Shooting, self).__init__() self.animations = pg.sprite.Group() self.world = World(True) self.cooldown = 0 self.cooldown_duration = 250 def startup(self, persistent): self.persist = persistent self.score = 0 self.score_label = Label("{}".format(self.score), {"topleft": (5, 5)}, font_size=64) self.world.reset() def get_event(self, event): if event.type == pg.QUIT: pg.mouse.set_visible(True) self.quit = True elif event.type == pg.KEYUP: if event.key == pg.K_ESCAPE: self.quit = True elif event.type == pg.MOUSEBUTTONDOWN: self.shoot() def update(self, dt): self.cooldown += dt self.animations.update(dt) self.world.update(dt) for sprite in self.world.all_sprites: self.world.all_sprites.change_layer(sprite, sprite.z * -1) self.score_label.set_text("{}".format(int(self.score))) if self.world.done: self.done = True self.persist["score"] = int(self.score) self.next_state = "HIGHSCORES" def shoot(self): if self.cooldown < self.cooldown_duration: return else: prepare.SFX["gunshot"].play() self.cooldown = 0 for clay in [x for x in self.world.clays if not x.shattered]: if clay.rect.collidepoint(pg.mouse.get_pos()): clay.shatter() self.add_points(clay) def add_points(self, clay): modifier = clay.z / 50. score = modifier * (100. / clay.speed) self.score += score def draw(self, surface): surface.fill(self.world.sky) surface.fill(self.world.grass, self.world.ground_rect) self.world.all_sprites.draw(surface) self.score_label.draw(surface)
class CourseInfoEntry(GameState): """Edit a course.""" def __init__(self): super(CourseInfoEntry, self).__init__() def startup(self, persistent): self.persist = persistent self.textbox_rect = pg.Rect(0, 0, 600, 200) self.textbox_rect.center = prepare.SCREEN_RECT.center self.course_info = OrderedDict( (("Course Name", None), ("Course Width", None), ("Course Length", None))) self.textbox_style = { "color": pg.Color(242, 255, 255), "font_color": pg.Color(48, 75, 50), "active_color": pg.Color(48, 75, 50), "outline_color": pg.Color(72, 96, 74)} self.textbox = TextBox(self.textbox_rect, **self.textbox_style) self.current_info = "Course Name" self.label_style = {"text_color": (48, 75, 50), "font_size": 48} w, h = prepare.SCREEN_SIZE self.prompt = Label("Enter {}".format(self.current_info), {"midtop": (w//2, 20)}, **self.label_style) self.textbox.update() def leave_state(self, next_state): self.done = True self.next_state = next_state def get_event(self, event): self.textbox.get_event(event, pg.mouse.get_pos()) if event.type == pg.QUIT: self.leave_state("MAIN_MENU") elif event.type == pg.KEYUP: if event.key == pg.K_ESCAPE: self.leave_state("MAIN_MENU") def update(self, dt): self.textbox.update() if not self.textbox.active: self.course_info[self.current_info] = self.textbox.final for info in self.course_info: if self.course_info[info] is None: self.textbox = TextBox(self.textbox_rect, **self.textbox_style) self.current_info = info if "Width" in info or "Length" in info: self.textbox.accepted = string.digits self.textbox.update() self.prompt.set_text("Enter {}".format(self.current_info)) break else: self.make_course() def add_random_trees(self, map_size): trees = [] w, h = map_size lift_rect = pg.Rect(0, 0, 150, h) lift_rect.centerx = w // 2 num_trees = int(w * h * .0005) for _ in range(num_trees): while True: pos = randint(0, w), randint(0, h) if not lift_rect.collidepoint(pos): trees.append(["tree", pos]) break return trees def make_course(self): course_info = {} name = self.course_info["Course Name"] course_info["map_name"] = name course_info["map_size"] = (int(self.course_info["Course Width"]), int(self.course_info["Course Length"])) obstacles = [] trees = self.add_random_trees(course_info["map_size"]) obstacles.extend(trees) course_info["obstacles"] = obstacles filepath = os.path.join("resources", "courses", "{}.json".format(name)) with open(filepath, "w") as f: json.dump(course_info, f) self.done = True self.persist["course_name"] = name self.next_state = "EDITOR" def draw(self, surface): surface.fill(pg.Color(242, 255, 255)) self.textbox.draw(surface) self.prompt.draw(surface)