簡體   English   中英

使用pygame旋轉圖像中心

[英]Rotate center of image using 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()
  1. 你永遠不會打電話給rot_center
  2. 您需要while running:rot_center之外循環(可能在run ), rot_center在循環中調用rot_center

具體來說,將while運行循環置於運行狀態,因為這就是您想要執行的操作,因此請運行循環。 然后在第二個Elif中,在刷新框架之前調用rot _ center。 並取出對rot_center的遞歸調用,否則您將永遠不會退出遞歸調用

因此,先調用init ,再調用run(),然后關閉pygame,然后退出程序。 您需要結束一個事件循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM