Write simple games in Numpy!
Project description
🕹️ npgame
npgame
makes it easy and fast to write simple games. is great for learning
and when you quickly want to get something done. npgame
is powered by Numpy
and PyGame. For large game projects, we recommend to use PyGame directly.
npgame
opens a window for you, supports simple graphics like colored
rectangles and images, and lets you detect keyboard input. With a focus on
simplicity, npgame
intentionally does not functionality for mouse input,
audio, or networking. PyGame can be used together with npgame
or by itself if
those features are needed.
Installation
pip install npgame
Quickstart
import npgame
# Initialize state
pos = 5.5
game = npgame.Game(grid=(20, 15), scale=40)
game.title('Quickstart')
while game.running:
game.update()
# Update state
if game.pressed('escape'):
game.close()
if game.pressed('a'):
pos -= game.delta * 2.0
if game.pressed('d'):
pos += game.delta * 2.0
# Display
game.draw(0, 0, 40, 30, (1, 1, 1)) # White background
game.draw(pos, 3.4, 1.5, 1.5, (1, 0.7, 0)) # Yellow box
Conventions
Coordinates mimic the convention for numeric charts. In other words, X is the
horizontal axis and Y is the vertical axis and zero is in the lower left corner
of the window. The size of the coordinate system is specified with
Game(grid=(40, 30))
.
Documentation
npgame.Game(grid=(40, 30), scale=20, fps=60)
Create a Game object that processes keyboard events and graphics for us. The
grid
determines the size of coordinate system for drawing. The scale
sets
the size of each grid tile in pixels. It also determines the window size. The
fps
set the maximum number of frames per second, beyond which update()
will
add a pause.
Attributes
-
running
Boolean attribute that signals whether the game is still running. This should be used as condition in the main loop. Closing the game window or calling
close()
switches the flag toFalse
. -
delta
Float attribute that contains the time that has passed between the last two
update()
calls. When moving objects, this value should be used to multiply the velocities to ensure that objects move at constant speed regardless of how fast the computer is. Multiplying velocities bydelta
effectively gives them the unit of grid tiles per second.
Methods
-
title(text)
Set the title of the window. This can be called either once in the beginning or repeatedly later on, for example to display status information to the user.
-
update()
This function should be called early inside the main loop. It displays the things that have been drawn and processes external events, such as key presses and checks whether the window has been closed.
-
pressed(key)
Returns a boolean indicating whether the given key is pressed. The requested key is passed in as a string, corresponding to the [pygame key name][keynames]. The function detects both keys that are currently held down and keys that were briefly pressed between the last two
update()
calls. -
draw(x, y, w, h, color_or_image)
Draws either a rectangle of a color specified as RGB tuple or an image provided by string path. The position of the area is specified with
x
andy
and its size withw
andh
. Images are resized as needed and cached for efficiency. For the area to appear,update()
must be called inside the main loop. -
close()
Shuts down pygame, which closes the window, and sets
running
toFalse
so that the main loop ends.
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
File details
Details for the file npgame-0.1.3.tar.gz
.
File metadata
- Download URL: npgame-0.1.3.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.10.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72292a25a799a0861ba453d2bff3e620b0f3fbcf9316a2cbc381be409fc4e958 |
|
MD5 | 74a7a831805127926fe01aa512e22d30 |
|
BLAKE2b-256 | a78f75dd56f772bffb624fdd54837576baa8a852dc4495bfabab387cb6ca5b55 |