我想在 pygame 的表面上画线(任意位置和长度),它本身是从磁盘上的文件加载的图像。
谁能指出我这样做的一些示例代码?
这应该可以满足您的要求:
# load the image
image = pygame.image.load("some_image.png")
# draw a yellow line on the image
pygame.draw.line(image, (255, 255, 0), (0, 0), (100, 100))
通常,您不会绘制到原始图像,因为您必须重新加载图像才能取回原始图像(或在开始绘制之前创建它的副本)。也许你真正需要的是更像这样的东西:
# initialize pygame and screen
import pygame
pygame.init()
screen = pygame.display.set_mode((720, 576))
# Draw the image to the screen
screen.blit(image, (0, 0))
# Draw a line on top of the image on the screen
pygame.draw.line(screen, (255, 255, 255), (0, 0), (50, 50))
pygame 中模块 pygame.draw 的帮助:
名称 pygame.draw - 用于绘制形状的 pygame 模块
文件 d:\program files\python25\lib\site-packages\pygame\draw.pyd
函数 aaline(...) pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect 绘制精细的抗锯齿线
aalines(...)
pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect
arc(...)
pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect
draw a partial section of an ellipse
circle(...)
pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect
draw a circle around a point
ellipse(...)
pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect
draw a round shape inside a rectangle
line(...)
pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect
draw a straight line segment
lines(...)
pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect
draw multiple contiguous line segments
polygon(...)
pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect
draw a shape with any number of sides
rect(...)
pygame.draw.rect(Surface, color, Rect, width=0): return Rect
draw a rectangle shape