Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame指向鼠标的点图像_Python_Pygame - Fatal编程技术网

Python Pygame指向鼠标的点图像

Python Pygame指向鼠标的点图像,python,pygame,Python,Pygame,我正试图让这张“宇宙飞船”的图像指向屏幕上的鼠标,这被证明是非常困难的,因此我非常感谢您的帮助:哦,如果您能帮助将图像移向光标,我想这将非常类似于向它旋转。。 这是我的密码: import sys, pygame, math; from pygame.locals import *; spaceship = ('spaceship.png') mouse_c = ('crosshair.png') backg = ('background.jpg') pygame.init() screen =

我正试图让这张“宇宙飞船”的图像指向屏幕上的鼠标,这被证明是非常困难的,因此我非常感谢您的帮助:哦,如果您能帮助将图像移向光标,我想这将非常类似于向它旋转。。 这是我的密码:

import sys, pygame, math;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            print("test1")
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            print("test3")
    screen.blit(bk, (0, 0))
    pos = pygame.mouse.get_pos()
    screen.blit(mousec, (pos))
    screen.blit(space_ship, (400, 300)) #I need space_ship to rotate towards my cursor
    pygame.display.update()
在这里:

首先获取太空船和鼠标之间的角度,然后旋转图像并设置图像的中心,这样图像在旋转时不会四处移动

import sys, pygame, math;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            print("test1")
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            print("test3")
    screen.blit(bk, (0, 0))
    pos = pygame.mouse.get_pos()
    screen.blit(mousec, (pos))
    angle = 360-math.atan2(pos[1]-300,pos[0]-400)*180/math.pi
    rotimage = pygame.transform.rotate(space_ship,angle)
    rect = rotimage.get_rect(center=(400,300))
    screen.blit(rotimage,rect) #I need space_ship to rotate towards my cursor
    pygame.display.update()