Example #1
0
    def __init__(self, app, state=None):

        super().__init__(app, state, self)

        self.scene = Scene(self.app, self)
        self.terminal = self.scene.add(Terminal(self.app, self.scene))
        self.bigterm = self.scene.add(Terminal(self.app, self.scene, 32))
        self.camera = self.scene.add(Camera(app, self.scene, self.app.size))
        self.scene.ground_color = "darkgreen"
        self.time = 0

        rows = 8
        backdrop_h = 150
        for i in range(rows):
            h = int(backdrop_h) // rows
            y = h * i
            backdrop = pygame.Surface((self.app.size.x, h))
            interp = i / rows
            interp_inv = 1 - i / rows
            backdrop.set_alpha(255 * interp_inv * 0.2)
            backdrop.fill(pg_color(ncolor("white") * interp_inv))
            self.scene.on_render += lambda _, y=y, backdrop=backdrop: self.app.screen.blit(
                backdrop, (0, y))

        rows = 8
        backdrop_h = 100
        for i in range(rows):
            h = int(backdrop_h) // rows
            y = h * i
            backdrop = pygame.Surface((self.app.size.x, h))
            interp = i / rows
            interp_inv = 1 - i / rows
            backdrop.set_alpha(255 * interp_inv * 0.1)
            backdrop.fill(pg_color(ncolor("white") * interp_inv))
            self.scene.on_render += lambda _, y=y, backdrop=backdrop: self.app.screen.blit(
                backdrop, (0, y))

        backdrop_h = int(24)
        rows = 4
        for i in range(rows, 0, -1):
            h = int(backdrop_h) // rows
            y = h * i
            backdrop = pygame.Surface((self.app.size.x, h))
            interp = i / rows
            interp_inv = 1 - i / rows
            backdrop.set_alpha(200 * interp_inv)
            backdrop.fill((0))
            # backdrop.fill(pg_color(ncolor('black')*interp_inv))
            self.scene.on_render += lambda _, y=y, backdrop=backdrop: self.app.screen.blit(
                backdrop, (0, self.app.size.y - y))
Example #2
0
    def update(self, dt):
        super().update(dt)  # needed for script

        self.scene.update(dt)
        self.time += dt
        self.bg_color = (ncolor("darkgreen") +
                         math.sin(self.time % 1 * math.tau * 2) * 0.05)
Example #3
0
    def __init__(self, app, scene, pos: vec3, z_vel: float, **kwargs):

        super().__init__(app,
                         scene,
                         None,
                         position=pos,
                         velocity=Z * 1000,
                         **kwargs)

        self._surface = None
        gcolor = self.scene.ground_color
        if gcolor:

            if "ROCK" not in self.app.cache:
                # self.color = ncolor("white")
                self._surface = pygame.Surface(ivec2(4))
                # self.color = ncolor("white")
                self._surface.fill(
                    pg_color(glm.mix(ncolor("black"), gcolor, 0.4)))
                # self._surface.fill((0,0,0))

                self.app.cache["ROCK"] = self._surface

                # self.velocity = Z * 10000 + z_vel
            else:
                self._surface = self.app.cache["ROCK"]
Example #4
0
    def __init__(self, app, state=None):

        super().__init__(app, state)

        self.scene = Scene(self.app, self)
        self.gui = Scene(self.app, self)
        self.slots = SlotList()
        self.paused = False

        # self.scene.add(ButtaBomber(app, self.scene, vec3(0, 0, -3000)))
        # self.scene.add(Powerup(app, self.scene, 'star', position=vec3(0, 0, -3000)))

        # create terminal first since player init() writes to it
        self.terminal = self.gui.add(Terminal(self.app, self.scene))

        self.app.inputs = self.build_inputs()
        self.camera = self.scene.add(Camera(app, self.scene, self.app.size))
        stats = self.stats = self.app.data["stats"] = self.app.data.get(
            "stats", Stats()
        )
        self.level = stats.level
        self.player = self.scene.add(Player(app, self.scene, level=self.level))

        # self.scripts += self.score_screen

        # self.camera.slots.append(
        #     self.player.on_move.connect(lambda: self.camera.update_pos(self.player))
        # )

        self.debug = False
        self.slots += [
            app.inputs["debug"].on_press(lambda _: self.debug_mode(True)),
            app.inputs["debug"].on_release(lambda _: self.debug_mode(False)),
        ]
        self.slots += [
            app.inputs["pause"].on_press(self.toggle_pause),
        ]

        self.time = 0

        # score backdrop
        backdrop_h = int(24 * 1.8)

        # draw a score backdrop
        rows = 8
        for i in range(rows):
            h = int(backdrop_h) // rows
            y = h * i
            backdrop = pygame.Surface((self.app.size.x, h))
            interp = i / rows
            interp_inv = 1 - i / rows
            backdrop.set_alpha(255 * interp * 0.4)
            # backdrop.fill((0))
            backdrop.fill(pg_color(ncolor("white") * interp_inv))
            self.scene.on_render += lambda _, y=y, backdrop=backdrop: self.app.screen.blit(
                backdrop, (0, y)
            )
Example #5
0
 def set_ground_color_opt(self, c):
     """
     Optimized for fades.
     """
     if not self.ground:
         self.ground = self.add(Ground(self.app, self, GROUND_HEIGHT))
     if c:
         self.ground.fade_opt(ncolor(c))
     else:
         self.ground = None
Example #6
0
    def __init__(self, app, state=None):

        super().__init__(app, state, self)

        self.scene = Scene(self.app, self)

        self.terminal = self.scene.add(Terminal(self.app, self.scene))
        self.camera = self.scene.add(Camera(app, self.scene, self.app.size))

        self.time = 0
        self.bg_color = ncolor("darkred")
Example #7
0
    def ground_color(self, color):
        if color is None:
            return

        self._ground_color = ncolor(color)
        if not self.ground:
            self.ground = self.add(Ground(self.app, self, GROUND_HEIGHT))

        self.ground.color = color
        if "ROCK" in self.app.cache:
            del self.app.cache["ROCK"]  # rocks need to reload their color
Example #8
0
    def change_logo_color(self, script):
        yield
        bigterm = self.bigterm

        while True:
            if self.scene.ground_color:
                break
            yield
        c = glm.mix(
            self.scene.ground_color,
            glm.mix(ncolor("white"), random_rgb(), random.random()),
            0.2,
        )

        r = 0
        # rc = vec4()
        self.scene.play_sound("explosion.wav")
        while True:
            if r % 30 == 0:
                rc = random_rgb()
            s = "BUTTERFLY     "
            for i in range(len(s)):
                # c = ncolor('purple') * i/len(s) + math.sin(r / 200 + i+r) ** 2 + .6
                c = (ncolor("purple") * i / len(s) +
                     ((math.sin(i + r) + 0.4) * script.dt) + 0.3)
                bigterm.write(s[i], (i - len(s) - 8, 1), c)
            if r > 15:
                s = "DESTROYERS     "
                for i in range(len(s)):
                    c = (self.scene.ground_color * i / len(s) +
                         ((math.sin(i + r) + 4) * script.dt) + 0.3)
                    bigterm.write(s[i], (i - len(s) - 3, 2), c)
                if r == 15:
                    self.scene.play_sound("explosion.wav")
            yield script.sleep(0.1)
            r += 1
Example #9
0
    def set_sky_color_opt(self, c):
        """
        Optimized for fades.
        """
        if self.delay_t > EPSILON:
            return False
        # print("delay")

        self.delay_t = self.delay

        self._sky_color = ncolor(c) if c else None
        if self._sky_color:
            self.draw_sky()
            # reset ground gradient (depend on sky color)
            self.set_ground_color_opt(self.ground_color)
        return True
Example #10
0
    def write_weapon_stats(self):
        if not self.alive:
            return

        if not self.hide_stats:
            ty = 0
            ofs = ivec2(0, 10)

            terminal = self.app.state.terminal

            wpn = self.weapons[self.current_weapon]
            # extra space here to clear terminal

            if wpn.max_ammo < 0:
                ammo = wpn.letter + " ∞"
            else:
                ammo = f"{wpn.letter} {wpn.ammo}/{wpn.max_ammo}"

            if len(ammo) < 10:
                ammo += " " * (10 - len(ammo))  # pad

            col = glm.mix(ncolor(wpn.color), ncolor("white"),
                          self.weapon_flash)
            self.game_state.terminal.write("      ", (1, ty), col)
            self.game_state.terminal.write(ammo, (1, ty), col, ofs)

            col = glm.mix(ncolor("red"), ncolor("white"), self.health_flash)
            # self.game_state.terminal.write(
            #     " " + "♥" * self.hp + " " * (3 - self.hp), 1, "red"
            # )
            self.game_state.terminal.write_center("      ", ty + 1, col)
            self.game_state.terminal.write_center("      ", ty, col)
            self.game_state.terminal.write_center(
                "♥" * self.hp + " " * (self.hp - self.max_hp), ty, "red", ofs)

            # Render Player's Score
            score_display = "Score: {}".format(self.stats.score)
            score_pos = (
                terminal.size.x - len(score_display) - 1,
                ty,
            )
            col = glm.mix(ncolor("white"), ncolor("yellow"), self.score_flash)
            self.game_state.terminal.write("        ", score_pos + ivec2(0, 1),
                                           col)
            self.game_state.terminal.write(score_display, score_pos, col, ofs)

            # self.game_state.terminal.write("WPN " + wpn.letter, (0,20), wpn.color)
            # if wpn.max_ammo == -1:
            #     self.game_state.terminal.write("AMMO " + str(wpn.ammo) + "  ", (0,21), wpn.color)
            # else:
            #     self.game_state.terminal.write("AMMO n/a  ", (0,21), wpn.color)
        else:
            self.game_state.terminal.clear(0)
Example #11
0
    def draw_sky(self):
        width, height = self.app.size / 8

        self.sky = pygame.Surface((width, height))
        sky_color = self.sky_color or ncolor(pygame.Color("blue"))
        sky_color = [255 * s for s in sky_color]

        # for y in range(height):
        #     interp = (1 - y / height) * 2
        #     for x in range(width):
        #         rand = rand_RGB()
        #         color = rgb_mix(sky_color, rand, 0.02)
        #           c = (sk * 0.98 + rand * 0.02) / i**1.1

        #         if interp == 0:
        #             color = (255, 255, 255)
        #         else:
        #             color = [min(int(c / interp ** 1.1), 255) for c in color]
        #         self.sky.set_at((x, y), color)

        # Draw gradient
        for y in range(height):
            interp = (1 - y / height) * 2
            base = [min(int(c / interp**1.1), 255) for c in sky_color]
            pygame.draw.line(self.sky, base, (0, y), (width, y))

        noise = noise_surf_dense_bottom(self.sky.get_size(),
                                        random.randrange(5))
        self.sky.blit(
            noise,
            (0, 0),
            None,
        )
        self.sky = pygame.transform.scale(self.sky, self.app.size)

        # if self.stars_visible:
        #     self.draw_stars(self.sky, self.star_pos)

        self.sky = pygame.transform.scale(self.sky, self.app.size)
Example #12
0
 def set_ground_color(self, c):
     self.ground_color = ncolor(c) if c else None
Example #13
0
 def set_sky_color(self, c):
     self.sky_color = ncolor(c) if c else None
Example #14
0
    def __call__(self, script):
        yield
        scene = self.scene
        terminal = self.terminal
        self.scene.music = "butterfly.ogg"
        when = script.when

        # self.scene.sky_color = "#4c0b6b"
        # self.scene.ground_color = "#e08041"
        self.scene.stars()
        self.scene.cloudy()

        textdelay = 0.02

        fades = [
            when.fade(
                10,
                (0, 1),
                lambda t: scene.set_sky_color(
                    glm.mix(ncolor("#4c0b6b"), ncolor("#e08041"), t)),
            ),
            when.fade(
                10,
                (0, 1),
                lambda t: scene.set_ground_color(
                    glm.mix(ncolor("darkgreen"), ncolor("yellow"), t)),
                lambda: fades.append(
                    when.every(
                        0, lambda: scene.set_ground_color(scene.ground_color))
                ),
            ),
        ]
        yield

        pages = [
            [
                "CREDITS",
                "",
                "flipcoder",
                "    " + "Programming, Music, Sounds",
                "ddorn",
                "    " + "Programming, Graphics",
                "MysteryCoder456",
                "    " + "Programming",
                "Tamwile",
                "    " + "Graphics",
                "Jtiai",
                "    " + "Sounds",
                "",
                "Additional Assets: ",
                "    opengameart.org/users/pitrizzo",
            ],
            [
                "This game was created by PythonixCoders",
                "for PyWeek 29, a week-long Python game",
                "jam, where individuals or groups give",
                "themselves only one week to create a",
                "game.",
                "",
                "Participate next time at pyweek.org",
            ],
        ]
        for p, page in enumerate(pages):
            for y, line in enumerate(page):
                if line:
                    scene.ensure_sound("message.wav")
                    if p == 0:
                        if line == "CREDITS":
                            col = "white"
                        elif not line.startswith(" "):
                            col = "green"
                        else:
                            col = "white"
                    else:
                        col = "white"
                    for x, m in enumerate(line):
                        terminal.write(m, (x + 1, y + 1), col)
                        # terminal.write(m[:x], (x + 1, y * 2 + 3), "white")
                        # terminal.write(m[-1], (x + 1 + len(m) - 1, y * 2 + 3), "red")
                        yield script.sleep(0.01)
                        self.scene.play_sound("message.wav")
                else:
                    continue
                delay = 0.1
            yield script.sleep(4)
            self.terminal.clear()

        terminal.write_center("Thanks for Playing!!!", 10)
        while True:
            if script.keys_down:
                break
            yield script.sleep(0.1)

        self.app.state = None
Example #15
0
 def sky_color(self, c):
     self._sky_color = ncolor(c) if c else None
     self.draw_sky()
     # reset ground gradient (depend on sky color)
     self.ground_color = self._ground_color
    def __call__(self, script):
        yield
        scene = self.scene
        terminal = self.terminal
        stats = self.stats
        self.scene.music = "intermission.ogg"
        when = script.when

        # self.scene.sky_color = "#4c0b6b"
        # self.scene.ground_color = "#e08041"
        self.scene.stars()
        self.scene.cloudy()

        textdelay = 0.02

        fade = []
        fades = [
            when.fade(
                10,
                (0, 1),
                lambda t: scene.set_sky_color(
                    glm.mix(ncolor("#4c0b6b"), ncolor("#e08041"), t)),
            ),
            when.fade(
                10,
                (0, 1),
                lambda t: scene.set_ground_color(
                    glm.mix(ncolor("darkgreen"), ncolor("yellow"), t)),
                lambda: fades.append(
                    when.every(
                        0, lambda: scene.set_ground_color(scene.ground_color))
                ),
            ),
        ]
        yield

        msg = [
            ("Level " + str(stats.level), "COMPLETED"),
            ("Damage Done", int(stats.damage_done)),
            ("Damage Taken", int(stats.damage_taken)),
            ("Kills", int(stats.kills)),
            # ("Lives Remaining", stats.lives),
            None,
            ("Score", stats.score),
        ]
        for y, line in enumerate(msg):
            if line:
                scene.ensure_sound("message.wav")
                for x, m in enumerate(line[0]):
                    terminal.write(m, (x + 1, y * 2 + 3), "white")
                    # terminal.write(m[:x], (x + 1, y * 2 + 3), "white")
                    # terminal.write(m[-1], (x + 1 + len(m) - 1, y * 2 + 3), "red")
                    yield script.sleep(0.01)
            else:
                continue
            if isinstance(line[1], int):  # total
                dd = 0
                for val in range(0, line[1] + 1):
                    terminal.write(
                        str(val),
                        (self.terminal.size.x - len(str(val)) - 1, y * 2 + 3),
                        "white",
                    )
                    dd += 0.6 / (val + 2)
                    if dd > 1 / max(self.app.fps, 10):
                        yield script.sleep(dd)
                        dd = 0
            else:
                yield script.sleep(0.1)
                terminal.write(
                    str(line[1]),
                    (self.terminal.size.x - len(str(line[1])) - 1, y * 2 + 3),
                    "green",
                )
                self.scene.play_sound("hit.wav")
                yield script.sleep(0.01)

        yield script.sleep(2)
        # while True:

        #     terminal.write_center("Press any key to continue", 20, "green")
        #     yield script.sleep(0.2)
        #     if script.keys_down:
        #         break
        #     terminal.clear(20)
        #     yield script.sleep(0.2)
        #     if script.keys_down:
        #         break

        self.stats.level += 1
        self.app.state = "game"
Example #17
0
    def __call__(self, script):
        yield

        self.scene.scripts += self.change_logo_color

        when = script.when
        scene = self.scene
        terminal = self.terminal

        self.scene.music = "butterfly2.ogg"
        # self.scene.sky_color = "#4c0b6b"
        # self.scene.ground_color = "#e08041"
        # self.scene.stars()
        self.scene.cloudy()

        textdelay = 0.03

        fades = [
            when.fade(
                10,
                (0, 1),
                lambda t: scene.set_sky_color_opt(
                    glm.mix(ncolor("#4c0b6b"), ncolor("#e08041"), t)),
            ),
            when.fade(
                10,
                (0, 1),
                lambda t: scene.set_ground_color_opt(
                    glm.mix(ncolor("darkgreen"), ncolor("yellow"), t)),
                lambda: fades.append(
                    when.every(
                        0, lambda: scene.set_ground_color_opt(scene.
                                                              ground_color))),
            ),
        ]
        yield

        # self.scene.set_ground_color = "#e08041"

        # scene.sky_color = "black"
        self.scene.music = "butterfly2.ogg"

        # for i in range(len(msg)):
        #     terminal.write(msg[i], (len(msg) / 2 - 1 + i, 1), self.scene.ground_color)
        #     # scene.ensure_sound("type.wav")
        # yield script.sleep(0.002)

        # script.push(self.logo_color)

        # yield from self.change_logo_color(script)

        yield script.sleep(3)

        msg = [
            "In the year 20XX, the butterfly",
            "overpopulation problem has",
            "obviously reached critical mass.",
            "The military has decided to intervene.",
            "Your mission is simple: defeat all the",
            "butterflies before the world ends.",
            "But look out for Big Butta, king of",
            "the butterflies.",
        ]
        for y, line in enumerate(msg):
            ty = y * 2 + 5
            for x, m in enumerate(line):
                terminal.write(random_char(), (x + 2, ty), random_rgb())
                cursor = (x + 2, ty)
                terminal.write(m, (x + 1, ty), "white")
                # scene.ensure_sound("type.wav")
                self.change_logo_color(script)
                # if not script.keys_down:
                #     yield
                # else:
                yield script.sleep(textdelay)
            terminal.clear(cursor)

        when = script.when
        scene = self.scene
        terminal = self.terminal

        yield script.sleep(3)

        # while True:
        #     terminal.write_center("Press any key to continue", 20, "green")
        #     self.change_logo_color(script)
        #     yield script.sleep(0.1)
        #     if script.keys_down:
        #         break
        #     terminal.clear(20)
        #     self.change_logo_color(script)
        #     yield script.sleep(0.1)
        #     if script.keys_down:
        #         break

        terminal.clear()
        terminal.write_center("Loading...", 10)

        self.app.state = "game"