Merge pull request #11 from Markil3/collectibles

Collectibles
This commit is contained in:
William Hubbard
2023-02-05 15:57:53 -07:00
committed by GitHub
3 changed files with 60 additions and 9 deletions

View File

@@ -11,7 +11,7 @@ from random import randrange
from Games.Throot.artrepository import * from Games.Throot.artrepository import *
from Games.Throot.player import Player, Camera from Games.Throot.player import Player, Camera
from Games.Throot.map import ObstacleSlice, OBSTACLE_SLICES from Games.Throot.map import ObstacleSlice, OBSTACLE_SLICES
from Games.Throot.sprites import Obstacle, update_entities from Games.Throot.sprites import Entity, Obstacle, Collectible, update_entities
from Games.Throot.constants import * from Games.Throot.constants import *
@@ -92,12 +92,19 @@ class GameLoop(Room):
self.waterx = float(0) 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): def start(self, event):
self.entities.clear() self.entities.clear()
self.entities.add(
self.seed
)
self.current_obstacle_slice = OBSTACLE_SLICES[0] self.current_obstacle_slice = OBSTACLE_SLICES[0]
self.player = Player() self.player = Player()
self.player.yspeed = 10 self.player.yspeed = 10
self.score = 0 self.score = 'score' in event and event['score'] or 0
self.level = 'level' in event and event['level'] or 0
self.finish_depth = 100 + 5 * self.level ** 2 self.finish_depth = 100 + 5 * self.level ** 2
self.camera = Camera(self.player) self.camera = Camera(self.player)
@@ -177,14 +184,21 @@ class GameLoop(Room):
'score': self.score, 'score': self.score,
'level': self.level + 1 'level': self.level + 1
} }
elif isinstance(entity, Collectible):
self.score += entity.score
to_remove.add(entity)
self.entities -= to_remove self.entities -= to_remove
self.score = int(abs(self.player.y) * 10) self.score += int(abs(self.player.ysub - self.player.prev_y) * 10)
# finish level # finish level
if self.player.y > self.finish_depth + int(thumby.display.height * 1.5): if self.player.y > self.finish_depth + int(thumby.display.height * 1.5):
self.score += 20
self.level += 1 self.level += 1
self.start({}) self.start({
'score': self.score,
'level': self.level
})
thumby.display.fill(0) thumby.display.fill(0)

View File

@@ -2,6 +2,27 @@ from collections import namedtuple
SpriteImage = namedtuple("SpriteImage", ["image", "collision", "width", "height"]) 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 # BITMAP: width: 8, height: 8
skull = SpriteImage( skull = SpriteImage(
bytearray([0,28,102,126,38,30,28,0]), bytearray([0,28,102,126,38,30,28,0]),

View File

@@ -5,7 +5,7 @@ import thumby
from Games.Throot.map import ObstacleSlice from Games.Throot.map import ObstacleSlice
from Games.Throot.player import Player, Camera from Games.Throot.player import Player, Camera
from Games.Throot.artrepository import OBSTACLES from Games.Throot.artrepository import *
class Entity: class Entity:
""" """
@@ -67,7 +67,7 @@ class Entity:
True if the game should keep this entity, false if the game should drop True if the game should keep this entity, false if the game should drop
this entity from memory. this entity from memory.
""" """
return False return True
def render(self, tpf: float, camera: Camera) -> bool: def render(self, tpf: float, camera: Camera) -> bool:
""" """
@@ -97,6 +97,14 @@ class Obstacle(Entity):
def update(self, tpf: float, player: Player, camera: Camera) -> bool: def update(self, tpf: float, player: Player, camera: Camera) -> bool:
return True 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_entities(entities, difficulty: int, current_depth: int, prev_depth: int): def update_entities(entities, difficulty: int, current_depth: int, prev_depth: int):
""" """
@@ -125,6 +133,14 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i
spawned = set() spawned = set()
max_spawned = randrange(0, difficulty + 1) max_spawned = 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: 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)))] sprite_image = OBSTACLES[randrange(0, min((difficulty + 1) * 3, len(OBSTACLES)))]
if sprite_image.width >= thumby.display.width: if sprite_image.width >= thumby.display.width:
x = 0 x = 0
@@ -142,5 +158,5 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i
break break
if not error: if not error:
sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, False, False) 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)) spawned.add(Clazz(x, y, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite))
return spawned return spawned