Page 1 of 1

Introduction to Pygame-2: Importing and Initialization of modules, Set

#1 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 02 March 2009 - 11:07 AM

Introduction to Pygame-2: Importing and Initialization of modules, Setting up screen, Loading, Drawing & Fliping of Image

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.

init()



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-

window=display.set_mode((228,148))



Now we will set caption for the window and after that, we get the display screen.

display.set_caption(‘Bangladesh’)
screen=display.get_surface()



So far we have set our screen. Now we have to create the pathname of our file. Here is how to do this-

bd_filename=os.path.join(‘Bangladesh.gif’)



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.

bd_surface=image.load(bd_filename)
screen.blit(bd_surface,(0,0))



Now we have to flip the diplay surface to update the whole screen. Flip is done to see the graphics drawn.

display.flip()



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())

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Page 1 of 1