Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements #285

Open
wants to merge 13 commits into
base: canon
Choose a base branch
from
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'pursuedpybear', 'PursuedPyBear Documentation',
[author], 1)
[author], 1),
]


Expand Down Expand Up @@ -181,4 +181,4 @@
# -- Options for todo extension ----------------------------------------------

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
todo_include_todos = True
2 changes: 1 addition & 1 deletion examples/animated_sprite.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import math

import ppb
from ppb.features.animation import Animation
import ppb.events as events
from ppb.features.animation import Animation


class Blob(ppb.BaseSprite):
Expand Down
1 change: 1 addition & 0 deletions examples/framecount.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import logging
import time

import ppb


Expand Down
7 changes: 4 additions & 3 deletions examples/targets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging

import ppb
from ppb import Vector
from ppb import keycodes
from ppb import Vector


class MoverMixin(ppb.BaseSprite):
Expand Down Expand Up @@ -39,7 +40,7 @@ def on_button_pressed(self, event, signal):
def _fire_bullet(self, scene):
scene.add(
Bullet(pos=self.position),
tags=['bullet']
tags=['bullet'],
)


Expand All @@ -51,7 +52,7 @@ def on_update(self, update, signal):
super().on_update(update, signal) # Execute movement

scene = update.scene

if self.position.y > scene.main_camera.frame_bottom:
scene.remove(self)
else:
Expand Down
28 changes: 15 additions & 13 deletions examples/targets_with_twisted.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import logging
import ppb
from ppb import Vector
from ppb import keycodes
from dataclasses import dataclass
from typing import Any

import klein
from twisted.internet import defer
from twisted.internet import task
from twisted.internet import endpoints
from twisted.internet import task
from twisted.web.server import Site
import klein
from dataclasses import dataclass
from typing import Any

import ppb
from ppb import keycodes
from ppb import Vector


class MoverMixin(ppb.BaseSprite):
Expand Down Expand Up @@ -45,7 +46,7 @@ def on_button_pressed(self, event, signal):
def _fire_bullet(self, scene):
scene.add(
Bullet(pos=self.position),
tags=['bullet']
tags=['bullet'],
)


Expand Down Expand Up @@ -74,7 +75,7 @@ def on_update(self, update, signal):
super().on_update(update, signal) # Execute movement

scene = update.scene

if self.position.y > scene.main_camera.frame_bottom:
scene.remove(self)
else:
Expand Down Expand Up @@ -102,7 +103,7 @@ def __init__(self, *p, **kw):
self.add(Target(pos=Vector(x, 1.875)), tags=['target'])


######### This is "non-game-specific code" ###########
# This is "non-game-specific code"
class _FinishLoop(Exception):
pass

Expand All @@ -119,7 +120,7 @@ def loop_once(engine):
yield loop.start(0.001)
except _FinishLoop:
pass
######### End of "non-game-specific code" ###########
# End of "non-game-specific code"


@defer.inlineCallbacks
Expand All @@ -128,10 +129,11 @@ def main(reactor):
TargetCounter.web_server(
reactor=reactor,
engine=engine,
description="tcp:8080"
description="tcp:8080",
)
yield twisted_engine_loop(engine)


if __name__ == "__main__":
import sys
task.react(main, sys.argv[1:])
5 changes: 3 additions & 2 deletions ppb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def _make_kwargs(setup, title):
}
return kwargs

def run(setup: Callable[[BaseScene], None]=None, *, log_level=logging.WARNING,

def run(setup: Callable[[BaseScene], None] = None, *, log_level=logging.WARNING,
starting_scene=BaseScene, title="PursuedPyBear"):
"""
Run a small game.
Expand All @@ -45,6 +46,6 @@ def run(setup: Callable[[BaseScene], None]=None, *, log_level=logging.WARNING,
eng.run()


def make_engine(setup: Callable[[BaseScene], None]=None, *,
def make_engine(setup: Callable[[BaseScene], None] = None, *,
starting_scene=BaseScene, title="PursedPyBear"):
return GameEngine(starting_scene, **_make_kwargs(setup, title))
4 changes: 3 additions & 1 deletion ppb/abc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Type, Container, Tuple, Iterable
from typing import Iterable
from typing import Tuple
from typing import Type


class Engine(object):
Expand Down
28 changes: 15 additions & 13 deletions ppb/camera.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import Sequence
from typing import Union
from numbers import Number
from typing import Sequence

from ppb import Vector
from ppb.sprites import BaseSprite
from ppb.flags import DoNotRender
from ppb.sprites import BaseSprite


class Camera(BaseSprite):

image = DoNotRender

def __init__(self, viewport: Sequence[int]=(0, 0, 800, 600),
pixel_ratio: float=64):
def __init__(self, viewport: Sequence[int] = (0, 0, 800, 600),
pixel_ratio: float = 64):
"""

viewport: A container of origin x, origin y, width, and
Expand Down Expand Up @@ -64,6 +64,7 @@ def half_height(self) -> float:
@property
def half_width(self) -> float:
return self.frame_width / 2

@property
def viewport_width(self) -> int:
return self._viewport_width
Expand All @@ -82,25 +83,26 @@ def viewport_height(self, value: int):
self._viewport_height = value
self.viewport_offset = Vector(self.viewport_width / 2, value / 2)

def point_in_viewport(self, point:Vector) -> bool:
def point_in_viewport(self, point: Vector) -> bool:
px, py = point
vpx, vpy = self.viewport_origin
vpw = self.viewport_width
vph = self.viewport_height
return vpx <= px <= (vpw+vpx) and vpy <= py <= (vph+vpy)
return vpx <= px <= (vpw + vpx) and vpy <= py <= (vph + vpy)

def in_frame(self, sprite: BaseSprite) -> bool:
return (self.frame_left <= sprite.right and
self.frame_right >= sprite.left and
self.frame_top <= sprite.bottom and
self.frame_bottom >= sprite.top
)
return (
self.frame_left <= sprite.right and
self.frame_right >= sprite.left and
self.frame_top <= sprite.bottom and
self.frame_bottom >= sprite.top
)

def translate_to_frame(self, point: Vector) -> Vector:
"""
Converts a vector from pixel-based window to in-game coordinate space
"""
offset = (point - self.viewport_offset) * (1/self.pixel_ratio)
offset = (point - self.viewport_offset) * (1 / self.pixel_ratio)
loc = self.position + offset
return loc.update(y=-loc.y)

Expand Down
7 changes: 4 additions & 3 deletions ppb/engine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import time
from collections import defaultdict
from collections import deque
from contextlib import ExitStack
from itertools import chain
import time
from typing import Any
from typing import Callable
from typing import DefaultDict
Expand All @@ -12,9 +12,9 @@

import ppb.events as events
from ppb.abc import Engine
from ppb.events import StartScene
from ppb.events import EventMixin
from ppb.events import Quit
from ppb.events import StartScene
from ppb.systems import PygameEventPoller
from ppb.systems import Renderer
from ppb.systems import Updater
Expand All @@ -40,7 +40,8 @@ def __init__(self, first_scene: Type, *,
# Engine State
self.scenes = []
self.events = deque()
self.event_extensions: DefaultDict[Union[Type, _ellipsis], List[Callable[[Any], None]]] = defaultdict(list)
self.event_extensions: DefaultDict[Union[Type, _ellipsis],
List[Callable[[Any], None]]] = defaultdict(list)
self.running = False
self.entered = False
self._last_idle_time = None
Expand Down
10 changes: 6 additions & 4 deletions ppb/events.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from dataclasses import dataclass
import logging
import re
from dataclasses import dataclass
from typing import Any
from typing import Collection
from typing import Dict
from typing import Set
from typing import Type
from typing import Union

from ppb import Vector
from ppb.abc import Scene
from ppb.buttons import MouseButton
from ppb.keycodes import KeyCode
from ppb import Vector

__all__ = (
'StartScene',
Expand All @@ -31,6 +31,7 @@
boundaries_finder = re.compile('(.)([A-Z][a-z]+)')
boundaries_finder_2 = re.compile('([a-z0-9])([A-Z])')


def camel_to_snake(txt):
s1 = boundaries_finder.sub(r'\1_\2', txt)
return boundaries_finder_2.sub(r'\1_\2', s1).lower()
Expand All @@ -46,9 +47,10 @@ def __init__(self, instance, method, event):
article = ['a', 'an'][int(e_name.lower()[0] in "aeiou")]

message = f"""
{o_name}.{method}() signature incorrect, it should accept {article} {e_name} object and a signal function.
The signature of {o_name}.{method}() is incorrect:
it should accept {article} {e_name} object and a signal function.

{e_name} is a dataclass that represents an event. Its attributes
{e_name} is a dataclass that represents an event. Its attributes
tell you about the event.

The signal function is a function you can call that accepts an event instance
Expand Down
2 changes: 1 addition & 1 deletion ppb/features/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

Only supports frame-by-frame, not gif, apng, or full motion video.
"""
import time
import re
import time

FILE_PATTERN = re.compile(r'\{(\d+)\.\.(\d+)\}')

Expand Down
20 changes: 10 additions & 10 deletions ppb/keycodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class H(KeyCode):
""


class I(KeyCode):
class I(KeyCode): # noqa: E742
""


Expand All @@ -72,7 +72,7 @@ class N(KeyCode):
""


class O(KeyCode):
class O(KeyCode): # noqa: E742
""


Expand Down Expand Up @@ -291,7 +291,7 @@ class AltLeft(KeyCode):
class Backslash(KeyCode):
"""
\\

Shift+\\ is | on american keyboards
"""

Expand All @@ -303,15 +303,15 @@ class Backspace(KeyCode):
class BracketLeft(KeyCode):
"""
[

Shift+[ is { on american keyboards
"""


class BracketRight(KeyCode):
"""
]

Shift+] is } on american keyboards
"""

Expand All @@ -323,7 +323,7 @@ class CapsLock(KeyCode):
class Comma(KeyCode):
"""
,

Shift+, is < on american keyboards
"""

Expand Down Expand Up @@ -395,7 +395,7 @@ class Menu(KeyCode):
class Minus(KeyCode):
"""
-

Shift+- is _ on american keyboards
"""

Expand All @@ -415,15 +415,15 @@ class PageUp(KeyCode):
class Pause(KeyCode):
"""
Pause, generally lives next to Print Screen and Scroll Lock.

Also Break.
"""


class Period(KeyCode):
"""
.

Shift+. is > on american keyboards
"""

Expand Down Expand Up @@ -499,4 +499,4 @@ class Up(KeyCode):
# Numpad codes (unified between pygame and pyglet)
# 0 1 2 3 4 5 6 7 8 9 add begin decimal delete divide down end enter equals
# f1 f2 f3 f4 home insert left minus multiply next page down page up period plus
# prior right separator space subtract tab up
# prior right separator space subtract tab up
Loading