做衣服网站,什么叫企业网站,wordpress淘宝客模版,wordpress中文版去广告文章目录 五种图形矩形圆形 五种图形
除了直线之外#xff0c;pygame中提供了多种图形绘制函数#xff0c;除了必要的绘图窗口、颜色以及放在最后的线条宽度之外#xff0c;它们的参数如下表所示
函数图形参数/类型rect矩形Rectellipse椭圆Rectarc椭圆弧Rect, st, edcircl… 文章目录 五种图形矩形圆形 五种图形
除了直线之外pygame中提供了多种图形绘制函数除了必要的绘图窗口、颜色以及放在最后的线条宽度之外它们的参数如下表所示
函数图形参数/类型rect矩形Rectellipse椭圆Rectarc椭圆弧Rect, st, edcircle圆形center, radiuspolygon多边形points
从参数类别可以看出矩形和椭圆的绘图逻辑是一致的可以理解为用矩形约束创造的椭圆即矩形的内切椭圆而椭圆弧也无非是多了个起止角度而已。
圆形和多边形则不然前者通过圆心和半径后者则根据一组点集。下面集中展示一下这几种绘图函数的基本用法。
import pygame
from pygame.draw import *pygame.init()
screen pygame.display.set_mode((640, 360))pts [(400, 0), (450, 100), (500, 0), (600, 100),(620, 300), (450, 350)]
while True:if pygame.QUIT in [e.type for e in pygame.event.get()]:pygame.quit()breakscreen.fill(purple)rect(screen, green, pygame.Rect(10, 30, 100, 300))ellipse(screen, red, pygame.Rect(120, 30, 100, 300))arc(screen, black, pygame.Rect(230, 30, 100, 300), 0, 4.5)circle(screen, blue, pygame.Vector2(360, 180), 40)polygon(screen, pink, pts)pygame.display.flip()效果如下 矩形
矩形在pygame中是非常基础的图形使用场景也非常多其优势在于可以非常便捷地判断两个物体是否发生了碰撞。其完整的参数列表如下
rect(surface, color, rect, width0, border_radius0, border_top_left_radius-1, border_top_right_radius-1, border_bottom_left_radius-1, border_bottom_right_radius-1)其中额外出现的5个以radius结尾的参数可用于设置矩形的圆角。示例如下
pygame.init()
screen pygame.display.set_mode((640, 360))
while True:if pygame.QUIT in [e.type for e in pygame.event.get()]:pygame.quit()breakscreen.fill(purple)rect(screen, green, pygame.Rect(50, 30, 200, 300), border_radius10)rect(screen, red, pygame.Rect(280, 30, 300, 300),border_top_left_radius10, border_top_right_radius40, border_bottom_left_radius80, border_bottom_right_radius160)pygame.display.flip()效果如下 圆形
圆形与矩形相似除了基础的绘图参数外也提供了4个可定制的参数。
circle(surface, color, center, radius, width0, draw_top_rightNone, draw_top_leftNone, draw_bottom_leftNone, draw_bottom_rightNone)这四个参数均为布尔类型用于控制圆形的四个角若均不指定则无影响。若指定其中某个参数为True则将只绘制这个方向的四分之一圆弧示例如下
pygame.init()
screen pygame.display.set_mode((640, 360))
while True:if pygame.QUIT in [e.type for e in pygame.event.get()]:pygame.quit()breakscreen.fill(purple)circle(screen, (1, 136, 225), pygame.Vector2(320, 180), 150,draw_top_leftTrue, draw_bottom_rightTrue)circle(screen, white, pygame.Vector2(320, 180), 150,draw_top_rightTrue, draw_bottom_leftTrue)pygame.display.flip()