In this part of tutorial, we will learn some basics including how to import and initialize all the pygame modules, set up screen, load an image and draw it to the screen.
First of all we need to import all the required modules.
import sys,os from pygame import * from pygame.locals import *
The next task is to initialize all the modules we have imported earlier. This will initialize sound, video and other pygame modules.
After the initialization of the modules, we have to set up our screen. The screen resulation is fixed depending on the size of the image. If our image is 228*14+8 pixels, then we will set the window like-
Now we will set caption for the window and after that, we get the display screen.
So far we have set our screen. Now we have to create the pathname of our file. Here is how to do this-
Now we can load the image and draw it onto the screen. We will draw the image at (0,0) coordinate. The coordinate system here is set in such that, x=0,y=0 represents top left of the screen.
Now we have to flip the diplay surface to update the whole screen. Flip is done to see the graphics drawn.
We have completed most of the works except the main loop. The below code defines main loop-
def run(event): for i in event: if i.type==QUIT: sys.exit(0) else: print i while True: run(i.get())
This loop is an infinite loop. It will keep running until the program exits.
So,here is the full code-
import sys,os from pygame import * from pygame.locals import * pygame.init() window=display.set_mode((228,148)) display.set_caption(‘Bangladesh’) screen=display.get_surface() bd_filename=os.path.join(‘Bangladesh.gif’) bd_surface=image.load(bd_filename) screen.blit(bd_surface,(0,0)) display.flip() def run(main): for event in main: if event.type==QUIT: sys.exit(0) else: print event while True: run(event.get())