Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

python - Can I use an image on a moving object within Pygame as opposed to to a color?

   def star1():
        global angle
        x = int(math.cos(angle) * 100) + 800
        y = int(math.sin(angle) * 65) + 400
        pygame.draw.circle(screen, white, (x, y), 17)
        angle += 0.02

The parameters pygame gives for the .draw.circle here only allow for an RGB color to be used to color the figure. I am wondering if there is a way I can pass an image as a color, or use a different method that allows me to place an image on the object/circle. The circles move around so the image also needs to stay with the circle as it moves.

question from:https://stackoverflow.com/questions/65851274/can-i-use-an-image-on-a-moving-object-within-pygame-as-opposed-to-to-a-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • Create a transparent pygame.Surface with the desired size

  • Draw a white circle in the middle of the Surface

  • Blend the image (my_image) on this Surface using the blend mode BLEND_RGBA_MIN:

size = 100
circular_image = pygame.Surface((size, size), pygame.SRCALPHA)
pygame.draw.circle(circular_image, (255, 255, 255), (size//2, size//2), size//2)
image_rect = my_image.get_rect(center = circular_image.get_rect().center)
circular_image.blit(my_image, image_rect, special_flags=pygame.BLEND_RGBA_MIN)

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

def create_circular_image(size, image):
    clip_image = pygame.Surface((size, size), pygame.SRCALPHA)
    pygame.draw.circle(clip_image, (255, 255, 255), (size//2, size//2), size//2)
    image_rect = my_image.get_rect(center = clip_image.get_rect().center)
    clip_image.blit(my_image, image_rect, special_flags=pygame.BLEND_RGBA_MIN)
    return clip_image

def create_test_image():
    image = pygame.Surface((100, 100))
    ts, w, h, c1, c2 = 25, 100, 100, (255, 64, 64), (32, 64, 255)
    [pygame.draw.rect(image, c1 if (x+y) % 2 == 0 else c2, (x*ts, y*ts, ts, ts)) 
        for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
    return image

my_image = create_test_image()
circular_image = create_circular_image(100, my_image)

rect = circular_image.get_rect(center = window.get_rect().center)
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5
    rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 5            

    window.fill((64, 64, 64))
    window.blit(circular_image, rect)
    pygame.display.flip()

pygame.quit()
exit()

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...