当前位置: 代码迷 >> 综合 >> python笔记 (三十九)pygame(6) 绘制简单图形
  详细解决方案

python笔记 (三十九)pygame(6) 绘制简单图形

热度:5   发布时间:2024-01-30 19:40:16.0

一.矩形

import pygame
import syspygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)size = width, height = 640, 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")clock = pygame.time.Clock()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)

函数pygame.draw.rect(screen, BLACK, (50, 50, 150, 50), 0)
总共四个参数,第三个参数是左上角坐标,宽度,深度
第四个是边框宽度,如果是0,就全部填充
在这里插入图片描述

二.多边形

import pygame
import syspygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)POINTS = [(200, 75), (300, 25),(400, 75),(450, 25),(450, 125), (400, 75),(300,125)]size = width, height = 640, 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(WHITE)pygame.draw.polygon(screen, BLACK, POINTS, 0)pygame.display.flip()clock.tick(10)

函数pygame.draw.polygon(screen, BLACK, POINTS, 0)
第三个参数是各个顶点的坐标
第四个依旧是边框宽度,0的时候填充
在这里插入图片描述

三.圆形

import pygame
import syspygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,0,255)
RED = (255,0,0)size = width, height = 640, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")position = size[0]//2,size[1]//2clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(WHITE)pygame.draw.circle(screen, BLACK, position, 25, 0)pygame.draw.circle(screen, BLUE, position, 75, 1)pygame.draw.circle(screen, RED, position, 125, 1)pygame.display.flip()clock.tick(10)

函数pygame.draw.circle(screen, BLACK, position, 25, 0)
第三个参数是圆心
第四个是半径
第四个依旧是边框宽度,0的时候填充
在这里插入图片描述

四.椭圆

import pygame
import syspygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,0,255)
RED = (255,0,0)size = width, height = 640, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")clock = pygame.time.Clock()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), 0)pygame.display.flip()clock.tick(10)

函数pygame.draw.ellipse(screen, BLACK, (100, 100, 440, 100), 1)
第三个参数就是矩形的左上角,宽度和深度
第四个是边框宽度,0填充
在这里插入图片描述

五.弧线

import pygame
import sys
import mathpygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,0,255)
RED = (255,0,0)size = width, height = 640, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")clock = pygame.time.Clock()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)

函数pygame.draw.arc(screen, BLACK, (100, 100, 440, 100),0, math.pi, 1)
第三个还是矩形的边界数据
第四个和第五个是指开始的角度和结束的角度
最后一个是边框宽度,但是0的时候不会填充
在这里插入图片描述

六.线段

在这里插入图片描述

import pygame
import sys
import mathpygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,0,255)
RED = (255,0,0)size = width, height = 640, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")POINTS = [(200, 75), (300, 25),(400, 75),(450, 25),(450, 125), (400, 75),(300,125)]clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(WHITE)pygame.draw.lines(screen, BLACK, 0, POINTS, 1)pygame.display.flip()clock.tick(10)

函数pygame.draw.lines(screen, BLACK, 0, POINTS, 1)第三个是是否要闭合,0是不闭合,1是闭合
在这里插入图片描述
在这里插入图片描述
但是最后一个参数为0时不能填充


在这里插入图片描述
aa是抗锯齿

import pygame
import sys
import mathpygame.init()WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,0,255)
RED = (255,0,0)size = width, height = 640, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图形")POINTS = [(200, 75), (300, 25),(400, 75),(450, 25),(450, 125), (400, 75),(300,125)]clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(WHITE)pygame.draw.line(screen, BLACK, (100, 200),(540, 250), 1)pygame.draw.aaline(screen, BLACK, (100, 250),(540,300), 1)pygame.draw.aaline(screen, BLACK, (100, 300),(540,350), 0)pygame.display.flip()clock.tick(10)

函数pygame.draw.aaline(screen, BLACK, (100, 250),(540,300), 1)
最后一个参数是是否开启抗锯齿
1是开启,0是不开启
在这里插入图片描述