调整矩形的图像 [英] Adjust image of rect

查看:37
本文介绍了调整矩形的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动一个 Rect 对象的图像,这可能吗?

示例:1 - 用动画制作一个瀑布(使水图像滚动)2 - 调整图像的位置而不是矩形

注意:这些只是示例,而不是我正在处理的代码

解决方案

您可以使用

导入pygamedef scroll_y(surf, dy):scroll_surf = surf.copy()scroll_surf.scroll(0, dy)如果 dy >0:scroll_surf.blit(冲浪,(0,dy-surf.get_height()))别的:scroll_surf.blit(surf, (0, surf.get_height()+dy))返回滚动冲浪pygame.init()窗口 = pygame.display.set_mode((400, 400))时钟 = pygame.time.Clock()Rain_surf = pygame.image.load('rain.png')dy = 0运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误window_center = window.get_rect().centerscroll_surf = scroll_y(rain_surf, dy)dy = (dy + 1) %rain_surf.get_height()window.fill(0)window.blit(scroll_surf, scroll_surf.get_rect(center = window_center))pygame.display.flip()pygame.quit()出口()

I want to move the image of a Rect object, is this possible?

examples: 1 - make a waterfall with the water animated (make the water image scroll) 2 - adjust location of the image not the rect

note: these are just examples not the code I am working on

解决方案

You can shift the surface image in place with pygame.Surface.scroll. For instance, call

water_surf.scroll(0, 1)

However, this will not satisfy you. See pygame.Surface.scroll:

Move the image by dx pixels right and dy pixels down. dx and dy may be negative for left and up scrolls respectively. Areas of the surface that are not overwritten retain their original pixel values.

You may want to write a function that overwrites the areas wich are not overwritten, with the pixel that is scrolled out of the surface:

def scroll_y(surf, dy):
    scroll_surf = surf.copy()
    scroll_surf.scroll(0, dy)
    if dy > 0:
        scroll_surf.blit(surf, (0, dy-surf.get_height()))
    else:
        scroll_surf.blit(surf, (0, surf.get_height()+dy))
    return scroll_surf

once per frame to create a water flow effect like a waterfall.

To center an image in a rectangular area, you need to get the bounding rectangle of the image and set the center of the rectnagle through the center of the area. Use the rectangle to blit the image:

area_rect = pygame.Rect(x, y, w, h)
image_rect = surf.get_rect()
image_rect.center = area_rect.center
screen.blit(surf, image_rect)

The same in one line:

screen.blit(surf, surf.get_rect(center = area_rect.center))


Minimal example:

import pygame

def scroll_y(surf, dy):
    scroll_surf = surf.copy()
    scroll_surf.scroll(0, dy)
    if dy > 0:
        scroll_surf.blit(surf, (0, dy-surf.get_height()))
    else:
        scroll_surf.blit(surf, (0, surf.get_height()+dy))
    return scroll_surf

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

rain_surf = pygame.image.load('rain.png')
dy = 0

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

    window_center = window.get_rect().center
    scroll_surf = scroll_y(rain_surf, dy)
    dy = (dy + 1) % rain_surf.get_height()

    window.fill(0)
    window.blit(scroll_surf, scroll_surf.get_rect(center = window_center))
    pygame.display.flip()

pygame.quit()
exit()

这篇关于调整矩形的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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