使用pygame旋转图像中心

时间:2014-10-17 22:01:20

标签: python pygame

所以我不确定我是否做得对,但我想围绕它的中心旋转一个圆形图像...这是我到目前为止的尝试。当我尝试运行它时,它只是打开一个黑色的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()

2 个答案:

答案 0 :(得分:2)

  1. 您永远不会致电rot_center
  2. 您希望while running:之外的rot_center循环(可能在run中)并在循环中调用rot_center
  3. 具体来说,将while运行循环运行,因为这是你想要它做的,运行循环。然后在第二个elif中,在刷新帧之前调用rot _ center。并取出对rot_center的递归调用,否则你永远不会退出递归调用

答案 1 :(得分:1)

因此,调用 init ,然后调用run(),然后关闭pygame,然后程序退出。您需要以事件循环结束。