Package: npgame
🕹️ npgamenpgamemakes it easy and fast to write simple games, powered by Numpy and
PyGame. It's a lightweight API to quickly get something done and for learning
rather than a solution full-blown feature-rich games.Features include opening a window, showing graphics like colored rectangles and
images, and detecting keyboard input. For additional features like mouse input
and audio, check out PyGame, which can be combined freely.InstallationpipinstallnpgameQuickstartimportnpgame# Initialize statepos=5.5game=npgame.Game(grid=(20,15),scale=40)game.title('Quickstart')whilegame.running:game.update()# Update stateifgame.pressed('escape'):game.close()ifgame.pressed('a'):pos-=game.delta*2.0ifgame.pressed('d'):pos+=game.delta*2.0# Displaygame.draw(0,0,40,30,(1,1,1))# White backgroundgame.draw(pos,3.4,1.5,1.5,(1,0.7,0))# Yellow boxConventionsCoordinates 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 withGame(grid=(40, 30)).Documentationnpgame.Game(grid=(40, 30), scale=20, fps=60)Create a Game object that processes keyboard events and graphics for us. Thegriddetermines the size of coordinate system for drawing. Thescalesets
the size of each grid tile in pixels. It also determines the window size. Thefpsset the maximum number of frames per second, beyond whichupdate()will
add a pause.AttributesrunningBoolean attribute that signals whether the game is still running. This should
be used as condition in the main loop. Closing the game window or callingclose()switches the flag toFalse.deltaFloat attribute that contains the time that has passed between the last twoupdate()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 bydeltaeffectively gives
them the unit of grid tiles per second.Methodstitle(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.resize(grid=None, scale=None)Change the size of the grid that is displayed on the window. This changes the
size of the window. Thegridargument is a tuple of integers for the width
and height of the grid. Thescaleis an integer indicating how many pixels
big each grid tile is. If omitted, the values are not changed.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 twoupdate()calls.draw(x, y, w, h, color=None, array=None, image=None)Draws a rectangle of a color given as RGB tuple, an Numpy array, or an image
given as string path. The position of the area is specified withxandyand its size withwandh. Images are resized as needed and cached for
efficiency. For the area to appear,update()must be called inside the main
loop.text(x, y, message, font, color=(0.5, 0.5, 0.5, 1), size=1)Render text given the filename of a font in TTF format. An example file of
the free DejaVu font can be found in theexamplesdirectory of the npgame
repository. The color of the font can include the opacity as the last number.
The position and size are relative to the grid of theGameobject.close()Shuts down pygame, which closes the window, and setsrunningtoFalseso
that the main loop ends.