如何在pygame中的图片背景上绘制线条

时间:2008-11-29 19:31:42

标签: python drawing pygame

我想在pygame中绘制一条(任意位置和长度)的线条,它本身就是从磁盘上的文件加载的图像。

有人能指出我这样做的一些示例代码吗?

2 个答案:

答案 0 :(得分:2)

这应该做你要求的:

# 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))

答案 1 :(得分:0)

帮助pygame中的模块pygame.draw:

NAME     pygame.draw - 用于绘制形状的pygame模块

FILE     d:\ program files \ python25 \ lib \ site-packages \ pygame \ draw.pyd

功能     aaline(...)         pygame.draw.aaline(Surface,color,startpos,endpos,blend = 1):返回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