13. Pygame Flashcards
infinity loop
while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit()
get keyboard (constantly)
keys_pressed = pygame.key.get_pressed()
get keyboard (one time press)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL
clock to set FPS
clock = pygame.time.Clock()
clock.tick(FPS)
Rectangle
1) create object:
yellow = pygame.Rect(pos.x, pox.y, width, height)
x = to right
y = to down
2)
pygame. draw.rect(surface, BLACK, yellow (object))
or
pygame.draw.rect(WIN, BLACK, (WIDTH//2, 0, 10, WIDTH))
blit
surface method
WIN.blit(RED_SPACESHIP, (pos.x, pos.y))
WIN.blit(yellow_health_text,(10,10))
image
pygame. image.load
pygame. transform
Text
initialize -> set font -> render
pygame.font.init()
HEALTH_FONT = pygame.font.SysFont(‘comicsans’, 40)
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(yellow_health_text,(10,10))
Surface
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
User event
YELLOW_HIT = pygame.USEREVENT + 1 RED_HIT = pygame.USEREVENT + 2
pygame.event.post(pygame.event.Event(RED_HIT))
if event.type == RED_HIT:
update display
pygame.display.update()
WIN.fill(WHITE)
pygame.?
.display.set_mode .image.load .font.SysFont .transform .time .event .Rect
pygame direction
(x,y) x to the right, y to down
always x then y, and start at left upper corner which is 0,0