[英]Rotate center of image using pygame
So I'm not sure if I'm doing it right but I want to rotate a circular image around its center...this is my attempt so far. 所以我不确定我是否做得对,但是我想围绕其中心旋转圆形图像...这是我到目前为止的尝试。 When I try to run this it just opens a black pygame screen that closes off immediately and doesn't tell me what the problem is
当我尝试运行它时,它只是打开一个黑色的pygame屏幕,该屏幕立即关闭,并且没有告诉我问题出在哪里
from __future__ import division
import math
import sys
import pygame
class MyGame(object):
def __init__(self):
# """Initialize a new game"""
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
# set up a 640 x 480 window
self.width = 800
self.height = 600
self.screen = pygame.display.set_mode((self.width, self.height))
# load image
self.img = pygame.image.load("basketball.png")
# use a white background
self.bg_color = 255, 255, 255
# Setup a timer to refresh the display FPS times per second
self.FPS = 30
self.REFRESH = pygame.USEREVENT+1
pygame.time.set_timer(self.REFRESH, 1000//self.FPS)
def run(self):
#"""Loop forever processing events"""
running = True
def rot_center(image, rect, angle):
# Rotate function
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image, rot_rect
rect = self.img.get_rect()
self.img2 = rot_center(self.img, rect, 90)
while running:
event = pygame.event.wait()
# player is asking to quit
if event.type == pygame.QUIT:
running = False
# time to draw a new frame
elif event.type == self.REFRESH:
self.draw()
else:
pass # an event type we don't handle
def draw(self):
# Update the display
# everything we draw now is to a buffer that is not displayed
self.screen.fill(self.bg_color)
rect = self.img.get_rect()
rect = rect.move(self.width//2-rect.width//2, self.height//2-rect.height//2)
self.screen.blit(self.img2)
# flip buffers so that everything we have drawn gets displayed
pygame.display.flip()
MyGame().run()
pygame.quit()
sys.exit()
rot_center
rot_center
while running:
loop outside of rot_center
(perhaps in run
) and call rot_center
in the loop while running:
在rot_center
之外循环(可能在run
), rot_center
在循环中调用rot_center
Specifically, put the while running loop in run, since that's what you want it to do, run the loop. 具体来说,将while运行循环置于运行状态,因为这就是您想要执行的操作,因此请运行循环。 Then in the second elif, call rot _ center before you refresh the frame.
然后在第二个Elif中,在刷新框架之前调用rot _ center。 And take out the recursive call to rot_center, otherwise you'll never get out of the recursive calls
并取出对rot_center的递归调用,否则您将永远不会退出递归调用
So, init is called, then run() is called, then pygame is closed, then the program exits. 因此,先调用init ,再调用run(),然后关闭pygame,然后退出程序。 You need to end up in an event loop.
您需要结束一个事件循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.