Pygame Study Notes (3)-time, event, and text

Source: Internet
Author: User

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

1. Motion Rate
In the previous section, a car was driven down the road and the time was delayed using pygame. Time. Delay (200. After reading a lot of reference materials, basically every material will talk about the speed of motion under different configurations of machines. Some are solved by setting the frequency, some are solved by setting the speed, and their own levels are limited, after reading a few articles, I think it is better to refer to a method mentioned in "beginning game development with Python and pygame.CodeAs shown in the following figure, the main code is changed in the code, with clock = pygame. time. clock () is used to define the clock. speed = 250.0 defines the speed, 250 pixels per second, and time_passed = clock. tick () indicates that the unit of last run time is millisecond, time_passed_seconds = time_passed/1000.0 changes the unit to second, and distance_moved = time_passed_seconds * speed times the speed to get the moving distance, which ensures smoother performance.

 #  @ 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-2000Plotpoints. 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 () Clock = Pygame. Time. Clock () Logoff = 480 Speed = 250.0 While  True:  For Event In Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit () pygame. Draw. rect (screen ,[ 255,255,255], [310, (logoff + 132), 83,132 ], 0) time_passed = Clock. Tick () time_passed_seconds = Time_passed/1000.0 Distance_moved = Time_passed_seconds * Speed Logoff -= Distance_moved  If Logint <-480 : Logoff = 480Loadcar (logoff) 

2. Events

What I understand is how the keyboard, mouse, remote control, and other inputs are reflected. For example, in the above example, you can move the car up by pressing the up arrow key, and press down to move the car down. When the car goes out from below, it will appear again from above, when the car goes out from above, it will appear again from below. The Code is as follows. Event. type = pygame. keydown is used to define the event type. If event. Key = pygame. k_up, the vehicle advances when the up arrow is pressed. If event. Key = pygame. k_down, it means to press the down arrow to move the car back.

 #  @ 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 () loft = 480 While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit ()  Elif Event. type =Pygame. keydown:  If Event. Key = Pygame. k_up: Logoff = Login-50 If Logint <-480 : Logoff = 480 Pygame. Draw. rect (screen ,[ 255,255,255], [310, (logoff + 132), 83,132 ], 0) loadcar (logoff)  If Event. Key = Pygame. k_down: Logoff = Logoff + 50If Logoff> 480 : Logoff =-480 Pygame. Draw. rect (screen ,[ 255,255,255], [310, (Logoff-132), 83,132 ], 0) loadcar (logoff) 

3. font and Character Display
The font module is used to display text for games. Most games have text information such as score, time, and life value. Pygame is mainly implemented using the pygame. Font module. Some common methods are as follows:
Pygame. Font. sysfont (none, 16). The first parameter indicates the font, which can be "Arial". Here none indicates the default font. The second parameter indicates the word size. If you cannot know which fonts are installed in the current system, you can use pygame. Font. get_fonts () to obtain all available fonts.
Pygame. Font. Font ("AAA. TTF", 16), used to use TTF font files.
Render ("Hello world! ", True, (255,255,255, 0), (), the render method is used to create text. The first parameter is the written text. The second parameter indicates whether to enable anti-aliasing. If it is true, the font will be smoother, but the corresponding speed will be slightly affected. The third parameter is the font color; the fourth is the background color, which is transparent.
The following example adds the coordinates of the current car:

 # @ 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 ()  Def  Loadtext (xloc, yloc): textstr = '  Location:  ' + STR (xloc) + '  ,  ' + STR (yloc) text_screen = My_font.render (textstr, true, (255 , 0, 0) screen. bloads (text_screen ,( 50, 50 ))  If  _ Name __ = '  _ Main __  '  : Pygame. INIT () Screen = Pygame. display. set_caption ( '  Hello world!  '  ) Screen = Pygame. display. set_mode ([640,480 ]) My_font = Pygame. Font. sysfont (none, 22 ) Screen. Fill ([ 255,255,255 ]) Loadtext ( 310, 0) lineleft () lineright () linemiddle () loft = 480 While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit ()  Elif Event. type = Pygame. keydown:  If Event. Key = Pygame. k_up: Logoff = Login-50 If Logint <-132 : Logoff = 480 If Event. Key = Pygame. k_down: Logoff = Logoff + 50 If Logoff> 480 : Logoff =-132 Loadtext ( 310 , Logoff) screen. Fill ([ 255,255,255 ]) Loadtext ( 310, Loft) lineleft () lineright () linemiddle () loadcar (locar) 

In this example, the background will be re-painted, and the blank rect will not be used to overwrite the previous module as in 1 and 2.

 

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.