SoFunction
Updated on 2024-11-15

pygame learning notes (2): three methods of drawing points and animation examples

1. Individual pixels (drawing points)

There are three main ways to draw points using pygame:
Method 1: Draw a square with a length and width of 1 pixel

Copy Code The code is as follows.

import pygame,sys
()
screen=.set_caption('hello world!')
screen=.set_mode([640,480])
([255,255,255])
(screen,[0,0,0],[150,50,1,1],1) #Draw a 1*1 rectangle with a line width of 1. It can't be 0 here because 1*1 has no blank area.
()
while True:
for event in ():
if ==:
()

Method 2: Draw a circle with a diameter of 1

Copy Code The code is as follows.

import pygame,sys
()
screen=.set_caption('hello world!')
screen=.set_mode([640,480])
([255,255,255])
(screen,[0,0,0],[150,200],1,1)
()
while True:
for event in ():
if ==:
()

Method 3: This method doesn't draw on it, but changes the color of a point on the surface so that it looks like a point was drawn screen.set_at(). Alternatively, to get the color of a pixel, you can use screen.get_at().
Copy Code The code is as follows.

import pygame,sys
()
screen=.set_caption('hello world!')
screen=.set_mode([640,480])
([255,255,255])
screen.set_at([150,150],[255,0,0])# change 150,150 to red.
()
while True:
for event in ():
if ==:
()

2、Connect multiple points to form a line

The () method connects multiple points into a line. The method has 5 parameters: surface surface, color, closed or non-closed line (True if closed, False otherwise), list of points, line width. (surface,[color],False/True, plotpoints,1). The following example draws a road as follows:

Copy Code The code is as follows.

import pygame,sys
def lineleft(): #draw the left boundary of the road
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
([x,y])
(screen,[0,0,0],False,plotpoints,5)
()
def lineright():#draw the right boundary of the street
plotpoints=[]
for x in range(0,640):
y=5*x-2000
([x,y])
(screen,[0,0,0],False,plotpoints,5)
()
def linemiddle():#Draw the dotted line in the middle of the street
plotpoints=[]
x=300
for y in range(0,480,20):
([x,y])
if len(plotpoints)==2:
(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
()

()
screen=.set_caption('hello world!')
screen=.set_mode([640,480])
([255,255,255])
lineleft()
lineright()
linemiddle()
while True:
for event in ():
if ==:
()

3. Citing images
The easiest way to reference an image in pygame in a barbaric way is the image function. Below is an example of adding a car to the road example. The first() function loads an image from the hard disk and creates an object named my_car. Here, my_car is a surface, but is present in memory, and did not display, and then use blit (block shift) method to copy my_car to the surface of the screen surface, so as to display. The specific code is as follows:

Copy Code The code is as follows.

import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
([x,y])
(screen,[0,0,0],False,plotpoints,5)
()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
([x,y])
(screen,[0,0,0],False,plotpoints,5)
()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
([x,y])
if len(plotpoints)==2:
(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
()
def loadcar(): #Load car image
my_car=('') #files in current folder
(my_car,[320,320])
()

()
screen=.set_caption('hello world!')
screen=.set_mode([640,480])
([255,255,255])
lineleft()
lineright()
linemiddle()
loadcar()
while True:
for event in ():
if ==:
()

Vegetation:

4. Animation

Computer animation is actually moving the image from one place to another, at the same time a few connecting actions to display will produce a realistic effect. Therefore, in doing animation, the most basic to consider the main three factors, one is time, what time to move, how long to change the next action, two is the position, from what position to what position, three is the action, before and after the continuity of the two actions. In this example, since the car is looking down, the wheel rotation is not actually visible, so instead of considering the change of continuous action, just consider the position of the car and how long it takes to move. The first step () is used to implement the time delay; the second step uses () to overwrite the image at the original position; and the third step () introduces the image at the new position. The following example realizes the process of a car from driving in to driving out.

Copy Code The code is as follows.

import pygame,sys
def lineleft():
    plotpoints=[]
    for x in range(0,640):
        y=-5*x+1000
        ([x,y])
    (screen,[0,0,0],False,plotpoints,5)
    ()
def lineright():
    plotpoints=[]
    for x in range(0,640):
        y=5*x-2000
        ([x,y])
    (screen,[0,0,0],False,plotpoints,5)
    ()   
def linemiddle():
    plotpoints=[]
    x=300
    for y in range(0,480,20):
        ([x,y])
        if len(plotpoints)==2:
            (screen,[0,0,0],False,plotpoints,5)
            plotpoints=[]
    ()
def loadcar(yloc):
    my_car=('')
    locationxy=[310,yloc]
    (my_car,locationxy)
    ()

   
if __name__=='__main__':
    ()
    screen=.set_caption('hello world!')
    screen=.set_mode([640,480])
    ([255,255,255])
    lineleft()
    lineright()
    linemiddle()

    while True:
        for event in ():
            if ==:
                ()
        for looper in range(480,-140,-50):
            (200)
            (screen,[255,255,255],[310,(looper+132),83,132],0)
            loadcar(looper)