Tuesday, July 5, 2011

Display methodes in Pygame

In this post I want to explain the important functions of pygame.display functions and the blit function. It helps to draw or update the screen in order to give a feel of a game is happening.


This function updates the entire game screen(canvas). Its used in my game program to draw the car, enemies and the energy symbols. We should take care of the use of pgame.display.flip() so that our objects will correctly display and update(move) on the screen. If we use two screens then it will swap both in a small time interval.


It updates the portions of the screen. This function is like an optimized version of pygame.display.flip(). It allows only a portion of the screen to updated, instead of the entire area. If no argument is passed it updates the entire Surface area like pygame.display.flip().You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles. 


It is used to actually render the objects in the screen. We need to render our objects first. Then we are using the pygame.display.flip() and pygame.display.update() to make a feel of moving the objects.
Blitting is the word used for rendering the objects in the screen.

screen.blit(car, [x_coord,y_coord])

The above code is used to render the car object at the specified position.
Another variety blit used in my code is,

screen.blit(scoreboard.render("Score " + str(score) , 4, (255,255,255)) , [40,40])

It renders the score board of the game at the position 40, 40 with the font color white(i.e.,255,255,255). A flip is also used with it to lively update the score.
Hope this post will help you to get the ideas about how we draw and update the objects in the game screen.



No comments:

Post a Comment

Comments with advertisement links will not be published. Thank you.