Skip to main content

A Python library for creating games in the terminal. test

Project description

PyRetroGame ( Beta )

Installation

pip install pyretrogame

Getting Started

Create a file and import the library

import pyRetroGame

Declare the game variable

game = pyRetroGame.game.Game(size = Vector2(15, 15))

And start the game

game.start()

Congrats! Now you can initialize the game window.


Initialize Player

Create a Player class and extend pyRetroGame.objects.gameEntity

class Player(pyRetroGame.objects.gameEntity):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __str__(self):
        # return the Player Texture
        return 'X'

Declare the player variable

# Must return game var to the player
player = Player(position=Vector2(0, 0), game=game)

Spawn the player

game.spawn(player)

Congrats! You've spawned the Player.


Player Movements and Keyboard Inputs

To move the Player we use the move function that accepts the direction argument as Vector2(x, y)

player.move(Vector2(1, 0))

In pyRetroGame for getting inputs we use Decorators, Here's an example:

@game.inputHandler('w')
def moveUp():
    player.move(Vector2(0, -1))

You can also use a list of key, but you need to pass a key argument to the function

@game.inputHandler(['w', 's', 'a', 'd'])
def movements(key):
    if key == 'w':
        player.move(Vector2(0, -1))

Here's a total movements function

@game.inputHandler(['w', 's', 'a', 'd'])
def movements(key):
    if key == 'w':
        player.move(Vector2(0, -1))
    if key == 's':
        player.move(Vector2(0, 1))
    if key == 'a':
        player.move(Vector2(-1, 0))
    if key == 'd':
        player.move(Vector2(1, 0))

Congrats! Now you have a Player who moves in the game window.


Custom Background

To change the background of the game window you need to use a Background class that return the background texture

class Background():
    def __str__(self):
        # return the Background Texture
        return '0'

And pass the Background class to game

game = pyRetroGame.game.Game(background=Background, size = Vector2(15, 15))

Congrats! You have changed the background of the game window.


Game Objects and Collisions

To create a game object you need to create a class and extend pyRetroGame.objects.gameObject

class SolidBlock(pyRetroGame.objects.gameObject):
    def __init__(self, *argvs, **kwargs):
        super().__init__(*argvs, **kwargs)

        # Game Objects has an argument called solid, if you want to make the object solid you need to set it to True
        self.solid = True

    def __str__(self):
        # return the SolidBlock Texture
        return 'B'

Declare the solidBlock variable

wall = SolidBlock(position=Vector2(5, 2), game=game)

And spawn the solid block

game.spawn(wall)

Now, when the player collides with the solidBlock, it will be stopped

If you want to check when the player collides with the solidBlock, you can use the collisionHandler decorator

@player.collisionHandler(wall)
def onCollideWithWall():
    # Do something when the player collides with the wall
    pass

If you want to check when the player collides with the solidBlock, you can use the limitHandler decorator

@player.limitHandler()
def onCollideWithLimit(side):
    # side argument return 'right' - 'left' - 'top' - 'bottom'

    # Do something when the player collides with the limit of the game window
    pass

Congrats! Now you have a solid block that stops the player when it collides with it.


Game Process

If you need a loop you can use the @game.process decorator

@game.process()
def process():
    deltaTime = 0.1

    while True:
        
        # Insert your Code Here

        # Very important to declare a delay if you won't the game crash
        time.sleep(deltaTime)

Congrats! Now you have a game loop.


Congrats! You've finished the tutorial. You're now ready to start your own game.

Other Functionalities

In the game var you can pass the fixedSize argument to set the window size perfectly match with the game size

game = pyRetroGame.game.Game(background=Background, size = Vector2(15, 15), fixedSIze = True)

In game.start() you can pass the fps argument to set the game FPS

game.start(fps = 60)

And you can also pass a debugging argument to set the debug mode

game.start(fps = 60, debugging = True)

If you need to print some Text you have 2 built-in functions:

game.printText(text = "Hello World", timeout = 2)
game.printAnimatedText(text = "Hello World", animationTimeout = 0.05, timeout = 2)

If you need for some assets you can import the assets package

from pyRetroGame.assets import *

TextAssets.FilledBlock # 'â–ˆ'
TextAssets.BlankSpace # ' '

If need for some calculations you can import the gameMath package

from pyRetroGame.gameMath import *

# Return the distance beetween two Vector2
gameMath.distance(Vector2(0, 0), Vector2(1, 1)) # 1.4142135623730951

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

pyRetroGame-1.1.29.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyRetroGame-1.1.29-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pyRetroGame-1.1.29.tar.gz.

File metadata

  • Download URL: pyRetroGame-1.1.29.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for pyRetroGame-1.1.29.tar.gz
Algorithm Hash digest
SHA256 ac39bb4b378c06f56638af1f73d912d3708b874c90363541d7c15fb11276b628
MD5 905437920a92af4ec5ff1f52f9c548dc
BLAKE2b-256 0f4afadd79ed419547e6703c374d7b93124d433afd6ea73c687db584e7d3c9b4

See more details on using hashes here.

File details

Details for the file pyRetroGame-1.1.29-py3-none-any.whl.

File metadata

  • Download URL: pyRetroGame-1.1.29-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for pyRetroGame-1.1.29-py3-none-any.whl
Algorithm Hash digest
SHA256 da0be3c4b42b9fbc145502cc2b00a738551cbdbe4e1280362dbca17da3ee4f86
MD5 593e4a593b2b66aa89bf76aa129dc5f9
BLAKE2b-256 09c25d3a2a03972272f3dfcba40f51531c7d265922c4050e7e038db91da09804

See more details on using hashes here.

Supported by

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