Introduction to Pygame
In this tutorial, you will learn the pygame tutorial using python. This will be useful for beginners
Pygame
- A cross platform set of python modules, that allows the user to write games
- Primarily used for developing video games
- This package includes graphics & sound libraries designed to be used with python language
- It can handle time, videos, music, fonts, different image formats, cursors, mouse, keyboard, joystick and much more
- Pygame is based on the Simple Direct Media (SDL) Layer. A C library that is cross platform and also very simple.
- It deals with media nicely (pictures, sound)
- It interacts with user nicely (keyboard, mouse input, joystick)
- Lot of more advanced features for working with graphics, etc
SOFTWARE REQUIREMENTS
- Pygame 1.9.1
- Python 2 or 3
Target Module
import pygame
BUILT-IN SUB MODULES OF PYGAME
S.N | Sub-Module | Description |
1. | display | Control the display window or screen |
2. | draw | Draw simple shapes (like rectangle, circle, polygon) onto surface |
3. | event | Manage events and event queue |
4. | font | Create and render TrueType fonts |
5. | image | Load and save images |
6. | key | Manage the keyboard |
7. | mouse | Manage the mouse |
8. | time | Control time related tasks |
9. | transform | Scale, rotate, & flip images |
10. | cursors | Load cursor images, including standard cursors. |
BUILT-IN FUNCTIONS
1. display.set_mode(int height, int width)
- Here display is module and set_mode() is a built-in function
- This function will create a display surface
- It is used to launch program window with desired size
- Return type
- It returned the surface object (Ex. screen)
2. fill(int rgb, int rgb, int rgb)
- It is an instance function of screen object
- This function will set the background colour of the screen (fill the entire screen with given RGB colour)
- It accepts three RGB arguments as input
3. draw.circle(surface object, (color RGB), position ,radius)
- This function is used to draw a circle on the game screen
- It takes 4 arguments
- 1st argument is a surface object. Here the object is screen
- 2nd argument is the RGB values. It ranges from 0-255. (RGB values). This is specified using tuple.
- 3rd argument is X & Y axis
- 4th argument is the radius value of the circle. It is an integer value.
4. draw.rect(surface object, (color RGB), (x,y,height,width))
- This function is used to draw a rectangle on the game screen
- It takes 3 arguments
- 1st argument is a surface object. Here the object is screen
- 2nd argument is the RGB values. It ranges from 0-255. (RGB values). This is specified using tuple.
- 3rd argument is the dimensions of the rectangle. This is specified using tuple.
5. draw.ellipse(surface object, (color RGB), (x,y,height,width), thickness)
- It is used to draw an elliptical shape on the game screen
- It takes 4 arguments
- 1st argument is a surface object. Here the object is screen
- 2nd argument is the RGB values. It ranges from 0-255. (RGB values). This is specified using tuple.
- 3rd argument is the dimensions of the rectangle. The given rectangle is the area that the circle will fill. This is specified using tuple.
- 4th argument is the thickness to draw the outer edge
6. draw.polygon(surface object, (color RGB), point list, thickness)
- This function is used to draw a polygon shape on the game screen
- It takes 4 arguments
- 1st argument is a surface object. Here the object is screen
- 2nd argument is the RGB values. It ranges from 0-255. (RGB values). This is specified using tuple.
- 3rd argument is the point list of polygon. The point list is a list of tuples of x-y coordinates for the polygon. Each point list is separated by comma operator
- 4th argument is the thickness of the polygon.
7. display.update()
- It will update only the portions of the game screen
8. display.filp()
- This function will update the full display surface to the screen
BASIC STEPS FOR PYGAME PROGRAMMING
1. Load the pygame module
2. Initialize the game screen using init() function
3. Run the infinite game loop until receives the QUIT event
- Add code logic
4. Update the screen using update() or fill() function
EXAMPLE OF HELLO WORLD PYGAME
STEPS FOR DRAWING TEXT INTO PYGAME
- Create a font object using SysFont() function of sub-module font which takes two parameters like font name and font size and returns the font object.
- Draw the message using built-in instance function render() of the font object (which is returned in previous step 1). This function takes three arguments where,
- 1st argument is message to be drawn
- second argument is the boolean value either True or False
- 3rd argument is the RGB value of colors which are tuples.
- Finally, draw the text message using a special built-in function blit(message, (x,y)). This blit function will take two arguments where,
- 1st argument is the message to be drawn
- 2nd argument is the x, y coordinate points of tuple values
SOURCE CODE
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((500, 400))
# set Agency FB font with size 50
font=pygame.font.SysFont(“Agency FB”, 90)
# define welcome message with green text color using RGB values
msg = font.render(“Hello PyGame”, True, (0, 215, 0))
# infinite loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# set background window as white
screen.fill((255, 255, 255))
# draw message on screen
screen.blit(msg,(35,90))
# update game screen after the shape is drawn
pygame.display.update()
RESULT
More Useful Tutorials