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

一.矩形

import pygame
import sys

pygame.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 sys

pygame.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 sys

pygame.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]//2

clock = 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 sys

pygame.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 math

pygame.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 math

pygame.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 math

pygame.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是不开启
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值