如何在 pygame 中进行平滑移动 + 旋转? [英] How do I make smooth movement + rotation in pygame?

查看:110
本文介绍了如何在 pygame 中进行平滑移动 + 旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让飞船朝着鼠标指针移动,如果它四处移动,则旋转以跟随指针移动.目前,旋转工作正常,线性运动工作正常,但由于某种原因,如果船同时旋转和移动,它在旋转时会在原地摇晃,不再向前移动.

我刚刚发布了整个课程,希望能让事情变得不那么混乱.

class player():def __init__(self, x_position, y_position, length, height):self.x = x_positionself.y = y_positionself.l = 长度self.h = 高度self.player_ship = pygame.transform.scale(ships[0], (128, 128))self.angle = 0def draw(self, win):cx, cy = pygame.mouse.get_pos()img_copy = pygame.transform.rotate(self.player_ship, math.atan2(self.x - cx, self.y - cy)*57.2957795)win.blit(img_copy, (self.x - int(img_copy.get_width()/2), self.y - int(img_copy.get_height()/2)))定义移动(自我,速度):cx, cy = pygame.mouse.get_pos()self.angle = math.atan2(cx - self.x, cy - self.y)*57.2957795运动_x = math.cos(self.angle) * 速度运动_y = math.sin(self.angle) * 速度self.x -= 运动_xself.y +=motion_y

我在某处读到我不应该将我的位置存储为整数,但我不完全确定如何跟踪位置.我非常感谢任何人提供的任何提示或指示.

解决方案

问题是由旋转精灵的代码引起的.

旋转图像,并获得涂层图像的pygame.Rect.通过对象的位置设置矩形的中心.使用矩形blit图像.
请参阅

I am trying to make a spaceship move towards the mouse pointer, rotating to follow the pointer if it moves around. Currently, the rotation works fine and the linear movement works fine, but for some reason if the ship rotates and moves simultaneously, it sort of shakes in place while rotating and doesn't move forward any more. Here is a gif of my problem for clarification. Below I have the relevant code.

EDIT: I just posted the entire class to hopefully make things less confusing.

class player():
    def __init__(self, x_position, y_position, length, height):
        self.x = x_position
        self.y = y_position
        self.l = length
        self.h = height
        self.player_ship = pygame.transform.scale(ships[0], (128, 128))
        self.angle = 0
    def draw(self, win):
        cx, cy = pygame.mouse.get_pos()
        img_copy = pygame.transform.rotate(self.player_ship, math.atan2(self.x - cx, self.y - cy)*57.2957795)
        win.blit(img_copy, (self.x - int(img_copy.get_width() / 2), self.y - int(img_copy.get_height() / 2)))
    def move(self, speed):
        cx, cy = pygame.mouse.get_pos()
        self.angle = math.atan2(cx - self.x, cy - self.y)*57.2957795
        movement_x = math.cos(self.angle) * speed
        movement_y = math.sin(self.angle) * speed
        self.x -= movement_x
        self.y += movement_y

I read somewhere that I should not store my location as integers but I'm not completely sure how else to track location. I greatly appreciate any tips or pointer anyone has to offer.

解决方案

The issue is caused by the code which rotates the sprite.

Rotate the image, and get a pygame.Rect of the coated image. Set the center of the rectangle by the position of the object. Use the rectangle to blit the image.
See How do I rotate an image around its center using Pygame?.

img_copy = pygame.transform.rotate(self.player_ship, self.angle)
rotated_rect = img_copy.get_rect(center = (round(self.x), round(self.y)))

To rotate the image it is sufficient to compute the unit direction vector [See Unit vector.)] from the current position to the mouse position. Scale the vector by the speed and add it to the position:

cx, cy = pygame.mouse.get_pos()
dx, dy = cx - self.x, cy - self.y
if abs(dx) > 0 or abs(dy) > 0:
    dist = math.hypot(dx, dy)
    self.x += min(dist, speed) * dx/dist
    self.y += min(dist, speed) * dy/dist

See the example

class player():
    def __init__(self, x_position, y_position, length, height):
        self.x = x_position
        self.y = y_position
        self.l = length
        self.h = height
        self.player_ship = pygame.transform.scale(ships[0], (128, 128))
        self.angle = 0
    
    def draw(self, win):
     
        cx, cy = pygame.mouse.get_pos()
        dx, dy = cx - self.x, cy - self.y
        if abs(dx) > 0 or abs(dy) > 0:
            self.angle = math.atan2(-dx, -dy)*57.2957795

        img_copy = pygame.transform.rotate(self.player_ship, self.angle)
        rotated_rect = img_copy.get_rect(center = (round(self.x), round(self.y)))
        
        win.blit(img_copy, rotated_rect)
    
    def move(self, speed):
        cx, cy = pygame.mouse.get_pos()
        dx, dy = cx - self.x, cy - self.y
        if abs(dx) > 0 or abs(dy) > 0:
            dist = math.hypot(dx, dy)
            self.x += min(dist, speed) * dx/dist
            self.y += min(dist, speed) * dy/dist

这篇关于如何在 pygame 中进行平滑移动 + 旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆