Python Pygame:我如何旋转一个表面区域而不使图像闪烁两次?
我试着旋转整个屏幕和表面上的所有物体。但当我对曲面进行blit时,它只会创建一个新曲面,剩下四个曲面。换言之;我有两个图像是不旋转的,还有两个是旋转的。我想移除那些不旋转的。 我找了好几个小时试图找到答案。但什么都没有。甚至有可能。。。? (鼠标移动时旋转。)Python Pygame:我如何旋转一个表面区域而不使图像闪烁两次?,python,pygame,rotation,double,blit,Python,Pygame,Rotation,Double,Blit,我试着旋转整个屏幕和表面上的所有物体。但当我对曲面进行blit时,它只会创建一个新曲面,剩下四个曲面。换言之;我有两个图像是不旋转的,还有两个是旋转的。我想移除那些不旋转的。 我找了好几个小时试图找到答案。但什么都没有。甚至有可能。。。? (鼠标移动时旋转。) 代码没有正确处理事件,这就是为什么它只在鼠标移动时旋转 # import os import pygame from pygame.locals import * pygame.init() screen = pygame.displa
代码没有正确处理事件,这就是为什么它只在鼠标移动时旋转
# 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()
这两个图像不断出现,因为它们正在主循环中重新绘制。代码将它们绘制到窗口,然后复制窗口,旋转窗口,最后向右偏移重新绘制。因此,它复制了屏幕上已经出现的内容,因此您可以看到4倍的对象
我对你问题的措辞不是100%清楚,但我认为你只是想看到那些物体旋转的图像。因此,首先要做的是制作一个包含这些图像的屏幕外表面,并将其用于旋转和绘制到实际屏幕:
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))
然后在代码主体中,旋转原始曲面
,并将其绘制到屏幕上
# 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 )
当正方形位图图像旋转时,大小会改变,因为结果(比如现在的菱形,角向上)仍然必须适合正方形位图。如果你只是一直画在同一个坐标上,旋转看起来是不稳定的
解决此问题的一种方法是围绕其中心绘制旋转位图,使该中心点保持在屏幕上的相同位置。这会使位图看起来围绕自身旋转(围绕其质心)。为此,我们需要将位图的中心移动到所需的屏幕坐标。这就是我们需要处理旋转曲面的原因
注意:选择了可怕的闪烁背景色组合来说明旋转位图的部分,以及它后面的屏幕部分
为糟糕的动画.GIF帧速率道歉。屏幕上很流畅
参考代码:
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()
旋转操作的预期结果是什么?整个屏幕都在旋转?哇。这比我要求的还要多。对不起,如果我不清楚的话。但是是的。我只想旋转所有的东西,(两个物体),不需要加倍,得到4。但我会用你所有的建议来提高。谢谢你的帮助!