EXAMPLE OF SIMPLE RECTANGLE CREATION USING PYGAME
In this tutorial, you will learn how to draw rectangle using pygame python. This example is done with help of visual studio 2017 IDE which is an optional.
TESTED PACKAGE & IDE DETAILS
- Visual Studio 2017 Professional
- Python 3.6.4 (X64 Bit)
- Pygame 1.9.1
NOTE
- Before to start the programming, pygame software should be installed. This can be installed using the command like pip install pygame in the command window or VSC editor.
STEP 1: CREATING A PROJECT IN VISUAL STUDIO 2017
STEP 2: APPLICATION SELECTION
SOURCE CODE
from pygame import *
from pygame.display import *
from pygame.draw import *
from pygame.event import *
// load required python modules
from sys import *
// initialize all modules
init()
// start window with desired size (height, width)
screen = set_mode((400, 300))
// infinite loop (running state)
while True:
// list events in pygame (event queue)
for event in get():
// event type, it is fired, when you click on close button
if event.type == QUIT:
exit()
rect(screen, (0, 255, 230), (100,70,150,80))
flip()
RESULT
1. init()
- This function initializes all the modules required for PyGame.
2. set_mode((width, height))
- This function will launch a window of the desired size.
- The return value is a surface object that is used to perform graphical operations in the pygame environment
3. get()
- This empties the event queue (returns the list of events)
- If you don’t call this function, the game screen will become unresponsive in the opinion of the operating system.
4. QUIT
- This is the one of the type of event, that is fired when user click on the close button in the corner of the window
- This is available in pygame module
5. flip()
- This function is used to update the entire screen after everything is drawn
- This is available in display sub module
Color Name = (r,g,b)
- Specify the name the color
- (r,g,b) is a tuple with values for red, green, and blue
- All three should be integers between 0 and 255, with 255 being brightest, and 0 being darkest.
Sample RGB Based Color Codes
red = (255,0,0) green = (0,255,0) blue = (0,0,255) dark blue = (0,0,128) white = (255,255,255) black = (0,0,0) pink = (255,200,200) |