Skip to main content

Object oriented ascii art python game engine

Project description


PyPI version image Build Status License: MIT

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!!

Youtube Video of Playscii Game

Installation

$ pip install pyplayscii --user

Features

System Linux macOS Windows
Status Unit Test (Ubuntu) Unit Test (macOS) Unit Test (Windows)
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyplayscii-0.2.5.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

pyplayscii-0.2.5-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyplayscii-0.2.5.tar.gz.

File metadata

  • Download URL: pyplayscii-0.2.5.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

Hashes for pyplayscii-0.2.5.tar.gz
Algorithm Hash digest
SHA256 34d1424ccf6537598ff0deeb0daa040b725000f17d0b3a0d799026cc057f33a3
MD5 534ad1ad809c395aa267c9836b74dc33
BLAKE2b-256 6f06b2bc14c7736f1700758e396a2b9d5841a3f0a42b2914c3b81097fc0f87fe

See more details on using hashes here.

File details

Details for the file pyplayscii-0.2.5-py2.py3-none-any.whl.

File metadata

  • Download URL: pyplayscii-0.2.5-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

Hashes for pyplayscii-0.2.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c1178ef07c61e13db7907306fc89960cca41e72ef148f2405a684b7ee6c5ead1
MD5 563aaed5959c39ace61dae6182096091
BLAKE2b-256 fe6aec324d38faa7d7b38230cfdcb21cd1c83458073b6c348b79e28f854040b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page