From 81bce7fe6356bbc5a29ccb27ec049cf71376c640 Mon Sep 17 00:00:00 2001 From: Neal Finger Date: Sat, 11 Feb 2023 14:10:35 -0700 Subject: [PATCH 1/3] Update player.py player root animation grows based on distance not just the y value refactor the PlayerAnimation class --- player.py | 85 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/player.py b/player.py index 78ef1dc..489e960 100644 --- a/player.py +++ b/player.py @@ -2,19 +2,21 @@ import time import thumby import math import random +from collections import namedtuple from Games.Throot.constants import CONST_FPS # didplay width 72 # display height 40 -# constants +### constants CONST_PL_SCREEN_Y = int((0.5) * thumby.display.height) CONST_INP_MOVE_LEFT = thumby.buttonL CONST_INP_MOVE_RIGHT = thumby.buttonA +CONST_INP_MOVE_RIGHT_ALT = thumby.buttonR -# classes +### classes class Player: def __init__(self): self.prev_x = 0 @@ -32,7 +34,7 @@ class Player: self.accx = 1 / (CONST_FPS * 4) self.accaccx = self.accx - self.anim = PlayerAnimation(self.x, self.y) + self.anim = PlayerAnimation(self) self.debugvisible = False def update_phys(self, tpf, camera: Camera): @@ -41,7 +43,7 @@ class Player: inp = int(0) if CONST_INP_MOVE_LEFT.pressed(): inp -= 1 - if CONST_INP_MOVE_RIGHT.pressed(): + if CONST_INP_MOVE_RIGHT.pressed() or CONST_INP_MOVE_RIGHT_ALT.pressed(): inp += 1 # debug visible @@ -70,7 +72,7 @@ class Player: self.y = int(self.ysub) # animation - self.anim.update_phys(self.x, self.y, camera) + self.anim.update_phys(camera) def update_draw(self, camera): # debug draw @@ -103,44 +105,61 @@ class Camera: class PlayerAnimation: - # width and height of range bounding box must be odd - RANDOM_RANGE = int(5) - POS_RANGE = int(3) - POS_X = int(0) - POS_Y = int(1) + MAX_ROOT_OFFSET = int(2) - def __init__(self, plworldx, plworldy): - self.poslist = [(plworldx, plworldy)] - self.debugrandomrange = self.RANDOM_RANGE - self.debugposrange = self.POS_RANGE + MAX_GROW_DISTANCE = int(5)#int(3) + MIN_GROW_DISTANCE = int(1) - def update_phys(self, plworldx, plworldy, camera): - - if plworldy < self.poslist[-1][self.POS_Y] + self.POS_RANGE: - return + Point = namedtuple("Point", ["x", "y"]) + + def __init__(self, player): + self.player = player - ### get new position - rad = self.RANDOM_RANGE // 2 + self.root_offset = self.MAX_ROOT_OFFSET + self.grow_distance = self.MAX_GROW_DISTANCE - # move last pos - self.poslist[-1] = ( - plworldx + random.randrange(-rad, rad), - plworldy + random.randrange(-rad, rad) + self.points = [] + self.add_point() + + def add_point(self): + self.points.append(self.Point(self.player.x, self.player.y)) + + def update_phys(self, camera): + + # exit if distance from player to last root point is less than gorw distance + dist_sqr = ( + (self.player.x - self.points[-1].x) ** 2 + + (self.player.y - self.points[-1].y) ** 2 ) - # new pos is perfectly on player - self.poslist.append((plworldx, plworldy)) + if dist_sqr < self.grow_distance ** 2: + return - ### remove the fist item in the list if its full line is off screen - if not camera.entity_in_camera(self.poslist[1][self.POS_X], self.poslist[1][self.POS_Y]): - self.poslist.pop(0) + # set new grow distance + self.grow_distance = random.randint(self.MIN_GROW_DISTANCE, self.MAX_GROW_DISTANCE) + + # offset last root point to look random + self.points[-1] = self.Point( + self.points[-1].x + random.randrange(-self.root_offset, self.root_offset), + self.points[-1].y + random.randrange(-self.root_offset, self.root_offset) + ) + + # new root point created exactly on player + self.add_point() + + # remove points in list that would make lines off the camera + for point in self.points[1:]: + if camera.entity_in_camera(point.x, point.y): + break + + self.points.pop(0) def update_draw(self, camera, isdecend=1): - ### draw line from first position to next - for i in range(len(self.poslist) - 1): - x1, y1 = camera.relative_to_camera(self.poslist[i][self.POS_X], self.poslist[i][self.POS_Y]) - x2, y2 = camera.relative_to_camera(self.poslist[i + 1][self.POS_X], self.poslist[i + 1][self.POS_Y]) + # draw line from first position to next + for i in range(len(self.points) - 1): + x1, y1 = camera.relative_to_camera(self.points[i].x, self.points[i].y) + x2, y2 = camera.relative_to_camera(self.points[i + 1].x, self.points[i + 1].y) thumby.display.drawLine( int(x1), int(y1), From 00f03652eb73d42bd63fcedfc51bf10829c05ff4 Mon Sep 17 00:00:00 2001 From: Neal Finger Date: Sun, 12 Feb 2023 12:24:06 -0700 Subject: [PATCH 2/3] player movement -input class rough draft -use of time per frame for x movement -y speed needed to be halved for some reason -animation line drawing starts from player then moves outward to prepare for storing more points than can fit on the screen (scrolling up after complete stage) --- player.py | 109 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/player.py b/player.py index 489e960..fa5ee4e 100644 --- a/player.py +++ b/player.py @@ -9,12 +9,17 @@ from Games.Throot.constants import CONST_FPS # didplay width 72 # display height 40 -### constants -CONST_PL_SCREEN_Y = int((0.5) * thumby.display.height) - -CONST_INP_MOVE_LEFT = thumby.buttonL -CONST_INP_MOVE_RIGHT = thumby.buttonA -CONST_INP_MOVE_RIGHT_ALT = thumby.buttonR +class Input: + def move_right(self): + return ( + thumby.buttonA.pressed() or + thumby.buttonR.pressed() + ) + + def move_left(self): + return thumby.buttonL.pressed() + +inp = Input() ### classes class Player: @@ -26,61 +31,82 @@ class Player: self.x = int(self.xsub) self.y = int(self.ysub) - self.movespeed = 32 / CONST_FPS self.isdecend = 1 - self.xspeed = 32 / CONST_FPS - self.yspeed = 32 - self.accx = 1 / (CONST_FPS * 4) - self.accaccx = self.accx + self.input_x = float(0) + + self.speed_x_start = 32 + self.speed_x_max = 200 + self.acc_x_start = 64 + self.jerk_x = 128 + self.speed_x = self.speed_x_start + self.acc_x = self.acc_x_start + + # y speed is constant + self.speed_y = 16 self.anim = PlayerAnimation(self) self.debugvisible = False def update_phys(self, tpf, camera: Camera): + # debug visible + if thumby.buttonB.justPressed(): + self.debugvisible = not self.debugvisible + + # prevoius sub pixel position self.prev_x = self.xsub self.prev_y = self.ysub - inp = int(0) - if CONST_INP_MOVE_LEFT.pressed(): - inp -= 1 - if CONST_INP_MOVE_RIGHT.pressed() or CONST_INP_MOVE_RIGHT_ALT.pressed(): - inp += 1 - - # debug visible - if thumby.buttonU.pressed(): - self.debugvisible = not self.debugvisible ### x movement - self.xspeed += self.accx - self.accx += self.accaccx - if (inp == 0) and (self.xspeed > self.movespeed): - self.xspeed = self.movespeed - self.accx = self.accaccx + # x input + if inp.move_left() and (not inp.move_right()): + self.input_x = -1 + + if inp.move_right() and (not inp.move_left()): + self.input_x = 1 + + if (not inp.move_left()) and (not inp.move_right()): + self.input_x = 0 - xx = self.xsub + inp * self.xspeed + # acceleration and jerk + self.speed_x += self.acc_x * tpf + self.acc_x += self.jerk_x * tpf - # clamp to screen - self.xsub = max(min(xx, thumby.display.width - 1), 0) + # limit speed + self.speed_x = min(self.speed_x_max, self.speed_x) + + # reset speed and acceleration if no input + if (self.input_x == 0) and (self.speed_x != self.speed_x_start): + self.speed_x = self.speed_x_start + self.acc_x = self.acc_x_start + + # set x sub pixel position + self.xsub += self.speed_x * tpf * self.input_x ### y movement - self.ysub += self.yspeed * tpf + # set y sub pixel position + self.ysub += self.speed_y * tpf + + ### stay in world bounds + self.xsub = max(min(self.xsub, thumby.display.width - 1), 0) ### pixel positions self.x = int(self.xsub) self.y = int(self.ysub) - # animation + ### animation physics self.anim.update_phys(camera) def update_draw(self, camera): - # debug draw + # debug render if self.debugvisible: pix_x, pix_y = camera.relative_to_camera(self.x, self.y) thumby.display.setPixel(int(pix_x), int(pix_y), self.isdecend) + return - # animation + ### animation render self.anim.update_draw(camera, self.isdecend) @@ -157,9 +183,12 @@ class PlayerAnimation: def update_draw(self, camera, isdecend=1): # draw line from first position to next - for i in range(len(self.points) - 1): - x1, y1 = camera.relative_to_camera(self.points[i].x, self.points[i].y) - x2, y2 = camera.relative_to_camera(self.points[i + 1].x, self.points[i + 1].y) + point_prev = self.points[-1] + for point in self.points[-2::-1]: + x1, y1 = camera.relative_to_camera(point_prev.x, point_prev.y) + x2, y2 = camera.relative_to_camera(point.x, point.y) + point_prev = point + thumby.display.drawLine( int(x1), int(y1), @@ -167,3 +196,13 @@ class PlayerAnimation: int(y2), isdecend ) + # for i in range(len(self.points) - 1): + # x1, y1 = camera.relative_to_camera(self.points[i].x, self.points[i].y) + # x2, y2 = camera.relative_to_camera(self.points[i + 1].x, self.points[i + 1].y) + # thumby.display.drawLine( + # int(x1), + # int(y1), + # int(x2), + # int(y2), + # isdecend + # ) From f34883b84f4ffb48f72738b0bbf1eea92e215671 Mon Sep 17 00:00:00 2001 From: Neal Finger Date: Sun, 12 Feb 2023 13:21:21 -0700 Subject: [PATCH 3/3] smooth root render -root will always draw a line to the player position. This makes the root growing animation look smoother and show the exact player position. -dpad up toggles this -button b toggles the debug render --- player.py | 60 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/player.py b/player.py index fa5ee4e..2893279 100644 --- a/player.py +++ b/player.py @@ -35,10 +35,12 @@ class Player: self.input_x = float(0) + # constants self.speed_x_start = 32 self.speed_x_max = 200 self.acc_x_start = 64 self.jerk_x = 128 + self.speed_x = self.speed_x_start self.acc_x = self.acc_x_start @@ -46,13 +48,9 @@ class Player: self.speed_y = 16 self.anim = PlayerAnimation(self) - self.debugvisible = False + def update_phys(self, tpf, camera: Camera): - # debug visible - if thumby.buttonB.justPressed(): - self.debugvisible = not self.debugvisible - # prevoius sub pixel position self.prev_x = self.xsub self.prev_y = self.ysub @@ -100,12 +98,6 @@ class Player: self.anim.update_phys(camera) def update_draw(self, camera): - # debug render - if self.debugvisible: - pix_x, pix_y = camera.relative_to_camera(self.x, self.y) - thumby.display.setPixel(int(pix_x), int(pix_y), self.isdecend) - return - ### animation render self.anim.update_draw(camera, self.isdecend) @@ -129,12 +121,11 @@ class Camera: # position player self.y = self.target.y + self.offset - class PlayerAnimation: - MAX_ROOT_OFFSET = int(2) + MAX_ROOT_OFFSET = int(4) - MAX_GROW_DISTANCE = int(5)#int(3) - MIN_GROW_DISTANCE = int(1) + MAX_GROW_DISTANCE = int(6)#int(3) + MIN_GROW_DISTANCE = int(2) Point = namedtuple("Point", ["x", "y"]) @@ -147,11 +138,21 @@ class PlayerAnimation: self.points = [] self.add_point() + self.debug_visible = False + self.debug_exact = True + def add_point(self): self.points.append(self.Point(self.player.x, self.player.y)) def update_phys(self, camera): - + # debug visible + if thumby.buttonB.justPressed(): + self.debug_visible = not self.debug_visible + + # debug exact + if thumby.buttonU.justPressed(): + self.debug_exact = not self.debug_exact + # exit if distance from player to last root point is less than gorw distance dist_sqr = ( (self.player.x - self.points[-1].x) ** 2 + @@ -181,6 +182,23 @@ class PlayerAnimation: self.points.pop(0) def update_draw(self, camera, isdecend=1): + # debug render + if self.debug_visible: + pix_x, pix_y = camera.relative_to_camera(self.x, self.y) + thumby.display.setPixel(int(pix_x), int(pix_y), self.isdecend) + return + + # root always reaches player position + if self.debug_exact: + x1, y1 = camera.relative_to_camera(self.player.x, self.player.y) + x2, y2 = camera.relative_to_camera(self.points[-1].x, self.points[-1].y) + thumby.display.drawLine( + int(x1), + int(y1), + int(x2), + int(y2), + isdecend + ) # draw line from first position to next point_prev = self.points[-1] @@ -196,13 +214,3 @@ class PlayerAnimation: int(y2), isdecend ) - # for i in range(len(self.points) - 1): - # x1, y1 = camera.relative_to_camera(self.points[i].x, self.points[i].y) - # x2, y2 = camera.relative_to_camera(self.points[i + 1].x, self.points[i + 1].y) - # thumby.display.drawLine( - # int(x1), - # int(y1), - # int(x2), - # int(y2), - # isdecend - # )