fix sieve position reset; add rotate flag; fix previous shift update

This commit is contained in:
ohsqueezy 2022-12-16 16:13:41 -05:00
parent 9068830c41
commit a309096c38
3 changed files with 47 additions and 49 deletions

2
config
View File

@ -18,7 +18,7 @@ title-font-path = font/Oxygen.ttf
scoreboard-font-path = font/terminus/Terminus.ttf
initials-font = font/terminus/Terminus-Bold.ttf
show-framerate = yes
rotate = yes
rotate = no
[mouse]
visible = no

View File

@ -42,11 +42,13 @@ class ElectricSieve(Game):
# Add type declarations for custom config entries
self.get_configuration().type_declarations.add("bool", "display", "rotate")
# Rotate the display if requested in the configuration
self.is_rotated = False
# 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
if self.get_configuration("display", "rotate"):
self.display.rotate()
self.is_rotated = True
self.rotated = True
# Initialize GPIO input and callbacks if GPIO library is loaded
if "RPi.GPIO" in sys.modules:
@ -99,52 +101,42 @@ class ElectricSieve(Game):
self.input.post_command("right", cancel=cancel)
self.input.post_any_command(id=pin, cancel=cancel)
def rotated(self):
"""
@returns True if display has been rotated, False otherwise
"""
return self.is_rotated
def orient(self, geometry, reference=None):
def orient(self, geometry):
"""
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():
if not self.rotated:
return geometry
else:
if isinstance(geometry, Vector):
return self.rotated_point(geometry, reference)
return self.rotated_point(geometry)
elif isinstance(geometry, pygame.Rect):
return self.rotated_rect(geometry, reference)
return self.rotated_rect(geometry)
elif isinstance(geometry, pygame.Surface):
return pygame.transform.rotate(geometry, 90)
def rotated_point(self, point, reference=None):
def rotated_point(self, point):
"""
Return a new pgfw.Vector with the X and Y values of a pgfw.Vector rotated 90 degrees.
@param point pgfw.Vector to rotate in place
@param point pgfw.Vector to rotate
@return rotated pgfw.Vector
"""
if reference is None:
reference = self.get_display_surface()
return Vector(reference.get_height() - point.x, point.y)
return Vector(self.get_display_surface().get_height() - point.x, point.y)
def rotated_rect(self, rect, reference=None):
def rotated_rect(self, rect):
"""
Return a new pygame.Rect rotated 90 degrees.
@param rect pygame.Rect to rotate in place
@param rect pygame.Rect to rotate
@return rotated pygame.Rect
"""
if reference is None:
reference = self.get_display_surface()
rotated = pygame.Rect(0, 0, 0, 0)
rotated.x = rect.y
rotated.y = reference.get_height() - rect.x + rect.w
rotated.y = self.get_display_surface().get_height() - rect.x + rect.w
rotated.w = rect.h
rotated.h = rect.w
return rotated
@ -168,11 +160,11 @@ class Title(GameChild):
self.background = surface = Surface(self.display_surface.get_size())
tile = Surface((2, 2))
tile.fill(bg_color)
tile.set_at(self.get_game().orient(Vector(0, 1)), (220, 119, 41))
tile.set_at(self.get_game().orient(Vector(1, 0)), (220, 119, 41))
tile.set_at(Vector(0, 1), (220, 119, 41))
tile.set_at(Vector(1, 0), (220, 119, 41))
for y in range(0, surface.get_height(), 2):
for x in range(0, surface.get_width(), 2):
surface.blit(tile, (x, y))
surface.blit(self.get_game().orient(tile), (x, y))
# font = Font(self.get_resource("display", "title-font-path"), 20)
# font.set_italic(True)
# font.set_bold(True)
@ -233,7 +225,10 @@ class Strip(Sprite):
self.deactivate()
self.display_surface = self.get_display_surface()
self.delegate = self.get_game().delegate
self.hshifts = Shift(self, 1, "shift-2"), Shift(self, -1, "shift-2")
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")
self.add_frames()
self.subscribe(self.respond)
@ -268,7 +263,7 @@ class Strip(Sprite):
for shift in self.hshifts:
shift.update()
if shift.time:
if not self.get_game().rotated():
if not self.get_game().rotated:
self.move(shift.get_change())
else:
self.move(dy=shift.get_change())
@ -296,7 +291,7 @@ class Shift(GameChild):
self.time = max(self.time - self.timer.get_last_frame_duration(), least)
def get_change(self):
return self.nodeset.get_y(self.time) * (self.direction if not self.get_game().rotated() else -self.direction)
return self.nodeset.get_y(self.time) * self.direction
class Scoreboard(GameChild):
@ -346,7 +341,7 @@ class Scoreboard(GameChild):
sprites[ii][0].add_frame(self.get_game().orient(
render_box(font, score[2], False, self.NEW, self.BACKGROUND, self.FOREGROUND, self.BORDER, self.PADDING)))
blink = True
if not self.get_game().rotated():
if not self.get_game().rotated:
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(sizes) / 2)
@ -388,7 +383,7 @@ class Sieve(Strip):
Strip.__init__(self, parent)
self.delegate = self.get_game().delegate
self.electric = Electric(self)
if not self.get_game().rotated():
if not self.get_game().rotated:
self.location.left = 0
self.add_location(offset=(self.location.w, 0))
else:
@ -419,7 +414,7 @@ class Sieve(Strip):
for ii, frame in enumerate(frames):
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():
if self.get_game().rotated:
for ii, rect in enumerate(bar_rects):
bar_rects[ii] = self.get_game().orient(rect)
bar_rects[ii].move_ip(0, -6)
@ -428,16 +423,16 @@ class Sieve(Strip):
def reset(self):
Strip.reset(self)
if not self.get_game().rotated():
if not self.get_game().rotated:
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
self.locations[1].centery = self.location.centery - self.location.h
def update(self):
if self.active:
if not self.get_game().rotated():
if not self.get_game().rotated:
if self.location.right < 0:
self.move(self.location.w)
if self.locations[1].left > self.display_surface.get_width():
@ -455,7 +450,7 @@ class Sieve(Strip):
self.electric.location.centerx = self.location.centerx + 13
self.electric.update()
for rect in self.bar_rects:
if not self.get_game().rotated():
if not self.get_game().rotated:
rect.centery = self.location.centery
else:
rect.centerx = self.location.centerx
@ -470,7 +465,7 @@ class Electric(Sprite):
self.add_frames()
def add_frames(self):
if not self.get_game().rotated():
if not self.get_game().rotated:
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))
@ -514,12 +509,12 @@ class Triangles(GameChild, list):
def populate(self):
if not self:
self.append(Triangle(self))
if not self.get_game().rotated():
if not self.get_game().rotated:
self[-1].location.bottom = 0
else:
self[-1].location.right = 0
self.set_next_gap()
if not self.get_game().rotated():
if not self.get_game().rotated:
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
@ -561,9 +556,9 @@ class Triangles(GameChild, list):
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 \
tr_offset = (self[0].location.left, 0) if not self.get_game().rotated else \
(0, self[0].location.bottom - self.get_display_surface().get_height())
br_offset = (sieve.location.left, 0) if not self.get_game().rotated() else \
br_offset = (sieve.location.left, 0) if not self.get_game().rotated else \
(0, sieve.location.bottom - self.get_display_surface().get_height())
if tr.move(tr_offset).colliderect(br.move(br_offset)):
self.remove(self[0])
@ -598,13 +593,13 @@ class Triangle(Sprite):
(x + width - margin // 2 - 1, height - 2), \
(x + width / 2.0, 1)
polygon(surface, (60, 255, 220), points)
if not self.get_game().rotated():
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))
x += width - sieve.bar_w
self.add_frame(self.get_game().orient(surface))
if not self.get_game().rotated():
if not self.get_game().rotated:
self.location.centerx = self.get_display_surface().get_rect().centerx
else:
self.location.centery = self.get_display_surface().get_rect().centery
@ -612,12 +607,12 @@ class Triangle(Sprite):
def update(self):
step = 9.5 * self.get_game().acid.get_volume() + 3.8 + self.parent.get_boost()
if not self.get_game().rotated():
if not self.get_game().rotated:
self.move(dy=step)
else:
self.move(dx=step)
for rect in self.collision_rects:
if not self.get_game().rotated():
if not self.get_game().rotated:
rect.bottom = self.location.bottom
else:
rect.right = self.location.right
@ -637,7 +632,7 @@ class Acid(GameChild):
self.substance = 0
def get_top(self):
if not self.get_game().rotated():
if not self.get_game().rotated:
return self.display_surface.get_height() - self.get_level()
else:
return self.display_surface.get_width() - self.get_level()
@ -823,7 +818,7 @@ class Initials(GameChild):
box = self.get_game().orient(render_box(
self.font, letter, False, self.FOREGROUND, self.BACKGROUND, self.FOREGROUND, padding=self.PADDING))
rect = box.get_rect()
if not self.get_game().rotated():
if not self.get_game().rotated:
rect.centery = ds.get_rect().centery
rect.centerx = ii * ds.get_width() / 3 + ds.get_width() / 6
else:
@ -831,7 +826,7 @@ class Initials(GameChild):
rect.centery = (len(self.text) - 1 - ii) * ds.get_height() / 3 + ds.get_height() / 6
ds.blit(box, rect)
if ii == self.index:
if not self.get_game().rotated():
if not self.get_game().rotated:
margin = self.ARROW_MARGIN
else:
margin = self.ARROW_MARGIN // 2

View File

@ -145,3 +145,6 @@
1671161829.4724848 14 ---
1671168164.8205705 0 ---
1671206866.9558506 39 ---
1671215305.8678455 25 ---
1671224984.921691 13 ---
1671225084.4043906 3 ---