Pygame Study Notes (2)-from painting points to animation

Source: Internet
Author: User

Reprint Please note: @ small Wuyi http://www.cnblogs.com/xiaowuyi

1. single pixel (painting point)
There are three main methods to use pygame to draw points:
Method 1: draw a square with a length and width of 1 pixel

 #  @ Xiaowuyi http://www.cnblogs.com/xiaowuyi  Import  Pygame, syspygame. INIT () Screen = Pygame. display. set_caption ( '  Hello world!  '  ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ]) Pygame. Draw. rect (screen, [0, 0], [ , 50,], 1) # Draw a rectangle of 1*1 with a line width of 1. The line width cannot be 0, because 1*1 has no blank area.  Pygame. display. Flip ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit () 

 

Method 2: Draw a circle with a diameter of 1

 #  @ Xiaowuyi http://www.cnblogs.com/xiaowuyi  Import  Pygame, syspygame. INIT () Screen = Pygame. display. set_caption ( '  Hello world!  '  ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ]) Pygame. Draw. Circle (screen, [0, 0], [ 150,200 ], ) Pygame. display. Flip ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type =Pygame. Quit: SYS. Exit () 

 

Method 3: This method does not change the color of a point on the surface. It looks like a screen. set_at () is drawn (). To obtain the color of a pixel, use screen. get_at ().

 #  @ Xiaowuyi http://www.cnblogs.com/xiaowuyi  Import  Pygame, syspygame. INIT () Screen = Pygame. display. set_caption ( '  Hello world!  '  ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ]) Screen. set_at ([ 150,150], [, 0]) #  Set 150,150 to red.  Pygame. display. Flip ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit () 

2. connect multiple points to form a line

The pygame. Draw. Lines () method can connect multiple points into a line. This method has five parameters: surface, color, closed line, or non-closed line (if closed to true, otherwise false), vertex list, and line width. Pygame. Draw. Lines (surface, [color], false/true, plotpoints, 1 ). The following example shows a road:

 # @ Xiaowuyi http://www.cnblogs.com/xiaowuyi  Import  Pygame, sys  Def Lineleft (): #  Draw the left border of the road Plotpoints = []  For X In Range (0,640 ): Y -5 * x + 1000 Plotpoints. append ([x, y]) pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Pygame. display. Flip ()  Def Lineright (): #  Draw the right border of the road Plotpoints = []  For X In Range (0,640 ): Y = 5 * x-2000 Plotpoints. append ([x, y]) pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Pygame. display. Flip ()  Def Linemiddle (): #  Draw a dotted line in the middle of the road Plotpoints = [] X = 300For Y In Range (0,480, 20 ): Plotpoints. append ([x, y])  If Len (plotpoints) = 2 : Pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Plotpoints = [] Pygame. display. Flip () pygame. INIT () Screen = Pygame. display. set_caption ( '  Hello world!  '  ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ]) Lineleft () lineright () linemiddle ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit () 

 

3. reference images
In pygame, the simplest way to reference images is the image function. Add a car to the street instance. First, the pygame. image. Load () function loads an image from the hard disk and creates an object named my_car. Here, my_car is a surface, but it is in memory and is not displayed. Then, copy my_car to the screen using the blit (Block shifting) method to display it. DetailsCodeAs follows:

 #  @ Xiaowuyi http://www.cnblogs.com/xiaowuyi Import  Pygame, sys  Def  Lineleft (): plotpoints = []  For X In Range (0,640 ): Y -5 * x + 1000 Plotpoints. append ([x, y]) pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Pygame. display. Flip ()  Def  Lineright (): plotpoints = [] For X In Range (0,640 ): Y = 5 * x-2000 Plotpoints. append ([x, y]) pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Pygame. display. Flip ()  Def  Linemiddle (): plotpoints = [] X = 300 For Y In Range (0,480, 20 ): Plotpoints. append ([x, y])  If Len (plotpoints) = 2: Pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Plotpoints = [] Pygame. display. Flip ()  Def Loadcar (): #  Load car Images My_car = pygame. image. Load ( '  Ok1.jpg  ' ) #  Ok1.jpg file under the current folder Screen. bcar (my_car, [320,320 ]) Pygame. display. Flip () pygame. INIT () Screen = Pygame. display. set_caption ('  Hello world!  '  ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ]) Lineleft () lineright () linemiddle () loadcar ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit () 

 

Material: ok1.jpg

4. Animation
Computer Animation is actually to move an image from one place to another. At the same time, the display of several connected actions will produce a realistic effect. Therefore, in animation, there are three most basic factors to consider: first, time, time to move, how long to change to the next action, and second, location, from the position to the position, the third is the action, and the continuity of the two actions. In this example, because the car is down-looking, the wheel turns are not actually visible, so you don't need to consider the changes in continuous action, but just consider the location of the car and how long it takes to move. Step 1: pygame. time. delay () to implement time delay; Step 2 uses pygame. draw. rect () overwrites the original image; Step 3 screen. BITs () introduce the image at the new position. The following example shows how a car goes in and out.

 #  @ Xiaowuyi http://www.cnblogs.com/xiaowuyi  Import  Pygame, sys  Def  Lineleft (): plotpoints = []  For X In Range (0,640): Y -5 * x + 1000 Plotpoints. append ([x, y]) pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Pygame. display. Flip ()  Def  Lineright (): plotpoints = []  For X In Range (0,640 ): Y = 5 * x-2000 Plotpoints. append ([x, y]) pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Pygame. display. Flip () Def  Linemiddle (): plotpoints = [] X = 300 For Y In Range (0,480, 20 ): Plotpoints. append ([x, y])  If Len (plotpoints) = 2 : Pygame. Draw. Lines (screen, [0, 0], false, plotpoints, 5 ) Plotpoints = [] Pygame. display. Flip ()  Def  Loadcar (yloc): my_car = Pygame. image. Load ( '  Ok1.jpg  '  ) Locationxy = [1, 310 , Yloc] screen. bcar (my_car, locationxy) pygame. display. Flip ()  If   _ Name __ = '  _ Main __  '  : Pygame. INIT () Screen = Pygame. display. set_caption ( '  Hello world!  ' ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ]) Lineleft () lineright () linemiddle ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit ()  For Logoff In Range (480,-140,-50): Pygame. Time. Delay ( 200 ) Pygame. Draw. rect (screen ,[ 255,255,255], [310, (logoff + 132), 83,132 ], 0) loadcar (logoff) 

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.