Make a presentation with Python and Pygame
If you want to impress your audience you have to create something unique. You will use powerpoint? I do not think so. If you are a programmer, you want to use Python and if you want envolvment you will use the pygame libraries, right? Let’s dive into this project and show you a way, my own way, to do it, step by step.
A long journey starts from a little step. (anonymous)
First things first (the window)
The following code will make a window for you. Nothing in it, but you can close it.
import pygame as pg
# THE SCREEN SIZE AND DISPLAY + WINDOW NAME + CLOCK
def windowinit():
"""Initialize the window with a way to quit it"""
W, H = 800, 600
display = pg.display.set_mode((W, H))
pg.display.set_caption("Mario Game")
clock = pg.time.Clock()
crashed = False
while not crashed:
for event in pg.event.get():
if event.type == pg.QUIT:
crashed = True
pg.display.update()
clock.tick(60)
pg.quit()
if __name__ == "__main__":
windowinit()
Let’s add some text to the window
Let’s create a class Text that displays the text of single objects
class Text:
def __init__(self, screen, t, x=0, y=0, dim = 30, color=(0,0,0)):
self.myfont = pg.font.SysFont("Comic sans MS", dim)
self.textsurface = self.myfont.render(t, False, color)
screen.blit(self.textsurface,(x,y))
In the function windowinit() we will create a list of string that will be our rows of text for each slide and then we will iterate the list with a for loop that will call for each string the class Text to render the text of the string in different positions (one under the previous).
textrows = [ "FULL COSTING", "Metodo per attribuire i costi ad", " un oggetto di calcolo (un prodotto).", " -------------------------", "Costi diretti + indiretti", "Costi diretti: facili da calcolare", "Costi indiretti:", " sono comuni a più oggetti di calcolo" " e quindi occorre dividere i", " costi comuni in base a dei criteri di riparto." ] z = 0 dim = 50 for row in textrows: if z==0: textsurface = Text(screen, row, 0, z, 50, (0,255,0)) else: textsurface = Text(screen, row, 0, z, 50, (255,255,0)) z += dim
The whole code will be this:
import pygame as pg
# THE SCREEN SIZE AND DISPLAY + WINDOW NAME + CLOCK
pg.init()
pg.font.init()
class Text:
def __init__(self, screen, t, x=0, y=0, dim = 30, color=(0,0,0)):
self.myfont = pg.font.SysFont("Comic sans MS", dim)
self.textsurface = self.myfont.render(t, False, color)
screen.blit(self.textsurface,(x,y))
def windowinit():
"""Initialize the window with a way to quit it"""
W, H = 1100, 600
screen = pg.display.set_mode((W, H))
pg.display.set_caption("Mario Game")
clock = pg.time.Clock()
# ======== text =================
textrows = [
"FULL COSTING",
"Metodo per attribuire i costi ad",
" un oggetto di calcolo (un prodotto).",
" -------------------------",
"Costi diretti + indiretti",
"Costi diretti: facili da calcolare",
"Costi indiretti:",
" sono comuni a più oggetti di calcolo"
" e quindi occorre dividere i",
" costi comuni in base a dei criteri di riparto."
]
z = 0
dim = 50
for row in textrows:
if z==0:
textsurface = Text(screen, row, 0, z, 50, (0,255,0))
else:
textsurface = Text(screen, row, 0, z, 50, (255,255,0))
z += dim
crashed = False
while not crashed:
for event in pg.event.get():
if event.type == pg.QUIT:
crashed = True
pg.display.update()
clock.tick(60)
pg.quit()
if __name__ == "__main__":
windowinit()
Below is the first slide, now, let’s add the others.
The whole code of the program
import pygame as pg
import time
# THE SCREEN SIZE AND DISPLAY + WINDOW NAME + CLOCK
# PygamePresent
pg.init()
pg.font.init()
class Text:
def __init__(self, screen, t, x=0, y=0, dim = 30, color=(0,0,0)):
self.myfont = pg.font.SysFont("Comic sans MS", dim)
self.textsurface = self.myfont.render(t, False, color)
screen.blit(self.textsurface,(x,y))
def nextpage(numpage):
z = 0
for row in textrows[numpage]:
if z==0:
textsurface = Text(screen, row, 0, z, 50, (0,255,0))
else:
textsurface = Text(screen, row, 0, z, 50, (255,255,0))
z += dim
def windowinit():
"""Initialize the window with a way to quit it"""
global textrows, screen, dim
W, H = 1100, 600
screen = pg.display.set_mode((W, H))
pg.display.set_caption("Mario Game")
clock = pg.time.Clock()
# ======== text =================
textrows = [
#page 1 - This appears
["Giovanni Gatto presenta:"],
["FULL COSTING",
"Metodo per attribuire i costi ad",
" un oggetto di calcolo (un prodotto).",
" -------------------------",
"Costi diretti + indiretti",
"Costi diretti: facili da calcolare",
"Costi indiretti:",
" sono comuni a più oggetti di calcolo"
" e quindi occorre dividere i",
" costi comuni in base a dei criteri di riparto."
],
# Di cosa parlo?
["Terza slide"]
# end of list of lists
]
z = 0
dim = 50
numpage = 0
for row in textrows[0]:
if z==0:
textsurface = Text(screen, row, 0, z, 50, (0,255,0))
else:
textsurface = Text(screen, row, 0, z, 50, (255,255,0))
z += dim
crashed = False
while not crashed:
for event in pg.event.get():
if event.type == pg.QUIT:
crashed = True
elif event.type == pg.MOUSEBUTTONUP:
numpage += 1
if numpage < len(textrows):
screen.fill((0,0,0))
nextpage(numpage)
elif numpage == len(textrows):
screen.fill((0,0,0))
Text(screen, "Fine della presentazione", 0, 50, 50, (0,255,0))
Text(screen, "Grazie per la cortese attenzione", 0, 150, 50, (255,255,0))
numpage = 0
#time.sleep(2)
pg.display.update()
clock.tick(60)
pg.quit()
if __name__ == "__main__":
windowinit()
Some adjustement in the code
import pygame as pg
import time
# THE SCREEN SIZE AND DISPLAY + WINDOW NAME + CLOCK
# PygamePresent
pg.init()
pg.font.init()
class Text:
def __init__(self, screen, t, x=0, y=0, dim = 30, color=(0,0,0)):
self.myfont = pg.font.SysFont("Comic sans MS", dim)
self.textsurface = self.myfont.render(t, False, color)
screen.blit(self.textsurface,(x,y))
def nextpage(numpage):
z = 0
slide = textrows[numpage].splitlines()[1:]
for row in slide:
if z==0:
textsurface = Text(screen, row, 0, z, 50, (0,255,0))
else:
textsurface = Text(screen, row, 0, z, 50, (255,255,0))
z += dim
def windowinit():
"""Initialize the window with a way to quit it"""
global textrows, screen, dim
W, H = 1100, 600
screen = pg.display.set_mode((W, H))
pg.display.set_caption("Mario Game")
clock = pg.time.Clock()
# ======== text =================
textrows = [
"""
L'azienda
""",
"""
L'azienda è formata da:
- organizzazione
- beni economici
- persone
- fine
""",
"""
L'azienda è formata da:
- organizzazione: durevole
----------------------------------
Non si può trattare di un affare
una tantum. L'azienda è destinata
a durare a tempo indefinito.
----------------------------------
""",
"""
L'azienda è formata inoltre da:
-------------------------
- beni economici:
Si dividono in:
- beni durevoli
- beni a breve ciclo di utilizzo
""",
]
z = 0
dim = 50
numpage = 0
crashed = False
nextpage(0)
while not crashed:
for event in pg.event.get():
if event.type == pg.QUIT:
crashed = True
elif event.type == pg.MOUSEBUTTONUP:
numpage += 1
if numpage < len(textrows):
screen.fill((0,0,0))
nextpage(numpage)
elif numpage == len(textrows):
screen.fill((0,0,0))
Text(screen, "FINE", 0, 50, 50, (0,255,0))
Text(screen, "CLICK TO RESTART", 0, 150, 50, (255,255,0))
numpage = 0
#time.sleep(2)
pg.display.update()
clock.tick(60)
pg.quit()
if __name__ == "__main__":
windowinit()
Pygame's Platform Game
