Eugman Home


Home -- Games -- Tutorials -- Blog


Code


This is probably some of the simplest code to get a blank pygame screen.

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill([255,255,255])
pygame.display.update()
clock = pygame.time.Clock()

while not pygame.event.get(pygame.QUIT):

	clock.tick(20)



How everything works:


import pygame

This imports all the functions and variables needed to use pygame.

pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill([255,255,255])
pygame.display.update()
clock = pygame.time.Clock()


The above block of code initializes pygame, makes a surface for the screen, fills the screen, updates the screen with the new fill, and makes a clock object.

while not pygame.event.get(pygame.QUIT):

This is the loop that will go on forever until you close the window and thus causing a quit event.

clock.tick(20)

This causes a framrate of 20 fps. The program will wait until the clock reaches the next frame. This is just here so the program doesn't use up your whole cpu.


Things to try


1. Change the [255,255,255] to [200,100,200]
2. Change the framerate to 2. See how long it take for the window to close.
3. Change pygame.display.update() to pygame.display.flip() . It should do the same thing in this instance.
4. Change (640, 480) to (320, 320)
eugmanhome.com logo Donate towards my web hosting bill! Valid HTML 4.01 Transitional