Compare commits
2 Commits
random-obs
...
end-game
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4e87421bb | ||
|
|
e73eb5a765 |
52
Throot.py
52
Throot.py
@@ -11,11 +11,9 @@ from Games.Throot.artrepository import OBSTACLES
|
|||||||
from Games.Throot.player import Player
|
from Games.Throot.player import Player
|
||||||
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 Obstacle, update_entities
|
||||||
from Games.Throot.constants import CONST_FPS
|
from Games.Throot.constants import *
|
||||||
|
|
||||||
class GameManager:
|
class GameManager:
|
||||||
EVENT_ROOM_CHANGE = {'start': 0, 'main': 1, 'end': 2, 'next': -1}
|
|
||||||
|
|
||||||
def __init__(self, *rooms):
|
def __init__(self, *rooms):
|
||||||
self.rooms = rooms
|
self.rooms = rooms
|
||||||
self.room_index = 0
|
self.room_index = 0
|
||||||
@@ -24,13 +22,14 @@ class GameManager:
|
|||||||
# Set the FPS (without this call, the default fps is 30)
|
# Set the FPS (without this call, the default fps is 30)
|
||||||
thumby.display.setFPS(CONST_FPS)
|
thumby.display.setFPS(CONST_FPS)
|
||||||
|
|
||||||
def room_goto(self, index):
|
def room_goto(self, event):
|
||||||
|
index = event['room']
|
||||||
if index < 0:
|
if index < 0:
|
||||||
self.room_index = min(self.room_index + 1, len(self.rooms))
|
self.room_index = min(self.room_index + 1, len(self.rooms))
|
||||||
else:
|
else:
|
||||||
self.room_index = index
|
self.room_index = index
|
||||||
|
|
||||||
self.rooms[self.room_index].start()
|
self.rooms[self.room_index].start(event)
|
||||||
|
|
||||||
def restart(self):
|
def restart(self):
|
||||||
self.room_index = 0
|
self.room_index = 0
|
||||||
@@ -41,11 +40,11 @@ class GameManager:
|
|||||||
thumby.display.update()
|
thumby.display.update()
|
||||||
|
|
||||||
if event:
|
if event:
|
||||||
self.room_goto(self.EVENT_ROOM_CHANGE[event])
|
self.room_goto(event)
|
||||||
|
|
||||||
|
|
||||||
class Room:
|
class Room:
|
||||||
def start(self):
|
def start(self, event):
|
||||||
t0 = time.ticks_ms() # Get time (ms)
|
t0 = time.ticks_ms() # Get time (ms)
|
||||||
thumby.display.fill(0) # Fill canvas to black
|
thumby.display.fill(0) # Fill canvas to black
|
||||||
|
|
||||||
@@ -58,7 +57,9 @@ class RoomTitle(Room):
|
|||||||
super().update(tpf)
|
super().update(tpf)
|
||||||
|
|
||||||
if thumby.inputPressed():
|
if thumby.inputPressed():
|
||||||
return 'main'
|
return {
|
||||||
|
'room': ROOM_MAIN
|
||||||
|
}
|
||||||
|
|
||||||
# draw title sprite
|
# draw title sprite
|
||||||
title_text = 'press a to start'
|
title_text = 'press a to start'
|
||||||
@@ -85,6 +86,13 @@ class GameLoop(Room):
|
|||||||
|
|
||||||
self.score = 0
|
self.score = 0
|
||||||
|
|
||||||
|
def start(self, event):
|
||||||
|
self.current_depth = 0
|
||||||
|
self.entities.clear()
|
||||||
|
self.current_obstacle_slice = OBSTACLE_SLICES[0]
|
||||||
|
self.player = Player()
|
||||||
|
self.score = 0
|
||||||
|
|
||||||
def get_obstacle_slice(self):
|
def get_obstacle_slice(self):
|
||||||
return OBSTACLE_SLICES[randrange(0, len(OBSTACLE_SLICES))]
|
return OBSTACLE_SLICES[randrange(0, len(OBSTACLE_SLICES))]
|
||||||
|
|
||||||
@@ -118,7 +126,10 @@ class GameLoop(Room):
|
|||||||
to_remove.add(entity)
|
to_remove.add(entity)
|
||||||
elif entity.intersects_pixel(self.player.xsub, self.player.ysub):
|
elif entity.intersects_pixel(self.player.xsub, self.player.ysub):
|
||||||
if isinstance(entity, Obstacle):
|
if isinstance(entity, Obstacle):
|
||||||
print("Hit!")
|
return {
|
||||||
|
'room': ROOM_END,
|
||||||
|
'score': self.score
|
||||||
|
}
|
||||||
self.entities -= to_remove
|
self.entities -= to_remove
|
||||||
|
|
||||||
self.score = int(self.current_depth * 10)
|
self.score = int(self.current_depth * 10)
|
||||||
@@ -130,10 +141,29 @@ class GameLoop(Room):
|
|||||||
self.player.update_draw(self.player.y)
|
self.player.update_draw(self.player.y)
|
||||||
|
|
||||||
thumby.display.drawText(str(self.score), thumby.display.width - (len(str(self.score)) + 2) * 5, 1, 1)
|
thumby.display.drawText(str(self.score), thumby.display.width - (len(str(self.score)) + 2) * 5, 1, 1)
|
||||||
thumby.display.update()
|
|
||||||
|
class EndRoom(Room):
|
||||||
|
def __init__(self):
|
||||||
|
self.score = 0
|
||||||
|
|
||||||
|
def start(self, event):
|
||||||
|
self.score = event['score']
|
||||||
|
# Used to make sure that we aren't triggering from input mean't for the game.
|
||||||
|
self.still_pressed = thumby.inputPressed()
|
||||||
|
|
||||||
|
def update(self, tpf):
|
||||||
|
if not thumby.inputPressed():
|
||||||
|
self.still_pressed = False
|
||||||
|
elif not self.still_pressed and thumby.inputPressed():
|
||||||
|
return {
|
||||||
|
'room': ROOM_START
|
||||||
|
}
|
||||||
|
thumby.display.fill(0)
|
||||||
|
thumby.display.drawText("Final Score:", (thumby.display.width - 60) // 2, 1, 1)
|
||||||
|
thumby.display.drawText(str(self.score), (thumby.display.width - (len(str(self.score)) * 5)) // 2, 9, 1)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
game = GameManager(RoomTitle(), GameLoop())
|
game = GameManager(RoomTitle(), GameLoop(), EndRoom())
|
||||||
game.start()
|
game.start()
|
||||||
|
|
||||||
prev_time = 0
|
prev_time = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user