Object oriented ascii art python game engine
Project description
Description
PyPlayScii is a Python package that enables an simple object oriented implementation of ascii art games. By asigning the shapes of the game objects by texts seprated by newline characters and determining what to do every frame, you can quickly implement an ascii art game which can be run directly on terminal window. The following shows an example of an ascii art game implemented by PyPlayScii.
Click the image to watch Galaga made with PyPlayscii!!
Installation
$ pip install pyplayscii --user
Features
System | Linux | macOS | Windows |
---|---|---|---|
Status |
- Easy implementation of ascii style games on terminal screen
- Supports Windows 10, Ubuntu, and macOs => Tested on github action
- Support python 3.6, 3.6, 3.8 => Tested on github action
Quickstart
Let's make a very simple example of a bouncing ball!! A full source code for the following tutorial can be found here.
Step 1. Importing the package
Once you download the pyplayscii package, you can access the module by the following codes.
from playscii import GameObject, GameManager
from playscii.input import Input
GameObject and GamaManager are the most important classes of the pyplayscii package. GameManager will be the backbone of the game engine, and the GameObject will be the members of your game.
Step 2. Making a GameObject
The GameManager is the stage, and the GameObjects will be your actors!! Make a game object so that those objects can be used in the GameManager. You can use the plain form of GameObject, but you can also define your own one. In this example, we will make an object named a Ball. One of the most important methods of the GameObject is "update" method. This method will be called every frame when your gamemanager is running.
First, let's define how the ball looks like. All you need to do is make a string, separated by newline characters.
BALL = " **\n" \
" ****\n" \
" **"
See? It does look like a ball! In the game, your object look exactly like this.
Each gameobject has its position with respect to the lower right corner of the screen. We will update the position of the ball at each frame.
class Ball(GameObject): # Ball inherits the GameObject class
def __init__(self): # Constructor of your object
super().__init__(pos=(40, 10), render=BALL)
self.vel = (10, 10)
def update(self): # This method is called every frame. self.delta_time is the time it took between the frames.
self.x += self.vel[0] * self.delta_time # Update the position
self.y += self.vel[1] * self.delta_time # Update the posiiton
Step 3. Make a GameManager
Now, we are building a stage for your objects. You should always make a new GameManager by inheriting an abstract class, GameManager. Two methods that you must implement in GameManager are "setup()" and "update()." First, setup method is called at the very beginning of the game, right after the game starts. You will want to set the initial properties for your gameobjects, or register gameobjects to your manager by add_object() method. Update method is just the same as that of GameObject; it is called at each frame.
class BounceManager(GameManager): # Inherits GameManager
def __init__(self): # Constructor: If you want to keep track of the object, construct them here!
super().__init__((80, 20)) # (80, 20) is the size of your game screen.
self.ball = Ball() # Create a Ball object
self.set_title("Press q to quit") # set_title changes the title which will appear at the top of your game.
def setup(self): # This is called right before the first update call.
self.add_object(self.ball) # Register the object to your manager. If the object is not registered, they will not appear on the screen.
def update(self): # This is called every frame.
if self.ball.x < 0 or self.ball.x > 74: # If the ball hits the wall,
self.ball.vel = (-self.ball.vel[0], self.ball.vel[1]) # Reflect the velocity
if self.ball.y < 2 or self.ball.y > 20: # Same logic for the floor and the ceiling.
self.ball.vel = (self.ball.vel[0], -self.ball.vel[1]) # Reflect the velocity
if Input.get_key_down('q'): # if 'q' key is pressed,
self.quit() # quit the game.
Step 4. Play the Game!
You have your gameobject, and well defined gamemanager. Now you have everything you need to play the game. To start the game, just call "start" method of your gamemanger. Make sure your terminal screen is big enough for your game.
if __name__ == "__main__":
manager = BounceManager()
manager.start()
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
File details
Details for the file pyplayscii-0.2.6.tar.gz
.
File metadata
- Download URL: pyplayscii-0.2.6.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d10d4c3989cf309df3307dcc1430d03937742dc2c8def6ecb8b6f3a04b4f997d |
|
MD5 | 485b31e1d41d9458848f88bc23723ed9 |
|
BLAKE2b-256 | bf9d0a3229c1c353d21a05021be9b705d34a08044f95cc2bd38795112ea2ebcd |
File details
Details for the file pyplayscii-0.2.6-py2.py3-none-any.whl
.
File metadata
- Download URL: pyplayscii-0.2.6-py2.py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a0fccc2f8b5557a66904dd3293411f51cd5c38c6821fdee09301b98ab621a6c |
|
MD5 | e2b68eb8c413a639483de524cf60ccf7 |
|
BLAKE2b-256 | ee45e7b48cfa325a19958cdb9448b12d50e78046536755a8a36637c777f5bc89 |