Package: pygstuff
pygstuffis a Python package to simplify developing with pygame.Install:$pipinstallpygstuffImportpygstuffaspygs(pronounced "pigs"):>>>importpygstuffaspygspygstuffis a pygame wrapper under developmentpygameis a great low-level tool for interactive applications.
Low-level means lots of freedom, but lots of boiler-plate code.pygstuffmakes pygame applications a little bit higher-level. It is
where my pygame code goes when I get tired of repeating it.I amvery activelyadding topygstuff. Check for changes:if cloned, dogit remote updateor if installed, update to the latest withpip install --upgrade pygstuffPlatform compatibilityDeveloped on Windows using Python3.8 andpygame 1.9.6. Tested on Linux Mint 19.3.Windows runs pygame with no problem, but Linux Mint and Ubuntu
distributions are usually missing SDL build dependencies and are
therefore unable to run pygame out of the box. There are two
steps to fix this.Install SDL dependencies on LinuxConfigure the source listFirst, if you have never built anything from source on your Linux
system, you need to configure your package manager source list
withdeb-srcURLs.Thesources.listfile usually has bothdebURLs anddeb-srcURLs, but thedeb-srcones are commented out. The package
manager needs these sources for installing build dependencies.Open/etc/apt/sources.listand find the lines starting
withdeb-srcthat are commented out. Uncomment these lines.sources.list is a protected file, so you will need to prefix
your text editor command withsudo.For example, my/etc/apt/sources.listhas this line:# deb-src http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 PackagesI remove the comment:deb-src http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 PackagesUpdate the package manager with the new sourcesNow doapt updateorapt-get updateto update the newly
configured sources.sudoaptupdateInstall SDL dependenciesNow install the SDL build dependencies:sudoapt-getbuild-deppython-pygameNote this does not install the python-pygame package, but
installs the dependencies for that package, which is exactly
what's needed topip install pygame.Build from sourceUnrelated to usingpygameorpygstuff, your Debian system is
now empowered to build other projects from source too:sudoapt-getbuild-depvim# powerful text editorsudoapt-getbuild-depardour# powerful audio recorder/editorThis only installs the necessary build dependencies. To actually
install Vim or Ardour, clone the official repository and follow
the instructions to run the configure and build scripts.Installation$pipinstallpygstuffThis installs thewheel(built
distribution).pygstuffrequirespygame. Ifpygameis not installed,pip install pygstufftakes care of installingpygameas well. If
the installation fails on Linux because of missing SDL
dependencies, see the previous section.SeeAltInstallallation.mdfor developer installation
('--editable') or simply editing PYTHONPATH (no installation).Quick example script usingpygstuffThis example opens a window and draws a line. Quit by pressingqor by clicking on the usual Window close button to quit.#!python.exe# -*- coding: utf-8 -*-'''Draw a line with pygame.'''importpygameimportpygstuffaspygsif__name__=='__main__':rgb=pygs.RGB()win=pygs.Window()win.open_window(1200,600)print(f"Display window size:{win.width}x{win.height}")clock=pygs.Clock(framerate=50)'''game loop'''quit=Falsewhilenotquit:clock.tick()'''--- EVENTS ---'''foreventinpygame.event.get():kp=pygame.key.get_pressed()km=pygame.key.get_mods()quit=pygs.user.quit(event,kp,km)'''--- UPDATE SCREEN ---'''win.surface.fill(rgb.blackestgravel)pygame.draw.aalines(win.surface,rgb.dress,False,# if True, connect first and last points[(100,100),(200,200),(200,300),(500,300)])# Flip to new surface drawingpygame.display.flip()Librariespkg window: Class WindowWrapspygame.display.set_mode(), pulling several pygame calls
into two methods:__init__andopen.The application only makes one instance of Window. This Window
singleton holds:the top-levelpygamesurface(for drawing everything on)thewidthandheightof the main game windowUsage for WindowInstantiate a Window:win=pygs.Window()initializes pygamesets window properties:locationcaptioniconOpen the Window:win.open()sets the window sizewin.widthandwin.heightopens the main game windowwin.surfacefor drawing tothe window closes when the application endsthis is just pygame's out-of-the-box behavior for closing
the game windowpkg clock: Class ClockThis is a very simple wrapper aroundpygame.time.Clock(). All
it does is avoid repeating the framerate value.Usage for ClockSet the framerate when Clock() is instantiated:clock=pygs.Clock(framerate=60)Then in the game loop, tick theclock:clock.tick()pkg colorsColor is a distraction when I first start development on
an application. I want nice contrast that is also easy on my eyes
and is good enough if I never bother changing it.This library gives me Steve Losh's Badwolf scheme in RGB and HEX
format. This is my default scheme for Vim, so it is a natural
default scheme for my applications. It is just what I am used to
looking at. I encourage Vim users to do something similar with
their favorite color schemes.Usage for colors namedtuple RGBPygame uses RGB. Instantiate anrgbversion of the Badwolf
palette:rgb=pygs.RGB()Use one of the gravels as a background:win.surface.fill(rgb.blackestgravel)Draw a tardis-colored plot (reminds me of the C64 blue):pygame.draw.aalines(win.surface,rgb.tardis,False,# if True, connect first and last pointsmeaningful_data# XY plot data [(x0,y0), ... (xn,yn)])The Badwolf scheme is a small set of colors with easy-to-remember
names:the gravels:wide range of brownish-greys from almost white to almost
blackgood for background fills and background line work of
varying emphasis: grid lines, box borders, texte.g., if the background isblackestgravel, then:a non-interactive text title is justgravelinteractive text isdarkgravelto indicate disabled
or out-of-focustext isbrightgravelor a highlight color to indicate
it is in-focus or it it just became activehighlight colors:tardis, taffy, saltwatertaffy, dalespale, orange, lime,
dressI usually have two or three of these in an application,
e.g.:a tardis command linea saltwatertaffy plotAnd, of course, there is simple black and white.Black (0, 0, 0) iscoal:rgb.coalWhite (255, 255, 255) issnow:rgb.snowpkg plotplothandles:reading data from filescaling the data values for fitting the plot on screenEither the application generates 2d data to plot, or there is a
file with 2d data. Either way, there are x and y values for
plotting.x values are stored as a list of floating point valuesy values are stored as a list of floating point valuesRight now it's up to the application to flip the data as needed,
dealing with the pygame convention that y=0 is the top line of
the window and y increases moving towards the bottom of the
window.Likeuser,plotis a work in progress. Right now,plotis
only handling conversions from the native data to the window
pixel values, scaling data to fit in the plot-window size and
rounding values to integers.Eventuallyplotwill include commands for drawing the plot,
managing multiple plot lines, changing plot styles (colors,
line-thickness, dot-size, line on/off, dot on/off), and typical
user interaction such as changing the plot axes and zooming.pkg userPygame user interaction is detected via key-presses and events.
Libraryuseris a collection of functions that check for common
user interactions and return a Boolean.About pygame events, key presses, and key modificationsTypical pygame applications check for user interaction on each
iteration of the game loop:foreventinpygame.event.get():kp=pygame.key.get_pressed()km=pygame.key.get_mods()This stores user interaction in three variables:eventskey presses (like a letter or a number)key modifications (like shift or control)Every function inpygs.usertakes all three of these variables. I
pass all three to simplify the interface. I don't want to bother
thinking about out which ones to pass.typical example: quitFor example, every application needs a way for the user to quit.
The game loop loops untilquitis True, and every iteration
checks if the user quit. The value ofquitis updated by
callingpygs.user.quit():quit=pygs.user.quit(event,kp,km)In this case, all three variables are actually used:clicking the Window's red X is an eventctrl-q is a keyboard shortcut requiringkpfor detectingqandkmfor detecting ifctrlwas held downMore about pygame eventsEvents are the catch-all for everything else:some events are defined by pygame, such as JOYBUTTONDOWN and
JOYAXISMOTIONevents are also defined by the developer, e.g., a text-entry
object might trigger an event when the user presses enter or
when the users selects text with the mouseFuture packagesFuture goals for pkg userThere is a lot of boilerplate in detecting specific key-presses
and events.Theuserlibrary is my attempt to simplify this with a
higher-level view asking "what did the user do?" in the form of
functions that return either True or False.There are many high-level views. This library just represents the
stuff I find most useful.I intent to redo this library to enable:customization in the application codeuse of user-specific keyboard shortcuts, like a.rcfileAs of now, the library is a mixture of high-level user
interaction, such as pygs.user.quit(), and low-level interaction
such as checking for specific key presses. I use these when the
interpretation of user-intent is application specific. For
example, this returns True if the user presses capital x:pygs.user.pressed_X(event,key_pressed,key_mods)new pkg linesThepygameline drawing interfaces specifies line segments with
a start x,y and a stop x,y.I'm working on alineslibrary that provides a higher-level
interface for working with lines:line:specify a point (x,y) the line passes throughspecify the slopeThe library converts lines to the visible line segment that fills
the game window.Building on thislineinterface,linesprovides aTilesclass.InstantiateTilesto generate a grid of parallelograms that
tile a portion of the game window. This started out of a need to
draw grids for axonometric (isometric) projection. With the right
parameter values, this simplifies to a rectangular grid for
plotting 2d data.