11 Commits

Author SHA1 Message Date
Neal Finger
7e454cd8e4 Game Manager
merge of game manager,  Gameloop and player animation. Animation is brocken.
2023-02-04 20:24:16 -07:00
William Hubbard
c356965c2d Adds obstacles and obstacles at once. 2023-02-04 18:05:08 -07:00
William Hubbard
1dbab66aa1 Merge branch 'master' into sprite-generator 2023-02-04 16:46:21 -07:00
William Hubbard
90b4e0f8d5 Merge branch 'master' of https://github.com/Markil3/Throot 2023-02-04 16:46:00 -07:00
William Hubbard
803b09e1b6 Adds an obstacle system 2023-02-04 16:45:36 -07:00
Neal Finger
2240e42640 Merge pull request #3 from Markil3/player_contoller
Update player_animations.py
2023-02-04 16:27:13 -07:00
Neal Finger
b79f0fa704 Update player_animations.py
player movement acceleration
2023-02-04 16:25:05 -07:00
Neal Finger
21deb06d84 Merge pull request #2 from Markil3/player_animations
Create player_animations.py
2023-02-04 14:39:18 -07:00
William Hubbard
0860df7cea Merge branch 'master' into sprite-generator 2023-02-04 11:16:38 -07:00
William Hubbard
d69ea1b6c7 Repairs some art repository bugs 2023-02-04 11:03:06 -07:00
William Hubbard
a218536601 Adds an obstacle generation system
This system is responsible for generating obstacles and moving them on-screen as the player moves in the world.
2023-02-04 10:49:04 -07:00
6 changed files with 354 additions and 46 deletions

131
Throot.py Normal file
View File

@@ -0,0 +1,131 @@
import time
import math
import thumby
import Games.Throot.map
import Games.Throot.sprites
from random import randrange
from Games.Throot.artrepository import OBSTACLES
from Games.Throot.player import Player
from Games.Throot.map import ObstacleSlice, OBSTACLE_SLICES
from Games.Throot.sprites import update_entities
from Games.Throot.constants import CONST_FPS
class GameManager:
EVENT_ROOM_CHANGE = {'start': 0, 'main': 1, 'end': 2, 'next': -1}
def __init__(self, *rooms):
self.rooms = rooms
self.room_index = 0
def start(self):
# Set the FPS (without this call, the default fps is 30)
thumby.display.setFPS(CONST_FPS)
def room_goto(self, index):
if index < 0:
self.room_index = min(self.room_index + 1, len(self.rooms))
else:
self.room_index = index
self.rooms[self.room_index].start()
def restart(self):
self.room_index = 0
def update(self, tpf):
event = self.rooms[self.room_index].update(tpf)
thumby.display.update()
if event:
self.room_goto(self.EVENT_ROOM_CHANGE[event])
class Room:
def start(self):
t0 = time.ticks_ms() # Get time (ms)
thumby.display.fill(0) # Fill canvas to black
def update(self, tpf):
pass
class RoomTitle(Room):
def update(self, tpf):
super().update(tpf)
if thumby.inputPressed():
return 'main'
# draw title sprite
title_text = 'press a to start'
chr_len = 5
x = int(round(thumby.display.width / 2 - (chr_len * len(title_text)) / 2))
y = int(round(thumby.display.height / 2 - chr_len / 2))
buff = 2
w = chr_len * len(title_text) + buff
h = chr_len + buff
thumby.display.drawFilledRectangle(x - buff, y - buff, w, h, 1)
thumby.display.drawText(title_text, x, y, 0)
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 = Player()
def get_obstacle_slice(self):
return OBSTACLE_SLICES[randrange(0, len(OBSTACLE_SLICES))]
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
if 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
self.entities |= update_entities(self.current_obstacle_slice, self.current_depth, prev_depth)
# Updates the entities
to_remove = set()
for entity in self.entities:
if not entity.update(tpf, self.current_depth, prev_depth):
to_remove.add(entity)
self.entities -= to_remove
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.current_depth, prev_depth)
def main():
game = GameManager(RoomTitle(), GameLoop())
game.start()
prev_time = 0
while True:
t0 = time.ticks_ms()
tps = (t0 - prev_time) / 1000
#loop.update(tps)
game.update(tps)
prev_time = t0
main()

View File

@@ -1,4 +1,4 @@
from collections.namedtuple
from collections import namedtuple
SpriteImage = namedtuple("SpriteImage", ["image", "collision", "width", "height"])
@@ -130,7 +130,7 @@ leaf = SpriteImage(
16
)
UNDERGROUND_OBSTACLES = [skull, pizza, boulder, ball, steve, mathroot, robot, dinoskull, dinojaw, dinospine, bone, bigdinohead, bigboneddiag, bigdoritotoobstacle]
OVERGROUND_OBSTACLES = [spaveinv, spaceinv2, leaf]
UNDERGROUND_OBSTACLES = [skull, pizza, boulder, ball, steve, mathroot, robot, dinoskull, dinojaw, dinospine, bone, bigdinohead, bigboneddiag, bigdoritoobstacle]
OVERGROUND_OBSTACLES = [spaceinv, spaceinv2, leaf]
OBSTACLES = UNDERGROUND_OBSTACLES + OVERGROUND_OBSTACLES

1
constants.py Normal file
View File

@@ -0,0 +1 @@
CONST_FPS = 60

37
map.py Normal file
View File

@@ -0,0 +1,37 @@
import thumby
class ObstacleSlice:
"""
The game world is divided up into distinct "slices," where each slice is a predefined set of
obstacles in specific positions. These slices are then put forward in random order
"""
"""
How many units an obstacle slice is high
"""
height=max(80, thumby.display.height * 2)
def __init__(self, obstacle_map, difficulty: int):
"""
Creates an obstacle slice
:param obstacle_map:
A two-dimensional list description what obstacles are here. Each tuple describes a
single obstacle, its x, and y position, the obstacle type, the x mirror, and the y
mirror.
:param difficulty:
The difficulty of the game. This influences when this slice appears.
"""
self.obstacle_map = obstacle_map
self.difficulty = difficulty
OBSTACLE_SLICES = [
ObstacleSlice(
[
(10, 0, 0, 0, 0),
(53, 30, 1, 0, 0),
(5, 45, 0, 0, 0)
],
0
)
]

View File

@@ -3,11 +3,12 @@ import thumby
import math
import random
from Games.Throot.constants import CONST_FPS
# didplay width 72
# display height 40
# constants
CONST_FPS = 60
CONST_PL_SCREEN_Y = int((0.5) * thumby.display.height)
CONST_INP_MOVE_LEFT = thumby.buttonL
@@ -21,41 +22,60 @@ class Player:
self.x = int(self.xsub)
self.y = int(self.ysub)
self.yspeed = 32 / CONST_FPS
self.movespeed = 32 / CONST_FPS
self.isRoot = 1
self.isdecend = 1
self.xspeed = 32 / CONST_FPS
self.yspeed = 32 / CONST_FPS
self.accx = 1 / (CONST_FPS * 4)
self.accaccx = self.accx
self.anim = PlayerAnimation(self.x, self.y)
self.debugvisible = True
def update_phys(self):
def update_phys(self, current_depth, prev_depth):
inp = int(0)
if CONST_INP_MOVE_LEFT.pressed():
inp -= 1
if CONST_INP_MOVE_RIGHT.pressed():
inp += 1
# debug visible
if thumby.buttonU.pressed():
self.debugvisible = not self.debugvisible
# clamp to screen
self.xsub = max(min(self.xsub + inp * self.movespeed, thumby.display.width), 0)
self.ysub += self.yspeed
### 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
xx = self.xsub + inp * self.xspeed
# clamp to screen
self.xsub = max(min(xx, thumby.display.width - 1), 0)
### y movement
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, CONST_PL_SCREEN_Y)
def update_draw(self, cam):
def update_draw(self, current_depth, prev_depth):
# debug draw
if self.debugvisible:
thumby.display.setPixel(self.x - cam.x, self.y - cam.y, self.isRoot)
pix_x = self.x
pix_y = int(current_depth - self.y)
thumby.display.setPixel(pix_x, pix_y, self.isdecend)
# animation
self.anim.update_draw(cam, self.isRoot)
self.anim.update_draw(current_depth, prev_depth, self.isdecend)
class Camera:
@@ -125,8 +145,10 @@ class PlayerAnimation:
# plworldy + random.randrange(-rad, rad)))
# move last pos
self.poslist[-1] = (plworldx + random.randrange(-rad, rad),
plworldy + random.randrange(-rad, rad))
self.poslist[-1] = (
plworldx + random.randrange(-rad, rad),
plworldy + random.randrange(-rad, rad)
)
# new pos is perfectly on player
self.poslist.append((plworldx, plworldy))
@@ -135,39 +157,19 @@ 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:
self.poslist.pop(0)
#if self.poslist[1][self.POS_Y] < plworldy - plscreeny:
# self.poslist.pop(0)
#print(len(self.poslist))
def update_draw(self, cam, isRoot=1):
def update_draw(self, current_depth, prev_depth, isdecend=1):
### draw line from first position to next
for i in range(len(self.poslist) - 1):
thumby.display.drawLine(self.poslist[i][self.POS_X] - cam.x, self.poslist[i][self.POS_Y] - cam.y,
self.poslist[i + 1][self.POS_X] - cam.x, self.poslist[i + 1][self.POS_Y] - cam.y, isRoot)
### start
# Set the FPS (without this call, the default fps is 30)
thumby.display.setFPS(CONST_FPS)
pl = Player()
cam = Camera(pl)
print("game ready")
### game loop
while(True):
t0 = time.ticks_ms() # Get time (ms)
thumby.display.fill(0) # Fill canvas to black
# update physics
pl.update_phys()
cam.update_phys(CONST_PL_SCREEN_Y)
# update drawing
pl.update_draw(cam)
thumby.display.update()
thumby.display.drawLine(
int(self.poslist[i][self.POS_X]),
int(current_depth - self.poslist[i][self.POS_Y]),
int(self.poslist[i + 1][self.POS_X]),
int(current_depth - self.poslist[i + 1][self.POS_Y]),
isdecend
)

137
sprites.py Normal file
View File

@@ -0,0 +1,137 @@
from random import randint
import thumby
from Games.Throot.map import ObstacleSlice
from Games.Throot.artrepository import OBSTACLES
class Entity:
"""
An entity exists within the game world. It has coordinates, bounds, and optionally collision and sprite
"""
def __init__(self, x: int = 0, y: int = 0, width: int = 8, height: int = 8, collision: bytearray = None, sprite: thumby.Sprite = None):
self.x = x
self.y = y
self.sprite = sprite
self.collision = collision
self.width = width
self.height = height
def intersects_pixel(self, loc_x: int, loc_y: int) -> bool:
"""
Checks to see if this entity intersects a location in the game world.
:param loc_x:
An global x location within the game world.
:param loc_y:
A global y position within the game world.
:return:
True if the collision shape of this entity intersects a point in the world,
False otherwise.
"""
if not self.collision:
return False
if loc_x < self.x or loc_x >= self.x + self.width or loc_y < self.y or loc_y >= self.y + self.height:
return False
rel_x = loc_x - self.x
rel_y = loc_y - self.y
return bool((self.collision[rel_x] >> (self.height - rel_y)) & 1)
def can_render(self, current_depth: int) -> bool:
"""
Checks to see if this sprite is within the game screen.
:param current_depth:
The current depth, as measured from the top of the screen. We can display anything
up to the screen height down from this number.
: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
def update(self, tpf: float, current_depth: int, prev_depth: int) -> bool:
"""
Updates the state of the entity based on the game state.
:param tpf:
The number of seconds since the last tick.
:param current_depth:
The current depth the game is on.
:param prev_depth:
The depth the game was on last tick.
:return:
True if the game should keep this entity, false if the game should drop
this entity from memory.
"""
return False
def render(self, tpf: float, current_depth: int, prev_depth: int) -> bool:
"""
Sets the position of the sprite and draws it.
:param tpf:
The number of seconds since the last tick.
:param current_depth:
The current depth the game is on.
:param prev_depth:
The depth the game was on last tick.
:return:
True if the game should actually draw the sprite, false the sprite
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):
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_entities(current_slice: ObstacleSlice, current_depth: int, prev_depth: int):
"""
Checks the current game state and generates new sprites based on said state.
:param current_slice:
The current obstacle slice we are using. Once the bottom of the slice displays, we switch
to a new slice.
:param current_depth:
The global world depth we are at. This is modulused by the slice height to determine how
far through the slice we are.
:param prev_depth:
The depth we were at during the last tick. This helps make sure we don't miss spawning
entities
:return:
A list of entities to generate this tick.
"""
if current_depth == prev_depth:
# If we haven't moved, we don't need to spawn anything
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 slice_prev_y > slice_y:
slice_prev_y -= ObstacleSlice.height
spawned = set()
for entity in current_slice.obstacle_map:
# The position the entity should spawn at, relative to the slice top
entity_y = entity[1]
if entity_y >= slice_prev_y and entity_y < slice_y:
sprite_image = OBSTACLES[entity[2]]
sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, entity[3], entity[4])
spawned.add(Obstacle(entity[0], entity_y + current_depth // ObstacleSlice.height * ObstacleSlice.height, sprite_image.width, sprite_image.height, sprite_image.collision, sprite))
return spawned