как рисовать линии на фоне картинки в Pygame - PullRequest
1 голос
/ 29 ноября 2008

Я хотел бы нарисовать линии (произвольной позиции и длины) на поверхности в Pygame, которая сама является изображением, загруженным из файла на диске.

Может кто-нибудь указать мне пример кода, который делает это?

Ответы [ 2 ]

2 голосов
/ 29 ноября 2008

Это должно сделать то, что вы просите:

# 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))
0 голосов
/ 29 ноября 2008

Помощь по модулю pygame.draw в pygame:

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
...