textobj = font.render(text, 1, TEXTCOLOR)Ĥ2. def drawText(text, font, surface, x, y):ģ7. def playerHasHitBaddie(playerRect, baddies):ģ2. if event.key = K_ESCAPE: # Pressing ESC quits.ģ0.
#Dodge game uniplayer code#
If you get errors after entering this code, compare the code you typed to the book’s code with the online diff tool at. When you run this program, the game will look like Figure 21-1. The higher the FPS, the faster the game runs. The Clock object has a tick() method, which can be passed the number of frames per second (FPS) we want the game to run. The Clock object in the pygame.time module is helpful for keeping our games from running faster than the player can see. SysFont() are a string of the font name (it’s common to pass None for the font name to get the default system font) and an integer of the font size. The pygame.font module uses the Font data type, which represents the typeface used for text in pygame. (See “ Handling Events” on page 292 for a complete list of all the event types.) QUIT, KEYDOWN, and MOUSEBUTTONUP are examples of some event types. You can determine the type of the Event object by checking its type attribute. The () function returns a list of these Event objects. The pygame.event module generates Event objects whenever the user provides keyboard, mouse, or other input. The Surface object returned by the _mode() function is special because anything drawn on that Surface object is displayed on the user’s screen when () is called. Surface objects have a blit() method that is used to draw the image on one Surface object onto another Surface object. A Surface object represents a rectangular image, while a Rect object represents only a rectangular space and location. Surface objects are areas of colored pixels. Rect objects have a colliderect() method that checks whether they are colliding with another Rect object. The size is determined by the width and height attributes, which are integers indicating how many pixels long or high the rectangle is. These corner attributes are a tuple of integers for the x- and y-coordinates. The location is determined by the Rect object’s topleft attribute (or the topright, bottomleft, and bottomright attributes). Rect objects represent a rectangular space’s location and size.
![dodge game uniplayer dodge game uniplayer](https://i.ytimg.com/vi/Mtt9ii3dAJs/maxresdefault.jpg)
Review of the Basic pygame Data Typesīefore we start making Dodger, let’s review some of the basic data types used in pygame: If the player holds down the Z key, the baddies will reverse their direction and travel up the screen instead of down. If the player holds down the X key, every baddie’s speed is reduced to a super slow rate. Just for fun, we’ll also add some cheat modes to this game. The longer the player can keep dodging the baddies, the higher their score will get. In the Dodger game, the player controls a sprite (the player’s character) who must dodge a whole bunch of baddies that fall from the top of the screen.