Pygame Study Notes (2): Three ways to draw points and animated instances

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 wide

The code is 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 a rectangle with a line width of 1, which cannot be 0, since 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 Two: Draw a circle with a diameter of 1

The code is 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 surface, so it looks like a dot Screen.set_at (). In addition, if you want to get the color of a pixel, you can use Screen.get_at ().

The code is 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, connecting multiple points to form a line

The Pygame.draw.lines () method can connect multiple points to a line. The method has 5 parameters: surface surfaces, colors, closed lines, or non-closed lines (false if closed), list of points, LineWeight. Pygame.draw.lines (surface,[color],false/true,plotpoints,1). The following example draws a road, specifically as follows:

The code is 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 images referenced in Pygame is the image function. Below in the example of the road, add a car. 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 exists in memory, it is not displayed, and then it is displayed by copying the my_car to the screen surface using the Blit (block) method. The specific code is as follows:

The code is 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, at the same time a few connection actions to explain the display will produce a realistic effect. Therefore, in doing animation, the most basic factors to consider are 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 two movement continuity. In this example, because the car is looking down, so the wheel is not actually visible, so do not consider the changes in the continuous movement, but only to consider the location of the car and how long to move. The first step Pygame.time.delay () to achieve the time delay, the second step is to use Pygame.draw.rect () to overwrite the image of the original position, and the third Step Screen.blit () introduces the image in the new position. The following example realizes the process of driving from the car to the exit.

The code is 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)

  • Related Article

    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.