From b80c215d98c49220f8d54e8f0e3715c6fb939211 Mon Sep 17 00:00:00 2001 From: William Hubbard <75867393+Markil3@users.noreply.github.com> Date: Sun, 5 Feb 2023 13:09:05 -0700 Subject: [PATCH 1/3] Sets the depth to be handled by the player Just a lot simpler. --- Throot.py | 34 ++++++++++++++-------------------- player.py | 53 +++++++++++++++++++++++++++++++++++------------------ sprites.py | 31 ++++++++++++++++--------------- 3 files changed, 65 insertions(+), 53 deletions(-) diff --git a/Throot.py b/Throot.py index bcade66..b8a3d77 100644 --- a/Throot.py +++ b/Throot.py @@ -8,7 +8,7 @@ import Games.Throot.sprites from random import randrange from Games.Throot.artrepository import OBSTACLES -from Games.Throot.player import Player +from Games.Throot.player import Player, Camera from Games.Throot.map import ObstacleSlice, OBSTACLE_SLICES from Games.Throot.sprites import Obstacle, update_entities from Games.Throot.constants import * @@ -82,7 +82,8 @@ class GameLoop(Room): self.current_obstacle_slice = OBSTACLE_SLICES[0] self.entities = set() - self.player = Player() + self.player = None + self.camera = None self.score = 0 @@ -91,7 +92,9 @@ class GameLoop(Room): self.entities.clear() self.current_obstacle_slice = OBSTACLE_SLICES[0] self.player = Player() + self.player.yspeed = 10 self.score = 0 + self.camera = Camera(self.player) def get_obstacle_slice(self): return OBSTACLE_SLICES[randrange(0, len(OBSTACLE_SLICES))] @@ -101,28 +104,19 @@ class GameLoop(Room): Executes one tick of the game """ super().update(tpf) - prev_depth = self.current_depth - self.diving_velocity += self.diving_accel * tpf - self.current_depth += self.diving_velocity * tpf + + self.player.update_phys(tpf, self.camera) - #print("%f (%f)" % (self.current_depth, self.current_depth % ObstacleSlice.height)) - - #print("%f -> %f: %f -> %f" % (prev_depth, self.current_depth, prev_depth % ObstacleSlice.height, self.current_depth % ObstacleSlice.height)) - if ( - self.current_depth > prev_depth and self.current_depth % ObstacleSlice.height < prev_depth % ObstacleSlice.height - or self.current_depth < prev_depth and self.current_depth % ObstacleSlice.height > prev_depth % ObstacleSlice.height - ): - self.current_obstacle_slice = self.get_obstacle_slice() - - self.player.update_phys(self.current_depth, prev_depth) + self.camera.update_phys() # Generate new entities as needed - self.entities |= update_entities(self.entities, 5, self.current_depth, prev_depth) + self.entities |= update_entities(self.entities, 5, self.player.ysub, self.player.prev_y) # Updates the entities to_remove = set() for entity in self.entities: - if not entity.update(tpf, self.current_depth, prev_depth): + if not entity.update(tpf, self.player, self.camera) or abs(entity.y - self.player.y) > self.camera.height * 1.5: + print("Removing entity at (%d, %d) (or %d)" % (entity.x, entity.y, abs(entity.y - self.player.y))) to_remove.add(entity) elif entity.intersects_pixel(self.player.xsub, self.player.ysub): if isinstance(entity, Obstacle): @@ -132,13 +126,13 @@ class GameLoop(Room): } self.entities -= to_remove - self.score = int(self.current_depth * 10) + self.score = self.player.y#int(abs(self.player.y) * 10) thumby.display.fill(0) # Draw the entities for entity in self.entities: - entity.render(tpf, self.current_depth, prev_depth) - self.player.update_draw(self.player.y) + entity.render(tpf, self.camera) + self.player.update_draw(self.camera) thumby.display.drawText(str(self.score), thumby.display.width - (len(str(self.score)) + 2) * 5, 1, 1) diff --git a/player.py b/player.py index 08b0235..6919fe1 100644 --- a/player.py +++ b/player.py @@ -17,6 +17,8 @@ CONST_INP_MOVE_RIGHT = thumby.buttonA # classes class Player: def __init__(self): + self.prev_x = 0 + self.prev_y = 0 self.xsub = float(thumby.display.width / 2) self.ysub = float(0) self.x = int(self.xsub) @@ -26,14 +28,16 @@ class Player: self.isdecend = 1 self.xspeed = 32 / CONST_FPS - self.yspeed = 32 / CONST_FPS + self.yspeed = 32 self.accx = 1 / (CONST_FPS * 4) self.accaccx = self.accx self.anim = PlayerAnimation(self.x, self.y) self.debugvisible = True - def update_phys(self, current_depth, prev_depth): + def update_phys(self, tpf, camera: Camera): + self.prev_x = self.xsub + self.prev_y = self.ysub inp = int(0) if CONST_INP_MOVE_LEFT.pressed(): inp -= 1 @@ -58,35 +62,46 @@ class Player: self.xsub = max(min(xx, thumby.display.width - 1), 0) ### y movement - self.ysub = current_depth - (thumby.display.height / 2) + + self.ysub += self.yspeed * tpf ### pixel positions self.x = int(self.xsub) self.y = int(self.ysub) # animation - self.anim.update_phys(self.x, self.y, CONST_PL_SCREEN_Y, current_depth) + self.anim.update_phys(self.x, self.y, camera) - def update_draw(self, plworldy): + def update_draw(self, camera): # debug draw if self.debugvisible: - pix_x = self.x - pix_y = int(self.y - plworldy + 20) - thumby.display.setPixel(pix_x, pix_y, self.isdecend) + pix_x, pix_y = camera.relative_to_camera(self.x, self.y) + thumby.display.setPixel(int(pix_x), int(pix_y), self.isdecend) # animation - self.anim.update_draw(plworldy - 20, self.isdecend) + self.anim.update_draw(camera, self.isdecend) class Camera: def __init__(self, target): self.x = int(0) self.y = int(0) + self.width = thumby.display.width + self.height = thumby.display.height + self.offset = -self.height / 2 self.target = target - def update_phys(self, plscreeny): + def entity_in_camera(self, ent_x, ent_y, ent_width = 0, ent_height = 0) -> bool: + if ent_x + ent_width >= self.x and ent_y + ent_height >= self.y and ent_x < self.x + self.width and ent_y < self.y + self.height: + return True + print("Dropping entity (%d, %d - %d, %d) from (%d, %d - %d, %d)" % (ent_x, ent_y, ent_x + ent_width, ent_y + ent_height, self.x, self.y, self.x + self.width, self.y + self.height)) + + def relative_to_camera(self, global_x, global_y): + return (global_x - self.x, global_y - self.y) + + def update_phys(self): # position player - self.y = self.target.y - plscreeny + self.y = self.target.y + self.offset class PlayerAnimation: @@ -101,7 +116,7 @@ class PlayerAnimation: self.debugrandomrange = self.RANDOM_RANGE self.debugposrange = self.POS_RANGE - def update_phys(self, plworldx, plworldy, plscreeny, current_depth): + def update_phys(self, plworldx, plworldy, camera): #print(plworldx, plworldy, plscreeny, current_depth) @@ -159,21 +174,23 @@ class PlayerAnimation: # self.poslist.append((plworldx, plworldy)) ### remove the fist item in the list if its full line is off screen - if self.poslist[1][self.POS_Y] < plworldy - plscreeny: + if not camera.entity_in_camera(self.poslist[1][self.POS_X], self.poslist[1][self.POS_Y]): self.poslist.pop(0) #print(len(self.poslist)) - def update_draw(self, topleft, prev_depth, isdecend=1): + def update_draw(self, camera, isdecend=1): ### draw line from first position to next #print(topleft) 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]) thumby.display.drawLine( - int(self.poslist[i][self.POS_X]), - int(self.poslist[i][self.POS_Y] - topleft), - int(self.poslist[i + 1][self.POS_X]), - int(self.poslist[i + 1][self.POS_Y] - topleft), + int(x1), + int(y1), + int(x2), + int(y2), isdecend ) diff --git a/sprites.py b/sprites.py index 0d2698c..cfc20e1 100644 --- a/sprites.py +++ b/sprites.py @@ -4,6 +4,7 @@ from random import randrange, random import thumby from Games.Throot.map import ObstacleSlice +from Games.Throot.player import Player, Camera from Games.Throot.artrepository import OBSTACLES class Entity: @@ -38,7 +39,7 @@ class Entity: rel_y = int(loc_y - self.y) return bool((self.collision[rel_x] >> (self.height - rel_y)) & 1) - def can_render(self, current_depth: int) -> bool: + def can_render(self, camera: Camera) -> bool: """ Checks to see if this sprite is within the game screen. @@ -49,15 +50,9 @@ class Entity: :return: True if the sprite shows up on screen in any capacity, false otherwise. """ - if self.x < -self.width or self.x >= thumby.display.width: - return False - max = current_depth - min = max - thumby.display.height - if self.y < (min - self.height) or self.y > max: - return False - return True + return camera.entity_in_camera(self.x, self.y, self.width, self.height) - def update(self, tpf: float, current_depth: int, prev_depth: int) -> bool: + def update(self, tpf: float, player: Player, camera: Camera) -> bool: """ Updates the state of the entity based on the game state. @@ -74,7 +69,7 @@ class Entity: """ return False - def render(self, tpf: float, current_depth: int, prev_depth: int) -> bool: + def render(self, tpf: float, camera: Camera) -> bool: """ Sets the position of the sprite and draws it. @@ -90,16 +85,17 @@ class Entity: should be hidden. """ if self.sprite: - self.sprite.y = int(self.y - current_depth + thumby.display.height) - self.sprite.x = int(self.x) - if self.can_render(current_depth): + coordinates = camera.relative_to_camera(self.x, self.y) + self.sprite.x = coordinates[0] + self.sprite.y = coordinates[1] + if self.can_render(camera): thumby.display.drawSprite(self.sprite) return True return False class Obstacle(Entity): - def update(self, tpf: float, current_depth: int, prev_depth: int) -> bool: - return self.can_render(current_depth) + def update(self, tpf: float, player: Player, camera: Camera) -> bool: + return True def update_entities(entities, difficulty: int, current_depth: int, prev_depth: int): @@ -135,12 +131,17 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i else: x = randrange(0, thumby.display.width - sprite_image.width) y = current_depth + if current_depth > prev_depth: + y += thumby.display.height // 2 + elif current_depth < prev_depth: + y -= thumby.display.height // 2 + sprite_image.height error = False for entity in entities | spawned: if x > (entity.x - OBSTACLE_BUFFER) and x <= (entity.x + entity.width + OBSTACLE_BUFFER) and y > (entity.y - OBSTACLE_BUFFER) and y <= (entity.y + entity.height + OBSTACLE_BUFFER): error = True break if not error: + print("Creating entity at %d, %d" % (x, y)) sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, False, False) spawned.add(Obstacle(x, y, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite)) return spawned From 518bf5f5e651ad564d496c317d6b204ba254b17f Mon Sep 17 00:00:00 2001 From: William Hubbard <75867393+Markil3@users.noreply.github.com> Date: Sun, 5 Feb 2023 13:58:32 -0700 Subject: [PATCH 2/3] Fixes collision code. --- Throot.py | 3 +-- player.py | 4 +--- sprites.py | 8 ++++---- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Throot.py b/Throot.py index b8a3d77..59c26ac 100644 --- a/Throot.py +++ b/Throot.py @@ -116,7 +116,6 @@ class GameLoop(Room): to_remove = set() for entity in self.entities: if not entity.update(tpf, self.player, self.camera) or abs(entity.y - self.player.y) > self.camera.height * 1.5: - print("Removing entity at (%d, %d) (or %d)" % (entity.x, entity.y, abs(entity.y - self.player.y))) to_remove.add(entity) elif entity.intersects_pixel(self.player.xsub, self.player.ysub): if isinstance(entity, Obstacle): @@ -126,7 +125,7 @@ class GameLoop(Room): } self.entities -= to_remove - self.score = self.player.y#int(abs(self.player.y) * 10) + self.score = self.player.x#int(abs(self.player.y) * 10) thumby.display.fill(0) # Draw the entities diff --git a/player.py b/player.py index 6919fe1..93187d2 100644 --- a/player.py +++ b/player.py @@ -92,9 +92,7 @@ class Camera: self.target = target def entity_in_camera(self, ent_x, ent_y, ent_width = 0, ent_height = 0) -> bool: - if ent_x + ent_width >= self.x and ent_y + ent_height >= self.y and ent_x < self.x + self.width and ent_y < self.y + self.height: - return True - print("Dropping entity (%d, %d - %d, %d) from (%d, %d - %d, %d)" % (ent_x, ent_y, ent_x + ent_width, ent_y + ent_height, self.x, self.y, self.x + self.width, self.y + self.height)) + return ent_x + ent_width >= self.x and ent_y + ent_height >= self.y and ent_x < self.x + self.width and ent_y < self.y + self.height def relative_to_camera(self, global_x, global_y): return (global_x - self.x, global_y - self.y) diff --git a/sprites.py b/sprites.py index cfc20e1..4f4a9ed 100644 --- a/sprites.py +++ b/sprites.py @@ -37,7 +37,7 @@ class Entity: return False rel_x = int(loc_x - self.x) rel_y = int(loc_y - self.y) - return bool((self.collision[rel_x] >> (self.height - rel_y)) & 1) + return bool((self.collision[rel_x] >> (rel_y)) & 1) def can_render(self, camera: Camera) -> bool: """ @@ -123,8 +123,8 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i return set() spawned = set() - max_spawned = 1#randrange(0, difficulty + 1) - while len(spawned) < max_spawned and random() < (2 * math.pow(difficulty + 0.01, 2)) / (3 * math.pow(difficulty + 0.01, 2) + 20 * (difficulty + 0.01)) + 0.05: + max_spawned = randrange(0, difficulty + 1) + while len(entities) < max_spawned and random() < (2 * math.pow(difficulty + 0.01, 2)) / (3 * math.pow(difficulty + 0.01, 2) + 20 * (difficulty + 0.01)) + 0.05: sprite_image = OBSTACLES[randrange(0, min((difficulty + 1) * 3, len(OBSTACLES)))] if sprite_image.width >= thumby.display.width: x = 0 @@ -141,7 +141,7 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i error = True break if not error: - print("Creating entity at %d, %d" % (x, y)) sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, False, False) + print("Adding obstacle of to (%d, %d) of size (%d, %d)" % (x, y, sprite_image.width, sprite_image.height)) spawned.add(Obstacle(x, y, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite)) return spawned From 05dd0a1cfdf9b950ae45f41e947e53275aa2d269 Mon Sep 17 00:00:00 2001 From: William Hubbard <75867393+Markil3@users.noreply.github.com> Date: Sun, 5 Feb 2023 14:04:42 -0700 Subject: [PATCH 3/3] Fixes some debug code. --- sprites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sprites.py b/sprites.py index 4f4a9ed..a57f250 100644 --- a/sprites.py +++ b/sprites.py @@ -124,7 +124,7 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i spawned = set() max_spawned = randrange(0, difficulty + 1) - while len(entities) < max_spawned and random() < (2 * math.pow(difficulty + 0.01, 2)) / (3 * math.pow(difficulty + 0.01, 2) + 20 * (difficulty + 0.01)) + 0.05: + while len(spawned) < max_spawned and random() < (2 * math.pow(difficulty + 0.01, 2)) / (3 * math.pow(difficulty + 0.01, 2) + 20 * (difficulty + 0.01)) + 0.05: sprite_image = OBSTACLES[randrange(0, min((difficulty + 1) * 3, len(OBSTACLES)))] if sprite_image.width >= thumby.display.width: x = 0