因此,在这个程序中,你可以使用WASD键在屏幕周围移动一艘宇宙飞船,并且工作正常。但这里的问题是我希望宇宙飞船精灵根据鼠标指针旋转,基本上我想让它看一下鼠标。而且我有点工作,当我在屏幕上移动鼠标时它有点摇摆,但它实际上并没有旋转。该部分是所有角度计算,是在太空船的get_angle()和rotate()方法中。
我的代码:
import pygame
from livewires import games, color
import math
games.init(screen_width = 700, screen_height = 650, fps = 50)
class Create_spaceship(games.Sprite):
'''A spaceship controlled by the arrow keys'''
def update(self):
'''Move the coordinates'''
key = pygame.key.get_pressed()
dist = 4
if key[pygame.K_w]:
self.y -= dist
if key[pygame.K_s]:
self.y += dist
if key[pygame.K_a]:
self.x -= dist
if key[pygame.K_d]:
self.x += dist
#Check to see if the spaceship is oustside boundries
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
if self.top < 0:
self.top = 0
if self.bottom > games.screen.height:
self.bottom = games.screen.height
self.get_angle()
def get_angle(self):
'''Will get the angle between the mouse and the image'''
mouseX = games.mouse.x
mouseY = games.mouse.y
spaceshipX = self.x
spaceshipY = self.y
self.angle = math.atan2(mouseX - spaceshipX, mouseY - spaceshipY)
print self.angle
def rotate(self):
'''Rotates the sprite at the angle found in get_angle()'''
self.angle = self.angle
def main():
screen_background = games.load_image('resized_stars background.png', transparent = False)
games.screen.background = screen_background
spaceship_image = games.load_image('8-bit_Spaceship_withBG.png')
spaceship = Create_spaceship(image=spaceship_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(spaceship)
games.mouse.is_visible = True
games.screen.mainloop()
main()
答案 0 :(得分:0)
使用pygame.transform.rotate(spaceship.angle)