Pygame Study Notes (3): Motion rate, time, event, text

Source: Internet
Author: User
1. Movement Rate

In the previous section, a car was implemented on the road from bottom to top, and Pygame.time.delay (200) was used to delay the time. Read a lot of reference materials, basic each material will talk about the different configuration of the machine under the speed of the problem, some by setting the frequency to solve, some through the set speed to solve, their own level limited, see a few, think or "Beginning Game development with Python and Pygame This refers to a better method. The code below, where the code changes in the main code, which uses Clock=pygame.time.clock () to define the clock, speed=250.0 defines the speed, 250 pixels per second, time_passed=clock.tick () For the last run time unit is milliseconds, time_passed_seconds=time_passed/1000.0 changes the unit to seconds, distance_moved=time_passed_seconds*speed time times the speed to get the moving distance , so that you can be more fluent.

Copy the Code code as follows:


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,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,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,0],false,plotpoints,5)
Plotpoints=[]
Pygame.display.flip ()
def loadcar (Yloc):
My_car=pygame.image.load (' ok1.jpg ')
Locationxy=[310,yloc]
Screen.blit (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 ()
looper=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, (looper+132), 83,132],0)
Time_passed=clock.tick ()
time_passed_seconds=time_passed/1000.0
Distance_moved=time_passed_seconds*speed
Looper-=distance_moved

If looper<-480:
looper=480
Loadcar (Looper)

2. Events

I understand that is used to solve the keyboard, mouse, remote control and other input to make a reflection of what. For example, the above example can be made by pressing the upper direction key to make the car move upward, press down, so that the car downward movement. When the trolley is poured from below, it will appear again from above, when the car from above, will appear from below. The code is as follows. Event.type = = Pygame. KeyDown is used to define the event type, if Event.key==pygame. K_up here refers to the car moving forward when the UP arrow is pressed. If Event.key==pygame. K_down, on the other hand, refers to the downward arrow, which backs the car.

Copy the Code code as follows:


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,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,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,0],false,plotpoints,5)
Plotpoints=[]
Pygame.display.flip ()
def loadcar (Yloc):
My_car=pygame.image.load (' ok1.jpg ')
Locationxy=[310,yloc]
Screen.blit (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 ()

looper=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:
Looper=looper-50
If looper<-480:
looper=480

Pygame.draw.rect (screen,[255,255,255],[310, (looper+132), 83,132],0)
Loadcar (Looper)
If Event.key==pygame. K_down:
Looper=looper+50
If looper>480:
looper=-480
Pygame.draw.rect (screen,[255,255,255],[310, (looper-132), 83,132],0)
Loadcar (Looper)

3. Font and character display

Using the font module to do the game text display, most games will have such as scores, time, health and other text messages. Pygame is mainly done using the Pygame.font module, and some of the methods used are:
Pygame.font.SysFont (None, 16), the first parameter is a description of the font, can be "Arial" and so on, where None represents the default font. The second parameter represents the size of a word. If you do not know which fonts are loaded in the current system, you can use Pygame.font.get_fonts () to get all available fonts.
Pygame.font.Font ("Aaa.ttf", 16), used to use TTF font files.
Render ("Hello world!", True, (0,0,0), (255, 255, 255)), the Render method is used to create the text. The first parameter is the written text, the second argument is anti-aliasing, that is true, the font will be smoother, but the corresponding speed has a little effect; the third parameter is the color of the font; The fourth one is the background colour and no transparency.
The following example adds the current car coordinates:
Copy the Code code as follows:


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,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,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,0],false,plotpoints,5)
Plotpoints=[]
Pygame.display.flip ()
def loadcar (Yloc):
My_car=pygame.image.load (' ok1.jpg ')
Locationxy=[310,yloc]
Screen.blit (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.blit (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 ()

looper=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:
Looper=looper-50
If looper<-132:
looper=480
If Event.key==pygame. K_down:
Looper=looper+50
If looper>480:
looper=-132
LoadText (310,looper)
Screen.fill ([255,255,255])
LoadText (310,looper)
Lineleft ()
Lineright ()
Linemiddle ()
Loadcar (Looper)

In this example, you can simply redraw the background and not overwrite the previous module with a blank rect like 1 or 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.