Monday, 7 December 2015

Pygame: Fill the screen with color

‘screen.fill(color)’ statement is used to fill the screen with some color. Use ‘pygame.display.flip()’ method to update the contents of the entire display.

pygame_hello.py
#import pygame
import pygame

#initialize game engine
pygame.init()

#Open a window
size = (800, 600)
screen = pygame.display.set_mode(size)

#Set title to the window
pygame.display.set_caption("Hello World")

dead=False

WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
BLACK=(0,0,0)

global counter
counter=0

while(dead==False):
 for event in pygame.event.get(): 
  if event.type == pygame.QUIT:
   dead = True
  elif event.type == pygame.KEYDOWN:
   if(counter==0):
    screen.fill(WHITE)
   elif(counter==1):
    screen.fill(RED)
   elif(counter==2):
    screen.fill(GREEN)
   elif(counter==3):
    screen.fill(BLUE)
   elif(counter==4):
    screen.fill(BLACK)
   pygame.display.flip()
   counter+=1
   if(counter>=5):
    counter=0
   
#Shutdown display module
pygame.display.quit()

Run the program ‘python3 pygame_hello.py’, it opens a window. Type some key on window, you can able to see white, red, green, blue and black screen respectively.








Previous                                                 Next                                                 Home

No comments:

Post a Comment