赞
踩
import sys import pygame from math import pi def homework(): pygame.init() screen = pygame.display.set_mode((800, 600)) screen.fill((255, 255, 255)) # 画正方形 rect = (300,200,200,200) pygame.draw.rect(screen, (0,0,255), rect,5) # 画上侧的弧 rect = (300, 100, 200, 200) pygame.draw.arc(screen, (0,255,0), rect, pi, 2*pi, width=5) # 画下侧的弧 rect = (300, 300, 200, 200) pygame.draw.arc(screen, (0, 255, 0), rect, 0, pi, width=5) # 画左侧的弧 rect = (200, 200, 200, 200) pygame.draw.arc(screen, (0, 255, 0), rect, - pi/2, pi/2, width=5) # 画右侧弧 rect = (400, 200, 200, 200) pygame.draw.arc(screen, (0, 255, 0), rect, pi / 2, -pi / 2, width=5) pygame.display.update() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.quit() if __name__ == '__main__': homework()
pygame.draw.line(Surface, color, startpos, endpos, width)
def draw_line(): pygame.init() screen = pygame.display.set_mode((800, 600)) screen.fill((255, 255, 255)) start_pos = 400, 300 # 起点 end_pos = 400, 200 # 终点 RED = (255, 0, 0) # 定义红色 pygame.draw.line(screen, RED, start_pos, end_pos, 20) # 画线段 pygame.display.update() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.quit()
示例图形:
pygame.draw.lines(Surface, color, closed, pointlist, width)
def draw_lines():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
p_list = [(400, 300), (400, 200), (500, 200), (500, 400)] # 定义各个折点的坐标
RED = (255, 0, 0) # 定义红色
pygame.draw.lines(screen, RED, True, p_list, 1) # 画封闭的图
pygame.display.update() # 更新屏幕
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
pygame.draw.aaline(Surface, color, startpos, endpos, width)
pygame.draw.aalines(Surface, color, closed, pointlist, width)
pygame.draw.polygon(Surface, color, pointlist, width)
这边就不详细解释.用法了,除了函数名多了两个a之外用法与画线段与画折线段一样.
def draw_balance(): pygame.init() screen = pygame.display.set_mode((800, 600)) screen.fill((255, 255, 255)) RED = (255, 0, 0) # 定义红色 # 底座三角形三个点 triangle_list = [(400, 300), (350, 350), (450, 350)] pygame.draw.polygon(screen, RED, triangle_list) # 秤杆 start_pos = 150, 275 end_pos = 700, 325 # pygame.draw.line(screen, RED, start_pos, end_pos, 3) pygame.draw.aaline(screen, RED, start_pos, end_pos, 3) # 画重物 # 左边放一个正方形,右边放一个圆, 因为秤杆是斜的,所以正方形不能直接用rect来画.选择用多边形来画 # 定义蓝色 BLUE = (0, 0, 255) rect_list = [(150, 275),(200, 280),(208,230), (158, 225)] pygame.draw.polygon(screen, BLUE, rect_list) circle_center = 680, 300 r = 22 pygame.draw.circle(screen, BLUE, circle_center, r) pygame.display.update() # 更新屏幕 while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.quit()
尝试画出如下组合图形
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。