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

py39 + black + typing + typehints #61

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9']
python-version: ['3.9', '3.10', '3.11', '3.12']
name: Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pyscroll
========

For Python 3.7+ and pygame 2.0+
For Python 3.9+ and pygame 2.0+

__pygame-ce is supported__

Expand Down Expand Up @@ -105,7 +105,7 @@ class Sprite(pygame.sprite.Sprite):
Simple Sprite class for on-screen things

"""
def __init__(self, surface):
def __init__(self, surface) -> None:
self.image = surface
self.rect = surface.get_rect()

Expand Down
9 changes: 4 additions & 5 deletions apps/demo/demo-stitched.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from __future__ import annotations

from pathlib import Path
from typing import List

import pygame
from pygame.locals import (
Expand Down Expand Up @@ -57,11 +56,11 @@ def __init__(self) -> None:
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)

@property
def position(self) -> List[float]:
def position(self) -> list[float]:
return list(self._position)

@position.setter
def position(self, value: List[float]) -> None:
def position(self, value: list[float]) -> None:
self._position = list(value)

def update(self, dt: float) -> None:
Expand Down Expand Up @@ -167,14 +166,14 @@ def handle_input(self) -> None:
else:
self.hero.velocity[0] = 0

def update(self, dt: float):
def update(self, dt: float) -> None:
"""
Tasks that occur over time should be handled here

"""
self.group.update(dt)

def run(self):
def run(self) -> None:
clock = pygame.time.Clock()
self.running = True

Expand Down
43 changes: 23 additions & 20 deletions apps/demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def init_screen(width, height):


class ScrollTest:
""" Test and demo of pyscroll
"""Test and demo of pyscroll

For normal use, please see the quest demo, not this.

"""
def __init__(self, filename):

def __init__(self, filename) -> None:

# load data from pytmx
tmx_data = load_pygame(filename)
Expand All @@ -50,19 +51,22 @@ def __init__(self, filename):
map_data = pyscroll.data.TiledMapData(tmx_data)

# create new renderer
self.map_layer = pyscroll.orthographic.BufferedRenderer(map_data, screen.get_size())
self.map_layer = pyscroll.orthographic.BufferedRenderer(
map_data, screen.get_size()
)

# create a font and pre-render some text to be displayed over the map
f = pygame.font.Font(pygame.font.get_default_font(), 20)
t = ["scroll demo. press escape to quit",
"arrow keys move"]
t = ["scroll demo. press escape to quit", "arrow keys move"]

# save the rendered text
self.text_overlay = [f.render(i, 1, (180, 180, 0)) for i in t]

# set our initial viewpoint in the center of the map
self.center = [self.map_layer.map_rect.width / 2,
self.map_layer.map_rect.height / 2]
self.center = [
self.map_layer.map_rect.width / 2,
self.map_layer.map_rect.height / 2,
]

# the camera vector is used to handle camera movement
self.camera_acc = [0, 0, 0]
Expand All @@ -72,7 +76,7 @@ def __init__(self, filename):
# true when running
self.running = False

def draw(self, surface):
def draw(self, surface) -> None:

# tell the map_layer (BufferedRenderer) to draw to the surface
# the draw function requires a rect to draw to.
Expand All @@ -81,15 +85,14 @@ def draw(self, surface):
# blit our text over the map
self.draw_text(surface)

def draw_text(self, surface):
def draw_text(self, surface) -> None:
y = 0
for text in self.text_overlay:
surface.blit(text, (0, y))
y += text.get_height()

def handle_input(self):
""" Simply handle pygame input events
"""
def handle_input(self) -> None:
"""Simply handle pygame input events"""
for event in pygame.event.get():
if event.type == QUIT:
self.running = False
Expand Down Expand Up @@ -126,10 +129,10 @@ def handle_input(self):
else:
self.camera_acc[0] = 0

def update(self, td):
def update(self, td) -> None:
self.last_update_time = td

friction = pow(.0001, self.last_update_time)
friction = pow(0.0001, self.last_update_time)

# update the camera vector
self.camera_vel[0] += self.camera_acc[0] * td
Expand Down Expand Up @@ -164,21 +167,21 @@ def update(self, td):
# in a game, you would set center to a playable character
self.map_layer.center(self.center)

def run(self):
def run(self) -> None:
clock = pygame.time.Clock()
self.running = True
fps = 60.
fps = 60.0
fps_log = collections.deque(maxlen=20)

try:
while self.running:
# somewhat smoother way to get fps and limit the framerate
clock.tick(fps*2)
clock.tick(fps * 2)

try:
fps_log.append(clock.get_fps())
fps = sum(fps_log)/len(fps_log)
dt = 1/fps
fps = sum(fps_log) / len(fps_log)
dt = 1 / fps
except ZeroDivisionError:
continue

Expand All @@ -197,7 +200,7 @@ def run(self):
pygame.init()
pygame.font.init()
screen = init_screen(800, 600)
pygame.display.set_caption('pyscroll Test')
pygame.display.set_caption("pyscroll Test")

try:
filename = sys.argv[1]
Expand Down
3 changes: 1 addition & 2 deletions apps/demo/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@


class Dummy:

def run(self):
def run(self) -> None:
surface = None

for spr in self.sprites():
Expand Down
11 changes: 6 additions & 5 deletions apps/tutorial/quest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from __future__ import annotations

from pathlib import Path
from typing import List

import pygame
from pygame.locals import (
Expand Down Expand Up @@ -70,6 +69,7 @@ class Hero(pygame.sprite.Sprite):
it collides with level walls.

"""

def __init__(self) -> None:
super().__init__()
self.image = load_image("hero.png").convert_alpha()
Expand All @@ -80,11 +80,11 @@ def __init__(self) -> None:
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)

@property
def position(self) -> List[float]:
def position(self) -> list[float]:
return list(self._position)

@position.setter
def position(self, value: List[float]) -> None:
def position(self, value: list[float]) -> None:
self._position = list(value)

def update(self, dt: float) -> None:
Expand Down Expand Up @@ -113,6 +113,7 @@ class QuestGame:
Finally, it uses a pyscroll group to render the map and Hero.

"""

map_path = RESOURCES_DIR / "grasslands.tmx"

def __init__(self, screen: pygame.Surface) -> None:
Expand Down Expand Up @@ -206,7 +207,7 @@ def handle_input(self) -> None:
else:
self.hero.velocity[0] = 0

def update(self, dt: float):
def update(self, dt: float) -> None:
"""
Tasks that occur over time should be handled here

Expand All @@ -220,7 +221,7 @@ def update(self, dt: float):
if sprite.feet.collidelist(self.walls) > -1:
sprite.move_back(dt)

def run(self):
def run(self) -> None:
"""
Run the game loop

Expand Down
2 changes: 1 addition & 1 deletion docs/_build/html/_sources/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
pyscroll
========

for Python 2.7 & 3.3 and Pygame 1.9
for Python 3.9 and Pygame 1.9

A simple, fast module for adding scrolling maps to your new or existing game.

Expand Down
2 changes: 1 addition & 1 deletion docs/_build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h3>Navigation</h3>

<div class="section" id="pyscroll">
<h1>pyscroll<a class="headerlink" href="#pyscroll" title="Permalink to this headline">¶</a></h1>
<p>for Python 2.7 &amp; 3.3 and Pygame 1.9</p>
<p>for Python 3.9 and Pygame 1.9</p>
<p>A simple, fast module for adding scrolling maps to your new or existing game.</p>
</div>
<div class="section" id="introduction">
Expand Down
Loading