pygame learning (2) - drawing lines, circles, rectangles and other patterns

Introduction

pygame is a cross-platform Python library (pygame news) specially used to develop games. Pygame is mainly designed for the development and design of 2D electronic games. It provides image modules (image), sound modules (mixer), input/output (mouse, keyboard, display) modules, etc. Using pygame, you can theoretically develop and design all 2D games on the market.

 text

1. Draw lines 

We can use pygame.draw.line() function to draw a straight line.

pygame.draw.line (screen, line segment color, starting point coordinates, end point coordinates, line width)

pygame.draw.line(screen ,lightgreen, (300,0), (300,600), linewidth)

import pygame #导包
from pygame.locals import*
import sys

black=0,0,0
lightgreen=144,238,144

pygame.init() #初始化
screen = pygame.display.set_mode(size=(600,600),flags=0)
#绘制一个600*600的框框


while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()#python的退出程序

    linewidth=4 #“线”的粗细
    pygame.draw.line(screen,lightgreen,(300,0),(300,600),linewidth)
    pygame.display.update()  # 刷新展示

2. Draw the grid 

Draw a network with a grid size of thirty pixels by looping 

import pygame #导包
from pygame.locals import*
import sys

black=0,0,0
lightgreen=144,238,144

screen_width=600
screen_height=600

pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height),flags=0)
#绘制一个600*600的框框

# 设置网格大小
grid_size = 30  # 每个网格的大小为30个像素

# 绘制网格
for i in range(0, screen_width, grid_size):
    pygame.draw.line(screen, lightgreen, (i, 0), (i, screen_height))
for i in range(0, screen_height, grid_size):
    pygame.draw.line(screen, lightgreen, (0, i), (screen_width, i))

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()#python的退出程序

    linewidth=4
    pygame.display.update()  # 刷新展示

3. Draw a circle

pygame.draw.circle(screen,lightgreen,position,radius,linewidth)

You can draw a circle through pygame.draw.circle()

  • screen represents screen
  • lightgreen represents the color of the circle
  • position represents the location of the center of the circle
  • radius represents the radius of the circle
  • linewidth represents the thickness of the line 
import pygame #导包
from pygame.locals import*
import sys

black=0,0,0
lightgreen=144,238,144

screen_width=600
screen_height=600

pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height),flags=0)
#绘制一个600*600的框框


# 主循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    color = 255,255,255
    position = 300,250
    radius = 100
    width = 10
    linewidth=4
    screen.fill(color)
    pygame.draw.circle(screen,lightgreen,position,radius,linewidth)

    pygame.display.update()  # 刷新展示

 

4. Draw a rectangle

pygame.draw.rect() is a function in the pygame library that draws a rectangle on a given screen.

The parameters of the function are explained below:

  • screen: This is the pygame Surface object on which you want to draw the rectangle.
  • lightgreen: This is the color of the rectangle. It is a tuple containing RGB values, such as (144, 238, 144).
  • pos: This is the coordinate of the upper left corner of the rectangle + the coordinate of the lower right corner with the upper left punctuation point as the origin . Specific examples are given below.
  • width: This is the width of the rectangle. This is an integer.
import pygame #导包
from pygame.locals import*
import sys

screen_width=600
screen_height=600
pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height))
pygame.display.set_caption("这是标题")
pos_x = 300
pos_y =300

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    lightgreen=144,238,144
    width = 0
    pos = pos_x, pos_y, 300, 300
    pygame.draw.rect(screen, lightgreen, pos, width)#300*300的矩形
    pygame.display.update()

 Draw the rectangular area in the lower right corner:

 5. Draw a moving rectangle

Set the screen width and height to 600x600 pixels. Define the horizontal and vertical movement speed of the rectangle to be 0.14 pixels/frame. Each loop fills the background with black. Update the rectangle's position based on the speed. When the rectangle exceeds the bounds of the screen, change the direction of its speed so that it moves in the opposite direction. Use the pygame.draw.rect() method to draw a rectangle, set the color to yellow and the width to 0 (solid rectangle).

import pygame #导包
from pygame.locals import*
import sys

screen_width=600
screen_height=600
pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height))
pygame.display.set_caption("这是标题")
pos_x = 300
pos_y =300
vel_x = 0.14
vel_y = 0.1#粗略滴可以看作矩形的移动速度
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((0,0,0))#每次循环都要将背景置为黑色
    pos_x += vel_x
    pos_y += vel_y
    #当矩形超过屏幕范围后返回
    if pos_x > 500 or pos_x < 0:
        vel_x = -vel_x
    if pos_y > 500 or pos_y < 0:
        vel_y = -vel_y
#绘制矩形
    color = 255, 255, 1
    width = 0
    pos = pos_x,pos_y, 100, 100
    pygame.draw.rect(screen,color,pos,width)
    pygame.display.update()

 

test2.5

 6. What other commonly used methods for drawing graphics?

Pygame provides a variety of graphics drawing methods, which are mainly used to draw basic geometric shapes on Surface objects. The following are some commonly used methods for drawing graphics in Pygame:

  1. pygame.draw.polygon(): Used to draw polygons. You need to provide a list of points to define the vertices of the polygon, and the color of the polygon.

  2. pygame.draw.ellipse(): Used to draw ellipses. You need to specify a rectangular area within which the ellipse will be drawn, as well as the color of the ellipse.

  3. pygame.draw.arc(): Used to draw arcs. You need to specify a rectangular area within which the arc will be drawn, as well as the starting and ending angles and color of the arc.

  4. pygame.draw.lines(): Used to draw a series of connected line segments, creating open or closed polygons. You need to provide a list of points to define the vertices of the line segment, as well as the color and width of the line segment.

  5. pygame.draw.aaline() and  pygame.draw.aalines(): used to draw anti-aliased line segments, which can smooth the edges of the line segments. These two methods can only draw a line segment with a width of one pixel and do not support setting the line width.

summary

In this article, we mainly introduce how to draw a line, circle, rectangle, and simply "move" the rectangle. Each example has its corresponding complete code, which can be copied and used directly.

Guess you like

Origin blog.csdn.net/weixin_53197693/article/details/135341041