Pygame 绘制形状

Pygame 绘制形状

可以使用pygame.draw模块中的函数在游戏窗口上绘制不同的形状,如矩形、圆形、椭圆、多边形和线条。

绘制矩形 rect(surface, color, rect)
绘制多边形 polygon(surface, color, points)
绘制圆形 circle(surface, color, center, radius)
绘制椭圆 ellipse(surface, color, rect)
绘制椭圆弧 arc(surface, color, rect, start_angle, stop_angle)
绘制直线 line(surface, color, start_pos, end_pos, width)

示例

以下示例使用这些函数来绘制不同的形状 –

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
while not done:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         done = True
   pygame.draw.rect(screen, red, pygame.Rect(100, 30, 60, 60))
   pygame.draw.polygon(screen, blue, ((25,75),(76,125),(275,200),(350,25),(60,280)))
   pygame.draw.circle(screen, white, (180,180), 60)
   pygame.draw.line(screen, red, (10,200), (300,10), 4)
   pygame.draw.ellipse(screen, green, (250, 200, 130, 80)) 
   pygame.display.update()

输出

Pygame 绘制形状

如果向函数中添加一个可选的整数参数,将会以指定的颜色绘制形状作为轮廓颜色。数字对应轮廓的粗细和形状内部的背景颜色。

pygame.draw.rect(screen, red, pygame.Rect(100, 30, 60, 60),1)
pygame.draw.circle(screen, white, (180,180), 60,2)
pygame.draw.ellipse(screen, green, (250, 200, 130, 80),5)

输出

Pygame 绘制形状

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程