P. 1


|Views: 19|Likes:
Published by italoppl
Invente seus próprios jogos com pygme.

obs: Pygame é uma biblioteca para python
Invente seus próprios jogos com pygme.

obs: Pygame é uma biblioteca para python

More info:

Published by: italoppl on Oct 28, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






47.# get a pixel array of the surface
48.pixArray = pygame.PixelArray(windowSurface)
49.pixArray[480][380] = BLACK

On line 48 we create a pygame.PixelArray object (which we will just call a
PixelArray object for short). The PixelArray object is a list of lists of color tuples
that represents the Surface object you passed it. We passed windowSurface object
when we called the PixelArray() constructor function on line 48, so assigning BLACK
to pixArray[480][380] will change the pixel at the coordinates (480, 380) to be a
black pixel. Pygame will automatically modify the windowSurface object with this

The first index in the PixelArray object is for the X-coordinate. The second index is
for the Y-coordinate. PixelArray objects make it easy to set individual pixels on a
PixelArray object to a specific color.

50.del pixArray

Creating a PixelArray object from a Surface object will lock that Surface object.
Locked means that no blit() function calls (described next) can be made on that
Surface object. To unlock the Surface object, you must delete the PixelArray
object with the del operator. If you forget to delete the Surface object, you will get an
error message that says pygame.error: Surfaces must not be locked
during blit.


17 - Graphics and Animation

The blit() Method for Surface Objects

52.# draw the text onto the surface
53.windowSurface.blit(text, textRect)

The blit() method will draw the contents of one Surface object onto another
Surface object. Line 54 will draw the "Hello world!" text (which was drawn on the
Surface object stored in the text variable) and draws it to the Surface object stored
in the windowSurface variable.

Remember that the text object had the "Hello world!" text drawn on it on line 22 by
the render() method. Surface objects are just stored in the computer's memory (like
any other variable) and not drawn on the screen. The Surface object in
windowSurface is drawn on the screen (when we call the
pygame.display.update() function on line 56 below) because this was the
Surface object created by the pygame.display.set_mode() function.

The second parameter to blit() specifies where on the windowSurface surface the
text surface should be drawn. We will just pass the Rect object we got from calling
text.get_rect() (which was stored in textRect on line 23).

The pygame.display.update() Function

55.# draw the window onto the screen

In Pygame, nothing is drawn to the screen until the pygame.display.update()
function is called. This is done because drawing to the screen is a slow operation for the
computer compared to drawing on the Surface objects while they are in memory. You do
not want to draw to the screen after each drawing function is called, but only draw the
screen once after all the drawing functions have been called.

You will need to call pygame.display.update() each time you want to update
the screen to display the contents of the Surface object returned by
pygame.display.set_mode(). (In this program, that object is the one stored in
windowSurface.) This will become more important in our next program which covers

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->