def update(self): if self.running: self.ship.update(self.dt) # adjusted gl. 20 is approx ship radius adj = 17 if self.landed: if self.mt.is_above_ground(self.ship, adj): self.landed = False else: self.ship.y = self.mt.get_y(self.ship.x) + adj self.ship.null() else: level = self.mt.is_level(self.ship.x - 10, self.ship.x + 10) hot = abs(self.ship.vy) > 1.0 or abs( self.ship.vx) > 0.30 or not level self.label.text = f'vx:{self.ship.vx:+4.2f} vy:{self.ship.vy:+4.2f} r:{self.ship.r:4.2f} l:{level}' self.label.color = '#f44' if hot else 'white' if not self.mt.is_above_ground(self.ship, adj) and self.ship.vy < 0: if hot: self.crash() else: self.land() self.ship.y = self.mt.get_y(self.ship.x) + adj # recompute scale and update positions of all children self.update_scale() for c in self.children: if isinstance(c, Part): c.update(self.dt) if not self.mt.is_above_ground(c, 0): c.y = self.mt.get_y(c.x) c.vr = 0 c.vx = -c.vx / 2 # viewport is centered on ship so just adjust for that c.position = (self.size.w / self.scale / 2 + c.x - self.ship.x, c.y) c.rotation = c.r self.ship.position = Point(self.size.w / self.scale / 2, self.ship.y) self.ship.rotation = self.ship.r self.stats.position = Point(self.size.w / 2, self.size.h - 40) / self.scale self.stats.scale = 1 / self.scale self.label.position = Point(self.size.w / 2, self.size.h - 20) / self.scale self.label.scale = 1 / self.scale self.mt.position = Point( self.size.w / self.scale / 2 + 0 - self.ship.x, 0) self.stars.scale = 1 / self.scale self.update_status()
def generate_mountain(self, size): self.points = [Point(0, 0), Point(20, 20)] x, y = 10, 10 while x < size.w: dx = random.uniform(40, 80) if random.uniform(0, 1) < 0.6: dy = random.uniform(-size.h, size.h) else: dy = 0 x = min(x + dx, size.w) y = max(y + dy, 20) y = min(y, MAX_EXTENT) self.points.append(Point(x, y))
def fire(self): path = make_path([(0, 0), (0, 1)]) path.line_width = 2 bullet = ShapeNode(path=path, fill_color='clear', stroke_color='white', parent=self) r = self.ship.rotation cannon = Point(math.cos(r), math.sin(r)) * 20 bullet.position = self.ship.position + cannon end = bullet.position + Point(math.cos(r), math.sin(r)) * 350 bullet.run_action(A.sequence(A.move_to(end.x, end.y, 1.5), A.remove())) sound.play_effect('fire.mp3', looping=False, volume=0.0)
def is_level(self, x1, x2): if x1 > 0 and x2 > 0: pp = Point(0, 0) for p in self.points: if x1 > pp.x and x2 > pp.x and x1 < p.x and x2 < p.x: if pp.y == p.y: return True else: return False pp = p return False
def generate_shapes(self): start_i = 0 end = self.points[-1] for i, p in enumerate(self.points): start_x = self.points[start_i].x width = p.x - start_x # shapes cant be larger than 2048 wide so create multiple ShapeNodes if width > MAX_EXTENT or p == end: # get a sublist of points adjusted for the start of the panel points = [ p - Point(start_x, 0) for p in self.points[start_i:i] ] base = [ Point(points[-1].x, 0), Point(0, 0), Point(0, points[0].y) ] path = make_path(points + base, 1.5) panel = ShapeNode(path=path, fill_color=BACKGROUND, stroke_color='clear') panel.anchor_point = (0, 0) panel.position = (start_x, 0) self.add_child(panel) path = make_path(points, 1.5) path.move_to(0, 0) path.line_to(0, 0) panel = ShapeNode(path=path, fill_color='clear', stroke_color='#bbb') panel.anchor_point = (0, 0) panel.position = (start_x, 0) self.add_child(panel) start_i = i - 1
def end_game(self): self.game_state = GAME_DEAD self.player.velocity = Point(0, 0) death_loc = self.player.frame.center() death_loc.y = max(death_loc.y, 80) self.player.die() del self.player self.player = None score = int(self.climb / 10) if self.high_scores.is_high_score(player_name, score): self.smoke_special.frame.center(death_loc) self.smoke_special.configure(frame_count=0, is_done=False) # console.hud_alert('New high score!') # for debugging purposes run_in_thread(high_score_sounds) fmt = 'Congratulations {}:\nYou have a new high score!' self.high_score_msg = fmt.format(player_name) else: self.smoke_normal.frame.center(death_loc) self.smoke_normal.configure(frame_count=0, is_done=False)
def crash(self): self.thrust_sound.stop() sound.play_effect('explosion_large_distant.mp3') self.fuel = max(self.fuel - 50, 0) # create ship parts prevx, prevy = self.points[0] for x, y in self.points[1:]: p = Part([(prevx, prevy), Point(x, y)]) p.x, p.y, p.r = self.x + (x + prevx) / 2, self.y + ( y + prevy) / 2, self.r prevx, prevy = x, y p.vr = random.uniform(-0.3, 0.3) p.vx = self.vx + random.uniform(-1, 1) p.vy = self.vy * -0.3 + 1 p.run_action(A.sequence(A.fade_to(0.25, 30), A.remove())) self.parent.add_child(p)
def __init__(self, rect=Rect(), parent=None, image_name='Boy'): super(Sprite, self).__init__(rect) if parent: parent.add_layer(self) self.image = image_name self.velocity = Point(0, 0)
def distance_from_start(self): return abs(Point(*self.location) - Point(*self.start_location))
def get_center_location(self): center_loc = Point(0, 0) for touch in self.touches.values(): center_loc += Point(*touch.location) center_loc /= len(self.touches) return center_loc
def a(self): return abs(Point(self.ax, self.ay))