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
8 changes: 4 additions & 4 deletions examples/targets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

import ppb
from ppb import Vector
from ppb import keycodes
from ppb import keycodes, Vector
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer one per import per line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, there were already places which had multiple imports per line, and in ppb-vector too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, she and I kinda disagree on that.

Copy link
Contributor Author

@nbraud nbraud May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so then @pathunstrom, can we agree to disagree, instead of expecting contributors to conform with a styleguide that only exists in your head, that the codebase doesn't comply with, and giving them imperative-mode feedback without explanations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: I just realised the tone might be wrong there; to clarify, I wasn't annoyed at you, though I felt it was important to provide feedback on that being frustrating & why (not necessarily just for me, but also for other contributors), esp. since that's not the first time it happens.



class MoverMixin(ppb.BaseSprite):
Expand Down Expand Up @@ -39,7 +39,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 +51,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
27 changes: 13 additions & 14 deletions examples/targets_with_twisted.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import logging
import ppb
from ppb import Vector
from ppb import keycodes
from twisted.internet import defer
from twisted.internet import task
from twisted.internet import endpoints
from twisted.web.server import Site
import klein
from dataclasses import dataclass
from typing import Any

import klein
from twisted.internet import defer, endpoints, task
from twisted.web.server import Site

import ppb
from ppb import keycodes, Vector


class MoverMixin(ppb.BaseSprite):
velocity = Vector(0, 0)
Expand Down Expand Up @@ -45,7 +43,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 +72,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 +100,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 +117,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 +126,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:])
7 changes: 4 additions & 3 deletions ppb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
from typing import Callable

from ppb_vector import Vector
from ppb.engine import GameEngine
from ppb.scenes import BaseScene
from ppb.sprites import BaseSprite
from ppb_vector import Vector

__all__ = (
# Shortcuts
Expand All @@ -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))
2 changes: 1 addition & 1 deletion ppb/abc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Type, Container, Tuple, Iterable
from typing import Iterable, Tuple, 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
9 changes: 4 additions & 5 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,7 @@

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 EventMixin, Quit, StartScene
from ppb.systems import PygameEventPoller
from ppb.systems import Renderer
from ppb.systems import Updater
Expand All @@ -40,7 +38,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
Loading