gpt4 book ai didi

python - 如何向鼠标方向旋转 Sprite 并移动它?

转载 作者:行者123 更新时间:2023-12-04 00:53:15 28 4
gpt4 key购买 nike

令我困惑的是,在我的代码中, Sprite 的坐标似乎并没有改变它所在的位置。输入 (200, 200) 与输入 (900, 100000) 是一样的。所以基本上我无法在指定位置协调 Sprite 。你能帮忙吗?

致谢 Ann Zen但是我的 Sprite 问题 Pygame sprite not turning accordingly to mouse

我的代码:

import pygame
from math import atan2, degrees
# tank = pygame.image.load('Sprite0.png')
wn = pygame.display.set_mode((400, 400))

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('Sprite0.png')
self.x = 0
self.y = 0
self.rect = self.image.get_rect()



def point_at(self, x, y):
rotated_image = pygame.transform.rotate(self.image, degrees(atan2(x-self.rect.x, y-self.rect.y)))
new_rect = rotated_image.get_rect(center=self.rect.center)
wn.fill((255, 255, 255))
wn.blit(rotated_image, new_rect.topleft)




player = Player()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
player.point_at(*pygame.mouse.get_pos())

# wn.blit(tank, Player)
pygame.display.update()

最佳答案

请仔细阅读对How to rotate an image(player) to the mouse direction?的回答。角度的计算

degrees(atan2(x-self.rect.x, y-self.rect.y))

工作偶然。它有效,因为 atan2(x, y) == atan2(-y, x)-pi/2

矢量 (x, y) 的角度是 atan2(y, x)。 y 轴需要反转 (-y),因为 y 轴通常指向上方,但在 PyGame 坐标系中,y 轴指向下方。您的 Sprite 很可能指向上方并且您想计算:

angle = degrees(atan2(self.rect.y - y, x - self.rect.x)) - 90

分别

direction = pygame.math.Vector2(x, y) - self.rect.center
angle = direction.angle_to((0, -1))

另见 How to know the angle between two points?


Sprite 绘制在存储在 rect 属性中的位置。您根本不需要 xy 属性。只需设置矩形的位置 (rect)。
向构造函数添加 xy 参数:

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('Sprite0.png')
self.rect = self.image.get_rect(center = (x, y))

添加方法move并使用pygame.Rect.move_ip改变Sprite的位置:

class Player(pygame.sprite.Sprite):
# [...]

def move(self, x, y):
self.rect.move_ip(x, y)

调用move,当你想改变Sprite的位置时:

keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player.move(0, -1)
if keys[pygame.K_s]:
player.move(0, 1)
if keys[pygame.K_a]:
player.move(-1, 0)
if keys[pygame.K_d]:
player.move(1, 0)

分别

keys = pygame.key.get_pressed()
player.move(keys[pygame.K_d]-keys[pygame.K_a], keys[pygame.K_s]-keys[pygame.K_w])

Sprite 应该始终包含在 pygame.sprite.Group 中。参见 pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.

添加一个Group并将Sprite添加到Group:

player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)

当您想绘制中的所有Sprites时调用draw:

all_sprites.draw(wn)

确保旋转后的图像存储在 image 属性中:

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.original_image = pygame.image.load('Sprite0.png')
self.image = self.original_image
self.rect = self.image.get_rect(center = (x, y))

def point_at(self, x, y):
direction = pygame.math.Vector2(x, y) - self.rect.center
angle = direction.angle_to((0, -1))
self.image = pygame.transform.rotate(self.original_image, angle)
self.rect = self.image.get_rect(center=self.rect.center)

# [...]

最小示例: repl.it/@Rabbid76/PyGame-SpriteRotateToMouse

import pygame
wn = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.original_image = pygame.image.load('Sprite0.png')
self.image = self.original_image
self.rect = self.image.get_rect(center = (x, y))
self.velocity = 5

def point_at(self, x, y):
direction = pygame.math.Vector2(x, y) - self.rect.center
angle = direction.angle_to((0, -1))
self.image = pygame.transform.rotate(self.original_image, angle)
self.rect = self.image.get_rect(center=self.rect.center)

def move(self, x, y):
self.rect.move_ip(x * self.velocity, y * self.velocity)


player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)

while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()

player.point_at(*pygame.mouse.get_pos())
keys = pygame.key.get_pressed()
player.move(keys[pygame.K_d]-keys[pygame.K_a], keys[pygame.K_s]-keys[pygame.K_w])

wn.fill((255, 255, 255))
all_sprites.draw(wn)
pygame.display.update()

如果你想在鼠标的方向移动对象,那么你必须添加类型为 pygame.math.Vecotr2directionposition 属性。方向在 point_at 中改变,位置在 move 中改变,具体取决于方向。 rect 属性必须更新。


最小示例: repl.it/@Rabbid76/PyGame-SpriteFollowMouse

import pygame
wn = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.original_image = pygame.image.load('Sprite0.png')
self.image = self.original_image
self.rect = self.image.get_rect(center = (x, y))
self.direction = pygame.math.Vector2((0, -1))
self.velocity = 5
self.position = pygame.math.Vector2(x, y)

def point_at(self, x, y):
self.direction = pygame.math.Vector2(x, y) - self.rect.center
if self.direction.length() > 0:
self.direction = self.direction.normalize()
angle = self.direction.angle_to((0, -1))
self.image = pygame.transform.rotate(self.original_image, angle)
self.rect = self.image.get_rect(center=self.rect.center)

def move(self, x, y):
self.position -= self.direction * y * self.velocity
self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity
self.rect.center = round(self.position.x), round(self.position.y)


player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)

while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
player.point_at(*event.pos)

keys = pygame.key.get_pressed()
player.move(keys[pygame.K_d]-keys[pygame.K_a], keys[pygame.K_s]-keys[pygame.K_w])

wn.fill((255, 255, 255))
all_sprites.draw(wn)
pygame.display.update()

关于python - 如何向鼠标方向旋转 Sprite 并移动它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64805267/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com