Pygame

Input text in pygame

Pygame live – day by day code

Get the code for my most recent pygame tutorial here
Github repository
https://github.com/formazione/pygame_days

1 https://youtu.be/wAKLVFMb7eE Move Sprite
2 https://youtu.be/rsBJgWqsoa4 collisions
3 https://youtu.be/J3k4-tqy_kM Ai enemy 1
4 https://youtu.be/vwYu9Eel9QM frame rate
6 https://youtu.be/UaPjKtuQyfA Enemy AI 2
7 https://youtu.be/ZuEc6gncGNs Images, movements
8 https://youtu.be/wobJoDgI0LQ Bullet 1

Pygame is a module that makes you create games with python using SDL 2.

First you install it from the comman line interface:

pip install pygame

On the Mac you may have to do

pip3 install pygame

A blank window

Design an empty window. On the screen surface everything will happen.

First import pygame

  • then initialize it (pygame.init())
  • create the screen main surface
  • the clock is for the frame rate
  • the infinite loop (it stops with the pygame.QUIT event)
  • fills with a color every frame
  • update screen
  • put the frame rate to 60 udates per second
import pygame
import sys


pygame.init()
screen = pygame.display.set_mode((500,500)) # this is the main surface where sprites are shown every frame
clock = pygame.time.Clock() # this is needed to limit frame rate (to 60 in the last line)
# This loop goes on forever till you quit with the x button of the window
while True:
	for event in pygame.event.get():
		if event.type == pygame. QUIT: # this makes the window quit clicking the x button
			pygame.quit()
			sys.exit()
	screen.fill((200,30,30)) # fills the screen with this color every frame
	pygame.display.update()
	clock.tick(60)