http://codememo.tistory.com/18 의 코드를 이용하기
<실행화면>
<소스코드>
# -*- coding: utf-8 -*- # # rotation_ship_by_mouse_and_bullet.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.angle = 0 def update(self): mouse_x = pygame.mouse.get_pos()[0] mouse_y = pygame.mouse.get_pos()[1] self.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.angle)))) self.rect = self.image.get_rect() self.rect.center = (int(screen_width / 2), int(screen_height / 2)) class Bullet(pygame.sprite.Sprite): # Bullet def __init__(self): super().__init__() self.bullet_image = pygame.image.load("bullet.png") mouse_x = pygame.mouse.get_pos()[0] mouse_y = pygame.mouse.get_pos()[1] self.angle = math.pi - math.atan2(mouse_x - int(screen_width / 2), mouse_y - int(screen_height / 2)) self.image = pygame.transform.rotate(self.bullet_image, -(int(math.degrees(self.angle)))) self.speed = 10 self.x = 0 self.y = 0 def update(self): self.x += self.speed * math.sin(self.angle) self.y -= self.speed * math.cos(self.angle) self.rect = self.image.get_rect() self.rect.center = (int(screen_width / 2) + self.x, int(screen_height / 2) + self.y) def main(): screen = pygame.display.set_mode([screen_width, screen_height], 0, 32) pygame.display.set_caption("Rotation ship by mouse and Bullet") background = Background() ship = Ship() background_sprites_list = pygame.sprite.Group(background) all_sprites_list = pygame.sprite.Group(ship) bullet_list = pygame.sprite.Group() 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 if running: key = pygame.key.get_pressed() if key[pygame.K_SPACE]: bullet = Bullet() bullet_list.add(bullet) clock.tick(FPS) background_sprites_list.update() bullet_list.update() all_sprites_list.update() background_sprites_list.draw(screen) bullet_list.draw(screen) all_sprites_list.draw(screen) pygame.display.flip() if __name__ == "__main__": main()
<이미지소스>
<첨부파일>
rotation_ship_by_mouse_and_bullet.zip