简体   繁体   中英

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

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. You never call rot_center
  2. You want the while running: loop outside of rot_center (perhaps in run ) and call rot_center in the loop

Specifically, put the while running loop in run, since that's what you want it to do, run the loop. Then in the second elif, call rot _ center before you refresh the frame. And take out the recursive call to rot_center, otherwise you'll never get out of the recursive calls

So, init is called, then run() is called, then pygame is closed, then the program exits. You need to end up in an event loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM