rotate sprite; merge config assignments from command line

This commit is contained in:
ohsqueezy 2023-02-04 23:58:21 -05:00
parent 34451c3f4c
commit 71cc53f399
3 changed files with 47 additions and 9 deletions

View File

@ -1,3 +1,4 @@
import argparse
from os import sep, getcwd from os import sep, getcwd
from os.path import join, exists, basename, dirname, expanduser from os.path import join, exists, basename, dirname, expanduser
from sys import argv, version_info from sys import argv, version_info
@ -303,9 +304,7 @@ class Configuration(RawConfigParser):
def set_screen_captures_path(self): def set_screen_captures_path(self):
section, option = "screen-captures", "path" section, option = "screen-captures", "path"
if not self.has_option(section, option): if not self.has_option(section, option):
self.set(section, option, join(self.build_home_path(), self.set(section, option, join(self.build_home_path(), self.get(section, "rel-path")), False)
self.get(section, "rel-path")),
False)
def build_home_path(self): def build_home_path(self):
return join("~", "." + self.get("setup", "package-root")) return join("~", "." + self.get("setup", "package-root"))
@ -313,9 +312,7 @@ class Configuration(RawConfigParser):
def set_video_recordings_path(self): def set_video_recordings_path(self):
section, option = "video-recordings", "path" section, option = "video-recordings", "path"
if not self.has_option(section, option): if not self.has_option(section, option):
self.set(section, option, join(self.build_home_path(), self.set(section, option, join(self.build_home_path(), self.get(section, "rel-path")), False)
self.get(section, "rel-path")),
False)
def set_data_exclusion_list(self): def set_data_exclusion_list(self):
section, option = "setup", "data-exclude" section, option = "setup", "data-exclude"
@ -404,6 +401,26 @@ class Configuration(RawConfigParser):
for option in self.options(section): for option in self.options(section):
self.remove_option(section, option) self.remove_option(section, option)
def merge_command_line(self, command_line=None):
"""
Instantiate an argument parser to parse the command line. Load all settings submitted through the `--config` flag. Each setting
is a string starting with the chosen delimiter, followed by the section name, option name, and option value each separated by the
chosen delimiter.
For example,
$ ./OPEN-GAME --config "|display|dimensions|960,540" ",keys,quit,K_q"
@param command_line A list of strings to use as the command line. If this is `None`, `sys.argv` is used instead.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--config", nargs="*", default=[])
# Loop over all assignment strings found
for assignment in parser.parse_known_args(command_line)[0].config:
# Split each assigment string by the first character in the string and pass along the parsed values as arguments
# to the set member function
self.set(*assignment.split(assignment[0])[1:])
class TypeDeclarations(dict): class TypeDeclarations(dict):

View File

@ -1,4 +1,4 @@
import collections import collections, pygame
from os import listdir from os import listdir
from os.path import isfile, join from os.path import isfile, join
from glob import glob from glob import glob
@ -123,9 +123,12 @@ class Sprite(Animation):
def get_current_frame(self): def get_current_frame(self):
return self.frames[self.get_current_frameset().get_current_id()] return self.frames[self.get_current_frameset().get_current_id()]
def move(self, dx=0, dy=0): def move(self, dx=0, dy=0, rotated=False):
for location in self.locations: for location in self.locations:
location.move_ip(dx, dy) if not rotated:
location.move_ip(dx, dy)
else:
location.move_ip(dy, -dx)
def reset_motion_overflow(self): def reset_motion_overflow(self):
for location in self.locations: for location in self.locations:
@ -297,6 +300,21 @@ class Sprite(Animation):
for location in self.locations: for location in self.locations:
location.fader.draw(areas, substitute, flags) location.fader.draw(areas, substitute, flags)
def rotate(self):
"""
Rotate the sprite's frame surfaces and location rects by 90 degrees clockwise
"""
for ii, frame in enumerate(self.frames):
self.frames[ii] = pygame.transform.rotate(frame, 90)
for ii, location in enumerate(self.locations):
rotated = pygame.Rect(0, 0, 0, 0)
rotated.x = location.y
rotated.y = self.get_display_surface().get_height() - location.x + location.w
rotated.w = location.h
rotated.h = location.w
self.locations[ii] = Location(self, rect=rotated)
for child in self.children.values():
child.rotate()
class Location(Rect): class Location(Rect):

View File

@ -7,3 +7,6 @@ from .Game import *
# Not sure why but pgfw.GameChild causes a module error without importing Animation directly # Not sure why but pgfw.GameChild causes a module error without importing Animation directly
from .Animation import * from .Animation import *
# Import all classes defined in Sprite
from .Sprite import *