Creating a window in pygame

Vishal Rashmika published on
1 min, 105 words

Categories: Pygame

Creating A Window

import pygame
from sys import exit

pygame.init() # start pygame

screen = pygame.display.set_mode((800,400)) # creating display surface ((width,height))

pygame.display.set_caption('Runner') # title of the game

clock = pygame.time.Clock() #controlling the frame rate

while True:
    for event in pygame.event.get():  # get all the events and loop through them
        if event.type == pygame.QUIT:
            pygame.quit() #end the game
            exit() # breaking the loop

    # draw elements
    #update everything
    pygame.display.update() #getting all the things and put them in the display surface
    clock.tick(60) # should not run the while true loop faster than 60x per second

Common Event Types

  • QUIT
  • ACTIVEEVENT
  • KEYDOWN
  • KEYUP
  • MOUSEMOTION
  • MOUSEBUTTONUP
  • MOUSEBUTTONDOWN
  • JOYAXISMOTION
  • JOYBALLMOTION
  • JOYHATMOTION
  • JOYBUTTONUP
  • JOYBUTTONDOWN
  • VIDEORESIZE
  • VIDEOEXPOSE
  • USEREVENT