Make games with Python & Pygame (2)

Source: Internet
Author: User

Then the last time to continue.

A simple drawing function

Pygame provides us with a few simple drawing functions, such as drawing rectangles, circles, ellipses, lines, and independent pixel points.

The following program implements some simple drawing operations.

Import Pygame, Sysfrom pygame.locals import *pygame.init () Displaysurf = Pygame.display.set_mode ((500,400), 0,32) BLACK = (0, 0, 0) White = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) displaysurf.fill (white) Pygame.draw.polygon (DI Splaysurf, GREEN, ((146,0), (291,106), (236,277), (56,277), (0,106))) Pygame.draw.line (Displaysurf, BLUE, (60, 60), ( 120,60), 4) pygame.draw.line (Displaysurf, Blue, (+), (60,20)) Pygame.draw.line (Displaysurf, Blue, (60, 120), ( 120,120), 4) pygame.draw.circle (Displaysurf,blue, (300,50), 0) Pygame.draw.ellipse (displaysurf,red, ( 300,250,40,80), 1) pygame.draw.rect (displaysurf,red, (200,150,100,50)) Pixobj = Pygame. PixelArray (displaysurf) pixobj[480][380] = blackpixobj[482][382] = blackpixobj[484][384] = blackpixobj[486][386] =            blackpixobj[488][388] = Blackdel pixobjwhile true:for event in Pygame.event.get (): if Event.type = = QUIT: Pygame.quit () Sys.exit () pygame.display.update ()

The effect after execution


There's a lot of new pygame in here.

Fill (color) is a method of surface objects that fills the window of a surface object with a color, or background color.

Pygame.draw.polygon (surface, color, pointlist, width) draws a polygon, where surface and color parameters tell which object to paint and which color to use, The pointlist parameter is a tuple or list that consists of multiple points, and then starts from the first point, joins the next point in turn to form a polygon, the width parameter is optional, and if not, the polygon is filled with the color of the colored parameter, if any, and not filled, The width of the polygon contour is determined by the widths parameter. Passing 0 to the width parameter is the same as no width parameter.

It is important to note that all Pygame.draw drawing functions have a selection parameter width at the end, and they work the same way as the width parameter in the Pygame.draw,polygon function.

pygame.draw.line (surface, color, start_point, end_point, width) Draw line function

pygame.draw.lines (surface, color, closed, pointlist, width) draws multiple connected lines, from the starting point to the end point, Similar to Pygame.draw.polygon, the only difference is that if the closed parameter is false, the end point is not connected to the starting point, and if true, the end point will be connected to the starting point.

pygame.draw.circle (surface, color, center_point, radius, width) Draw a circle

pygame.draw.ellipse (surface, color, bounding_rectange, width) draw an ellipse

pygame.draw.rect (surface, color, rectangle_tuple, width) draw a rectangle


Pygame,pixelarray Object

Pygame does not have a separate draw point function. And the method is to create Pygame. The Pixelarray object then sets the independent pixel point. Creating a Pixelarray object about surface will ' lock ' the surface object. Once the surface object is locked, the paint function can still be used, but a picture like PNG or JPG doesn't work on the surface object, and if you want to see if a surface object is locked, you can call the Surface object Get_locked () method, If it is locked, it returns true, and vice versa.

The methods for setting these independent pixels are accessed with two index values, such as pixobj[480][380] = black, which sets the x-coordinate value of the 480 and y coordinates of the 380 points to black.

To tell Pygame you finished drawing the independent pixel, you need to delete the Pixarray object through a del statement. Deleting a Pixarray object will ' unlock ' the surface object, so you can continue loading the image on it. If you forget to delete it, the next time you want to load a picture on your surface object, you'll get an error.

Pygame.display.update () function

After you have completed all the drawing functions, you must call the Pygame.display.update () function to make it appear on the monitor.

One thing to keep in mind is that the pygame.display.update () function only shows the display object (that is, the object returned by calling Pygame.display.set_mode ()) to the screen. If you want the pictures to appear on the screen, you must "blit" them (copying their meaning) onto the display object via the Blit () method.

Animation

It's really simple to move an image from one place to another, to erase the image from where it was originally, and then load the image in another place so that it looks like it's moving. In the computer's view, it is actually a group of pixels moving. is an example


Here is a sample code

Import Pygame, Sysfrom pygame.locals import *pygame.init () FPS = # frames per Second Settingfpsclock = Pygame.time.Clock () # set up the Windowdisplaysurf = Pygame.display.set_mode ((+ 0), pygame.display.set_caption (' Animation ') White = (255, 255, 255) catimg = pygame.image.load (' cat.png ') catx = 10caty = 10direction = "Right" while True: # the main GA Me loop Displaysurf.fill (white) if direction = = ' Right ': catx + = 5 if Catx = = 280:directio n = ' down ' elif direction = = ' Down ': caty + = 5 if Caty = = 220:direction = ' Left ' elif dire  ction = = ' Left ': catx-= 5 if Catx = = 10:direction = ' up ' elif direction = = ' Up ': caty -= 5 if Caty = = 10:direction = ' right ' Displaysurf.blit (catimg, (Catx, Caty)) as event in Pygam    E.event.get (): if Event.type = = QUIT:pygame.quit () sys.exit () pygame.display.update () Fpsclock.tick (FPS)

One of the cat.png PDFs above has been given. After downloading, it must be placed in a folder with the source file. The effect is that a cat moves inside a window. First right, then down, then left, then up, so loop.

Something new has appeared in the code above.

Frame Rate and Pygame.time.Clock objects

Frame rate and refresh rate refers to the number of pictures that the program can display in one second, expressed as FPS and frames per second. (on other computer monitors, FPS has a popular name called Hertz, many monitors have a 60 Hz frame rate, or 60 frames per second) low frame rate appears in some image games will cause the game to appear to be jumping and jitter, if the program has too much code and often refresh the screen, Will cause the FPS to drop. But the game in this book is simple enough so that even on some old computers there is no problem.

The Pygame.time.Clock object helps us to confirm that the program can run to a maximum determined FPS value. The clock object will insert a small pause in the main game loop to ensure that our game program does not run too fast. If we don't have these pauses, our game will run as fast as the computer. It was really fast for the people who played the game. Calling the Tick () method of the clock object in the game loop will ensure that our game runs to a certain speed no matter how fast the computer runs. Create a Clock object with the following statement

Fpsclock = Pygame.time.Clock ()

The Tick method should be called at the very end of the game loop, that is, after pygame.display.update, the call statement is as follows

Fpsclock.tick (FPS)

Make games with Python & Pygame (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.