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()