Added coins
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
# show extra spaces after certains emojis to align them in the terminal
|
||||||
|
SHOW_EXTRA_EMOJI_SPACES=true
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# show extra spaces after certains emojis to align them in the terminal
|
||||||
|
SHOW_EXTRA_EMOJI_SPACES=true
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
from .stoneBlock import StoneBlock
|
from .stoneBlock import StoneBlock
|
||||||
from .spawnPointBlock import SpawnPointBlock
|
from .spawnPointBlock import SpawnPointBlock
|
||||||
from .finishBlock import FinishBlock
|
from .finishBlock import FinishBlock
|
||||||
|
from .coinBlock import CoinBlock
|
||||||
|
|
||||||
BLOCKS = [
|
BLOCKS = [
|
||||||
StoneBlock,
|
StoneBlock,
|
||||||
SpawnPointBlock,
|
SpawnPointBlock,
|
||||||
FinishBlock,
|
FinishBlock,
|
||||||
|
CoinBlock
|
||||||
]
|
]
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from game.blocks.block import Block
|
||||||
|
from game.gameState import GameState
|
||||||
|
|
||||||
|
class CoinBlock(Block):
|
||||||
|
def __init__(self):
|
||||||
|
self.collected = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def character(self):
|
||||||
|
return '°'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self):
|
||||||
|
return 'coin'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def solid(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def on_collision(self, gameState: GameState):
|
||||||
|
if not self.collected:
|
||||||
|
gameState.add_coin()
|
||||||
|
self.collected = True
|
||||||
|
|
||||||
|
def to_string(self):
|
||||||
|
return " " if self.collected else "🪙" + (" " if os.getenv("SHOW_EXTRA_EMOJI_SPACES", "false").lower() == "true" else "")
|
||||||
+5
-5
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Represents the current state of the game.
|
Represents the current state of the game.
|
||||||
Contains :
|
Contains :
|
||||||
- Player score
|
- Player coins
|
||||||
- stopGame flag
|
- stopGame flag
|
||||||
- Player position
|
- Player position
|
||||||
"""
|
"""
|
||||||
@@ -10,15 +10,12 @@ class GameState:
|
|||||||
PLAYER_VELOCITIES = [*PLAYER_MOVEMENTS, "down"]
|
PLAYER_VELOCITIES = [*PLAYER_MOVEMENTS, "down"]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.score = 0
|
|
||||||
self.playerPosition = (0, 0)
|
self.playerPosition = (0, 0)
|
||||||
self.stopGame = False
|
self.stopGame = False
|
||||||
self.movesCount = 0
|
self.movesCount = 0
|
||||||
self.playerVelocity = -1 # -1 means no velocity
|
self.playerVelocity = -1 # -1 means no velocity
|
||||||
self.gameWon = False
|
self.gameWon = False
|
||||||
|
self.coins = 0
|
||||||
def add_score(self, points):
|
|
||||||
self.score += points
|
|
||||||
|
|
||||||
def set_player_position(self, x, y):
|
def set_player_position(self, x, y):
|
||||||
self.playerPosition = (x, y)
|
self.playerPosition = (x, y)
|
||||||
@@ -44,3 +41,6 @@ class GameState:
|
|||||||
|
|
||||||
def set_game_won(self, won):
|
def set_game_won(self, won):
|
||||||
self.gameWon = won
|
self.gameWon = won
|
||||||
|
|
||||||
|
def add_coin(self):
|
||||||
|
self.coins += 1
|
||||||
@@ -14,11 +14,23 @@ class UserPlayerGameHandler(GameHandler):
|
|||||||
"""
|
"""
|
||||||
Refresh the screen
|
Refresh the screen
|
||||||
"""
|
"""
|
||||||
print("\033[2J\033[H" + self.game.to_string())
|
print("\n\033[2J") # Add margin for stupid vscode terminal
|
||||||
|
|
||||||
|
game_string = self.game.to_string()
|
||||||
|
for i, line in enumerate(game_string.splitlines()):
|
||||||
|
if i == 0:
|
||||||
|
line += "\tCoins: " + str(self.game.gameState.coins)
|
||||||
|
elif i == 1:
|
||||||
|
line += "\tMoves: " + str(self.game.gameState.movesCount)
|
||||||
|
print(line)
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
|
|
||||||
def on_input_request(self) -> str:
|
def on_input_request(self) -> str:
|
||||||
"""
|
"""
|
||||||
Listen for keyboard input, and map it to a gameState PLAYER_MOVEMENTS
|
Listen for keyboard input, and map it to a gameState PLAYER_MOVEMENTS
|
||||||
"""
|
"""
|
||||||
return self.game.gameState.PLAYER_MOVEMENTS[self.KEYBOARD_INPUTS.index(input(f"Enter a movement ({', '.join(self.KEYBOARD_INPUTS)}): "))]
|
user_input = input(f"Enter a movement ({', '.join(self.KEYBOARD_INPUTS)}): ")
|
||||||
|
if user_input not in self.KEYBOARD_INPUTS:
|
||||||
|
print(f"Invalid input: {user_input}. Please enter one of {', '.join(self.KEYBOARD_INPUTS)}.")
|
||||||
|
return self.on_input_request()
|
||||||
|
return self.game.gameState.PLAYER_MOVEMENTS[self.KEYBOARD_INPUTS.index(user_input)]
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from game.game import Game
|
from game.game import Game
|
||||||
from game.gameState import GameState
|
from game.gameState import GameState
|
||||||
from game.handlers import GAME_HANDLERS
|
from game.handlers import GAME_HANDLERS
|
||||||
@@ -5,6 +7,9 @@ from game.map import Map
|
|||||||
from game.mapReader import MapReader
|
from game.mapReader import MapReader
|
||||||
from game.mapSet import MapSet
|
from game.mapSet import MapSet
|
||||||
|
|
||||||
|
# loading variables from .env file
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mapReader = MapReader()
|
mapReader = MapReader()
|
||||||
mapSet = MapSet(mapReader, mapReader.get_map_names())
|
mapSet = MapSet(mapReader, mapReader.get_map_names())
|
||||||
|
|||||||
Reference in New Issue
Block a user