Pygame Learning Notes (2): Three ways to draw points and animation examples _python

Source: Internet
Author: User

1, single pixel (draw point)

There are three main ways to draw points using Pygame:
Method One: Draw a square with a length of 1 pixels

Copy Code code as follows:

Import Pygame,sys
Pygame.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,0],[150,50,1,1],1) #画1 the *1 rectangle, the line width is 1, this cannot be 0, because 1*1 no blank area.
Pygame.display.flip ()
While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()

Method Two: Draw a circle with a diameter of 1

Copy Code code as follows:

Import Pygame,sys
Pygame.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,0],[150,200],1,1)
Pygame.display.flip ()
While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()

Method Three: This method is not painted, but changes the color of a point on the surface, so it looks like a point screen.set_at (). In addition, if you want to get the color of a pixel, you can use Screen.get_at ().
Copy Code code as follows:

Import Pygame,sys
Pygame.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],[255,0,0]) #将150, 150 changed 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 to a line. The method has 5 parameters: the surface surface, the color, the closed line, or the closed line (false if the closure is true), the list of points, the line width. Pygame.draw.lines (surface,[color],false/true,plotpoints,1). The following example draws a road, as follows:

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 ()

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 image
the simplest of the pygame to refer to an image in the Yi is the image function. In the example of the road below, join a car. First the Pygame.image.load () function loads an image from the hard disk and creates an object named My_car. Here, the My_car is a surface, but it is in memory, it is not displayed, and then the Blit (Block Move) method is used to copy the My_car to the screen surface, which is displayed. The specific code is as follows:

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 (): #载入car图像
My_car=pygame.image.load (' ok1.jpg ') #当前文件夹下的ok1. jpg file
Screen.blit (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 the image from one place to another place, while several connected actions to show the display will produce realistic results. Therefore, in the animation, the most basic factors to consider is three, one is the time, what time to move, how long time to change the next action, the second is the position, from what position to what position, three is the movement, before and after the continuity of two movements. In this example, because the car is overlooking, so the wheel rotation is actually invisible, so do not consider the change of continuous action, but only consider the position of the car and how long time to move. The first step Pygame.time.delay () to achieve the time delay, the second step uses Pygame.draw.rect () to cover the original position of the image, and the third Step Screen.blit () introduces the image in the new location. The following example implements the process of the car from entering to coming out.

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 ()

While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()
For Looper in range (480,-140,-50):
Pygame.time.delay (200)
Pygame.draw.rect (screen,[255,255,255],[310, (looper+132), 83,132],0)
Loadcar (Looper)

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.