Skip to content

Commit

Permalink
Add potato
Browse files Browse the repository at this point in the history
This is a quality commit message.
  • Loading branch information
Juici committed Mar 19, 2018
1 parent d2fc8e3 commit 70f329f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'WINDOW_SIZE',
'FULLSCREEN',
'HIDPI_FACTOR',
'PLAYER_POTATO',
'PLAYER_SIZE',
'PLAYER_VELOCITY',
'PLAYER_DEATH_VELOCITY',
Expand Down Expand Up @@ -62,7 +63,8 @@ def _show_bg() -> bool:

HIDPI_FACTOR = _get_hidpi_factor() # HiDPI screen scale factor.

PLAYER_SIZE = (BLOCK_SIZE, BLOCK_SIZE * 2)
PLAYER_POTATO = True
PLAYER_SIZE = (BLOCK_SIZE, BLOCK_SIZE)
PLAYER_VELOCITY = (5, 20) # (run, jump)
PLAYER_DEATH_VELOCITY = (10, 25)
PLAYER_RESPAWN_X_OFFSET = 40
Expand Down
20 changes: 15 additions & 5 deletions level_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import TYPE_CHECKING, Tuple
from constants import PLAYER_SIZE, PLAYER_VELOCITY, PLAYER_DEATH_VELOCITY, PLAYER_RESPAWN_X_OFFSET, \
ACCEL_GRAVITY, BLOCK_SIZE, GRID_SIZE, WINDOW_SIZE, Key
ACCEL_GRAVITY, BLOCK_SIZE, GRID_SIZE, WINDOW_SIZE, Key, PLAYER_POTATO
from sprite import Sprite
from util import Color
from geom import Vector, BoundingBox
Expand Down Expand Up @@ -223,7 +223,9 @@ def __init__(self, world: 'World'):
self.score = 0
self.lives = 3

self.sprite = Sprite
self.sprite_cols = 8
self.sprite = Sprite('assets/player.png', self.sprite_cols, 1)
self.roll = 0

def jump(self):
self.on_ground = False
Expand Down Expand Up @@ -293,15 +295,23 @@ def render(self, canvas: simplegui.Canvas):
dpi_factor = self.window.hidpi_factor

# Draw player.
point_list = [p.multiply(dpi_factor).into_tuple() for p in bounds]
color = Color(120, 120, 200)
if PLAYER_POTATO:
dest_center = self.pos + self.size / 2
index = (self.roll // self.sprite_cols) % self.sprite_cols
self.sprite.draw(canvas, dest_center * dpi_factor, self.size * dpi_factor, (index, 0))
else:
point_list = [p.multiply(dpi_factor).into_tuple() for p in bounds]
color = Color(120, 120, 200)

canvas.draw_polygon(point_list, 1, str(color), str(color)) # TODO: sprite?
canvas.draw_polygon(point_list, 1, str(color), str(color))

# Update position.
self.last_pos = self.pos.copy()
self.pos.add(self.vel)

self.roll += self.vel.x * 2 / PLAYER_VELOCITY[0]
self.roll += 1

if abs(self.vel.x) > PLAYER_VELOCITY[0]:
self.vel.x = math.copysign(PLAYER_VELOCITY[0], self.vel.x)

Expand Down
14 changes: 8 additions & 6 deletions sprite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import util
import simplegui

from typing import Tuple
from typing import Tuple, Optional
from geom import Vector


Expand All @@ -13,7 +14,7 @@ def __init__(self, image: str, cols: int = 1, rows: int = 1):
"""
Creates a sprite with the specified image and number of cols and rows.
"""
self.image = simplegui.load_image(image)
self.image = util.load_image(image)
self.width = self.image.get_width()
self.height = self.image.get_height()
self.cols = cols
Expand All @@ -22,13 +23,14 @@ def __init__(self, image: str, cols: int = 1, rows: int = 1):
self.sprite_size = (self.width // cols, self.height // rows)
self.sprite_center = (self.sprite_size[0] / 2, self.sprite_size[1] / 2)

def draw(self, canvas: simplegui.Canvas, pos: Vector, size: Tuple[int, int] = None,
def draw(self, canvas: simplegui.Canvas, pos: Vector, size: Optional[Vector] = None,
index: Tuple[int, int] = (0, 0)):
"""
Draw the sprite on the canvas.
"""
source_center = (self.sprite_size[i] * index[i] + self.sprite_center[i] for i in [0, 1])
if size is None:
source_center = [self.sprite_size[i] * index[i] + self.sprite_center[i] for i in [0, 1]]
if size is not None:
size = size.into_tuple()
else:
size = self.sprite_size

canvas.draw_image(self.image, source_center, self.sprite_size, pos.into_tuple(), size)

0 comments on commit 70f329f

Please sign in to comment.