我可以在 Pygame 中的移动对象上使用图像而不是颜色吗? [英] Can I use an image on a moving object within Pygame as opposed to to a color?

查看:60
本文介绍了我可以在 Pygame 中的移动对象上使用图像而不是颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 def star1():全局角度x = int(math.cos(angle) * 100) + 800y = int(math.sin(angle) * 65) + 400pygame.draw.circle(屏幕,白色,(x,y),17)角度 += 0.02

这里的 .draw.circle 参数 pygame 只允许使用 RGB 颜色为图形着色.我想知道是否有一种方法可以将图像作为颜色传递,或者使用不同的方法来让我在对象/圆圈上放置图像.圆圈会四处移动,因此图像也需要在圆圈移动时与圆圈保持一致.

解决方案

  • 创建一个透明的

    导入pygamepygame.init()窗口 = pygame.display.set_mode((300, 300))时钟 = pygame.time.Clock()def create_circular_image(大小,图像):clip_image = pygame.Surface((大小,大小),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)返回剪辑图像def create_test_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))对于范围内的 x((w+ts-1)//ts) 对于范围内的 y((h+ts-1)//ts)]返回图像my_image = create_test_image()circle_image = create_circular_image(100, my_image)rect = circle_image.get_rect(center = window.get_rect().center)运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误键 = pygame.key.get_pressed()rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 5window.fill((64, 64, 64))window.blit(circular_image, rect)pygame.display.flip()pygame.quit()出口()


    另见:

       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.

    解决方案

    • 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:

    这篇关于我可以在 Pygame 中的移动对象上使用图像而不是颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆