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)
def __init__(self, data_dict, field_widths, field_height, field_text_font, field_text_size, field_text_color): """ ARGS data_dict: an OrderedDict of field_name: value pairs field_widths: list of the widths for each field (ordered to match the order of keys in data_dict) field_height: the height of a field's rect text_color: color that field values should be rendered in """ self.data = data_dict left, top = 0, 0 total_width = sum(field_widths) self.image = pg.Surface((total_width, field_height)).convert_alpha() self.image.fill((0, 0, 0, 0)) right = left for width, field_name in zip(field_widths, data_dict): right += width value = data_dict[field_name] label = Label("{}".format(value), {"midright": (right - 3, top + (field_height // 2))}, text_color=field_text_color, font_size=field_text_size, font_path=field_text_font) label.draw(self.image) self.rect = self.image.get_rect()
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 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 labels_from_video(video_url): video = read_video(video_url) points = [0] points.extend( visual_filter_cuts_base( Label( input_url=video_url, input_start_point=0, input_final_point=video.duration ), video.url)) points.append(video.duration) p = [points[0]] points[1:-1] = [ points[i] for i in range(1, len(points) - 1) if points[i] - p[0] >= args.labels_min_length and points[-1] - points[i] >= args.labels_min_length and not p.remove(p[0]) and not p.append(points[i]) ] return [ Label( output_start_point=points[i], output_final_point=points[i + 1] ) for i in range(len(points) - 1) ]
def __init__(self, name, topleft, width, height, fill_color, line_color, line_weight, header_text_font, header_text_size, header_text_color): """" ARGS name: the name of the field the header controls topleft: the topleft screen position of the table width: the width of the field the header controls height: the height of the header's rect fill_color: color to fill the header's rect with line_color: the line color of the SortableTable """ self.image = pg.Surface((width, height)) self.image.fill(fill_color) center = (width // 2), height // 2 self.field_name = name label = Label(name, {"center": center}, font_path=header_text_font, font_size=header_text_size, text_color=header_text_color) label.draw(self.image) pg.draw.rect(self.image, line_color, ((0, 0), (width, height)), line_weight) self.rect = self.image.get_rect(topleft=topleft) self.direction = -1 self.selected = False
def make_score_labels(self): self.labels = pg.sprite.Group() centerx = prepare.SCREEN_RECT.centerx title = Label("High Scores", {"midtop": (centerx, 10)}, self.labels, font_size=64, text_color=(225, 111, 4)) top = 80 for score in self.high_scores: label = Label("{}".format(score), {"midtop": (centerx, top)}, self.labels, font_size=48) top += 60
def label_from_template(self, *keys: List[str]): label_info = dict() for key in keys: label_info.update(self._label_templates.get(key, None)) try: return Label(**label_info) except Exception: print( f"Labeler.label_from_template() Exception: " f"arguments passed to Label() object not correct.\n" f"An empty Label() object has been returned." ) return Label()
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 __init__(self, date, description, amount, balance): self.date = date self.description = description self.amount = float(amount) self.balance = float(balance) self.is_out = self.amount < 0 self.type = Label.from_description(self.description)
def create_label(n): random.seed(n + random.randint(0, sys.maxsize)) retries = args.visual_filter_retries while True: label, label_changed = change_label(n) if not label_changed: break if args.increment: cache_input(label, n) duration = label.output_final_point - label.output_start_point cache_label = Label( output_start_point=label.output_start_point, output_final_point=label.output_final_point, input_url=args.video_cache % (n + 1), input_start_point=0, input_final_point=duration ) cache_video = Video( url=args.video_cache % (n + 1), duration=duration ) accept = visual_filter(cache_label, cache_video.url) else: accept = visual_filter(label, videos[label.input_url].url) if accept or retries == 0: labels.labels[n] = label break retries -= 1 if label_changed: write_labels() if args.increment and not os.path.isfile(args.video_cache % (n + 1)): cache_input(label, n) return label
def __init__(self): super(Splash, self).__init__() self.labels = pg.sprite.Group() Label("Splash Screen", {"midbottom": prepare.SCREEN_RECT.center}, self.labels, text_color="gold4", font_size=96)
def __init__(self): super(Splash, self).__init__() self.next_state = "GAMEPLAY" self.labels = pg.sprite.Group() Label("Splash Screen", {"midbottom": prepare.SCREEN_RECT.center}, self.labels, font_path=prepare.FONTS["weblysleekuisb"], text_color="gold4", font_size=96)
def make_labels(self, frequencies): self.labels = pg.sprite.Group() names = ["WOLD", "WMLW", "WEDM", "WTCH"] cx, top = prepare.SCREEN_RECT.centerx, 20 for name, num in zip(names, frequencies): Label("{} {}".format(name, num), {"midtop": (cx, top)}, self.labels, font_size=24, text_color="antiquewhite", font_path=prepare.FONTS["weblysleekuisl"]) top += 30
def create_labels(): if (args.labels_from_chords_chroma or args.labels_from_chords_cnn): args.labels_from_chords = True if (args.labels_from_beats_detection or args.labels_from_beats_detection_crf or args.labels_from_beats_tracking or args.labels_from_beats_tracking_dbn): args.labels_from_beats = True if (args.labels_from_notes_rnn or args.labels_from_notes_cnn): args.labels_from_notes = True if (not args.labels_from_chords and not args.labels_from_beats and not args.labels_from_notes and not args.labels_from_onsets): args.labels_from_chords = True args.labels_from_beats = True points = sorted( set([ 0, *(points_from_chords(args.audio_output) if args.labels_from_chords else []), *(points_from_beats( args.audio_output) if args.labels_from_beats else []), *(points_from_notes(args.audio_output) if args.labels_from_notes else []), *(points_from_onsets(args.audio_output) if args.labels_from_onsets else []), duration(args.audio_output) ])) if args.output_max_length: points[:] = [p for p in points if p < args.output_max_length ] + [args.output_max_length] if args.labels_joints and args.labels_joints > 1: points[:] = points[:-1:args.labels_joints] + [points[-1]] if args.labels_splits and args.labels_splits > 1: points[:] = [ points[i] + (points[i + 1] - points[i]) * j / args.labels_splits for i in range(len(points) - 1) for j in range(args.labels_splits) ] + [points[-1]] if args.labels_max_length: points[:] = [ points[i] + j * args.labels_max_length for i in range(len(points) - 1) for j in range(1 + int((points[i + 1] - points[i]) / args.labels_max_length)) ] if args.labels_min_length: p = [points[0]] points[1:-1] = [ points[i] for i in range(1, len(points) - 1) if points[i] - p[0] >= args.labels_min_length and points[-1] - points[i] >= args.labels_min_length and not p.remove(p[0]) and not p.append(points[i]) ] return [ Label(output_start_point=points[i], output_final_point=points[i + 1]) for i in range(len(points) - 1) ]
def autolabel(self, var_name, unit): label_info = self._label_templates.get(unit, None) if label_info is not None: var_parse = re.match( label_info["symbol"] + r"_?([a-zA-Z0-9]+)", var_name ) if var_parse: label_info.update( self._label_templates.get(var_parse.group(1).title(), {}) ) try: return Label(**label_info) except Exception: print( f"Labeler.autolabel() Exception: " f"arguments passed to Label() object not correct.\n" f"Continuing with empty Label() object " f"for variable --> {var_name}" ) return Label()
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 __init__(self): super(Hunting, self).__init__() map_data = MapData() map_layer = BufferedRenderer(map_data, prepare.SCREEN_SIZE) self.all_sprites = PyscrollGroup(map_layer=map_layer) self.colliders = pg.sprite.Group() self.ui = pg.sprite.Group() self.noise_detector = NoiseDetector((10, 80), self.ui) self.animations = pg.sprite.Group() self.hunter = Hunter(map_layer.map_rect.center, 0, self.noise_detector, self.all_sprites) self.leaves = pg.sprite.Group() self.roasts = pg.sprite.Group() self.flocks = pg.sprite.Group() 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.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 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 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 order_print_barcode(request, order_pk): order = models.Orders.objects.get(pk=order_pk) MESSAGE_TAGS = {messages.ERROR: 'danger'} printer_id = request.GET.get('printer') p_label = Label() p_label.set_order_id(order_pk) p_label.set_labelprinter_id(printer_id) not_err, msg = p_label.print_label() if not_err: messages.info(request, msg) else: messages.error(request, 'Error when printer label [%s]' % msg, extra_tags='danger')
def change_label(n): l = labels.labels[n] output_duration = l.output_final_point - l.output_start_point input_url = l.input_url if ( l.input_url is not None and ( os.path.isfile(l.input_url) or validators.url(l.input_url))) else ( next_input_url(n)) input_duration = l.input_final_point - l.input_start_point input_start_point = l.input_start_point if ( input_url is None or l.input_start_point >= 0) else ( next_input_start_point( n, duration(input_url), output_duration)) input_final_point = l.input_final_point if ( input_url is None or l.input_start_point >= 0 and l.input_final_point >= 0 and abs(output_duration - input_duration) < 0.01) else ( next_input_final_point( n, duration(input_url), output_duration, input_start_point)) label_changed = ( input_url != l.input_url or input_start_point != l.input_start_point or input_final_point != l.input_final_point) return Label( l.output_start_point, l.output_final_point, input_url, input_start_point, input_final_point ), label_changed
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)
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)
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)
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
def fget(self): from labels import Label return [Label(self.__api, x['label']) for x in self.__raw.labels]
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)