PyGame¶
Pygame is a library that allows easy drawing to a screen using python. We can also show it here in a Jupyter notebook, but we need to add some extra code that you will not need on a Raspberry Pi. Here is enough to show a simple window on Raspberry Pi. Notice there is no output in our Jupyter notebook.
In [ ]:
import pygame
import time
pygame.init()
screen=pygame.display.set_mode([640, 480])
white = (255, 255, 255)
while True:
screen.fill(white)
pygame.display.update()
pygame 2.6.0 (SDL 2.28.4, Python 3.9.6) Hello from the pygame community. https://www.pygame.org/contribute.html
2024-08-25 18:05:54.306 Python[23325:954822] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:.
PyGame on Jupyter Notebook¶
Some extra code is added to these examples. The example below shows a blue rectangle using the rectantl object.
In [1]:
import pygame
# This is so we can output to a jpg to show up
from IPython.display import Image, display
pygame.init()
screen = pygame.display.set_mode((400, 400))
running = True
white = (255, 255, 255)
blue = (0, 0, 255)
blue_rectangle = pygame.Rect(30, 30, 50, 50)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.draw.rect(screen, blue, blue_rectangle)
pygame.display.flip()
# These two lines show the image in the Jupyter Notebook.
pygame.image.save(screen, 'frame.png')
display(Image(filename='frame.png'))
pygame.quit()
print("it ran")
pygame 2.6.0 (SDL 2.28.4, Python 3.9.6) Hello from the pygame community. https://www.pygame.org/contribute.html
2024-08-25 18:02:57.656 Python[12359:467955] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:.
it ran
Circle¶
We can draw a circle simply in pygame. The draw function has a subroutine for circles.
In [1]:
import pygame
# This is so we can output to a jpg to show up
from IPython.display import Image, display
import time
pygame.init()
screen = pygame.display.set_mode((400, 400))
running = True
white = (255, 255, 255)
teal = (0, 255, 150)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.draw.circle(screen, teal, (100, 100), 20)
time.sleep(.10)
pygame.display.flip()
# These two lines show the image in the Jupyter Notebook.
pygame.image.save(screen, 'frame.png')
display(Image(filename='frame.png'))
pygame.quit()
print("it ran")
pygame 2.6.0 (SDL 2.28.4, Python 3.9.6) Hello from the pygame community. https://www.pygame.org/contribute.html
2024-08-25 18:30:50.301 Python[23705:969300] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:.
it ran
Text¶
We can show text on PyGame, but it isn't easy. Consider this an advanced technique.
You need to create a font object
You need to then Create a surface for the text to show up on.
Lastly you can show the function with “blit.”
In [ ]:
import pygame, sys, random
import time
from IPython.display import Image, display
pygame.init()
screen=pygame.display.set_mode([640, 480])
teal = (0, 255, 150)
charcoal = (10, 30, 50)
fontObj = pygame.font.Font(None, 32)
textSurfaceObj = fontObj.render("SJCOE FabLab", True, charcoal, None)
white = (255, 255, 255)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.draw.circle(screen, teal, (100, 100), 20)
screen.blit(textSurfaceObj, (100,100))
pygame.display.update()
time.sleep(0.1)
pygame.display.flip()
# These two lines show the image in the Jupyter Notebook.
pygame.image.save(screen, 'frame.png')
display(Image(filename='frame.png'))
pygame.quit()
print("it ran")
pygame 2.6.0 (SDL 2.28.4, Python 3.9.6) Hello from the pygame community. https://www.pygame.org/contribute.html
2024-08-25 19:11:08.912 Python[24921:1013436] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:.
In [ ]: