简体   繁体   English

无法使用Pygame在同一代码内旋转和移动精灵

[英]Cannot rotate and move sprite within the same code using Pygame

I have been given homework to make a top down shooter game but everytime I get my sprite to rotate, the movement lines stop working and everytime I get the sprite to move, the rotation lines begin to fail. 我已经做过作业来制作自上而下的射击游戏,但是每当我使精灵旋转时,移动线就会停止工作,而每当我使精灵移动时,旋转线就会开始失效。 I've never used Pygame in my life but I think the problem resides in my lack of understanding of the self command. 我一生中从未使用过Pygame,但我认为问题出在我对自我命令的理解不足。 Don't hold back telling me how wrong I am, like I said I haven't touched Pygame before and I've only been coding for around 5 months so there's a chance some of the lines are redundant anyways. 不要隐瞒我是怎么错的,就像我说过我以前没有接触过Pygame一样,我只编码了大约5个月,所以无论如何有些线路是多余的。

import pygame
import math

class Player(pygame.sprite.Sprite):

    def __init__(self,x,y):
        super().__init__()
        self.image = pygame.image.load("blue.png")
        self.movex = 0 # move along X
        self.movey = 0 # move along Y
        self.orig_img = self.image
        self.pos = pygame.math.Vector2(x,y)
        self.rect = self.image.get_rect(center=self.pos)
        self.vel = pygame.math.Vector2(0, 0)

    def update(self):
        self.rect.x = self.rect.x + self.movex
        self.rect.y = self.rect.y + self.movey
        self.rotate()
        self.pos += self.vel
        self.rect.center = self.pos

    def move(self,x,y):
        #https://opensource.com/article/17/12/game-python-moving-player - movement source
        self.movex += x
        self.movey += y

    def rotate(self):
        rel_x, rel_y = pygame.mouse.get_pos() - self.pos
        angle = -math.degrees(math.atan2(rel_y, rel_x))
        self.image = pygame.transform.rotozoom(self.orig_img, angle, 1)
        self.rect = self.image.get_rect(center=self.pos)


player = Player(200,300)
steps = 10
sprite_list = pygame.sprite.Group()
sprite_list.add(player)

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                player.move(-steps,0) 

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == ord('a'):
            player.move(steps,0)

I'd like to be able to move and rotate within the same code, I don't mind stutter at the moment. 我希望能够在相同的代码中移动和旋转,目前我不介意出现断断续续的情况。 Sorry if I pasted too much code, I really don't know where the problem is so I decided it was better to give more information than less. 抱歉,如果我粘贴了太多的代码,我真的不知道问题出在哪里,所以我决定提供更多的信息总比少提供更多。

You've to change self.pos rather than self.rect , because self.rect is updated by self.pos in the method rotate() : 您必须更改self.pos而不是self.rect ,因为self.rect是由self.posrotate()方法中更新的:

 def rotate(self): # [...] self.rect = self.image.get_rect(center=self.pos) # set self.rect by self.pos 

Update self.pos in update() , before rotate() is called: 更新self.posupdate()rotate()被调用:

class Player(pygame.sprite.Sprite):

    # [...]

    def update(self):

        #self.rect.x = self.rect.x + self.movex <--- delete
        #self.rect.y = self.rect.y + self.movey <--- delete
        self.pos = self.pos + pygame.math.Vector2(self.movex, self.movey)

        self.rotate()
        self.pos += self.vel
        self.rect.center = self.pos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM