Python 如何";作物“;Pygame中的旋转图像
问题:Python 如何";作物“;Pygame中的旋转图像,python,python-3.x,rotation,pygame,Python,Python 3.x,Rotation,Pygame,问题: 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
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.
我试图在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.
我尝试过相对于旋转的矩形进行裁剪,如下所示:
crop = (newrect.topleft[0], newrect.topleft[1], newrect.midright[0], newrect.midright[1])
但是,这不起作用,并返回ValueError:subground rectangle outside surface area
,因为每个或一些点都在图像区域之外
其他信息:
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.
图像hudtimecircle
是120px*120px,是一个圆的图像,有一个透明的背景,我只想在旋转后绘制圆的顶部60px
amount
和time
只是从1到360的数字,我有这一行,因为我计划稍后处理amount
我不能只是在图像的下半部分的顶部点击另一个图像来“裁剪”它,因为在下面有许多不同的东西需要放置这个图像
我已经看过了:
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.
还有其他一些不太相关的问题 只是澄清一下,我的问题不是旋转图像,或者使旋转后的图像居中。问题是我只需要显示旋转的、居中的图像的上半部分
如果还需要其他信息,我很乐意提供。您必须将crop变量放在括号中,因此: 您的代码:
crop = (0, 0, 120, 60)
cropped = newcircle.subsurface(crop)
右代码:
crop = (0, 0, 120, 60)
cropped = newcircle.subsurface((crop))
注意我,如果这样做有效。这就是你想要达到的效果吗?每次旋转后都必须计算新的裁剪位置,因为旋转图像的大小一直在变化。在左上角,您可以看到父图像/曲面如何更改
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()
可悲的是,这并不能解决问题。您的代码和我的代码都会裁剪图像,但由于旋转,裁剪区域会根据旋转的角度发生变化。因此,如果图像上没有旋转,则裁剪可以正常工作并精确获得图像的上半部分,但任何旋转都会导致裁剪部分变大或变小。@Charlton Lane是否尝试设置裁剪曲面的矩形?此更改没有任何效果。你只要用多余的圆括号把作物包起来就行了!这正是我所需要的,我把它应用到我的图片上,效果非常好。