gpt4 book ai didi

python - Pygame:如何在不将图像闪电两次的情况下旋转表面区域?

转载 作者:行者123 更新时间:2023-12-04 09:33:48 26 4
gpt4 key购买 nike

我正在尝试旋转整个屏幕以及表面上的所有对象。但是当我 blit 表面时,它只会创建一个新的,剩下四个。换句话说;我有两个没有旋转的图像,两个是。我想删除那些不旋转的。
我已经找了几个小时试图找到答案。但没什么。甚至有可能吗……?
(鼠标移动时旋转。)

# import os
import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500, 500), pygame.RESIZABLE)
pic = pygame.image.load("image.png")
pic2 = pygame.image.load("image.png")

pygame.display.flip()

angle = 1


while True:
screen.fill((0, 0, 0))
screen.blit(pic, (30, 20))
screen.blit(pic2, (22, 40))
# This creates a copy and blitz it. So there's 4, when there should be 2.
# The 2 that are not rotating should not be on the screen.
screen.blit(pygame.transform.rotate(screen, angle), (100, 0)) # x = 100 so you
can see the other 2 behind it.
pygame.display.flip()

angle += 1

pygame.event.pump()
event = pygame.event.wait()
if event.type == QUIT:
pygame.display.quit()
elif event.type == VIDEORESIZE:
screen = pygame.display.set_mode(event.dict['size'], HWSURFACE | DOUBLEBUF
| RESIZABLE)
pygame.display.flip()

最佳答案

代码没有正确处理事件,这就是为什么它只在鼠标移动时旋转。
这两个图像不断出现,因为它们在主循环中被重新绘制。代码将它们绘制到窗口,然后复制窗口,旋转它,最后重新绘制它向右偏移。所以它正在复制屏幕上已经存在的内容,因此您会看到 4x 对象。
你的问题的措辞对我来说不是 100% 清楚,但我认为你只想看到这些物体旋转的图像。所以首先要做的是制作一个包含这些图像的离屏 Surface,并将其用于旋转和绘制到实际屏幕:

screen = pygame.display.set_mode((400, 400), pygame.RESIZABLE)
pic = pygame.image.load("emitter_128.png")
pic2 = pygame.image.load("big_alien.png")

# Make the original surface to rotate
original_surface = pygame.Surface( ( screen.get_rect().width, screen.get_rect().height ) )
original_surface.fill( (0, 0, 0))
original_surface.blit( pic, (30, 20))
original_surface.blit( pic2, (22, 40))
然后在代码的主体中,旋转 original_surface ,并将其绘制到屏幕上。
# This creates a copy and blitz it. So there's 4, when there should be 2.
# The 2 that are not rotating should not be on the screen.
screen.fill( ( 0, 0, 50 ) )
if ( rotating ):
angle += 1
rotated_surface = pygame.transform.rotate( original_surface, angle )
rotated_surface_rect = rotated_surface.get_rect()
rotated_surface_rect.center = screen_centre

screen.blit( rotated_surface, rotated_surface_rect )
当方形位图图像旋转时,大小会发生变化,因为结果(现在是菱形,角朝上)仍然必须适合方形位图。如果您只是继续绘制到相同的坐标,则旋转看起来很不稳定。
解决此问题的一种方法是围绕其中心绘制旋转位图,使该中心点保持在屏幕上的相同位置。这会导致位图似乎围绕自身旋转(围绕其质心)。为此,我们需要将位图的中心移动到所需的屏幕坐标。这就是为什么我们需要处理 rotated_surface矩形。
注意:选择可怕的闪烁背景颜色组合来说明哪些部分是旋转位图,哪些部分是它背后的屏幕。
为糟糕的动画 .GIF 帧率道歉。它在屏幕上非常流畅。
rotatey-thing
引用代码:
import pygame

pygame.init()
SURFACE = pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE
screen = pygame.display.set_mode((400, 400), SURFACE )
pic1 = pygame.image.load("emitter_128.png")
pic2 = pygame.image.load("big_alien.png")

# Make the original surface to rotate
original_surface = pygame.Surface( ( screen.get_rect().width, screen.get_rect().height ) )
original_surface.fill( (0, 0, 0))
original_surface.blit( pic1, (30, 20))
original_surface.blit( pic2, (22, 40))

# We need to centre the image so the rotation looks smooth
screen_centre = ( screen.get_rect().width//2, screen.get_rect().height//2 )

angle = 1
rotating = True # used to pause rotation
running = True # used to exit
while running:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
running = False # exit
elif ( event.type == pygame.KEYDOWN ):
rotating = not rotating # pause rotation on any key
elif ( event.type == pygame.VIDEORESIZE ):
screen = pygame.display.set_mode(event.dict['size'], SURFACE )
screen_centre = ( screen.get_rect().width//2, screen.get_rect().height//2 )

# This creates a copy and blitz it. So there's 4, when there should be 2.
# The 2 that are not rotating should not be on the screen.
# screen.fill( ( 0, 0, 50 ) ) - use for horrible background
screen.fill( ( 0, 0, 0 ) )
if ( rotating ):
angle += 1
rotated_surface = pygame.transform.rotate( original_surface, angle )
rotated_surface_rect = rotated_surface.get_rect()
rotated_surface_rect.center = screen_centre

# finally draw the rotated bitmap to the screen
screen.blit( rotated_surface, rotated_surface_rect )

# Update the window
pygame.display.flip()

pygame.display.quit()

关于python - Pygame:如何在不将图像闪电两次的情况下旋转表面区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62687782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com