问题:
我正在尝试在Pygame中绘制旋转的图像,但我只希望显示图像的上半部分。我设法旋转了图像,以使其正确居中,但是我无法将其裁剪为仅显示上半部分。
我认为这是由于矩形的旋转所致,当我将surface.subsurface()
其裁剪为特定的矩形时,裁剪的部分会产生“弹跳”效应,因为矩形的旋转角度会越来越大
代码:
def updateTime(screen, hudtimecircle, time):
center = (200, 200) # The center of where the rotated image will be drawn
amount = time # just a number 1-360
newcircle = pygame.transform.rotate(hudtimecircle, amount) # The rotated version of the un-rotated "hudtimecircle" image
newrect = newcircle.get_rect() # The rect of the rotated image
newrect.center = center # Setting the middle of the rotated rect to the point I want to be the center
crop = (0, 0, 120, 60) # An area that I want to be left when I "Crop", This is the part that needs fixing
cropped = newcircle.subsurface(crop) # The cropped part of the rotated image, I want/need this to only give the top half of the image.
screen.blit(cropped, newrect.topleft) # Drawing the rotated image with it's middle point where the variable "center" says.
我已经尝试过相对于旋转的rect裁剪它,如下所示:
crop = (newrect.topleft[0], newrect.topleft[1], newrect.midright[0], newrect.midright[1])
但是,这不起作用并会返回,ValueError: subsurface rectangle outside surface area
因为每个或某些点都在图像区域之外。
其他资讯:
该图像hudtimecircle
为120px * 120px,是一个圆形的图像,具有透明背景,我只想在旋转后绘制圆形的顶部60px。
amount
而time
只是数字从1到360,我有这条线,我打算以后做的东西与量。
我不能只在图像下半部分的顶部放大另一幅图像以“裁剪”它,因为在此图像的下面需要放置许多不同的东西。
我看了看:
https://www.reddit.com/r/learnpython/comments/3xeu4c/image_cropping_with_pygame/ http://blog.tankorsmash.com/?p=128
围绕中心点的pygame旋转
和其他一些不太相关的问题。
只是为了澄清,我的问题不是旋转图像或保持旋转的图像居中。问题是,我只需要显示旋转的居中图像的上半部分。
如果还有更多信息,我会很乐意提供。
这是您想要达到的效果吗?每次旋转后,您都必须计算新的裁切位置,因为旋转后的图像的大小一直在变化。在左上角,您可以看到父图像/表面如何变化。
import sys
import pygame as pg
def main():
pg.init()
screen = pg.display.set_mode((320, 240))
clock = pg.time.Clock()
done = False
bg_color = pg.Color(50, 80, 120)
# Create the original image that we use for the rotation
# to avoid visual degeneration.
orig_img = pg.Surface((100, 100))
orig_img.fill((20, 50, 130))
pg.draw.circle(orig_img, pg.Color('springgreen2'), (30, 30), 30)
pg.draw.circle(orig_img, pg.Color('lightgoldenrod3'), (60, 60), 30)
pg.draw.rect(orig_img, pg.Color('black'), (48, 0, 4, 100))
pg.draw.rect(orig_img, pg.Color('black'), (0, 48, 100, 4))
rect = orig_img.get_rect(topleft=(30, 30))
angle = 0
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
angle += 1
# Rotate the image.
img = pg.transform.rotate(orig_img, angle)
rect = img.get_rect(center=rect.center)
# Calculate new crop position. Half of the new
# width & height minus half of the original dimensions.
x, y = rect.width/2 - 50, rect.height/2 - 50
sub = img.subsurface((x, y, 100, 50))
screen.fill(bg_color)
screen.blit(img, rect)
screen.blit(sub, (150, 150))
pg.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()
pg.quit()
sys.exit()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句