Intro to Pygame Lecture 05
What is Pygame? It is a set of Python modules designed for writing games. It makes writing games possible for beginners. import pygame
Base Pygame Program import pygame pygame.init() size = (400,700) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip() clock.tick(30) pygame.quit() import pygame pygame.init() size = (400,700) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip() clock.tick(30) pygame.quit()
Initialization This is needed before you can use any of pygame’s features. import pygame pygame.init() import pygame pygame.init()
Screen Makes the window the game will be played in import pygame pygame.init() size = (400,700) screen = pygame.display.set_mode(size) import pygame pygame.init() size = (400,700) screen = pygame.display.set_mode(size)
Game Loop # Main Loop! while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill(blue) pygame.display.flip() clock.tick(30) # Main Loop! while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill(blue) pygame.display.flip() clock.tick(30)
Quitting This isn’t necessary, but it cleanly exits the program pygame.quit()
RGB Colours RGB is short for red, green, and blue Range from 0 to 255. white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255)
Normal Graphics
Pygame Graphics
Filling in the Background We usually need to fill the background in every loop screen.fill(blue)
Pygame drawing We can draw any kind of basic shapes in Python
How to Draw in Pygame We use the pygame.draw function pygame.draw.rect(Surface, color, Rect) pygame.draw.circle(Surface, color, pos, radius) pygame.draw.ellipse(Surface, color, Rect) pygame.draw.line(Surface, color, start_pos, end_pos, width) pygame.draw.rect(Surface, color, Rect) pygame.draw.circle(Surface, color, pos, radius) pygame.draw.ellipse(Surface, color, Rect) pygame.draw.line(Surface, color, start_pos, end_pos, width)
How Do I Remember All That Easy: go to the documentation