A Python package for simple text-based games.
Project description
PyGameBox
Team Members
Description
PyGameBox is a versatile Python package that simplifies the development of several popular text-based games. It abstracts away the intricate details of game logic, allowing you to effortlessly import and integrate the following games into your own projects:
Getting Started
Prerequisites
Before you start, make sure you have Python 3.7 or higher installed on your system. You can download Python from the official website.
Installation
You can install the package via pip:
pip install pygamebox
Check out pygamebox
package on PYPI: https://pypi.org/project/pygamebox.
Usage
After installing the package, import PyGameBox to your program.
from pygamebox import *
PyGameBox consists of the game classes: GuessTheNumber
, RockPaperScissors
, WordScramble
, Hangman
, and Wordle
.
Note: If you want to implement any one of these games, you can simply import it, instantiate a game object, and call
play()
on the game object. PyGameBox will handle all the game logic.However, if you wish to handle the game logic yourself, PyGameBox also provides you with a host of helper functions designed to make the implementation as simple as use.
Example use case
Check out examples/play.py
to see how to run the games using the pygamebox
package.
You can run the demo using:
python examples/play.py
Guess The Number
from pygamebox import GuessTheNumber
game = GuessTheNumber()
game.play()
Screenshot
Class Attributes
DEFAULT_START
(int
): The default start of the range for the number to guess (inclusive), which is1
.DEFAULT_END
(int
): The default end of the range for the number to guess (inclusive), which is100
.
Methods
-
__init__(self, start: int = DEFAULT_START, end: int = DEFAULT_END) -> None
- Description: Creates an instance the game with a specified start and end range.
- Parameters:
start
(int
, optional): The start of the range for the number to be guessed. Defaults toDEFAULT_START
.end
(int
, optional): The end of the range for the number to be guessed. Defaults toDEFAULT_END
.
-
reset(self) -> None
- Description: Resets the game by generating a new random number and setting the number of guesses to
0
.
- Description: Resets the game by generating a new random number and setting the number of guesses to
-
get_user_guess(self) -> str
- Description: Gets the user's guess as a string.
- Returns: The user's guess as a string.
-
validate_input(self, guess: Union[str, int]) -> bool
- Description: Validates the user's guess.
- Parameters:
guess
(Union[str, int]
): The user's guess.
- Returns:
True
if the guess is valid;False
otherwise.
-
guess(self, user_guess: int) -> int
- Description: Checks if the user's guess is correct.
- Parameters:
user_guess
(int
): The user's guess.
- Returns:
-1
if the guess is too low.1
if the guess is too high.0
if the guess is correct.
-
turn(self, user_guess: Union[str, int]) -> bool
- Description: Runs a turn of the game.
- Parameters:
- user_guess (Union[str, int]): The user's guess.
- Returns:
True
if the user guessed the number,False
otherwise.
-
play(self) -> None
- Description: Plays the game. Takes guesses from the user and checks until the user guesses the correct number.
Rock, Paper, Scissors
from pygamebox import RockPaperScissors
game = RockPaperScissors()
game.play()
Screenshot
Class Attributes
DEFAULT_ROUNDS
(int
): The default number of rounds to play, which is1
.
Methods
__init__(self) -> None
- Description: Creates an instance of the rock, paper, scissors game.
get_user_throw(self) -> int
- Description: Prompts the user to choose a move (Rock, Paper, or Scissors).
- Returns: An
int
(0: Rock, 1: Paper, 2: Scissors).
validate_input(self, user_input: Union[str, int])
-> bool`- Description: Checks if the user's input is valid.
- Parameters:
user_input
(Union[str, int]
): The user's input.
- Returns:
True
if the input is valid (1, 2, or 3).False
otherwise.
throw() -> int
- Description: Generates a random choice for the computer (Rock, Paper, or Scissors).
- Returns: The computer's choice as an
int
(0: Rock, 1: Paper, 2: Scissors).
round(user_throw: int, computer_throw: int) -> int
- Description: Determines the result of a round based on the user's and computer's choices.
- Parameters:
user_throw
(int
): The user's choice.computer_throw
(int
): The computer's choice.
- Returns:
1
if the user wins.-1
if the computer wins.0
if it's a draw.
validate_round_input(rounds: int) -> bool
- Description: Validates the user's input to ensure it's a valid number of rounds (1 or greater).
- Parameters:
rounds
(int
): The number of rounds to play.
- Returns:
True
if the input is valid,False
otherwise.
play(self, rounds: int = DEFAULT_ROUNDS) -> None
- Description: Runs the Rock, Paper, Scissors game for a specified number of rounds.
- Parameters:
rounds
(int
, optional): The number of rounds to play (defaults toDEFAULT_ROUNDS
).
Word Scramble
from pygamebox import WordScramble
game = WordScramble()
game.play()
Screenshot
Class Attributes
DEFAULT_DIFFICULTY
(str
): The default difficulty level, which is'easy'
.DEFAULT_ATTEMPTS
(int
): The default number of attempts allowed, which is3
.
Methods:
__init__(self) -> None
- Description: Creates an instance of the Word Scramble game by importing list of words for different difficulty levels.
reset(self, difficulty: str = DEFAULT_DIFFICULTY, attempts: int = DEFAULT_ATTEMPTS) -> None
- Description: Resets the game to the specified difficulty level and number of attempts. Samples a random word from the corresponding difficulty list and scrambles it.
- Parameters:
difficulty
(str
, optional): A difficulty level ('easy'
,'medium'
,'hard'
) (defaults toDEFAULT_DIFFICULTY
).attempts
(int
, optional): The number of attempts allowed (defaults toDEFAULT_ATTEMPTS
)
scramble_word(word: str) -> str
- Description: Scrambles a given word.
- Parameters:
word
(str
): The word to scramble.
- Returns: The scrambled word.
get_word_list(self, difficulty: str) -> list[str]
- Description: Retrieves a list of words based on the specified difficulty level.
- Parameters:
difficulty
(str
): A difficulty level ('easy'
,'medium'
,'hard'
)
- Returns: A list of words for the given difficulty.
validate_input(self, user_input: str) -> bool
- Description: Validates the user's input to ensure it contains the same number of characters as the scrambled word.
- Parameters:
user_input
(str
): The user's input.
- Returns:
True
if the input is valid,False
otherwise.
guess(self, user_input: str) -> bool
- Description: Checks if the user's guess is correct and updates
attempts
. - Parameters:
user_input
(str
): The user's input.
- Returns:
True
if the guess is correct,False
otherwise.
- Description: Checks if the user's guess is correct and updates
play(self, difficulty: str = DEFAULT_DIFFICULTY, attempts: int = DEFAULT_ATTEMPTS) -> None
- Description: Starts a game session for a specified difficulty level and number of attempts.
- Parameters:
difficulty
(str
, optional): A difficulty level ('easy'
,'medium'
,'hard'
) (defaults toDEFAULT_DIFFICULTY
).attempts
(int
, optional): The number of attempts allowed (defaults toDEFAULT_ATTEMPTS
)
Hangman
from pygamebox import Hangman
game = Hangman()
game.play()
Screenshot
Class Attributes
DEFAULT_DIFFICULTY
(str
): The default difficulty level, which is'easy'
.DEFAULT_ATTEMPTS
(int
): The default number of attempts allowed, which is10
.
Methods:
-
__init__(self) -> None
- Description: Creates an instance of the Hangman game with a set of words for different difficulty levels.
-
reset(self, difficulty: str = DEFAULT_DIFFICULTY, attempts: int = DEFAULT_ATTEMPTS) -> None
- Description: Resets the game to the specified difficulty level and number of attempts, and chooses a random word from the list.
- Parameters:
difficulty
(str
, optional): A difficulty level ('easy'
,'medium'
,'hard'
) (defaults toDEFAULT_DIFFICULTY
).attempts
(int
, optional): The number of attempts allowed (defaults toDEFAULT_ATTEMPTS
)
-
get_word_list(self, difficulty: str) -> list[str]
- Description: Retrieves a list of words based on the
difficulty
level. - Parameters:
difficulty
(str
): A difficulty level ('easy'
,'medium'
,'hard'
)
- Returns: A list of words for the given difficulty.
- Description: Retrieves a list of words based on the
-
validate_input(self, user_input: str) -> bool
- Description: Validates the user's input to ensure it is a single letter and that it hasn't been guessed.
- Parameters:
user_input
(str
): The user's input.
- Returns:
True
if the input is valid,False
otherwise.
-
guess(self, user_input: str) -> bool
- Description: Checks if the user's guess is correct and updates the game status (remaining attempts and guessed letters).
- Parameters:
user_input
(str
): The user's guess.
- Returns:
True
if the user has guessed the entire word,False
otherwise.
-
print_partial(self) -> None
- Description: Prints the word with the guessed letters filled in and displays the letters remaining to guess.
-
play(self, difficulty: str = DEFAULT_DIFFICULTY, attempts: int = DEFAULT_ATTEMPTS) -> None
- Description: Starts a Hangman game session.
- Parameters:
difficulty
(str
, optional): A difficulty level ('easy'
,'medium'
,'hard'
) (defaults toDEFAULT_DIFFICULTY
).attempts
(int
, optional): The number of attempts allowed (defaults toDEFAULT_ATTEMPTS
)
Wordle
from pygamebox import Wordle
game = Wordle()
game.play()
Screenshot
Methods:
__init__(self) -> None
- Description: Initializes the Wordle game with a list of possible words, valid words, and game parameters, such as the current word, guesses, and attempts.
reset(self, attempts: int = DEFAULT_ATTEMPTS) -> None
- Description: Resets the game, generating a new word to guess and resetting the guesses and attempts.
- Parameters:
attempts
(int
, optional): The number of attempts allowed (defaults toDEFAULT_ATTEMPTS
).
get_user_input() -> str
- Description: Gets the user's input (the guessed word).
- Returns: The user's input as a string.
validate_input(self, guess: str) -> bool
- Description: Ensures guessed word is in the list of valid words.
- Parameters:
guess
(str
): The user's input (guessed word).
- Returns:
True
if the word is valid,False
otherwise.
check_win(self, guess: str) -> bool
- Description: Checks if the user's guess is correct (equals the hidden word).
- Parameters:
guess
(str
): The user's input (guessed word).
- Returns:
True
if the guess is correct,False
otherwise.
check_guess(self, guess: str) -> list[int]
- Description: Checks what characters in the guess are in the correct positions, are misplaced, or not present.
- Parameters:
guess
(str
): The user's input (guessed word).
- Returns: A list of integers representing the status of each character in the guess (0: Correct, 1: Misplaced, -1: Not present).
print_output(self) -> None
- Description: Prints the Wordle game state, including feedback on previous guesses.
play(self, attempts: int = DEFAULT_ATTEMPTS) -> None
- Description: Starts a Wordle game session for a specified number of attempts.
- Parameters:
attempts
(int
, optional): The number of attempts allowed (defaults toDEFAULT_ATTEMPTS
).
Contributing
If you'd like to contribute to the PyGameBox project, follow these steps to set up your development environment:
-
Fork the repository or clone it directly:
git clone https://github.com/software-students-fall2023/3-python-package-exercise-prime-order.git
-
cd
to the folder and create/activate virutal environment:pipenv shell
-
Download and install dependencies.
pipenv install
-
You can work on the module, add other games, or resolve existing issues. Create unit tests for your functions to test them in
tests/
.Run the tests using:
python -m pytest
-
Push your code changes, create a pull request and contribute to the project's development.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pygamebox-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e49c3f295276f0359f69e021b5a1cc053155a073a59c8774551967fdd9479e0a |
|
MD5 | 88247ccaaf5c96648e7ed4684352e632 |
|
BLAKE2b-256 | 5da334f2a87af649b88694f8296240f611f5cf0af111be7ca52ca50f604375cd |