Lesson 13.6
A Pygame game is a window and a main loop: on every frame the world is updated and redrawn.
import pygame
pygame.init()
screen = pygame.display.set_mode((300, 200))
clock = pygame.time.Clock()
y = 40
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
y += 1 # in the task the square moves to the right
screen.fill((10, 10, 10))
pygame.draw.rect(screen, (0, 200, 255), (60, y, 40, 40))
pygame.display.flip()
clock.tick(30)
pygame.quit()
event.get() handles the events, otherwise the window won't close;fill paints the screen, flip shows the finished frame;clock.tick(60) — 60 frames per second;pygame.quit() closes everything neatly.A window cannot be opened in the browser, so the check puts in a little module
game_screen with the function game_screen.move(x) — "draw the square at position x".
The check gives the starting position x, the speed speed and the number of frames.
On every frame move the square and print Frame <i>: x=<x>.
In a game the movement goes inside the main loop, otherwise the frames never change.
Set the starting position, add the speed in the loop, pass the position to
game_screen.move(x) and print the frame: the number, a colon, x=.