Compare commits
1 Commits
collectibl
...
water
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf910238b9 |
109
Throot.py
109
Throot.py
@@ -7,14 +7,14 @@ import Games.Throot.map
|
||||
import Games.Throot.sprites
|
||||
|
||||
from random import randrange
|
||||
|
||||
from Games.Throot.artrepository import *
|
||||
from Games.Throot.player import Player, Camera
|
||||
from Games.Throot.player import Player
|
||||
from Games.Throot.map import ObstacleSlice, OBSTACLE_SLICES
|
||||
from Games.Throot.sprites import Entity, Obstacle, Collectible, update_entities
|
||||
from Games.Throot.sprites import Obstacle, update_entities
|
||||
from Games.Throot.constants import *
|
||||
|
||||
|
||||
|
||||
class GameManager:
|
||||
def __init__(self, *rooms):
|
||||
self.rooms = rooms
|
||||
@@ -78,13 +78,13 @@ class RoomTitle(Room):
|
||||
|
||||
class GameLoop(Room):
|
||||
def __init__(self):
|
||||
self.current_depth = 0
|
||||
self.diving_velocity = 10
|
||||
self.diving_accel = 0
|
||||
self.current_obstacle_slice = OBSTACLE_SLICES[0]
|
||||
self.entities = set()
|
||||
|
||||
self.player = None
|
||||
self.camera = None
|
||||
self.player = Player()
|
||||
|
||||
self.score = 0
|
||||
self.level = 0
|
||||
@@ -92,106 +92,79 @@ class GameLoop(Room):
|
||||
|
||||
self.waterx = float(0)
|
||||
|
||||
seed_sprite = thumby.Sprite(seed.width, seed.height, seed.image, 0, 0, 0, False, False)
|
||||
self.seed = Entity((thumby.display.width - seed.width) // 2, 0, seed.width, seed.height, None, seed_sprite)
|
||||
|
||||
def start(self, event):
|
||||
self.current_depth = 0
|
||||
self.entities.clear()
|
||||
self.entities.add(
|
||||
self.seed
|
||||
)
|
||||
self.current_obstacle_slice = OBSTACLE_SLICES[0]
|
||||
self.player = Player()
|
||||
self.player.yspeed = 10
|
||||
self.score = 'score' in event and event['score'] or 0
|
||||
self.level = 'level' in event and event['level'] or 0
|
||||
self.score = 0
|
||||
|
||||
self.finish_depth = 100 + 5 * self.level ** 2
|
||||
self.camera = Camera(self.player)
|
||||
|
||||
|
||||
def get_obstacle_slice(self):
|
||||
return OBSTACLE_SLICES[randrange(0, len(OBSTACLE_SLICES))]
|
||||
|
||||
def draw_sky(self):
|
||||
sky_x = 0
|
||||
sky_y = -thumby.display.height
|
||||
sky_width = thumby.display.width
|
||||
sky_height = thumby.display.height
|
||||
if self.camera.entity_in_camera(sky_x, sky_y, sky_width, sky_height):
|
||||
sky_x, sky_y = self.camera.relative_to_camera(sky_x, sky_y)
|
||||
thumby.display.drawFilledRectangle(int(sky_x), int(sky_y), int(sky_width), int(sky_height), 1)
|
||||
sky_x = 0
|
||||
sky_y = 0
|
||||
sky_width = thumby.display.width
|
||||
sky_height = thumby.display.height
|
||||
if self.camera.entity_in_camera(sky_x, sky_y, sky_width, sky_height):
|
||||
sky_x, sky_y = self.camera.relative_to_camera(sky_x, sky_y)
|
||||
thumby.display.drawFilledRectangle(int(sky_x), int(sky_y), int(sky_width), int(sky_height), 0)
|
||||
|
||||
def draw_water(self):
|
||||
# bookwater
|
||||
|
||||
# exit if depth is less than 2 screens of the finish depth
|
||||
if self.player.y + thumby.display.height < self.finish_depth:
|
||||
if self.current_depth + thumby.display.height * 2 < self.finish_depth:
|
||||
return
|
||||
|
||||
self.waterx += sprite_water.width / CONST_FPS
|
||||
if self.waterx > sprite_water.width:
|
||||
self.waterx = 0
|
||||
|
||||
to_screeny = int(thumby.display.height - self.current_depth)
|
||||
|
||||
# water surface.
|
||||
for i in range(math.ceil(thumby.display.width / sprite_water.width) + 1):
|
||||
|
||||
x, y = self.camera.relative_to_camera(
|
||||
(int(self.waterx) - sprite_water.width) + i * sprite_water.width,
|
||||
self.finish_depth
|
||||
)
|
||||
|
||||
thumby.display.blit(
|
||||
sprite_water[0],
|
||||
int(x), int(y),
|
||||
sprite_water.width, sprite_water.height,
|
||||
0,
|
||||
0, 0
|
||||
(int(self.waterx) - sprite_water.width) + i * sprite_water.width,
|
||||
self.finish_depth - sprite_water.height + to_screeny, sprite_water.width, sprite_water.height,
|
||||
0, 0, 0
|
||||
)
|
||||
|
||||
# water below surface
|
||||
x, y = self.camera.relative_to_camera(
|
||||
0,
|
||||
self.finish_depth
|
||||
)
|
||||
|
||||
thumby.display.drawFilledRectangle(
|
||||
int(x), int(y) + sprite_water.height,
|
||||
0, self.finish_depth + to_screeny,
|
||||
thumby.display.width, self.finish_depth + thumby.display.height * 3,
|
||||
1,
|
||||
)
|
||||
|
||||
# win text
|
||||
if self.player.y > self.finish_depth + (thumby.display.height // 2):
|
||||
thumby.display.drawText('water reach!', 0, thumby.display.height // 4, 0)
|
||||
thumby.display.drawText(f' stage {self.level + 1}', 0, thumby.display.height // 4 + 9, 0)
|
||||
if self.current_depth > self.finish_depth + (thumby.display.height // 2):
|
||||
thumby.display.drawText('water reach!', 0, thumby.display.height // 2, 0)
|
||||
|
||||
def update(self, tpf):
|
||||
"""
|
||||
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))
|
||||
|
||||
self.camera.update_phys()
|
||||
#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)
|
||||
|
||||
# Generate new entities as needed
|
||||
if abs(self.player.y) < abs(self.finish_depth):
|
||||
self.entities |= update_entities(self.entities, self.level, self.player.ysub, self.player.prev_y)
|
||||
else:
|
||||
self.entities.clear()
|
||||
self.entities |= update_entities(self.entities, 5, self.current_depth, prev_depth)
|
||||
|
||||
# Updates the entities
|
||||
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:
|
||||
if not entity.update(tpf, self.current_depth, prev_depth):
|
||||
to_remove.add(entity)
|
||||
elif entity.intersects_pixel(self.player.xsub, self.player.ysub):
|
||||
if isinstance(entity, Obstacle):
|
||||
@@ -200,31 +173,25 @@ class GameLoop(Room):
|
||||
'score': self.score,
|
||||
'level': self.level + 1
|
||||
}
|
||||
elif isinstance(entity, Collectible):
|
||||
self.score += entity.score
|
||||
to_remove.add(entity)
|
||||
self.entities -= to_remove
|
||||
|
||||
self.score += int(abs(self.player.ysub - self.player.prev_y) * 10)
|
||||
self.score = int(self.current_depth * 10)
|
||||
|
||||
# finish level
|
||||
if self.player.y > self.finish_depth + int(thumby.display.height * 1.5):
|
||||
self.score += 20
|
||||
if self.current_depth > self.finish_depth + int(thumby.display.height * 1.5):
|
||||
self.level += 1
|
||||
self.start({
|
||||
'score': self.score,
|
||||
'level': self.level
|
||||
})
|
||||
self.start({})
|
||||
|
||||
thumby.display.fill(0)
|
||||
self.draw_sky()
|
||||
|
||||
# Draw the entities
|
||||
for entity in self.entities:
|
||||
entity.render(tpf, self.camera)
|
||||
self.player.update_draw(self.camera)
|
||||
entity.render(tpf, self.current_depth, prev_depth)
|
||||
|
||||
self.draw_water()
|
||||
|
||||
self.player.update_draw(self.player.y)
|
||||
|
||||
thumby.display.drawText(str(self.score), thumby.display.width - (len(str(self.score)) + 2) * 5, 1, 1)
|
||||
|
||||
|
||||
|
||||
@@ -2,27 +2,6 @@ from collections import namedtuple
|
||||
|
||||
SpriteImage = namedtuple("SpriteImage", ["image", "collision", "width", "height"])
|
||||
|
||||
seed = SpriteImage(
|
||||
bytearray([8,20,46,46,62,62,46,62,46,12,0]),
|
||||
None,
|
||||
11,
|
||||
7
|
||||
)
|
||||
|
||||
collectibleover = SpriteImage(
|
||||
bytearray([9,4,6,9]),
|
||||
None,
|
||||
4,
|
||||
4
|
||||
)
|
||||
|
||||
collectibleunder = SpriteImage(
|
||||
bytearray([6,11,9,6]),
|
||||
None,
|
||||
4,
|
||||
4
|
||||
)
|
||||
|
||||
# BITMAP: width: 8, height: 8
|
||||
skull = SpriteImage(
|
||||
bytearray([0,28,102,126,38,30,28,0]),
|
||||
|
||||
@@ -4,4 +4,3 @@ ROOM_START = 0
|
||||
ROOM_MAIN = 1
|
||||
ROOM_END = 2
|
||||
ROOM_NEXT = -1
|
||||
|
||||
|
||||
55
player.py
55
player.py
@@ -17,8 +17,6 @@ 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)
|
||||
@@ -28,16 +26,14 @@ class Player:
|
||||
self.isdecend = 1
|
||||
|
||||
self.xspeed = 32 / CONST_FPS
|
||||
self.yspeed = 32
|
||||
self.yspeed = 32 / CONST_FPS
|
||||
self.accx = 1 / (CONST_FPS * 4)
|
||||
self.accaccx = self.accx
|
||||
|
||||
self.anim = PlayerAnimation(self.x, self.y)
|
||||
self.debugvisible = False
|
||||
self.debugvisible = True
|
||||
|
||||
def update_phys(self, tpf, camera: Camera):
|
||||
self.prev_x = self.xsub
|
||||
self.prev_y = self.ysub
|
||||
def update_phys(self, current_depth, prev_depth):
|
||||
inp = int(0)
|
||||
if CONST_INP_MOVE_LEFT.pressed():
|
||||
inp -= 1
|
||||
@@ -62,44 +58,35 @@ class Player:
|
||||
self.xsub = max(min(xx, thumby.display.width - 1), 0)
|
||||
|
||||
### y movement
|
||||
|
||||
self.ysub += self.yspeed * tpf
|
||||
self.ysub = current_depth - (thumby.display.height / 2)
|
||||
|
||||
### pixel positions
|
||||
self.x = int(self.xsub)
|
||||
self.y = int(self.ysub)
|
||||
|
||||
# animation
|
||||
self.anim.update_phys(self.x, self.y, camera)
|
||||
self.anim.update_phys(self.x, self.y, CONST_PL_SCREEN_Y, current_depth)
|
||||
|
||||
def update_draw(self, camera):
|
||||
def update_draw(self, plworldy):
|
||||
# debug draw
|
||||
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)
|
||||
pix_x = self.x
|
||||
pix_y = int(self.y - plworldy + 20)
|
||||
thumby.display.setPixel(pix_x, pix_y, self.isdecend)
|
||||
|
||||
# animation
|
||||
self.anim.update_draw(camera, self.isdecend)
|
||||
self.anim.update_draw(plworldy - 20, 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 entity_in_camera(self, ent_x, ent_y, ent_width = 0, ent_height = 0) -> bool:
|
||||
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)
|
||||
|
||||
def update_phys(self):
|
||||
def update_phys(self, plscreeny):
|
||||
# position player
|
||||
self.y = self.target.y + self.offset
|
||||
self.y = self.target.y - plscreeny
|
||||
|
||||
|
||||
class PlayerAnimation:
|
||||
@@ -114,7 +101,7 @@ class PlayerAnimation:
|
||||
self.debugrandomrange = self.RANDOM_RANGE
|
||||
self.debugposrange = self.POS_RANGE
|
||||
|
||||
def update_phys(self, plworldx, plworldy, camera):
|
||||
def update_phys(self, plworldx, plworldy, plscreeny, current_depth):
|
||||
|
||||
#print(plworldx, plworldy, plscreeny, current_depth)
|
||||
|
||||
@@ -136,7 +123,7 @@ class PlayerAnimation:
|
||||
# if plworldy < self.poslist[-1][self.POS_Y] + self.debugposrange:
|
||||
# return
|
||||
|
||||
if plworldy < self.poslist[-1][self.POS_Y] + self.POS_RANGE:
|
||||
if plworldy < self.poslist[-1][self.POS_Y] + random.randint(1, self.POS_RANGE):
|
||||
return
|
||||
|
||||
### get new position
|
||||
@@ -172,23 +159,21 @@ class PlayerAnimation:
|
||||
# self.poslist.append((plworldx, plworldy))
|
||||
|
||||
### 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]):
|
||||
if self.poslist[1][self.POS_Y] < plworldy - plscreeny:
|
||||
self.poslist.pop(0)
|
||||
|
||||
#print(len(self.poslist))
|
||||
|
||||
def update_draw(self, camera, isdecend=1):
|
||||
def update_draw(self, topleft, prev_depth, 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(x1),
|
||||
int(y1),
|
||||
int(x2),
|
||||
int(y2),
|
||||
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),
|
||||
isdecend
|
||||
)
|
||||
|
||||
56
sprites.py
56
sprites.py
@@ -4,8 +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 *
|
||||
from Games.Throot.artrepository import OBSTACLES
|
||||
|
||||
class Entity:
|
||||
"""
|
||||
@@ -37,9 +36,9 @@ class Entity:
|
||||
return False
|
||||
rel_x = int(loc_x - self.x)
|
||||
rel_y = int(loc_y - self.y)
|
||||
return bool((self.collision[rel_x] >> (rel_y)) & 1)
|
||||
return bool((self.collision[rel_x] >> (self.height - rel_y)) & 1)
|
||||
|
||||
def can_render(self, camera: Camera) -> bool:
|
||||
def can_render(self, current_depth: int) -> bool:
|
||||
"""
|
||||
Checks to see if this sprite is within the game screen.
|
||||
|
||||
@@ -50,9 +49,15 @@ class Entity:
|
||||
:return:
|
||||
True if the sprite shows up on screen in any capacity, false otherwise.
|
||||
"""
|
||||
return camera.entity_in_camera(self.x, self.y, self.width, self.height)
|
||||
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
|
||||
|
||||
def update(self, tpf: float, player: Player, camera: Camera) -> bool:
|
||||
def update(self, tpf: float, current_depth: int, prev_depth: int) -> bool:
|
||||
"""
|
||||
Updates the state of the entity based on the game state.
|
||||
|
||||
@@ -67,9 +72,9 @@ class Entity:
|
||||
True if the game should keep this entity, false if the game should drop
|
||||
this entity from memory.
|
||||
"""
|
||||
return True
|
||||
return False
|
||||
|
||||
def render(self, tpf: float, camera: Camera) -> bool:
|
||||
def render(self, tpf: float, current_depth: int, prev_depth: int) -> bool:
|
||||
"""
|
||||
Sets the position of the sprite and draws it.
|
||||
|
||||
@@ -85,25 +90,16 @@ class Entity:
|
||||
should be hidden.
|
||||
"""
|
||||
if self.sprite:
|
||||
coordinates = camera.relative_to_camera(self.x, self.y)
|
||||
self.sprite.x = coordinates[0]
|
||||
self.sprite.y = coordinates[1]
|
||||
if self.can_render(camera):
|
||||
self.sprite.y = int(self.y - current_depth + thumby.display.height)
|
||||
self.sprite.x = int(self.x)
|
||||
if self.can_render(current_depth):
|
||||
thumby.display.drawSprite(self.sprite)
|
||||
return True
|
||||
return False
|
||||
|
||||
class Obstacle(Entity):
|
||||
def update(self, tpf: float, player: Player, camera: Camera) -> bool:
|
||||
return True
|
||||
|
||||
class Collectible(Entity):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.score = 'score' in kwargs and kwargs['score'] or 10
|
||||
|
||||
def update(self, tpf: float, player: Player, camera: Camera) -> bool:
|
||||
return True
|
||||
def update(self, tpf: float, current_depth: int, prev_depth: int) -> bool:
|
||||
return self.can_render(current_depth)
|
||||
|
||||
|
||||
def update_entities(entities, difficulty: int, current_depth: int, prev_depth: int):
|
||||
@@ -131,26 +127,14 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i
|
||||
return set()
|
||||
|
||||
spawned = set()
|
||||
max_spawned = randrange(0, difficulty + 1)
|
||||
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:
|
||||
if random() < 0.2:
|
||||
Clazz = Collectible
|
||||
if current_depth >= 0:
|
||||
sprite_image = collectibleunder
|
||||
else:
|
||||
sprite_image = collectibleover
|
||||
else:
|
||||
Clazz = Obstacle
|
||||
sprite_image = OBSTACLES[randrange(0, min((difficulty + 1) * 3, len(OBSTACLES)))]
|
||||
if sprite_image.width >= thumby.display.width:
|
||||
x = 0
|
||||
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):
|
||||
@@ -158,5 +142,5 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i
|
||||
break
|
||||
if not error:
|
||||
sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, False, False)
|
||||
spawned.add(Clazz(x, y, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite))
|
||||
spawned.add(Obstacle(x, y, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite))
|
||||
return spawned
|
||||
|
||||
Reference in New Issue
Block a user