"""本程序用pygame的变功能让星星旋转,作者:李兴球@2018
通过案例,你能学到如何让图片旋转.
"""
import pygame
from pygame.locals import *
pygame.init()
screenWidth ,screenHeight = 480,360
screen = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption("旋转的星星_pygame旋转图像实例_作者:李兴球")
star = "star1.png"
class Star():
def __init__(self,image,x,y):
self.rawimage = pygame.image.load(image) #原始图形
self.image = self.rawimage #旋转后的图形,初始值就是原始图啦
self.rect = self.rawimage.get_rect() #获取原始图形的矩形.
self.rect.x = x
self.rect.y = y
def rotate(self,degree):
self.image = pygame.transform.rotate(self.rawimage,degree)
self.rect = self.image.get_rect()
self.rect.x = screenWidth//2 - self.rect.w//2
self.rect.y = screenHeight//2 - self.rect.h//2
def update(self):
screen.blit(self.image,self.rect)
星星 = Star(star,screenWidth//2,screenHeight//2)
clock = pygame.time.Clock()
running = True
d = 0
while running:
for event in pygame.event.get():
if event.type == QUIT:running = False
screen.fill((0,0,0))
星星.rotate(d)
d=d + 1
d = d % 360
星星.update()
pygame.display.update()
clock.tick(60)
pygame.quit()