http://codememo.tistory.com/18 에서 Layer 적용하기
# -*- coding: utf-8 -*- # # rotation_ship_by_mouse_with_sprite&layer.py # coding with python 3.5.3 # # http://codememo.tistory.com # import sys import os import math import pygame screen_width = 640 screen_height = 480 FPS = 60 class Background(pygame.sprite.Sprite): # Background def __init__(self): super().__init__() self.image = pygame.image.load("background.jpg") def update(self): self.rect = self.image.get_rect() class Ship(pygame.sprite.Sprite): # ship def __init__(self): super().__init__() self.original_image = pygame.image.load("ship.png") self.mouse_angle = 0 def update(self): mouse_x = pygame.mouse.get_pos()[0] mouse_y = pygame.mouse.get_pos()[1] self.mouse_angle = math.pi - math.atan2(mouse_x - int(screen_width / 2), mouse_y - int(screen_height / 2)) self.image = pygame.transform.rotate(self.original_image, -(int(math.degrees(self.mouse_angle)))) self.rect = self.image.get_rect() self.rect.center = (int(screen_width / 2), int(screen_height / 2)) def main(): screen = pygame.display.set_mode([screen_width, screen_height], 0, 32) pygame.display.set_caption("Rotation ship by mouse with sprite") background = Background() ship = Ship() layer = pygame.sprite.LayeredUpdates() layer.add(background) layer.add(ship) clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False clock.tick(FPS) layer.update() layer.draw(screen) pygame.display.flip() if __name__ == "__main__": main()