Skip to content

Commit

Permalink
hold space to run time in reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
JiffyRob committed Feb 12, 2024
1 parent 2387cd7 commit 474c4ed
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
20 changes: 20 additions & 0 deletions scripts/animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Animation:
def __init__(self, frames, speed=.2, flip_x=False, flip_y=False):
self.frames = list(frames)
self.time = 0
self.speed = speed
self.flip_x = flip_x
self.flip_y = flip_y

def update(self, dt):
self.time += dt

def restart(self):
self.time = 0

@property
def image(self):
image = self.frames[round(self.time / self.speed) % len(self.frames)]
image.flip_x = self.flip_x
image.flip_y = self.flip_y
return image
30 changes: 14 additions & 16 deletions scripts/platformer/immobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from math import sin

from scripts import sprite, util_draw
from scripts.animation import Animation


class Emerald(sprite.Sprite):
def __init__(self, level, rect=(0, 0, 16, 16)):
super().__init__(level, image=None, rect=rect, src_rect=None, z=0)
self.anim_speed = 0.08
self.anim_time = 0
self.anim = cycle(
self.level.game.loader.get_spritesheet("platformer-sprites.png")[0:5]
self.anim = Animation(
self.level.game.loader.get_spritesheet("platformer-sprites.png")[0:5], 0.08
)
self.age = random.randint(0, 10)
self.y = self.rect.top
Expand All @@ -22,10 +21,8 @@ def update(self, dt):
return False
if self.collision_rect.colliderect(self.level.player.collision_rect):
return False
self.anim_time += dt
if self.anim_time >= self.anim_speed:
self.anim_time = 0
self.image = next(self.anim)
self.anim.update(dt)
self.image = self.anim.image
self.rect.top = self.y + 1.5 * sin(self.age * 2)
self.age += dt
return True
Expand All @@ -34,26 +31,27 @@ def update(self, dt):
class BustedParts(sprite.Sprite):
def __init__(self, level, rect=(0, 0, 16, 16)):
super().__init__(level, image=None, rect=rect, src_rect=None, z=0)
self.anim_speed = 0.2
self.anim_time = 0
self.anim = cycle(
self.anim = Animation(
self.level.game.loader.get_spritesheet("platformer-sprites.png")[16:20]
)
self.hit_image = self.level.game.loader.get_spritesheet("platformer-sprites.png")[20]
self.hit_time = 0
self.hit_wait = 0.2
self.collision_rect = self.rect.inflate(-2, -12)
self.collision_rect.bottom = self.rect.bottom

def update(self, dt):
if not super().update(dt):
return False
self.anim.update(dt)
if self.collision_rect.colliderect(self.level.player.collision_rect):
self.image = self.hit_image
self.anim_time = 0
self.hit_time = 0.2
self.level.player.jump(True, .7)
self.anim_time += dt
if self.anim_time >= self.anim_speed:
self.anim_time = 0
self.image = next(self.anim)
if self.hit_time > 0:
self.hit_time -= dt
else:
self.image = self.anim.image
return True


Expand Down
4 changes: 2 additions & 2 deletions scripts/platformer/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ def update(self, dt):
self.player.walk_right()
else:
self.player.unwalk()
if not keys[pygame.K_UP]:
self.player.unjump()
if keys[pygame.K_DOWN]:
self.player.duck()
if keys[pygame.K_UP]:
self.player.jump()
if keys[pygame.K_SPACE]:
self.game.time_phase(-1)
# removes dead sprites from the list
self.sprites = [sprite for sprite in self.sprites if sprite.update(dt)]
self.viewport_rect.center = pygame.Vector2(self.viewport_rect.center).lerp(
Expand Down
59 changes: 25 additions & 34 deletions scripts/platformer/mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pygame

from scripts import sprite
from scripts.animation import Animation

GRAVITY = pygame.Vector2(0, 50) # TODO: sideways gravity?????
WALK_SPEED = 64
Expand Down Expand Up @@ -90,14 +91,15 @@ def update(self, dt):
class BoingerBeetle(PhysicsSprite):
def __init__(self, level, rect=(0, 0, 16, 16)):
super().__init__(level, rect=rect, weight=3)
self.anim_speed = 0.2
self.anim_time = 0
self.anim = cycle(
self.level.game.loader.get_spritesheet("platformer-sprites.png")[8:12]
self.anim = Animation(
self.level.game.loader.get_spritesheet("platformer-sprites.png")[8:12],
.15,
)
self.hit_image = self.level.game.loader.get_spritesheet("platformer-sprites.png")[12]
self.hit_wait = .2
self.hit_timer = 0
self.facing_left = True
self.image = next(self.anim)
self.image = self.anim.image

@property
def collision_rect(self):
Expand All @@ -108,17 +110,19 @@ def on_xy_collision(self):

def update(self, dt):
self.velocity.x = WALK_SPEED * .3 * (-1 + self.facing_left * 2)
self.anim.flip_x = self.facing_left
if not super().update(dt):
return False
self.anim_time += dt
self.anim.update(dt)
if self.collision_rect.colliderect(self.level.player.collision_rect) and self.level.player.velocity.y > 0:
self.image = self.hit_image
self.anim_time = 0
self.hit_timer = self.hit_wait
self.image.flip_x = self.anim.flip_x
self.level.player.jump(True, 1.5)
if self.anim_time >= self.anim_speed:
self.anim_time = 0
self.image = next(self.anim)
self.image.flip_x = self.facing_left
if self.hit_timer > 0:
self.hit_timer -= dt
else:
self.image = self.anim.image
return True


Expand All @@ -127,38 +131,29 @@ def __init__(self, level, rect=(0, 0, 16, 16)):
super().__init__(level, rect=rect, image=None)
images = level.game.loader.get_spritesheet("me-Sheet.png")
self.anim_dict = {
"walk": cycle(images[8:12]),
"idle": cycle((images[9],)),
"jump": cycle((images[12],)),
"walk": Animation(images[8:12]),
"idle": Animation((images[9],)),
"jump": Animation((images[12],)),
}
self.anim_speed = 0.2
self.anim_time = 0
self.state = "jump"
self.jump_forced = False
self.image = next(self.anim_dict[self.state])
self.image = self.anim_dict[self.state].image

def swap_state(self, new):
if self.state != new:
self.state = new
self.anim_time = 0
self.image = next(self.anim_dict[self.state])
self.image = self.anim_dict[self.state].image

def update(self, dt):
if self.state == "jump":
if self.on_ground:
print("jump swap")
self.swap_state("idle")
if not self.on_ground:
self.swap_state("jump")
elif self.velocity.x:
self.swap_state("walk")
self.image.flip_x = self.velocity.x < 0
else:
self.swap_state("idle")
self.anim_time += dt
if self.anim_time > self.anim_speed:
self.image = next(self.anim_dict[self.state])
self.anim_time = 0
if self.velocity.x:
self.image.flip_x = self.velocity.x < 0
self.anim_dict[self.state].update(dt)
self.anim_dict[self.state].flip_x = self.velocity.x < 0
self.image = self.anim_dict[self.state].image
return super().update(dt)

def walk_left(self):
Expand All @@ -177,9 +172,5 @@ def jump(self, pain=False, amp=1):
self.on_downer = False
self.jump_forced = pain

def unjump(self):
if not self.jump_forced:
self.velocity.y = max(0, self.velocity.y)

def duck(self):
self.ducking = True

0 comments on commit 474c4ed

Please sign in to comment.