如何在pygame中移动时旋转图像



我是pygame的新手,并试图开发一款游戏,其中玩家使用箭头键移动并随着鼠标位置旋转(如迷你miltia(。但是我可以旋转播放器,但不能移动它。它只显示玩家旋转,但它没有移动。

def rot_center(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image, rot_rect
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image_orig = load_image('player.png')
self.image = self.image_orig
self.rect = self.image.get_rect()
self.rect_orig = self.rect
self.mask = pygame.mask.from_surface(self.image)
self.x, self.y = int(pygame.display.Info().current_w / 2), int(pygame.display.Info().current_h / 2)
self.rect.topleft = self.x, self.y
self.health = 100
self.damage_done = 0
self.chspeed_x = 10
self.chspeed_y = 10
self.dir = 0
def rot_aim(self, tx, ty):
self.dir = (math.atan2(self.y - ty, self.x - tx) * 180 / PI)
self.image, self.rect = rot_center(self.image_orig, self.rect_orig, self.dir)
def move(self, dx, dy):
self.chspeed_x = dx
self.chspeed_y = dy
self.x = self.x + self.chspeed_x * math.cos(math.radians(270 - self.dir))
self.y = self.y + self.chspeed_y * math.cos(math.radians(270 - self.dir))
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
FPS = 30
paused = False
player = Player()
player_s = pygame.sprite.Group()
player_s.add(player)
while not paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEMOTION:
mousepos = pygame.mouse.get_pos()
mouseX = mousepos[0]
mouseY = mousepos[1]
player.rot_aim(mousepos[1], mousepos[0])
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_UP:
player.move(0, 10)
if event.type == pygame.K_DOWN:
player.move(0, -10)
if event.type == pygame.K_RIGHT:
player.move(10, 0)
if event.type == pygame.K_LEFT:
player.move(-10, 0)
player_s.draw(screen)
clock.tick(FPS)
pygame.display.flip()

您在旋转或移动播放器后错过了更新self.rect。实际上玩家的位置(self.xself.y(是改变的。但由于self.rect用于绘制玩家,因此此属性必须通过位置进行更新。位置必须round,因为pygame.Rect对象存储整数值:

class Player(pygame.sprite.Sprite):
# [...]
def rot_aim(self, tx, ty):
self.dir = (math.atan2(self.y - ty, self.x - tx) * 180 / PI)
self.image, self.rect = rot_center(self.image_orig, self.rect_orig, self.dir)
self.rect.center = round(self.x), round(self.y) # <--- this is missing
def move(self, dx, dy):
self.chspeed_x = dx
self.chspeed_y = dy
self.x = self.x + self.chspeed_x * math.cos(math.radians(270 - self.dir))
self.y = self.y + self.chspeed_y * math.cos(math.radians(270 - self.dir))
self.rect.center= round(self.x), round(self.y) # <--- this is missing

还有错别字。您必须将event.key与按钮进行比较,而不是event.type

例如:

if event.type == pygame.K_UP:

if event.key == pygame.K_UP:
# [...]

无论如何,我建议使用pygame.key.get_pressed()而不是按钮事件,以实现连续流畅的运动.
最后在绘制场景之前通过screen.fill(0)清除背景:

def main():
# [...]
while not paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEMOTION:
mousepos = pygame.mouse.get_pos()
mouseX = mousepos[0]
mouseY = mousepos[1]
player.rot_aim(mousepos[1], mousepos[0])
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player.move(0, -10)
if keys[pygame.K_DOWN]:
player.move(0, 10)
if keys[pygame.K_RIGHT]:
player.move(10, 0)
if keys[pygame.K_LEFT]:
player.move(-10, 0)
screen.fill(0)
player_s.draw(screen)
clock.tick(FPS)
pygame.display.flip()

相关内容

  • 没有找到相关文章

最新更新