def win (self): if self.winning: return self.winning = True self.next_level(progress = False) if self.ID not in conf.COMPLETED_LEVELS: conf.COMPLETED_LEVELS.append(self.ID) conf.dump() self.start_fading(lambda: self.next_level(False))
def update (self): # fade counter if self.fading: self.fade_counter -= 1 if self.fade_counter == 0: self.fading = False del self.fade_sfc self.fade_cb() # move player if not self.dying: pl = self.player pl.update() # get amount to move window by w = self.window self.old_window = w.copy() x0, y0 = self.centre if self.paused or self.first: dx = dy = 0 self.first = False else: x, y = pg.mouse.get_pos() dx, dy = x - x0, y - y0 # don't move too far outside the screen w_moved = w.move(dx, dy).clamp(self.window_bds) dx, dy = w_moved[0] - w[0], w_moved[1] - w[1] pg.mouse.set_pos(x0, y0) wx0, wy0, ww, wh = self.total_window = w.union(w.move(dx, dy)) # move window if self.dying: # just move window w.move_ip(dx, dy) self.update_rects() else: self.vert_dirn = 3 if dx == dy == 0: # just handle collisions self.handle_collisions() else: # check if player and window intersect wx1, wy1 = wx0 + ww, wy0 + wh r = pl.rect o_r = pl.old_rect px0, py0 = min(r[0], o_r[0]), min(r[1], o_r[1]) px1 = max(r[0] + r[2], o_r[0] + o_r[2]) py1 = max(r[1] + r[3], o_r[1] + o_r[3]) if px1 > wx0 and py1 > wy0 and px0 < wx1 and py0 < wy1: # if so, move window a few pixels at a time c = conf.WINDOW_MOVE_AMOUNT for axis, d in ((0, dx), (1, dy)): dirn = 1 if d > 0 else -1 while d * dirn > 0: d -= dirn * c rel = [0, 0] rel[axis] += c * dirn + (0 if d * dirn > 0 else d) w.move_ip(rel) self.update_rects() if not self.dying: self.handle_collisions() else: # else move it the whole way w.move_ip(dx, dy) self.update_rects() self.handle_collisions() if self.vert_dirn == 1: pl.on_ground = conf.ON_GROUND_TIME # clouds if self.clouds: # jitter jx = conf.CLOUD_JITTER jy = jx * conf.CLOUD_VERT_SPEED_RATIO v0 = self.cloud_vel v0[0] += jx * random0() v0[1] += jy * random0() r = conf.RES for p, v, s in self.clouds: for i, (i_w, r_w) in enumerate(zip(s, r)): # move x = p[i] x += v0[i] + v[i] # wrap if x + i_w < 0: x = r_w elif x > r_w: x = -i_w p[i] = x # particles ptcls = [] rects = [] for k, j, group in self.particles: g = [] x0, y0 = conf.RES x1 = y1 = 0 for c, p, v, size, t in group: x, y = p # update boundary if x < x0: x0 = x if y < y0: y0 = y if x + size > x1: x1 = x + size if y + size > y1: y1 = y + size t -= 1 if t != 0: # move vx, vy = v x += vx y += vy # update boundary if x < x0: x0 = x if y < y0: y0 = y if x + size > x1: x1 = x + size if y + size > y1: y1 = y + size # damp/jitter vx *= k vy *= k vx += j * random0() vy += j * random0() g.append((c, (x, y), (vx, vy), size, t)) if g: ptcls.append((k, j, g)) if x1 > x0 and y1 > y0: rects.append((int(x0), int(y0), ceil(x1 - x0), ceil(y1 - y0))) self.particles = ptcls self.particle_rects = rects # death counter if self.dying: self.dying_counter -= 1 if self.dying_counter == 0: self.init() return # player velocity pl.update_vel() # die if OoB if pl.rect[1] > conf.RES[1]: self.die() # win if at goal p = pl.rect c = w.clip(self.goal) if c and self.get_clip(p, c): self.win() # check if at checkpoints for c in self.checkpoints[self.current_cp + 1:]: if w.clip(c) and self.get_clip(p, c): self.current_cp += 1 # check if at stars for i, s in enumerate(self.stars): if not s.got and w.clip(s.rect) and self.get_clip(p, s.rect): #self.game.play_snd('collectstar') if self.star_channel is not None and all(s.got for s in self.stars): self.star_channel.pause() s.got = True conf.STARS.append([self.ID, i]) conf.dump()