Difference between revisions of "Basic pygame template"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with " <syntaxhighlight lang=python> #Import statements are to enable the code to use the functions from the library import pygame import sys import os #initialize pygame & window...")
 
(Complete PyGame blank template)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
This should be the start of any pygame project. It includes everything to import pygame, to set up the window, and to respond to closing the window:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
Line 24: Line 25:
 
             sys.exit()
 
             sys.exit()
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
You should get something similar to this when you run the program:
 +
 +
[[File:Helloworld pygame.gif|500px]]
 +
 +
=Complete PyGame blank template=
 +
 +
[https://drive.google.com/file/d/14QxTZ3A0dlkaB92405sKuRFnQWujerZU/view?usp=sharing PyGame Blank Project]

Latest revision as of 14:30, 18 November 2020

This should be the start of any pygame project. It includes everything to import pygame, to set up the window, and to respond to closing the window:

#Import statements are to enable the code to use the functions from the library
import pygame
import sys
import os

#initialize pygame & window
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

#caption for the game
pygame.display.set_caption("My first game in pygame")

#game loop
while True:
    for events in pygame.event.get(): #get all pygame events
        if events.type == pygame.QUIT: #if event is quit then shutdown window and program
            pygame.quit()
            sys.exit()

You should get something similar to this when you run the program:

Helloworld pygame.gif

Complete PyGame blank template

PyGame Blank Project