def generate_points( self, N, x_min=10, x_max=SCREEN_WIDTH - 10, y_min=20, y_max=SCREEN_HEIGHT - 10, v_min=0.1, v_max=2, ): pts = [] for _ in range(N): x = pyxel.rndi(x_min, x_max) y = pyxel.rndi(y_min, y_max) v = pyxel.rndf(v_min, v_max) angle = pyxel.rndf(0, 360) vx = v * pyxel.cos(angle) vy = v * pyxel.sin(angle) num_edges = pyxel.rndi(1, 2) # NOTE: can connect to self which isn't great connections = [pyxel.rndi(0, N - 1) for _ in range(num_edges)] pt = Point(x=x, y=y, connections=connections, vx=vx, vy=vy) pts.append(pt) return pts
def __init__(self): self.stars = [] for i in range(NUM_STARS): self.stars.append(( pyxel.rndi(0, pyxel.width - 1), pyxel.rndi(0, pyxel.height - 1), pyxel.rndf(1, 2.5), ))
def generate_apple(self): """Generate an apple randomly.""" snake_pixels = set(self.snake) self.apple = self.snake[0] while self.apple in snake_pixels: x = pyxel.rndi(0, WIDTH - 1) y = pyxel.rndi(HEIGHT_SCORE + 1, HEIGHT - 1) self.apple = Point(x, y)
def random(cls): # worked out the original numbers for 120 so whatever, bodge a scale factor VY_SCALE = 5.0 args = dict( x=pyxel.rndi(-10, SCREEN_WIDTH-1), y=pyxel.rndi(-10, 0), vx=0.05 + pyxel.rndf(-0.2, 2.0), vy=pyxel.rndf(VY_SCALE, VY_SCALE + 0.4), col=cls._COLORS[pyxel.rndi(0, len(cls._COLORS)-1)] # vx=0.0, # vy=0.0, ) return cls(**args)
def update_fruit(self, x, y, kind, is_alive): if is_alive and abs(x - self.player_x) < 12 and abs( y - self.player_y) < 12: is_alive = False self.score += (kind + 1) * 100 self.player_dy = min(self.player_dy, -8) pyxel.play(3, 4) x -= 2 if x < -40: x += 240 y = pyxel.rndi(0, 104) kind = pyxel.rndi(0, 2) is_alive = True return (x, y, kind, is_alive)
def __init__(self): pyxel.init(160, 120, title="Pyxel Jump") pyxel.load("assets/jump_game.pyxres") self.score = 0 self.player_x = 72 self.player_y = -16 self.player_dy = 0 self.is_alive = True self.far_cloud = [(-10, 75), (40, 65), (90, 60)] self.near_cloud = [(10, 25), (70, 35), (120, 15)] self.floor = [(i * 60, pyxel.rndi(8, 104), True) for i in range(4)] self.fruit = [(i * 60, pyxel.rndi(0, 104), pyxel.rndi(0, 2), True) for i in range(4)] pyxel.playm(0, loop=True) pyxel.run(self.update, self.draw)
def update(self): if pyxel.btnp(pyxel.KEY_Q): pyxel.quit() elif pyxel.btnp(pyxel.KEY_P): self.paused = not self.paused RAIN_RATE = 2 if not self.paused and pyxel.frame_count % RAIN_RATE == 0: num_new = pyxel.rndi(20, 30) new_drops = (Raindrop.random() for _ in range(num_new)) self._entities.extend(new_drops) if not self.paused: for eid, entity in enumerate(self._entities): entity.update() if not entity.alive: self._removals.append(eid) num_removed = 0 for eid in self._removals: del self._entities[eid-num_removed] num_removed += 1 self._removals.clear()
def __init__(self, x, y): self.x = x self.y = y self.w = ENEMY_WIDTH self.h = ENEMY_HEIGHT self.dir = 1 self.timer_offset = pyxel.rndi(0, 59) self.is_alive = True enemies.append(self)
def __init__(self): self.r = pyxel.rndf(3, 10) self.pos = Vec2( pyxel.rndf(self.r, SCREEN_WIDTH - self.r), pyxel.rndf(self.r, SCREEN_HEIGHT - self.r), ) self.vel = Vec2( pyxel.rndf(-MAX_BUBBLE_SPEED, MAX_BUBBLE_SPEED), pyxel.rndf(-MAX_BUBBLE_SPEED, MAX_BUBBLE_SPEED), ) self.color = pyxel.rndi(1, 15)
def generate_boxes( self, N, x_min=0, x_max=SCREEN_WIDTH, y_min=10, y_max=SCREEN_HEIGHT, w_min=0, w_max=20, h_min=0, h_max=20, ): boxes = [] for _ in range(N): x = pyxel.rndi(x_min, x_max) y = pyxel.rndi(y_min, y_max) w = pyxel.rndi(w_min, w_max) h = pyxel.rndi(h_min, h_max) color = pyxel.rndi(0, 15) # pyxel has 16 colors box = (x, y, w, h, color) boxes.append(box) return boxes
def update_floor(self, x, y, is_alive): if is_alive: if (self.player_x + 16 >= x and self.player_x <= x + 40 and self.player_y + 16 >= y and self.player_y <= y + 8 and self.player_dy > 0): is_alive = False self.score += 10 self.player_dy = -12 pyxel.play(3, 3) else: y += 6 x -= 4 if x < -40: x += 240 y = pyxel.rndi(8, 104) is_alive = True return x, y, is_alive
def update_play_scene(self): if pyxel.frame_count % 6 == 0: Enemy(pyxel.rndi(0, pyxel.width - ENEMY_WIDTH), 0) for enemy in enemies: for bullet in bullets: if (enemy.x + enemy.w > bullet.x and bullet.x + bullet.w > enemy.x and enemy.y + enemy.h > bullet.y and bullet.y + bullet.h > enemy.y): enemy.is_alive = False bullet.is_alive = False blasts.append( Blast(enemy.x + ENEMY_WIDTH / 2, enemy.y + ENEMY_HEIGHT / 2)) pyxel.play(1, 1) self.score += 10 for enemy in enemies: if (self.player.x + self.player.w > enemy.x and enemy.x + enemy.w > self.player.x and self.player.y + self.player.h > enemy.y and enemy.y + enemy.h > self.player.y): enemy.is_alive = False blasts.append( Blast( self.player.x + PLAYER_WIDTH / 2, self.player.y + PLAYER_HEIGHT / 2, )) pyxel.play(1, 1) self.scene = SCENE_GAMEOVER self.player.update() update_list(bullets) update_list(enemies) update_list(blasts) cleanup_list(enemies) cleanup_list(bullets) cleanup_list(blasts)