如何将图像闪烁到特定的矩形上?

发布于 2025-01-23 13:37:12 字数 733 浏览 0 评论 0原文

我是Pygame的初学者,尝试将图像爆炸到特定的矩形上很难。

obstacle_width = 70
obstacle_height = random.randint(150, 450)
obstacle_colour = (211, 253, 117)
obstacle_x_change = -4
obstacle_x = 500
obstacle_gap = 170


def display_obstacle(height):
    top = pygame.draw.rect(screen, obstacle_colour,
                     (obstacle_x, 0, obstacle_width, height))
    bottom_obstacle_height = 635 - height - obstacle_gap
    bottom = pygame.draw.rect(screen, obstacle_colour,
                     (obstacle_x, height + obstacle_gap, obstacle_width,
                      bottom_obstacle_height))

我想在topbottom rects中爆炸图像。我知道如何将图像添加到pygame上,但是我希望图像在内部 而不是上方/下方。

任何帮助将不胜感激!

I'm a beginner in Pygame and I'm having trouble trying to blit an image onto specific rects.

obstacle_width = 70
obstacle_height = random.randint(150, 450)
obstacle_colour = (211, 253, 117)
obstacle_x_change = -4
obstacle_x = 500
obstacle_gap = 170


def display_obstacle(height):
    top = pygame.draw.rect(screen, obstacle_colour,
                     (obstacle_x, 0, obstacle_width, height))
    bottom_obstacle_height = 635 - height - obstacle_gap
    bottom = pygame.draw.rect(screen, obstacle_colour,
                     (obstacle_x, height + obstacle_gap, obstacle_width,
                      bottom_obstacle_height))

I want to blit an image inside both the top and the bottom rects. I know how to add an image onto Pygame, but I want the image to be inside the rects, instead of above/below them.

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2025-01-30 13:37:12

参见使用pygame?图像。将图像加载 pygame.image.image.image.image.load()

obstacle_image = pygame.image.load("obstacle.png")

获取图像的边界矩形。创建一个 pygame.Rect 将边界矩形的中心通过障碍矩形的中心设置为 blit 屏幕上的图像:

obstacle_rect = pygame.Rect(obstacle_x, height + obstacle_gap, obstacle_width, bottom_obstacle_height)
image_rect = obstacle_image.get_rect(center = obstacle_rect.center)
screen.blit(obstacle_image, image_rect) 

See What is a good way to draw images using pygame?. Load the image with pygame.image.load()

obstacle_image = pygame.image.load("obstacle.png")

Get the bounding rectangle of the image. Create a pygame.Rect for the obstacle. Set the center of the bounding rectangle through the center of the obstacle rectangle and use it to blit the image on the screen:

obstacle_rect = pygame.Rect(obstacle_x, height + obstacle_gap, obstacle_width, bottom_obstacle_height)
image_rect = obstacle_image.get_rect(center = obstacle_rect.center)
screen.blit(obstacle_image, image_rect) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文