ibitfit/electric_sieve/ElectricSieve.py

1041 lines
41 KiB
Python
Raw Normal View History

2014-04-27 14:21:42 -04:00
# -*- coding: utf-8 -*-
import sys, random
2014-04-25 22:11:52 -04:00
from random import randint, randrange, choice
2014-04-27 18:12:18 -04:00
from time import time
2015-07-23 12:42:07 -04:00
from operator import itemgetter
2014-04-25 18:12:47 -04:00
2022-12-16 10:55:00 -05:00
import pygame
2014-04-26 06:06:54 -04:00
from pygame import Surface, PixelArray, Rect
2014-04-25 23:18:47 -04:00
from pygame.draw import aalines, polygon
2014-04-27 14:21:42 -04:00
from pygame.font import Font
2014-04-27 21:34:10 -04:00
from pygame.mixer import Sound
2014-04-25 23:18:47 -04:00
from pygame.locals import *
2014-04-25 13:22:01 -04:00
2015-07-21 17:44:07 -04:00
from lib.pgfw.pgfw.Game import Game
from lib.pgfw.pgfw.GameChild import GameChild
from lib.pgfw.pgfw.Sprite import Sprite, BlinkingSprite
2022-12-16 10:55:00 -05:00
from lib.pgfw.pgfw.Vector import Vector
2015-07-21 17:44:07 -04:00
from lib.pgfw.pgfw.extension import render_box
2014-04-25 13:22:01 -04:00
2022-12-16 20:50:24 -05:00
from electric_sieve.land.Land import Land
2022-12-13 23:04:57 -05:00
# Import GPIO library if available
try:
import RPi.GPIO as GPIO
except ImportError:
pass
2014-04-27 21:34:10 -04:00
class ElectricSieve(Game):
2014-04-25 13:22:01 -04:00
2022-12-13 16:56:33 -05:00
# The GPIO pins corresponding to the buttons and LED indicators
2022-12-16 11:11:56 -05:00
PIN_BUTTON_LEFT = 17
PIN_BUTTON_RIGHT = 27
2022-12-13 16:56:33 -05:00
PIN_LED_UP = 22
PIN_LED_DOWN = 23
2014-04-25 13:22:01 -04:00
def __init__(self):
2022-12-13 23:04:57 -05:00
"""
Initialize super class, GPIO input, background, and activate the title screen.
"""
# Initialize super
2014-04-25 13:22:01 -04:00
Game.__init__(self)
2022-12-16 10:55:00 -05:00
# Add type declarations for custom config entries
2022-12-16 20:50:24 -05:00
self.get_configuration().type_declarations.add_chart({
"display":
{
"bool": "rotate"
},
"input":
{
"int": ["title-hold", "initials-hold"]
},
"audio":
{
"int": "title-fade"
},
2022-12-16 20:50:24 -05:00
"land":
{
"int": ["gradient", "height", "x-step"],
"float": ["altitude-ratio", "spacing-factor", "velocity-ratio", "fade-speed"]
}})
2022-12-16 10:55:00 -05:00
# Rotate the display if requested on the command line or in the configuration
if self.check_command_line("-rotate"):
self.configuration.set("display", "rotate", True)
self.rotated = False
2022-12-16 10:55:00 -05:00
if self.get_configuration("display", "rotate"):
self.display.rotate()
self.rotated = True
2022-12-13 23:04:57 -05:00
# Initialize GPIO input and callbacks if GPIO library is loaded
if "RPi.GPIO" in sys.modules:
self.initialize_gpio()
2022-12-16 20:50:24 -05:00
self.velocity = Vector(0, 0)
# Create an intermediate surface to draw the triangles to and create a trail effect
self.trail_effect = Surface(self.get_display_surface().get_size(), pygame.SRCALPHA)
# Create a black background
self.background = Surface(self.get_display_surface().get_size())
self.background.fill((0, 0, 0))
# Alpha filter
self.alpha_filter = Surface(self.get_display_surface().get_size(), pygame.SRCALPHA)
self.alpha_filter.fill(Color(0, 0, 0, 80))
2022-12-13 23:04:57 -05:00
2022-12-16 10:55:00 -05:00
# Create game objects
self.title = Title(self)
self.sieve = Sieve(self)
self.triangles = Triangles(self)
self.acid = Acid(self)
self.static = Static(self)
2022-12-16 20:50:24 -05:00
self.land = Land(self)
2022-12-16 10:55:00 -05:00
2022-12-13 23:04:57 -05:00
# Start the title screen
2014-04-27 14:21:42 -04:00
self.title.activate()
2014-04-25 13:22:01 -04:00
def gpio_enabled(self):
"""
@return True if GPIO mode was requested at launch, False otherwise
"""
return "--gpio" in sys.argv
2022-12-13 16:56:33 -05:00
def initialize_gpio(self):
"""
Set pin numbering mode to GPIO, initialize all buttons to input pullup.
"""
# Use GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set button pins to pullup and attach to each a callback that runs on press or release
2022-12-16 11:11:56 -05:00
for pin in self.PIN_BUTTON_LEFT, self.PIN_BUTTON_RIGHT:
2022-12-13 16:56:33 -05:00
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.BOTH, self.gpio_input)
def gpio_input(self, pin):
"""
Translate GPIO input into PGFW commands, so Raspberry Pi wired controllers be used for input. The pin will be checked for a low
signal, meaning the pin has been connected to ground, which is translated to a button press. If the low signal is not detected, it
is translated to a button release. Usually called as a callback by `GPIO.add_event_detect` but can be called directly.
@param pin Raspberry Pi pin number as read by the RPi.GPIO library
"""
cancel = not (GPIO.input(pin) == GPIO.LOW)
2022-12-16 11:11:56 -05:00
if pin == ElectricSieve.PIN_BUTTON_LEFT:
self.input.post_command("left", cancel=cancel)
2022-12-16 11:11:56 -05:00
elif pin == ElectricSieve.PIN_BUTTON_RIGHT:
self.input.post_command("right", cancel=cancel)
self.input.post_any_command(id=pin, cancel=cancel)
2022-12-13 16:56:33 -05:00
def orient(self, geometry):
2022-12-16 10:55:00 -05:00
"""
Orient the passed pgfw.Vector, pygame.Rect, or pygame.Surface so it is rotated if necessary.
@param geometry A pgfw.Vector or pygame.Rect to rotate
@return Either a new pgfw.Vector or pygame.Rect depending on which was passed
"""
if not self.rotated:
2022-12-16 10:55:00 -05:00
return geometry
else:
if isinstance(geometry, Vector):
return self.rotated_point(geometry)
2022-12-16 10:55:00 -05:00
elif isinstance(geometry, pygame.Rect):
return self.rotated_rect(geometry)
2022-12-16 10:55:00 -05:00
elif isinstance(geometry, pygame.Surface):
return pygame.transform.rotate(geometry, 90)
def rotated_point(self, point):
2022-12-16 10:55:00 -05:00
"""
Return a new pgfw.Vector with the X and Y values of a pgfw.Vector rotated 90 degrees.
@param point pgfw.Vector to rotate
2022-12-16 10:55:00 -05:00
@return rotated pgfw.Vector
"""
return Vector(self.get_display_surface().get_height() - point.x, point.y)
2022-12-16 10:55:00 -05:00
def rotated_rect(self, rect):
2022-12-16 10:55:00 -05:00
"""
Return a new pygame.Rect rotated 90 degrees.
@param rect pygame.Rect to rotate
2022-12-16 10:55:00 -05:00
@return rotated pygame.Rect
"""
rotated = pygame.Rect(0, 0, 0, 0)
rotated.x = rect.y
rotated.y = self.get_display_surface().get_height() - rect.x + rect.w
2022-12-16 10:55:00 -05:00
rotated.w = rect.h
rotated.h = rect.w
return rotated
2014-04-25 13:22:01 -04:00
def update(self):
# Test if the level is being played
2014-04-27 14:21:42 -04:00
if self.triangles.active:
# Draw grid effect
2022-12-16 20:50:24 -05:00
self.land.update()
# Draw triangles onto the trail effect surface, update position
self.triangles.update()
if not self.title.active:
# Draw bottom layer background
self.get_display_surface().blit(self.background, (0, 0))
# Draw static behind objects on title screen
if self.title.active:
self.static.update()
# Draw the triangles to the screen, using the intermediate trail effect surface
self.get_display_surface().blit(self.trail_effect, (0, 0))
self.title.update()
# Draw the sieve
2014-04-25 13:22:01 -04:00
self.sieve.update()
if not self.title.active:
# Draw the static
self.static.update()
2014-04-25 13:22:01 -04:00
2014-04-27 14:21:42 -04:00
class Title(GameChild):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.display_surface = self.get_display_surface()
self.delegate = self.parent.delegate
# bg_color = (255, 222, 173)
# bg_color = (220, 220, 148)
2022-12-16 19:28:00 -05:00
bg_color = 200, 168, 122
2014-04-27 14:21:42 -04:00
self.background = surface = Surface(self.display_surface.get_size())
2014-04-27 16:27:58 -04:00
tile = Surface((2, 2))
tile.fill(bg_color)
# tile.set_at(Vector(0, 1), (220, 119, 41))
# tile.set_at(Vector(0, 0), (220, 119, 41))
2022-12-01 15:24:53 -05:00
for y in range(0, surface.get_height(), 2):
for x in range(0, surface.get_width(), 2):
surface.blit(self.get_game().orient(tile), (x, y))
2015-07-21 17:44:07 -04:00
# font = Font(self.get_resource("display", "title-font-path"), 20)
# font.set_italic(True)
# font.set_bold(True)
# self.captions = captions = Sprite(self), Sprite(self)
# colors = (0, 68, 170), (255, 255, 255), (128, 128, 128), \
# (220, 119, 41), (255, 80, 80), (0, 90, 110)
# texts = ["", ""]
# for ii, text in \
# enumerate(self.get_configuration("display",
# "caption").upper().split()):
# texts[ii] += "•" * (5 if ii else 3)
# for ch in text:
# texts[ii] += ch + " "
# texts[ii] = texts[ii].strip() + "•" * (5 if ii else 3)
2022-12-01 15:24:53 -05:00
# for _ in range(25):
2015-07-21 17:44:07 -04:00
# color = choice(colors)
# captions[0].add_frame(font.render(texts[0], True, color, (220, 208, 255)))
# captions[1].add_frame(font.render(texts[1], True, color, (220, 208, 255)))
# cx = self.display_surface.get_rect().centerx
# captions[0].location.center = cx, 301
# captions[1].location.center = cx, 398
self.button_prompt = BlinkingSprite(self, 500)
font = pygame.font.Font(self.get_resource("terminus/Terminus.ttf"), 32)
self.button_prompt.add_frame(self.get_game().orient(font.render("HOLD LEFT OR RIGHT TO PLAY", True, pygame.Color(0, 0, 0), pygame.Color(255, 255, 255))))
if not self.get_game().rotated:
self.button_prompt.location.midbottom = self.get_display_surface().get_rect().midbottom
else:
self.button_prompt.location.midright = self.get_display_surface().get_rect().midright
2014-04-27 16:27:58 -04:00
self.scoreboard = Scoreboard(self)
2014-04-27 21:34:10 -04:00
self.music = Sound(self.get_resource("audio", "title"))
self.advance = Sound(self.get_resource("audio", "title-advance"))
2014-04-27 14:21:42 -04:00
self.subscribe(self.respond)
def respond(self, event):
if self.active:
self.idle_time = 0
if not self.music.get_num_channels():
self.music.play(-1, 0, 1000)
self.get_game().static.noise.fadeout(1000)
if self.delegate.compare(event, "left", cancel=False) or self.delegate.compare(event, "right", cancel=False):
self.holding_button = True
elif self.delegate.compare(event, "left", cancel=True) or self.delegate.compare(event, "right", cancel=True):
self.holding_button = False
self.holding_button_elapsed = 0
2014-04-27 14:21:42 -04:00
def activate(self):
self.active = True
self.holding_button = False
self.holding_button_elapsed = 0
self.idle_time = 0
2014-04-27 21:34:10 -04:00
self.music.play(-1)
self.get_game().static.activate()
self.get_game().static.full()
self.get_game().static.noise.stop()
self.get_game().sieve.activate()
self.get_game().triangles.activate(music=False)
2014-04-27 14:21:42 -04:00
def deactivate(self):
self.active = False
2014-04-27 21:34:10 -04:00
self.music.fadeout(500)
2014-04-27 14:21:42 -04:00
def update(self):
if self.active:
if self.holding_button_elapsed > self.get_configuration("input", "title-hold"):
self.deactivate()
self.parent.triangles.reset()
while self.parent.triangles:
self.parent.triangles.pop()
self.parent.triangles.activate()
self.parent.sieve.activate()
self.parent.static.reset()
self.parent.static.activate()
self.advance.play()
self.get_game().trail_effect.fill(Color(0, 0, 0, 0))
self.get_display_surface().blit(self.get_game().background, (0, 0))
else:
if self.idle_time > self.get_configuration("audio", "title-fade"):
self.music.fadeout(5000)
if not self.get_game().static.noise.get_num_channels():
self.get_game().static.noise.set_volume(0.25)
self.get_game().static.noise.play(-1, 0, 5000)
else:
self.idle_time += self.get_game().time_filter.get_last_frame_duration()
if self.holding_button:
self.holding_button_elapsed += self.get_game().time_filter.get_last_frame_duration()
logo = Sprite(self)
font = pygame.font.Font(self.get_resource("terminus/TerminusItalic.ttf"),
62 + int(self.holding_button_elapsed / self.get_configuration("input", "title-hold") * 72))
logo.add_frame(self.get_game().orient(font.render("iBITFIT", True, pygame.Color(0, 0, 0), pygame.Color(255, 255, 255))))
if not self.get_game().rotated:
logo.location.midtop = self.get_display_surface().get_rect().midtop
else:
logo.location.midleft = self.get_display_surface().get_rect().midleft
logo.update()
self.button_prompt.update()
self.scoreboard.update()
2014-04-27 14:21:42 -04:00
2014-04-25 13:22:01 -04:00
class Strip(Sprite):
2014-04-25 14:37:23 -04:00
LEFT, RIGHT = range(2)
def __init__(self, parent, interval):
Sprite.__init__(self, parent, interval)
2014-04-27 14:21:42 -04:00
self.deactivate()
2014-04-25 13:22:01 -04:00
self.display_surface = self.get_display_surface()
2014-04-25 14:37:23 -04:00
self.delegate = self.get_game().delegate
if not self.get_game().rotated:
self.hshifts = Shift(self, 1, "shift-2"), Shift(self, -1, "shift-2")
else:
self.hshifts = Shift(self, -1, "shift-2"), Shift(self, 1, "shift-2")
2014-04-25 13:22:01 -04:00
self.add_frames()
2014-04-25 14:37:23 -04:00
self.subscribe(self.respond)
2014-04-25 13:22:01 -04:00
2014-04-27 14:21:42 -04:00
def deactivate(self):
self.active = False
def reset(self):
for shift in self.hshifts:
shift.reset()
2014-04-25 13:22:01 -04:00
def add_frames(self):
pass
2014-04-25 14:37:23 -04:00
def respond(self, event):
"""
Translate input events into movement of the sieve object. This function is usually set as a callback, but it can be called directly.
@param event `pygame.event.Event` with input
"""
2014-04-27 14:21:42 -04:00
if self.active:
compare = self.delegate.compare
if compare(event, "left") or compare(event, "left", True):
self.hshifts[self.LEFT].active = not event.cancel
elif compare(event, "right") or compare(event, "right", True):
self.hshifts[self.RIGHT].active = not event.cancel
2014-04-27 14:21:42 -04:00
def activate(self):
self.active = True
2014-04-25 14:37:23 -04:00
def update(self):
2014-04-27 14:21:42 -04:00
if self.active:
for shift in self.hshifts:
shift.update()
if shift.time:
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
self.move(shift.get_change())
else:
self.move(dy=shift.get_change())
2014-04-27 14:21:42 -04:00
Sprite.update(self)
2014-04-25 14:37:23 -04:00
2014-04-25 13:22:01 -04:00
2014-04-25 15:25:07 -04:00
class Shift(GameChild):
2014-04-26 01:06:45 -04:00
def __init__(self, parent, direction, nodeset):
2014-04-25 15:25:07 -04:00
GameChild.__init__(self, parent)
self.direction = direction
2014-04-27 14:21:42 -04:00
self.reset()
2014-04-25 15:25:07 -04:00
self.timer = self.get_game().time_filter
2014-04-26 01:06:45 -04:00
self.nodeset = self.get_game().interpolator.get_nodeset(nodeset)
2014-04-25 15:25:07 -04:00
2014-04-27 14:21:42 -04:00
def reset(self):
self.active = False
self.time = 0
2014-04-25 15:25:07 -04:00
def update(self):
least, greatest = self.nodeset[0].x, self.nodeset[-1].x
if self.active and self.time < greatest:
2022-12-16 10:55:00 -05:00
self.time = min(self.time + self.timer.get_last_frame_duration(), greatest)
2014-04-25 15:25:07 -04:00
elif not self.active and self.time > least:
2022-12-16 10:55:00 -05:00
self.time = max(self.time - self.timer.get_last_frame_duration(), least)
2014-04-25 15:25:07 -04:00
2014-04-26 01:06:45 -04:00
def get_change(self):
return self.nodeset.get_y(self.time) * self.direction
2014-04-25 15:25:07 -04:00
2015-07-21 17:44:07 -04:00
class Scoreboard(GameChild):
BACKGROUND = 255, 255, 255
FOREGROUND = 27, 27, 27
NEW = 27, 27, 27
SPACING = 45
MARGIN = 0
2015-07-21 17:44:07 -04:00
BLINK_INTERVAL = 400
PADDING = 0
2015-07-23 06:33:20 -04:00
BORDER = 1
SCORE_COUNT = 11
SIZES = [32, 28, 24, 22, 22, 20, 20, 20, 18, 18, 18]
2014-04-27 16:27:58 -04:00
def __init__(self, parent):
2014-04-27 17:35:35 -04:00
GameChild.__init__(self, parent)
2015-07-21 17:44:07 -04:00
ds = self.display_surface = self.get_display_surface()
2014-04-27 18:12:18 -04:00
self.scores_path = self.get_resource("score", "path")
self.most_recent_score = None
2014-04-27 17:35:35 -04:00
self.load()
def load(self):
self.sprites = sprites = []
2015-07-21 17:44:07 -04:00
font_path = self.get_resource("display", "scoreboard-font-path")
2015-07-23 12:42:07 -04:00
blink = False
for ii, score in enumerate(self.get_scores()[:len(self.SIZES)]):
font = Font(font_path, self.SIZES[ii])
sprites.append((Sprite(self, self.BLINK_INTERVAL), Sprite(self, self.BLINK_INTERVAL)))
2015-07-21 17:44:07 -04:00
score_text = str(score[1])
color = self.BACKGROUND if (self.most_recent_score and not blink and score[1:] == self.most_recent_score) else self.FOREGROUND
2015-07-21 17:44:07 -04:00
score_plate = font.render(score_text, False, color, self.BACKGROUND)
rect = score_plate.get_rect()
surface = Surface(rect.inflate((2, 2)).size)
surface.fill(self.FOREGROUND)
rect.center = surface.get_rect().center
surface.blit(score_plate, rect)
width = 80
2022-12-16 10:55:00 -05:00
sprites[ii][1].add_frame(self.get_game().orient(
render_box(font, score_text, True, color, self.BACKGROUND, (0, 0, 0), padding=self.PADDING, width=width)))
2022-12-16 10:55:00 -05:00
sprites[ii][0].add_frame(self.get_game().orient(
render_box(font, score[2], True, color, self.BACKGROUND, (0, 0, 0), padding=self.PADDING, width=width)))
2015-07-23 12:42:07 -04:00
if self.most_recent_score and not blink and score[1:] == self.most_recent_score:
2022-12-16 10:55:00 -05:00
sprites[ii][1].add_frame(self.get_game().orient(
render_box(font, score_text, True, self.NEW, self.BACKGROUND, (0, 0, 0), padding=self.PADDING, width=width)))
2022-12-16 10:55:00 -05:00
sprites[ii][0].add_frame(self.get_game().orient(
render_box(font, score[2], True, self.NEW, self.BACKGROUND, (0, 0, 0), padding=self.PADDING, width=width)))
2015-07-23 12:42:07 -04:00
blink = True
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
sprites[ii][0].location.left = self.MARGIN
sprites[ii][1].location.right = self.get_display_surface().get_rect().right - self.MARGIN
y = self.get_display_surface().get_rect().centery + self.SPACING * (ii - len(self.SIZES) / 2)
2022-12-16 10:55:00 -05:00
for sprite in sprites[ii]:
sprite.location.centery = y
else:
sprites[ii][0].location.bottom = self.get_display_surface().get_height() - self.MARGIN
sprites[ii][1].location.top = self.MARGIN
x = self.get_display_surface().get_rect().centerx + self.SPACING * (ii - len(self.SIZES) / 2)
2022-12-16 10:55:00 -05:00
for sprite in sprites[ii]:
sprite.location.centerx = x
2014-04-27 17:35:35 -04:00
2015-07-23 12:42:07 -04:00
def get_scores(self):
scores = []
2022-12-01 15:24:53 -05:00
for line in open(self.scores_path, "r"):
2015-07-23 12:42:07 -04:00
fields = line.split()
scores.append((float(fields[0]), int(fields[1]), fields[2]))
scores = sorted(scores, key=itemgetter(0))
return sorted(scores, key=itemgetter(1), reverse=True)
def write(self, initials):
2014-04-27 18:12:18 -04:00
score = int(round(self.get_game().triangles.score))
2015-07-23 12:42:07 -04:00
fields = str(time()), str(score), initials
2022-12-01 15:24:53 -05:00
open(self.scores_path, "a").write(fields[0] + " " + fields[1] + " " + fields[2] + "\n")
2015-07-23 12:42:07 -04:00
self.most_recent_score = score, initials
2014-04-27 18:12:18 -04:00
self.load()
2014-04-27 17:35:35 -04:00
def update(self):
2015-07-21 17:44:07 -04:00
for pair in self.sprites:
for sprite in pair:
sprite.update()
2014-04-27 16:27:58 -04:00
2014-04-25 13:22:01 -04:00
class Sieve(Strip):
2014-04-26 01:06:45 -04:00
UP, DOWN = range(2)
2014-04-25 13:22:01 -04:00
def __init__(self, parent):
Strip.__init__(self, parent, 400)
2014-04-26 01:06:45 -04:00
self.delegate = self.get_game().delegate
2014-04-25 17:07:57 -04:00
self.electric = Electric(self)
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
self.location.left = 0
self.add_location(offset=(self.location.w, 0))
else:
self.location.bottom = self.get_display_surface().get_height()
self.add_location(offset=(0, -self.location.h))
2014-04-25 13:22:01 -04:00
def add_frames(self):
2014-04-25 16:28:51 -04:00
bar_locations = []
2014-04-26 06:06:54 -04:00
self.bar_rects = bar_rects = []
2014-04-25 16:28:51 -04:00
x = 0
2014-04-26 06:06:54 -04:00
sh = 30
2014-04-25 16:28:51 -04:00
nodeset = self.get_game().interpolator.get_nodeset("scale")
2014-04-25 22:11:52 -04:00
self.bar_w = bar_w = 3
2014-04-25 18:12:47 -04:00
self.gaps = gaps = []
2014-04-25 17:07:57 -04:00
while x < nodeset[-1].x:
2014-04-25 16:28:51 -04:00
bar_locations.append(x)
2014-04-26 06:06:54 -04:00
bar_rects.append(Rect(x, 0, bar_w, sh))
2014-04-25 22:11:52 -04:00
gaps.append(nodeset.get_y(x, natural=True))
x += gaps[-1]
2014-04-26 06:06:54 -04:00
surface = Surface((x, sh))
2014-04-25 13:22:01 -04:00
transparent_color = (255, 0, 255)
surface.fill(transparent_color)
surface.set_colorkey(transparent_color)
frames = surface, surface.copy()
# colors = (0, 255, 0), (153, 0, 204)
colors = (255, 255, 255), (255, 255, 255)
2014-04-25 17:07:57 -04:00
for x in bar_locations:
2014-04-26 06:06:54 -04:00
bar_rects.append(Rect(x + surface.get_width(), 0, bar_w, sh))
2014-04-25 17:07:57 -04:00
for ii, frame in enumerate(frames):
2014-04-26 06:06:54 -04:00
frame.fill(colors[ii], (x, 0, bar_w, sh))
frame.fill(colors[ii - 1], (x + 1, 1, 1, sh - 2))
if self.get_game().rotated:
2022-12-16 10:55:00 -05:00
for ii, rect in enumerate(bar_rects):
bar_rects[ii] = self.get_game().orient(rect)
bar_rects[ii].move_ip(0, -6)
2014-04-25 17:07:57 -04:00
for frame in frames:
2022-12-16 10:55:00 -05:00
self.add_frame(self.get_game().orient(frame))
2014-04-25 17:07:57 -04:00
2014-04-27 14:21:42 -04:00
def reset(self):
Strip.reset(self)
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
self.location.centerx = self.display_surface.get_rect().centerx
self.locations[1].centerx = self.location.centerx + self.location.w
else:
self.location.centery = self.display_surface.get_rect().centery
self.locations[1].centery = self.location.centery - self.location.h
2014-04-27 14:21:42 -04:00
2014-04-25 17:07:57 -04:00
def update(self):
2014-04-27 14:21:42 -04:00
if self.active:
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
if self.location.right < 0:
self.move(self.location.w)
if self.locations[1].left > self.display_surface.get_width():
self.move(-self.location.w)
for location in self.locations:
location.bottom = self.parent.acid.get_top()
self.electric.location.centery = self.location.centery + 13
else:
if self.location.top > self.display_surface.get_height():
self.move(dy=-self.location.h)
if self.locations[1].bottom < 0:
self.move(dy=self.location.h)
for location in self.locations:
location.right = self.parent.acid.get_top()
self.electric.location.centerx = self.location.centerx + 13
2014-04-27 14:21:42 -04:00
self.electric.update()
for rect in self.bar_rects:
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
rect.centery = self.location.centery
else:
rect.centerx = self.location.centerx
2014-04-27 14:21:42 -04:00
Strip.update(self)
2014-04-25 17:07:57 -04:00
class Electric(Sprite):
def __init__(self, parent):
Sprite.__init__(self, parent)
self.display_surface = self.get_display_surface()
self.add_frames()
def add_frames(self):
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
surface = Surface((self.display_surface.get_width(), self.parent.location.h - 10))
else:
surface = Surface((self.display_surface.get_height(), self.parent.location.w - 10))
2014-04-25 17:07:57 -04:00
frames = surface, surface.copy()
# colors = (255, 255, 0), (100, 89, 213)
# colors = (180, 152, 111), (180, 152, 111)
colors = (255, 255, 255), (255, 255, 255)
2014-04-25 13:22:01 -04:00
pixel_arrays = PixelArray(frames[0]), PixelArray(frames[1])
2022-12-01 15:24:53 -05:00
for x in range(len(pixel_arrays[0])):
for y in range( len(pixel_arrays[0][0])):
pixel_arrays[0][x][y] = colors[(y + x) // 5 % 2]
pixel_arrays[1][x][y] = colors[(y + x + 1) // 5 % 2]
2014-04-25 13:22:01 -04:00
for pixels in pixel_arrays:
del pixels
for frame in frames:
2022-12-16 10:55:00 -05:00
self.add_frame(self.get_game().orient(frame))
2014-04-25 17:48:21 -04:00
class Triangles(GameChild, list):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.hue = 0
2014-04-27 21:34:10 -04:00
self.music = Sound(self.get_resource("audio", "triangles"))
2014-04-27 14:21:42 -04:00
self.deactivate()
self.display_surface = self.get_game().trail_effect
2014-04-26 12:51:02 -04:00
self.delegate = self.get_game().delegate
self.booster = Shift(self, 1, "boost")
2014-04-27 21:34:10 -04:00
self.hit = Sound(self.get_resource("audio", "hit"))
self.miss = Sound(self.get_resource("audio", "miss"))
2014-04-27 14:21:42 -04:00
self.reset()
2014-04-26 12:51:02 -04:00
self.subscribe(self.respond)
2014-04-26 03:18:46 -04:00
2014-04-27 14:21:42 -04:00
def deactivate(self):
self.active = False
2014-04-27 21:34:10 -04:00
self.music.fadeout(500)
2014-04-27 14:21:42 -04:00
def reset(self):
list.__init__(self, [])
self.streak = 0
self.score = 0
self.booster.reset()
2014-04-26 03:18:46 -04:00
def populate(self):
if not self:
self.append(Triangle(self))
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
self[-1].location.bottom = 0
else:
self[-1].location.right = 0
2014-04-26 03:18:46 -04:00
self.set_next_gap()
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
while self[-1].location.top > -self.display_surface.get_height():
self.append(Triangle(self))
self[-1].location.bottom = self[-2].location.top - self.next_gap
self.set_next_gap()
else:
while self[-1].location.left > -self.display_surface.get_width():
self.append(Triangle(self))
self[-1].location.right = self[-2].location.left - self.next_gap
self.set_next_gap()
2014-04-25 18:12:47 -04:00
def set_next_gap(self):
2014-04-27 08:58:48 -04:00
self.next_gap = randint(500, 800)
2014-04-25 18:12:47 -04:00
2014-04-26 12:51:02 -04:00
def respond(self, event):
2014-04-27 14:21:42 -04:00
if self.active:
compare = self.delegate.compare
if compare(event, "down") or compare(event, "down", True):
self.booster.active = not event.cancel
2014-04-26 12:51:02 -04:00
def get_boost(self):
return self.booster.get_change()
def activate(self, music=True):
2014-04-27 14:21:42 -04:00
self.active = True
if music:
self.music.play(-1, 0, 500)
2014-04-27 14:21:42 -04:00
2014-04-25 18:12:47 -04:00
def update(self):
2014-04-27 14:21:42 -04:00
if self.active:
self.populate()
self.booster.update()
if self[0].location.collidelist(self.parent.sieve.locations) != -1:
sieve = self.parent.sieve
removed = False
2014-04-27 14:21:42 -04:00
if self[0].location.colliderect(sieve.electric.location):
if not self.get_game().title.active:
self.parent.acid.increase()
self.streak += 1
self.score += self.streak ** .8 + self.parent.acid.get_volume() * 5 + self[0].count
2014-04-27 14:21:42 -04:00
self.remove(self[0])
2014-04-27 21:34:10 -04:00
self.hit.play()
removed = True
2014-04-27 14:21:42 -04:00
else:
for br in sieve.bar_rects:
for tr in self[0].collision_rects:
tr_offset = (self[0].location.left, 0) if not self.get_game().rotated else \
2022-12-16 10:55:00 -05:00
(0, self[0].location.bottom - self.get_display_surface().get_height())
br_offset = (sieve.location.left, 0) if not self.get_game().rotated else \
2022-12-16 10:55:00 -05:00
(0, sieve.location.bottom - self.get_display_surface().get_height())
if tr.move(tr_offset).colliderect(br.move(br_offset)):
if not self.get_game().title.active:
self.parent.static.increase()
self.streak = 0
2014-04-27 14:21:42 -04:00
self.remove(self[0])
2014-04-27 21:34:10 -04:00
self.miss.play()
removed = True
2014-04-27 14:21:42 -04:00
break
if removed:
self.get_display_surface().blit(self.get_game().alpha_filter, (0, 0), None, pygame.BLEND_RGBA_SUB)
2014-04-27 14:21:42 -04:00
for triangle in self:
triangle.update()
2014-04-25 18:12:47 -04:00
class Triangle(Sprite):
def __init__(self, parent):
2022-12-16 20:50:24 -05:00
Sprite.__init__(self, parent, 100)
2014-04-27 08:58:48 -04:00
mark = randint(112, 328)
2014-04-25 22:11:52 -04:00
sieve = self.parent.parent.sieve
gaps = sieve.gaps
start = randrange(0, len(gaps))
widths = [gaps[start]]
while sum(widths) < mark:
widths.append(gaps[(start + len(widths)) % len(gaps)])
surface = Surface((sum(widths), 20))
2014-04-25 23:18:47 -04:00
surface.set_colorkey((0, 0, 0))
height = surface.get_height()
2014-04-26 07:26:09 -04:00
margin = 26
2014-04-26 06:06:54 -04:00
self.collision_rects = collision_rects = []
for ii, lightness in enumerate(range(30, 110, 10)):
color = pygame.Color(0, 0, 0)
color.hsla = parent.hue, 100, lightness, 100
# opposite_color = pygame.Color(0, 0, 0)
# opposite_color.hsla = (parent.hue + 180) % 360, 100, lightness, 100
2022-12-16 20:50:24 -05:00
x = 0
surface = surface.copy()
for width in widths:
x += sieve.bar_w
points = ((x + margin // 2, height - 2),
(x + width - margin // 2 - 1, height - 2),
(x + width / 2.0, 1))
polygon(surface, color, points)
if ii == 0:
if not self.get_game().rotated:
collision_rects.append(Rect(points[0], (width - margin - 1, 1)))
else:
collision_rects.append(Rect(height - 2 - 1, self.get_display_surface().get_height() - x - width + margin // 2 + 1, 1, width - margin - 1))
# points = ((x + margin // 2 + (width * .1), height - 2 - 2),
# (x + width - margin // 2 - 1 - (width * .1), height - 2 - 2),
# (x + width / 2.0, 1 + 5))
# polygon(surface, opposite_color, points)
2022-12-16 20:50:24 -05:00
x += width - sieve.bar_w
self.add_frame(self.get_game().orient(surface))
next_hue = parent.hue
while abs(next_hue - parent.hue) < 60:
next_hue = random.randint(0, 359)
parent.hue = next_hue
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
self.location.centerx = self.get_display_surface().get_rect().centerx
else:
self.location.centery = self.get_display_surface().get_rect().centery
2014-04-27 14:21:42 -04:00
self.count = len(widths)
2014-04-25 18:12:47 -04:00
def update(self):
2022-12-16 10:55:00 -05:00
step = 9.5 * self.get_game().acid.get_volume() + 3.8 + self.parent.get_boost()
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
self.move(dy=step)
else:
self.move(dx=step)
2014-04-26 06:06:54 -04:00
for rect in self.collision_rects:
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
rect.bottom = self.location.bottom
else:
rect.right = self.location.right
2014-04-25 18:12:47 -04:00
Sprite.update(self)
2014-04-26 01:06:45 -04:00
class Acid(GameChild):
def __init__(self, parent):
GameChild.__init__(self, parent)
2014-04-26 12:33:42 -04:00
self.display_surface = self.get_display_surface()
self.level_r = 80, 320
self.nodeset = self.get_game().interpolator.get_nodeset("volume")
2014-04-27 14:21:42 -04:00
self.reset()
def reset(self):
self.substance = 0
2014-04-26 12:33:42 -04:00
def get_top(self):
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
return self.display_surface.get_height() - self.get_level()
else:
return self.display_surface.get_width() - self.get_level()
2014-04-26 01:06:45 -04:00
def get_level(self):
2022-12-16 10:55:00 -05:00
return self.get_volume() * (self.level_r[1] - self.level_r[0]) + self.level_r[0]
2014-04-26 12:33:42 -04:00
def get_volume(self):
return self.nodeset.get_y(self.substance)
def increase(self):
self.substance += 1
2014-04-27 08:58:48 -04:00
class Static(Sprite):
def __init__(self, parent):
2014-04-27 14:21:42 -04:00
Sprite.__init__(self, parent, 120)
2014-04-27 21:34:10 -04:00
self.noise = Sound(self.get_resource("audio", "noise"))
self.end = Sound(self.get_resource("audio", "end"))
2014-04-27 14:21:42 -04:00
self.deactivate()
self.delegate = self.get_game().delegate
self.increaser = Shift(self, 1, "intensity")
self.total = Total(self)
2015-07-23 12:42:07 -04:00
self.initials = Initials(self)
2014-04-27 14:21:42 -04:00
self.reset()
2014-04-27 08:58:48 -04:00
self.add_frames()
2014-04-27 14:21:42 -04:00
self.subscribe(self.respond)
def deactivate(self):
self.active = False
2014-04-27 21:34:10 -04:00
self.end.fadeout(500)
2014-04-27 14:21:42 -04:00
def reset(self):
self.complete = False
2014-04-27 08:58:48 -04:00
self.intensity = 0
2014-04-27 21:34:10 -04:00
self.noise.set_volume(0)
2014-04-27 14:21:42 -04:00
self.increaser.reset()
2014-04-27 08:58:48 -04:00
def add_frames(self):
surface = Surface(self.get_display_surface().get_size())
frames = surface, surface.copy(), surface.copy(), surface.copy()
tiles = []
2022-12-01 15:24:53 -05:00
for _ in range(32):
2014-04-27 08:58:48 -04:00
tiles.append(Surface((16, 16)))
pixel_arrays = []
for tile in tiles:
pixel_arrays.append(PixelArray(tile))
2022-12-16 19:28:00 -05:00
colors = (0, 0, 0), (64, 64, 64), (128, 128, 128), (196, 196, 196), (255, 255, 255)
2022-12-01 15:24:53 -05:00
for x in range(len(pixel_arrays[0])):
for y in range(len(pixel_arrays[0][0])):
2014-04-27 08:58:48 -04:00
for pixels in pixel_arrays:
pixels[x][y] = choice(colors)
for pixels in pixel_arrays:
del pixels
del pixel_arrays
for frame in frames:
2022-12-01 15:24:53 -05:00
for y in range(0, frame.get_height(), tiles[0].get_height()):
for x in range(0, frame.get_width(), tiles[0].get_width()):
2014-04-27 08:58:48 -04:00
frame.blit(choice(tiles), (x, y))
self.add_frame(frame)
2014-04-27 14:21:42 -04:00
def respond(self, event):
2015-07-23 12:42:07 -04:00
if self.active and self.complete and not self.initials.active:
if self.delegate.compare(event, "left") or self.delegate.compare(event, "right"):
if self.get_game().triangles.score > self.get_game().title.scoreboard.get_scores()[Scoreboard.SCORE_COUNT - 1][1]:
2015-07-23 12:42:07 -04:00
self.total.deactivate()
self.initials.activate()
else:
self.finish(wipe=True)
self.get_delegate().cancel_propagation()
self.get_game().suppress_input_temporarily(500)
2015-07-23 12:42:07 -04:00
def finish(self, text="---", wipe=False):
if wipe:
self.parent.title.scoreboard.most_recent_score = None
self.parent.title.scoreboard.write(text)
self.total.deactivate()
self.deactivate()
self.reset()
self.parent.acid.reset()
self.parent.triangles.reset()
self.parent.sieve.reset()
self.parent.title.activate()
2014-04-27 14:21:42 -04:00
2014-04-27 08:58:48 -04:00
def increase(self):
self.intensity += self.increaser.get_change()
if self.intensity > 1:
self.intensity = 1
self.increaser.time += 12000
if self.increaser.time >= self.increaser.nodeset[-1].x + 5000:
self.increaser.time = self.increaser.nodeset[-1].x + 5000
def full(self):
self.intensity = 1
2014-04-27 14:21:42 -04:00
def activate(self):
self.active = True
2014-04-27 21:34:10 -04:00
self.noise.play(-1)
2014-04-27 14:21:42 -04:00
2014-04-27 08:58:48 -04:00
def update(self):
2014-04-27 14:21:42 -04:00
if self.active:
if not self.get_game().title.active:
if not self.complete and self.intensity >= .65:
self.complete = True
self.parent.sieve.deactivate()
self.parent.triangles.deactivate()
self.set_alpha(255)
self.noise.fadeout(6000)
self.end.play(-1, 0, 4000)
self.total.load()
elif not self.complete:
self.set_alpha(min(150, int(self.intensity * 1.15 * 255)))
if self.intensity > 0:
self.intensity *= .998
self.increaser.update()
self.noise.set_volume(self.intensity)
2022-12-16 19:28:00 -05:00
if self.intensity > .1:
Sprite.update(self)
2014-04-27 14:21:42 -04:00
self.total.update()
2015-07-23 12:42:07 -04:00
self.initials.update()
class Initials(GameChild):
LETTER_SIZE = 24
FOREGROUND = 27, 27, 27
BACKGROUND = 255, 255, 255
PADDING = 10
ARROW_MARGIN = 20
2015-07-23 12:42:07 -04:00
ARROW_HEIGHT = 10
def __init__(self, parent):
GameChild.__init__(self, parent)
self.button_prompt = BlinkingSprite(self, 500)
font = pygame.font.Font(self.get_resource("terminus/Terminus.ttf"), 32)
self.button_prompt.add_frame(self.get_game().orient(font.render("HOLD RIGHT TO ENTER", True, pygame.Color(0, 0, 0), pygame.Color(255, 255, 255))))
if not self.get_game().rotated:
self.button_prompt.location.midbottom = self.get_display_surface().get_rect().midbottom
else:
self.button_prompt.location.midright = self.get_display_surface().get_rect().midright
2015-07-23 12:42:07 -04:00
self.reset()
self.deactivate()
self.font = Font(self.get_resource("display", "initials-font"), self.LETTER_SIZE)
self.subscribe(self.respond)
def reset(self):
self.text = "---"
self.index = 0
self.holding_button = False
self.holding_button_elapsed = 0
2015-07-23 12:42:07 -04:00
def deactivate(self):
self.active = False
def respond(self, event):
if self.active:
compare = self.get_game().delegate.compare
if compare(event, "right", cancel=False):
self.holding_button = True
elif compare(event, "right", cancel=True):
self.holding_button = False
self.holding_button_elapsed = 0
if compare(event, "right", cancel=True) or compare(event, "left", cancel=True):
if compare(event, "right", cancel=True):
2015-07-23 12:42:07 -04:00
increment = 1
elif compare(event, "left", cancel=True):
2015-07-23 12:42:07 -04:00
increment = -1
letter = self.text[self.index]
if letter == '-':
letter = 'A' if increment == 1 else 'Z'
else:
letter = chr(ord(letter) + increment)
if ord(letter) == 91 or ord(letter) == 64:
letter = '-'
replacement = ""
2022-12-01 15:24:53 -05:00
for ii in range(len(self.text)):
2015-07-23 12:42:07 -04:00
if ii == self.index:
replacement += letter
else:
replacement += self.text[ii]
self.text = replacement
def activate(self):
self.active = True
def update(self):
if self.active:
ds = self.get_display_surface()
self.button_prompt.update()
if self.holding_button:
self.holding_button_elapsed += self.get_game().time_filter.get_last_frame_duration()
if self.holding_button_elapsed > self.get_configuration("input", "initials-hold"):
self.index += 1
if self.index == len(self.text):
self.deactivate()
self.parent.finish(self.text)
self.reset()
else:
self.holding_button = False
self.holding_button_elapsed = 0
self.get_game().suppress_input_temporarily(500)
2015-07-23 12:42:07 -04:00
for ii, letter in enumerate(self.text):
2022-12-16 10:55:00 -05:00
box = self.get_game().orient(render_box(
self.font, letter, False, self.FOREGROUND, self.BACKGROUND, self.FOREGROUND, padding=self.PADDING))
2015-07-23 12:42:07 -04:00
rect = box.get_rect()
if not self.get_game().rotated:
2022-12-16 10:55:00 -05:00
rect.centery = ds.get_rect().centery
rect.centerx = ii * ds.get_width() / 3 + ds.get_width() / 6
else:
rect.centerx = ds.get_rect().centerx
rect.centery = (len(self.text) - 1 - ii) * ds.get_height() / 3 + ds.get_height() / 6
2015-07-23 12:42:07 -04:00
ds.blit(box, rect)
if ii == self.index:
hold_offset = self.holding_button_elapsed / self.get_configuration("input", "initials-hold") * 10
if not self.get_game().rotated:
x = rect.left - self.ARROW_MARGIN
left_points = ((x, rect.top), (x, rect.bottom), (x - self.ARROW_HEIGHT, rect.centery))
x = rect.right + self.ARROW_MARGIN + hold_offset
right_points = ((x, rect.top), (x, rect.bottom), (x + self.ARROW_HEIGHT, rect.centery))
2022-12-16 10:55:00 -05:00
else:
y = rect.top - self.ARROW_MARGIN - hold_offset
left_points = ((rect.left, y), (rect.right, y), (rect.centerx, y - self.ARROW_HEIGHT))
y = rect.bottom + self.ARROW_MARGIN
right_points = ((rect.left, y), (rect.right, y), (rect.centerx, y + self.ARROW_HEIGHT))
pygame.draw.polygon(ds, pygame.Color(0, 0, 0), left_points)
pygame.draw.polygon(ds, pygame.Color(0, 0, 0), right_points)
2014-04-27 14:21:42 -04:00
class Total(Sprite):
def __init__(self, parent):
Sprite.__init__(self, parent, 68)
self.deactivate()
2015-07-23 06:33:20 -04:00
self.font = Font(self.get_resource("display", "score-font-path"), 72)
# self.font.set_italic(True)
2014-04-27 14:21:42 -04:00
def deactivate(self):
self.active = False
def load(self):
self.clear_frames()
score = ""
for ch in str(int(round(self.get_game().triangles.score))):
score += ch + " "
2015-07-23 06:33:20 -04:00
colors = (255, 255, 180), (180, 255, 255), (255, 180, 255), \
(255, 220, 160), (160, 255, 220), (220, 160, 255)
2014-04-27 14:21:42 -04:00
template = Surface((self.display_surface.get_width(), 100))
transparent_color = (255, 0, 255)
template.fill(transparent_color)
template.set_colorkey(transparent_color)
tr = template.get_rect()
template.fill((255, 0, 0), (0, 20, tr.w, 1))
template.fill((255, 128, 128), (0, 21, tr.w, 1))
2022-12-01 15:24:53 -05:00
for y in range(22, 78, 2):
2014-04-27 14:21:42 -04:00
template.fill((255, 255, 255), (0, y, tr.w, 1))
template.fill((255, 128, 128), (0, 78, tr.w, 1))
template.fill((255, 0, 0), (0, 79, tr.w, 1))
2022-12-01 15:24:53 -05:00
for _ in range(20):
2015-07-23 06:33:20 -04:00
# surface = template.copy()
surface = Surface(template.get_size(), SRCALPHA)
# polygon(surface, choice(colors), ((tr.centerx - 7, 19),
# (tr.centerx, 0),
# (tr.centerx + 7, 19)))
2014-04-27 14:21:42 -04:00
text = self.font.render(score, True, choice(colors))
rect = text.get_rect()
rect.center = tr.centerx, tr.centery + 2
surface.blit(text, rect)
2015-07-23 06:33:20 -04:00
# polygon(surface, choice(colors), ((tr.centerx - 7, 80),
# (tr.centerx, tr.h - 1),
# (tr.centerx + 7, 80)))
2022-12-16 10:55:00 -05:00
self.add_frame(self.get_game().orient(surface))
2014-04-27 14:21:42 -04:00
self.location.center = self.display_surface.get_rect().center
self.active = True
def update(self):
if self.active:
Sprite.update(self)