How to install Pygame on Windows 10 and get started
In this article, you will learn how to install Pygame in Python on Windows 10 and get started with the basic concepts.
Pygame is a set of Python modules used to create fully-featured multimedia programs and video games. It is very simple and easy to use. It is free, platform-independent and runs on almost every operating system. So we can easily create freeware, shareware, and commercial games with it.
Install Pygame in Windows
Pygame needs Python. We hope you have already installed it, but if you have not, please follow this tutorial on Python Installation. Here is the command to install Pygame with the pip tool.
pip install pygame
The installation process will look something like this on the console-
Getting Started Pygame
To use the Pygame, we must import this at the top of the script. This includes all the functions that the pygame and sys modules have.
import pygame, sys
Where we call the function of pygame, we will have to write modulename.functionname() format. Instead, we can directly use functionname() by adding the following import statement.
from pygame.locals import*
The pygame.locals contains several constant variables that are easy to identify. Next, the pygame needs to be initialized using the following statement.
pygame.init()
Set the window
The pygame.display.set_mode() function returns the pygame.Surface object for the window. The set_mode() function accepts two arguments the width and height of the window.
pygame.display.set_mode((400, 300))
Set the window caption
To set the caption on the top of the window, call pygame.display.set_caption() function.
pygame.display.set_caption('Pygame Window Title')
Pygame Event Object
When we do any action on the window, it is recorded as an event, like- moving the mouse or pressing a key on the keyboard. Pygame provides 'pygame.event.Event' object to record these events and 'pygame.event.get()' to get a list of event objects. We can use the for loop to iterate over these events.
while True:
for event in pygame.event.get():
Quit Pygame Event
The event object has a member variable 'type', which tells us the type of the object. If the event object type equals 'QUIT', then the 'pygame.quit()' and the 'sys.exit()' functions are called.
if event.type == QUIT:
pygame.quit()
sys.exit()
Updating the screen
To immediately reflect the changes you make on the screen, use the following statement-
pygame.display.update()
Basic Pygame Code Pattern
Here, we have merged the above explained code and this is the basic pygame code patterns.
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Pygame Window Title')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Basic Pygame code to draw rectangle
Here is the basic code to draw rectangle and set color in pygame.
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((400, 300))
x = 50
y = 50
color = (52, 52, 235)
pygame.display.set_caption('Pygame Window Title')
while True: # main game loop
for event in pygame.event.get():
pygame.draw.rect(screen, color, pygame.Rect(x, y, 80, 80))
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
It returns the following output -
Related Articles
Convert list to dictionary PythonDictionary inside list Python
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml