Python之pygame学习绘制基本图形(3) | 您所在的位置:网站首页 › command函数如何画封闭矩形 › Python之pygame学习绘制基本图形(3) |
pygame绘制 ✕ 这次来说下怎么绘制基本的图形,如矩形,圆,直线等等 ![]() 这图片的代码在最最最下面!! ![]() pygame.draw.rect 画一个矩形 pygame.draw.polygon 绘制一个多边形 pygame.draw.circle 画一个圆圈 pygame.draw.ellipse 绘制一个椭圆 pygame.draw.arc 画一个椭圆弧 pygame.draw.line 画一条直线 pygame.draw.lines 绘制多个连续的直线段 pygame.draw.aaline 绘制直线抗锯齿线 pygame.draw.aalines 绘制多个连续的直线抗锯齿线段 在表面绘制几个简单的形状。这些函数可用于渲染任何格式的曲面。渲染到硬件表面将比常规软件表面慢。 大多数函数使用width参数来表示形状边缘周围的笔划(粗细)大小。如果宽度为0,则将填充形状(实线)。 所有绘图功能都遵循表面的剪辑区域,并将限制在该区域。这些函数返回一个矩形,表示已更改像素的边界区域。此边界矩形是包含受影响区域的“最小”边界框。 绘制矩形rect(表面,颜色,矩形) - > Rect rect(surface,color,rect,width = 0) - > Rect参数说明: surface(Surface)绘制表面color(Color或int 或tuple(int ,int ,int ,[ int])要绘制的颜色,使用元组(上篇讲过背景色相同),或者使用英文单词的如red之类的可用单词rect(Rect) - 要绘制的矩形,位置和尺寸要绘制的矩形,位置和尺寸width(int)(可选)用于线条粗细或表示要填充矩形(不要与rect参数的宽度值混淆)如果 width == 0 (默认)则填充内部如果 width > 0 则表示线条粗细如果使用width,边框的宽度不是很好控制返回:一个矩形边界变化的像素,如果没有绘制任何东西,返回宽高为0代码演示: pygame.draw.rect(screen,clock,(20,20,50,20),1)![]() 参数: surface(Surface)绘制表面color(Color或int 或tuple(int ,int ,int ,[ int])要绘制的颜色,使用元组(r,g,b)或者英文单词red之类可用的单词points(元组(坐标)或列表(坐标))构成多边形顶点的3个或更多(x,y)坐标的序列,序列中的每个坐标必须是元组/列表/width(int)(可选)用于线条粗细或表示要填充矩形(不要与rect参数的宽度值混淆)如果 width == 0 (默认)则填充内部是实心的内部全部填充颜色如果 width > 0 则表示线条粗细,空心的代表线条宽度如果使用width,边框的宽度不是很好控制返回:一个矩形边界变化的像素,如果没有绘制任何东西,返回宽高为0代码演示: # 绘制一个多边形 pygame.draw.polygon(screen, clock, [(100, 100), (150, 100), (125,150),(100, 100)], 0)![]() 参数: 表面:与矩形相同颜色:与矩形相同中心:元祖或列表指定一个点作为园的中心半径:圆的半径,半径为0则显示一个像素宽度:与矩形相同返回:与矩形相同代码:# 画一个正圆 pygame.draw.circle(screen,clock,(200,100),30)![]() 参数: 表面:与矩形相同颜色:与矩形相同矩形:用于指定椭圆的位置和尺寸的矩形,矩形限制椭圆的大小宽度:与矩形相同返回:与矩形相同# 绘制一个椭圆 pygame.draw.ellipse(screen,clock,(150,150,100,50),1)![]() 参数: 表面:与矩形相同颜色:与矩形相同矩形:绘制的位置和尺寸,椭圆将在矩形内居中起始角度:以弧度为单位的弧起始角度停止角度:以弧度为单位的弧的停止角度弧从哪顺时针方向绘制到结束方向起始角度小时停止角度,或超过起始角度360度后不会绘制内容如果起始起始角度等于停止则什么 # 画一个椭圆弧 # 这个我没看懂! # 官方代码! from math import pi pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2) pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2) pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2) pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)![]() 在给定曲面上绘制一条直线。没有端盖。对于粗线,末端是方形的。参数: 表面:同矩形颜色:同矩形起始点:指定一个起始位置 元组或列表终止点:指定一个终止位置 元组或列表宽度:同矩形返回:同矩形# # 画一条直线pygame.draw.line(screen,clock,(100,200),(100,300))![]() 在给定曲面上绘制一系列连续的直线。没有端盖或斜接接头。对于粗线,末端是方形的。用尖角绘制粗线会产生不希望的效果。参数: 表面:同矩形颜色:同矩形封闭:是否闭合,起点与结束点点:元组,或列表绘制一个个的点,如果闭合为真,则起点与终点自动闭合宽度:同矩形返回:同矩形# 绘制多个连续的直线段 points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)] pygame.draw.lines(screen,clock,1,points,1)![]() 在给定曲面上绘制直线抗锯齿线。所有参数同绘制直线 # 绘制直线抗锯齿线 pygame.draw.aaline(screen,clock,(300,300),(500,500))![]() 在给定曲面上绘制一系列连续的直线抗锯齿线所有参数同绘制多个连续的直线段 # 绘制多个连续的直线抗锯齿线段 points1= [(500, 75), (600, 25), (700, 75), (750, 25), (750, 125), (700, 75), (600, 125)] pygame.draw.aalines(screen,clock,1,points1)![]() 我测试的代码: import pygamefrom math import pi def rungame(): # 分辨率 800 * 600 win = (800, 600) # 一般情况我们都只是设置一个固定大小的窗口 screen = pygame.display.set_mode(win) # 设置当前窗口标题 pygame.display.set_caption('绘制基础图像!') # 设置窗口的背景色 screen.fill((0, 0, 0)) clock = (255,255,255) # 绘制矩形 # 显示窗口,(颜色),(左,顶,宽,高) pygame.draw.rect(screen,clock,(20,20,50,20),1) # 或者((左,上),(宽,高)) pygame.draw.rect(screen,clock,((40,60),(20,20)),0) # 绘制一个多边形 pygame.draw.polygon(screen, clock, [(100, 100), (150, 100), (125,150),(100, 100)], 0) # 画一个圆圈 pygame.draw.circle(screen,clock,(200,100),30) # 绘制一个椭圆 pygame.draw.ellipse(screen,clock,(150,150,100,50),1) # 画一个椭圆弧 # pygame.draw.arc(screen,clock,(580,700,100,300),0,2 / pi,3) # 画一条直线 pygame.draw.line(screen,clock,(100,200),(100,300)) # # 绘制多个连续的直线段 points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)] pygame.draw.lines(screen,clock,1,points,1) # 绘制直线抗锯齿线 pygame.draw.aaline(screen,clock,(300,300),(500,500)) # 绘制多个连续的直线抗锯齿线段 points1= [(500, 75), (600, 25), (700, 75), (750, 25), (750, 125), (700, 75), (600, 125)] pygame.draw.aalines(screen,clock,1,points1) # 开启一个事件循环处理发生的事件 while True: #clock.tick(60) # 从消息队列中获取事件并对事件进行处理 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) # 重绘背景 pygame.display.update() def main(): # 初始化导入的pygame中的模块 pygame.init() rungame() if __name__ == "__main__": main()![]() 官方给的演示代码: # Import a library of functions called 'pygame'import pygamefrom math import pi # Initialize the game enginepygame.init() # Define the colors we will use in RGB formatBLACK = (0, 0, 0)WHITE = (255, 255, 255)BLUE = (0, 0, 255)GREEN = (0, 255, 0)RED = (255, 0, 0) # Set the height and width of the screensize = [400, 300]screen = pygame.display.set_mode(size) pygame.display.set_caption("Example code for the draw module") # Loop until the user clicks the close button.done = Falseclock = pygame.time.Clock() while not done: # This limits the while loop to a max of 10 times per second. # Leave this out and we will use all CPU we can. clock.tick(10) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop # All drawing code happens after the for loop and but # inside the main while done==False loop. # Clear the screen and set the screen background screen.fill(WHITE) # Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide. pygame.draw.line(screen, GREEN, [0, 0], [50, 30], 5) # Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide. pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5) # Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide. pygame.draw.aaline(screen, GREEN, [0, 50], [50, 80], True) # Draw a rectangle outline pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2) # Draw a solid rectangle pygame.draw.rect(screen, BLACK, [150, 10, 50, 20]) # Draw an ellipse outline, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) # Draw an solid ellipse, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) # This draws a triangle using the polygon command pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5) # Draw an arc as part of an ellipse. # Use radians to determine what angle to draw. pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2) pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2) pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2) pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2) # Draw a circle pygame.draw.circle(screen, BLUE, [60, 250], 40) # Go ahead and update the screen with what we've drawn. # This MUST happen after all the other drawing commands. pygame.display.flip() # Be IDLE friendlypygame.quit() |
CopyRight 2018-2019 实验室设备网 版权所有 |