WASD 精灵移动以及旋转鼠标问题

时间:2021-05-20 15:27:03

标签: python pygame

我一直在尝试开发一种系统,让玩家角色既可以使用 WASD 键 在屏幕上移动,同时也始终 面向鼠标的位置。我的意思是,无论精灵在屏幕上的哪个位置,它的顶部边缘都将始终朝向鼠标所在的位置 - 鼠标的位置应该对其没有影响运动,只有它的旋转。顺便说一下,WASD 键应该对角色的旋转没有影响,只有它的位置

我已经能够实现 WASD 移动和向鼠标位置的连续角色旋转以单独工作,但我一直难以让它们同时一起工作(向鼠标的旋转应该是能够在屏幕上移动的同时发生)。

任何帮助将不胜感激。

import pygame
import time
import math

pygame.init()

#color defining
BLACK = (0 , 0 , 0)
WHITE = (255 , 255 , 255)
RED = (255 , 0 , 0)
GREEN = (0 , 255 , 0)
BLUE = (0 , 0 , 255)
DARKBLUE = (36 , 90 , 190)
LIGHTBLUE = (0 , 176 , 240)

#opening a window
screenSize = (800 , 600)
screen = pygame.display.set_mode(screenSize)
pygame.display.set_caption("Test")

#create sprites list
all_sprites_list = pygame.sprite.Group()

#sprites creation
class Char(pygame.sprite.Sprite):
    
    def __init__(self , color , width , height):
        super().__init__()
        
        #Set the colour, height and width and position
        self.image = pygame.Surface([width , height])
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
        
        #draw chars
        pygame.draw.rect(self.image , color , [0 , 0 , width , height])
        self.rect = self.image.get_rect(center = (width , height))
        self.orig_img = self.image
        
        #get initial sprite position
        self.pos = self.rect.x , self.rect.y
        self.x = self.rect.x
        self.y = self.rect.y
        self.rect.center = self.pos

    #----Char Methods
    #--Movement Methods
    
    #Check if moving off the screen
    def moveLeft(self, pixels):
        self.rect.x -= pixels   
        if self.rect.x < 0:
            self.rect.x = 0

    def moveRight(self, pixels):
        self.rect.x += pixels   
        if self.rect.x > 750:
            self.rect.x = 750

    def moveUp(self, pixels):
        self.rect.y -= pixels   
        if self.rect.y < 0:
            self.rect.y = 0

    def moveDown(self, pixels):
        self.rect.y += pixels   
        if self.rect.y > 550:
            self.rect.y = 550

    #Allows player rotation towards the mouse location
    def rotateToMouse(self):
      pos = self.rect.x, self.rect.y
      mouse_x, mouse_y = pygame.mouse.get_pos()
      rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
      angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
      self.image = pygame.transform.rotate(self.orig_img, int(angle))
      self.rect = self.image.get_rect(center=pos)

char = Char(LIGHTBLUE , 50 , 50)
char.rect.x = 450
char.rect.y = 450

#adding objects to group
all_sprites_list.add(char)

#game running flag
run = True

#clock setup
clock = pygame.time.Clock()

##----MAIN GAME----##
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                run = False

    #----key bindings
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        char.moveLeft(3) #moves left with speed of 3
    if keys[pygame.K_d]:
        char.moveRight(3) #moves right with speed of 3
    if keys[pygame.K_w]:
        char.moveUp(3) #moves up with speed of 3
    if keys[pygame.K_s]:
        char.moveDown(3) #moves down with speed of 3
    
    char.rotateToMouse()

    #----game logic
    all_sprites_list.update()

    #----drawing
    #reset screen
    screen.fill(DARKBLUE)

    #display statistics

    #draw sprites
    all_sprites_list.draw(screen)

    #update screen
    pygame.display.flip()

    #clock control
    clock.tick(60)

pygame.quit()

1 个答案:

答案 0 :(得分:0)

有两个问题。

  1. 获取 .rect 属性的中心而不是左上角:

pos = self.rect.x, self.rect.y

pos = self.rect.centerx, self.rect.centery
  1. 使用中心位置 (pos) 而不是 xy 属性来计算角度:

rel_x, rel_y = mouse_x - self.x, mouse_y - self.y

rel_x, rel_y = mouse_x - pos[0], mouse_y - pos[1]

xy 属性没有意义。你根本不需要它们。


rotateToMouse 方法:

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

    def rotateToMouse(self):
        pos = self.rect.centerx, self.rect.centery
        mouse_x, mouse_y = pygame.mouse.get_pos()
        rel_x, rel_y = mouse_x - pos[0], mouse_y - pos[1]
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
        self.image = pygame.transform.rotate(self.orig_img, int(angle))
        self.rect = self.image.get_rect(center=pos)