当前位置:   article > 正文

Python 之 pygame 学习(基本图形绘制)

Python 之 pygame 学习(基本图形绘制)

一、绘制矩形

pygame.draw.rect(Surface,color,Rect,width=0)

Surface:指定矩形将绘制在哪个Surface对象上
color:指定矩形的颜色
Rect:要绘制的矩形,位置和尺寸
width:指定矩形边框的大小,使用width值时,边线将在rect的原始边界之外生长
  • 1
  • 2
  • 3
  • 4
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 700,400
WHITE = (255,255,255)
BLACK = (0,0,0)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(WHITE)
    pygame.draw.rect(screen,BLACK,(50,50,150,50),0)
    pygame.draw.rect(screen,BLACK,(250,50,150,50),1)
    pygame.draw.rect(screen,BLACK,(450,50,150,50),10)

    # 更新界面,达到显示的效果
    pygame.display.flip()

    clock.tick(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在这里插入图片描述

二、绘制多边形

pygame.draw.polygon(surface,color,points,width = 0)

points(元组(坐标)或列表(坐标)构成多边形顶点的3个或更多(x,y)坐标的序列,
序列中的每个坐标必须是元组/列表/ 
例如[(x1, y1), (x2, y2), (x3, y3)]
  • 1
  • 2
  • 3
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 700,400
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)

points = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(WHITE)
    pygame.draw.polygon(screen,GREEN,points,0)

    # 更新界面,达到显示的效果
    pygame.display.flip()
    clock.tick(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

在这里插入图片描述

三、绘制圆形

pygame.draw.circle(Surface,color,pos,radius,width=0)

pos:圆的中心点
radius:圆的半径
  • 1
  • 2
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
moving = False
position = size[0]//2,size[1]//2
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                moving = True
        if event.type == MOUSEBUTTONUP:
            if event.button == 1:
                moving = False
    if moving:
        position = pygame.mouse.get_pos()
    screen.fill(WHITE)
    pygame.draw.circle(screen,RED,position,30,1)
    pygame.draw.circle(screen,GREEN,position,60,1)
    pygame.draw.circle(screen,BLUE,position,90,1)

    # 更新界面,达到显示的效果
    pygame.display.flip()
    clock.tick(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

在这里插入图片描述

四、绘制椭圆

pygame.draw.ellipse(Surface,color,Rect,width=0)

Rect:用于指示椭圆的位置和尺寸的矩形,椭圆将在矩形内居中并由其限定
  • 1
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
WHITE = (255,255,255)
BLACK = (0,0,0)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(WHITE)
    pygame.draw.ellipse(screen,BLACK,(100,100,440,100),1)
    pygame.draw.ellipse(screen,BLACK,(220,50,200,200),1)

    # 更新界面,达到显示的效果
    pygame.display.flip()
    clock.tick(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

五、绘制弧线

pygame.draw.arc(Surface,color,Rect,start_angle,stop_angle,width=1)

Rect:矩形表示弧将基于的椭圆的位置和尺寸,椭圆将在矩形内居中
start_angle(float):以弧度为单位的弧的起始角度
stop_angle(float):用弧度停止弧的角度
  • 1
  • 2
  • 3
import pygame
import sys
import math # 这里会用到 π,所以需要引入 math 包
from pygame.locals import *
pygame.init()
size = width,height = 600,400
WHITE = (255,255,255)
BLACK = (0,0,0)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(WHITE)
    pygame.draw.arc(screen,BLACK,(100,100,440,100),0,math.pi,1)
    pygame.draw.arc(screen,BLACK,(220,50,200,200),math.pi,math.pi * 2,1)

    # 更新界面,达到显示的效果
    pygame.display.flip()
    clock.tick(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

六、绘制线段

pygame.draw.line(Surface,color,start_pos,end_pos,width=1)
绘制一条线段

start_pos:行的起始位置,(x,y)
end_pos:行的结束位置,(x,y)
width(int)(可选)用于线条粗细
if width> = 1,用于线条粗细(默认为1)如果宽度<1,则不会绘制任何内容
  • 1
  • 2
  • 3
  • 4

pygame.draw.lines(Surface,color,closed,pointlist,width=1)
绘制多条线段

closed(bool):若True在points序列中的第一个和最后一个点之间绘制一个额外的线段,也就是会闭合
points:2个或更多(x,y)坐标的序列,依次绘制连接各点
  • 1
  • 2

开启抗锯齿之后,画面会有质的飞越
pygame.draw.aaline(Surface,color,startpos,endpos,blend=1)
绘制抗锯齿直线,比原型光滑

blend:是否开启抗锯齿,不开启,就相当于原型
  • 1

pygame.draw.aalines(Surface,color,closed,pointlist,blend=1)
绘制多条抗锯齿直线

import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 640,640
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)

points1 = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
points2 = [(200,350),(300,300),(400,350),(450,300),(450,400),(400,350),(300,400)]
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(WHITE)
    pygame.draw.lines(screen,GREEN,1,points1,1)
    pygame.draw.line(screen,BLACK,(100,150),(540,200),1)
    pygame.draw.aaline(screen,BLACK,(100,250),(540,300),1)
    pygame.draw.aalines(screen,BLACK,1,points2,1)
    # 更新界面,达到显示的效果
    pygame.display.flip()
    clock.tick(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/154549
推荐阅读
相关标签
  

闽ICP备14008679号