come disegnare linee su uno sfondo di immagine in pygame
Domanda
Vorrei tracciare linee (di posizione e lunghezza arbitrarie) su una superficie in pygame, che a sua volta è un'immagine caricata da un file su disco.
Qualcuno può indicarmi un codice di esempio che lo fa?
Soluzione
Questo dovrebbe fare quello che stai chiedendo:
# 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))
In genere non disegni all'immagine originale, poiché dovrai ricaricare l'immagine per recuperare l'originale (o crearne una copia prima di iniziare a disegnare su di esso). Forse quello di cui hai effettivamente bisogno è qualcosa di più simile a questo:
# 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))
Altri suggerimenti
Aiuto sul modulo pygame.draw in pygame:
NOME pygame.draw - modulo pygame per disegnare forme
FILE d: \ programmi \ python25 \ lib \ site-pacchetti \ pygame \ draw.pyd
FUNZIONI aaline (...) pygame.draw.aaline (Surface, color, startpos, endpos, blend = 1): return Rect traccia linee sottili antialiaste
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