Pygame Learning Notes (3): Movement rate, time, event, text _python

Source: Internet
Author: User

1. Motion Rate

In the previous section, a car was implemented on the road from bottom to top, and the Pygame.time.delay (200) was used for time delay. See a lot of reference material, basic each material will talk about the different configuration machine movement rate of the problem, some by setting the frequency to solve, some by setting speed to solve, their own level is limited, read a few, think or "beginning Game Development with Python and Pygame It's better to mention a method. The code is as follows, the code changes in the main is the code in Main, which uses Clock=pygame.time.clock () to define the clock, speed=250.0 defined 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 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 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 is used to solve the keyboard, mouse, remote control and so on after the input to make what reflect. For example, the above example, you can press on the direction of the key to make the car upward movement, press down, so that the car moving downward. When the car is poured out from below, it will appear from above, when the car from the 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 is when you press the UP ARROW, the car goes forward. If Event.key==pygame. K_down, on the contrary, refers to the downward arrow, the car back.

Copy 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

Use the font module to do the text display of the game, most games will have such as score, time, life value and so on text information. Pygame is mainly done using the Pygame.font module, and some of the methods commonly used are:
Pygame.font.SysFont (None, 16), the first parameter is the description font, can be "Arial" and so on, None represents the default font. The second parameter represents the size of the word. If you don't know what fonts are in the current system, you can use Pygame.font.get_fonts () to get all the available fonts.
Pygame.font.Font ("Aaa.ttf", 16), used to use TTF font files.
Render ("Hello world!", True, (0,0,0), (255, 255, 255), Render method is used to create text. The first argument is the text that is written; the second argument turns on anti-aliasing, which means that the font is smoother, but the corresponding speed has a slight effect; the third parameter is the color of the font, the fourth is the background color, and no transparency is shown.
The following example adds the current car coordinates:

Copy 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, let the background redraw directly, no longer like 1, 2 inside the blank rect to cover the front module.

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.