Changes the obstacle generation system.
More random, less level design needed.
This commit is contained in:
@@ -109,7 +109,7 @@ class GameLoop(Room):
|
|||||||
self.player.update_phys(self.current_depth, prev_depth)
|
self.player.update_phys(self.current_depth, prev_depth)
|
||||||
|
|
||||||
# Generate new entities as needed
|
# Generate new entities as needed
|
||||||
self.entities |= update_entities(self.current_obstacle_slice, self.current_depth, prev_depth)
|
self.entities |= update_entities(self.entities, 5, self.current_depth, prev_depth)
|
||||||
|
|
||||||
# Updates the entities
|
# Updates the entities
|
||||||
to_remove = set()
|
to_remove = set()
|
||||||
|
|||||||
41
sprites.py
41
sprites.py
@@ -1,4 +1,5 @@
|
|||||||
from random import randint
|
import math
|
||||||
|
from random import randrange, random
|
||||||
|
|
||||||
import thumby
|
import thumby
|
||||||
|
|
||||||
@@ -101,7 +102,7 @@ class Obstacle(Entity):
|
|||||||
return self.can_render(current_depth)
|
return self.can_render(current_depth)
|
||||||
|
|
||||||
|
|
||||||
def update_entities(current_slice: ObstacleSlice, current_depth: int, prev_depth: int):
|
def update_entities(entities, difficulty: int, current_depth: int, prev_depth: int):
|
||||||
"""
|
"""
|
||||||
Checks the current game state and generates new sprites based on said state.
|
Checks the current game state and generates new sprites based on said state.
|
||||||
|
|
||||||
@@ -118,24 +119,28 @@ def update_entities(current_slice: ObstacleSlice, current_depth: int, prev_depth
|
|||||||
:return:
|
:return:
|
||||||
A list of entities to generate this tick.
|
A list of entities to generate this tick.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
OBSTACLE_BUFFER = 10
|
||||||
|
|
||||||
if current_depth == prev_depth:
|
if current_depth == prev_depth:
|
||||||
# If we haven't moved, we don't need to spawn anything
|
# If we haven't moved, we don't need to spawn anything
|
||||||
return set()
|
return set()
|
||||||
# Local y relative to the top of the slice
|
|
||||||
slice_y = (current_depth) % ObstacleSlice.height
|
|
||||||
slice_prev_y = (prev_depth) % ObstacleSlice.height
|
|
||||||
if prev_depth < current_depth and slice_prev_y > slice_y:
|
|
||||||
slice_prev_y -= ObstacleSlice.height
|
|
||||||
elif current_depth < prev_depth and slice_y > slice_prev_y:
|
|
||||||
slice_prev_y += ObstacleSlice.height
|
|
||||||
min_y = min(slice_prev_y, slice_y)
|
|
||||||
max_y = max(slice_prev_y, slice_y)
|
|
||||||
spawned = set()
|
spawned = set()
|
||||||
for entity in current_slice.obstacle_map:
|
max_spawned = randrange(0, difficulty + 1)
|
||||||
# The position the entity should spawn at, relative to the slice top
|
while len(spawned) < max_spawned and random() < (2 * math.pow(difficulty, 2)) / (3 * math.pow(difficulty, 2) + 20 * difficulty):
|
||||||
entity_y = entity[1]
|
sprite_image = OBSTACLES[randrange(0, len(OBSTACLES))]
|
||||||
if entity_y >= min_y and entity_y < max_y:
|
if sprite_image.width >= thumby.display.width:
|
||||||
sprite_image = OBSTACLES[entity[2]]
|
x = 0
|
||||||
sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, entity[3], entity[4])
|
else:
|
||||||
spawned.add(Obstacle(entity[0], entity_y + current_depth // ObstacleSlice.height * ObstacleSlice.height, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite))
|
x = randrange(0, thumby.display.width - sprite_image.width)
|
||||||
|
y = current_depth
|
||||||
|
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:
|
||||||
|
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
|
return spawned
|
||||||
|
|||||||
Reference in New Issue
Block a user