📜  Pygame – 绘制对象和形状

📅  最后修改于: 2022-05-13 01:55:37.194000             🧑  作者: Mango

Pygame – 绘制对象和形状

在本文中,我们将了解如何使用 Pygame 绘制对象。绘制任何形状可以有两个版本,可以是实心的,也可以只是它的轮廓。

在 PyGame 中绘制对象和形状

您可以使用 pygame 的 draw 方法在 pygame 中轻松绘制基本形状。

绘制矩形形状:

要在您的 pygame 项目中绘制一个矩形,您可以使用 draw.rect()函数。

首先,导入需要的模块并初始化pygame。现在,使用 pygame 的 display.set_mode() 方法创建特定维度的表面对象。使用 pygame 的 fill()函数用白色填充表面对象的背景。使用 pygame 的 draw.rect() 方法创建一个矩形。更新 Surface 对象。

示例 1:使用 pygame 绘制轮廓矩形。

Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the outlined rectangle
pygame.draw.rect(window, (0, 0, 255), 
                 [100, 100, 400, 100], 2)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the solid rectangle
pygame.draw.rect(window, (0,   0, 255),
                 [100, 100, 400, 100], 0)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the solid circle
pygame.draw.circle(window, (0, 255, 0), 
                   [300, 300], 170, 3)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the solid circle
pygame.draw.circle(window, (0, 255, 0),
                   [300, 300], 170, 0)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the outlined polygon
pygame.draw.polygon(window, (255, 0, 0), 
                    [[300, 300], [100, 400],
                     [100, 300]])
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the outlined polygon
pygame.draw.polygon(window, (255, 0, 0), 
                    [[300, 300], [100, 400],
                     [100, 300]], 5)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the line
pygame.draw.line(window, (0, 0, 0), 
                 [100, 300], 
                 [500, 300], 5)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the rectangle
pygame.draw.rect(window, (0, 0, 255),
                 [50, 200, 500, 200])
  
# Using draw.rect module of
# pygame to draw the circle inside the rectangle
pygame.draw.circle(window, (0, 255, 0),
                   [300, 300], 100)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Creating a list of different rects
rectangle_list = [pygame.Rect(50, 100, 500, 200),
                  pygame.Rect(70, 120, 460, 160),
                  pygame.Rect(90, 140, 420, 120),
                  pygame.Rect(110, 160, 380, 80),
                  pygame.Rect(130, 180, 340, 40)
                  ]
  
# Creating list of different colors
color_list = [(0,   0,   0),
              (255, 255, 255),
              (0,   0, 255),
              (0, 255,   0),
              (255,   0,   0)
              ]
  
# Creating a variable which we will use
# to iterate over the color_list
color_var = 0
  
# Iterating over the rectangle_list using
# for loop
for rectangle in rectangle_list:
  
    # Drawing the rectangle 
    # using the draw.rect() method
    pygame.draw.rect(window, color_list[color_var],
                     rectangle)
  
    # Increasing the value of color_var
    # by 1 after every iteration
    color_var += 1
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# Creating Drawing function
  
  
def drawingfunction(x, y, width, height):
  
    # Creating rectangle using the draw.rect() method
    pygame.draw.rect(window, (0, 0, 255), [x, y, width, height])
  
    # Calculation the center of the circle
    circle_x = width/2 + x
    circle_y = height/2 + y
  
    # Calculating the radius of the circle
    if height < width:
        radius = height/2
    else:
        radius = width/2
  
    # Drawing the circle
    pygame.draw.circle(window, (0, 255, 0), [circle_x, circle_y], radius)
  
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Calling the drawing function
drawingfunction(50, 200, 500, 200)
  
# Draws the surface object to the screen.
pygame.display.update()


Python3
# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# creating list in which we will store
# the position of the circle
circle_positions = []
  
# radius of the circle
circle_radius = 60
  
# Color of the circle
color = (0, 0, 255)
  
# Creating a variable which we will use
# to run the while loop
run = True
  
# Creating a while loop
while run:
  
    # Iterating over all the events recieved from
    # pygame.event.get()
    for event in pygame.event.get():
  
        # If the type of the event is quit
        # then setting the run variable to false
        if event.type == QUIT:
            run = False
  
        # if the type of the event is MOUSEBUTTONDOWN
        # then storing the current position
        elif event.type == MOUSEBUTTONDOWN:
            position = event.pos
            circle_positions.append(position)
              
    # Using for loop to iterate
    # over the circle_positions
    # list
    for position in circle_positions:
  
        # Drawing the circle
        pygame.draw.circle(window, color, position,
                           circle_radius)
  
    # Draws the surface object to the screen.
    pygame.display.update()


输出 :

我们可以通过将宽度参数设置为 0 来创建一个实心矩形,其余方法保持不变。

示例 2:绘制一个实心矩形。

蟒蛇3



# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the solid rectangle
pygame.draw.rect(window, (0,   0, 255),
                 [100, 100, 400, 100], 0)
  
# Draws the surface object to the screen.
pygame.display.update()

输出 :

绘制圆形:

要在您的 pygame 项目中绘制一个圆圈,您可以使用 draw.circle()函数。整个方法和上面一样,只是函数和参数做了相应的改变。

示例 1:绘制一个空心圆。

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the solid circle
pygame.draw.circle(window, (0, 255, 0), 
                   [300, 300], 170, 3)
  
# Draws the surface object to the screen.
pygame.display.update()

输出 :

我们可以通过将宽度参数设置为 0 来创建一个实心圆。



示例 2:绘制一个实心圆

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the solid circle
pygame.draw.circle(window, (0, 255, 0),
                   [300, 300], 170, 0)
  
# Draws the surface object to the screen.
pygame.display.update()

输出 :

同样,您可以使用 pygame 的 draw 模块绘制其他基本形状。

绘制多边形形状:

可以使用polygon()函数绘制所需的多边形。

同样,方法保持不变,只是函数和参数发生了变化。

示例 1:绘制实心多边形

蟒蛇3



# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the outlined polygon
pygame.draw.polygon(window, (255, 0, 0), 
                    [[300, 300], [100, 400],
                     [100, 300]])
  
# Draws the surface object to the screen.
pygame.display.update()

输出 :

示例 2:绘制空心多边形

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the outlined polygon
pygame.draw.polygon(window, (255, 0, 0), 
                    [[300, 300], [100, 400],
                     [100, 300]], 5)
  
# Draws the surface object to the screen.
pygame.display.update()

输出:

画线形状:

线是最基本的绘图实体,可以在 pygame 中使用 line()函数进行绘制。

示例 1:画一条线

蟒蛇3



# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the line
pygame.draw.line(window, (0, 0, 0), 
                 [100, 300], 
                 [500, 300], 5)
  
# Draws the surface object to the screen.
pygame.display.update()

输出:

绘制多个形状:

您可以在同一个表面对象上绘制多个形状。为此,首先导入所需的模块并初始化 pygame。现在,使用 pygame 的 display.set_mode() 方法创建特定维度的表面对象。使用 pygame 的 fill()函数用白色填充表面对象的背景。创建所需的形状如上所述。更新 Surface 对象

示例 1:在矩形内画一个圆。

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Using draw.rect module of
# pygame to draw the rectangle
pygame.draw.rect(window, (0, 0, 255),
                 [50, 200, 500, 200])
  
# Using draw.rect module of
# pygame to draw the circle inside the rectangle
pygame.draw.circle(window, (0, 255, 0),
                   [300, 300], 100)
  
# Draws the surface object to the screen.
pygame.display.update()

输出:

示例 2:在彼此内部绘制矩形。

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Creating a list of different rects
rectangle_list = [pygame.Rect(50, 100, 500, 200),
                  pygame.Rect(70, 120, 460, 160),
                  pygame.Rect(90, 140, 420, 120),
                  pygame.Rect(110, 160, 380, 80),
                  pygame.Rect(130, 180, 340, 40)
                  ]
  
# Creating list of different colors
color_list = [(0,   0,   0),
              (255, 255, 255),
              (0,   0, 255),
              (0, 255,   0),
              (255,   0,   0)
              ]
  
# Creating a variable which we will use
# to iterate over the color_list
color_var = 0
  
# Iterating over the rectangle_list using
# for loop
for rectangle in rectangle_list:
  
    # Drawing the rectangle 
    # using the draw.rect() method
    pygame.draw.rect(window, color_list[color_var],
                     rectangle)
  
    # Increasing the value of color_var
    # by 1 after every iteration
    color_var += 1
  
# Draws the surface object to the screen.
pygame.display.update()

输出 :



编写自己的绘图函数:

您可以在 pygame 中轻松创建自己的专用绘图功能。

这可以通过遵循给定的程序来完成。创建一个以起始位置、宽度和高度为参数的绘图函数。绘制所需的形状如上所述。调用 drawfunction()

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# Creating Drawing function
  
  
def drawingfunction(x, y, width, height):
  
    # Creating rectangle using the draw.rect() method
    pygame.draw.rect(window, (0, 0, 255), [x, y, width, height])
  
    # Calculation the center of the circle
    circle_x = width/2 + x
    circle_y = height/2 + y
  
    # Calculating the radius of the circle
    if height < width:
        radius = height/2
    else:
        radius = width/2
  
    # Drawing the circle
    pygame.draw.circle(window, (0, 255, 0), [circle_x, circle_y], radius)
  
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# Calling the drawing function
drawingfunction(50, 200, 500, 200)
  
# Draws the surface object to the screen.
pygame.display.update()

输出:

用鼠标绘制形状:

现在让我们看看如何在用户单击鼠标时创建形状。我们将在下一个示例中创建圆形,但您可以创建任何您想要的形状。

创建一个列表来存储要绘制的形状的位置。创建一个变量来存储形状的颜色。创建一个变量,我们将使用它来运行 while 循环并创建一个 while 循环。迭代从 pygame.event.get() 接收到的所有事件。如果事件类型为退出,则将运行变量设置为 false。如果事件的类型是 MOUSEBUTTONDOWN(当用户按下鼠标按钮时发生此事件),则获取变量中的当前位置,然后将该位置附加到我们的 circle_positions 列表中。迭代使用 for 循环创建的数组中的所有位置。继续绘制形状。更新表面对象。

蟒蛇3

# Importing pygame module
import pygame
from pygame.locals import *
  
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
  
# Fill the scree with white color
window.fill((255, 255, 255))
  
# creating list in which we will store
# the position of the circle
circle_positions = []
  
# radius of the circle
circle_radius = 60
  
# Color of the circle
color = (0, 0, 255)
  
# Creating a variable which we will use
# to run the while loop
run = True
  
# Creating a while loop
while run:
  
    # Iterating over all the events recieved from
    # pygame.event.get()
    for event in pygame.event.get():
  
        # If the type of the event is quit
        # then setting the run variable to false
        if event.type == QUIT:
            run = False
  
        # if the type of the event is MOUSEBUTTONDOWN
        # then storing the current position
        elif event.type == MOUSEBUTTONDOWN:
            position = event.pos
            circle_positions.append(position)
              
    # Using for loop to iterate
    # over the circle_positions
    # list
    for position in circle_positions:
  
        # Drawing the circle
        pygame.draw.circle(window, color, position,
                           circle_radius)
  
    # Draws the surface object to the screen.
    pygame.display.update()

输出: