我试图在游戏中设置玩家控制的角色不断面对鼠标。我设法使角度工作得很好,除了朝向屏幕的边缘,就像在物体被打到的表面上一样,它似乎逐渐变得越来越不准确。它旋转分配较少,似乎想要面对屏幕的中心。在屏幕中间,物体几乎完美地旋转。
编辑:它实际上似乎并不想要面对中心,它想要面对它进入的原始点,而不是它当前所处的位置。
# Imports
import pygame
import os
import math
import pyganim
# -- Initialize the Surface --
# Startup
pygame.init()
# Screen
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.mouse.set_visible(True)
clock = pygame.time.Clock()
# -- Assign Global Variables --
#Sets the color of pure white to a variable to be called
WHITE = (255, 255, 255)
#Sets the color of pure black to a variable to be called
BLACK = (0, 0, 0)
#Sets the background to an image
menu_Background = pygame.image.load("image-menu_background.png")
#Sets the possible players to images
player_bright_blue = pygame.image.load("player-bright_blue.png")
player_dark_blue = pygame.image.load("player-dark_blue.png")
player_bright_green = pygame.image.load("player-bright_green.png")
player_dark_green = pygame.image.load("player-dark_green.png")
player_bright_purple = pygame.image.load("player-bright_purple.png")
player_dark_purple = pygame.image.load("player-dark_purple.png")
player_bright_red = pygame.image.load("player-bright_red.png")
player_dark_red = pygame.image.load("player-dark_red.png")
#Sets the weapons for the player to images\
player_bright_green_shortsword = pygame.image.load("player-bright_green_shortsword.png")
#Sets the pointer to an image
pointer = pygame.image.load("pointer-cross_green.png")
#Sets the dark and bright menu go button to an image
menu_go_dark = pygame.image.load("button-go_dark.png")
menu_go_bright = pygame.image.load("button-go_bright.png")
#Sets the dark and bright menu exit button to an image
menu_exit_dark = pygame.image.load("button-exit_dark.png")
menu_exit_bright = pygame.image.load("button-exit_bright.png")
#sets the dark and bright menu options button to an image
menu_options_dark = pygame.image.load("button-options_dark.png")
menu_options_bright = pygame.image.load("button-options_bright.png")
#sets the arcflash text to an image and animation
anim_arcflash = pyganim.PygAnimation([("anim-arcflash_001.png", 100),
("anim-arcflash_002.png", 100),
("anim-arcflash_003.png", 100),
("anim-arcflash_004.png", 100),
("anim-arcflash_005.png", 100),
("anim-arcflash_006.png", 100)])
anim_arcflash.play
arcflash = pygame.image.load("image-arcflash.png")
#Sets a variable to know whether the script has run once or not
firstRun = 0
#sets up the Main loop of the game
# -- Main Loop --
def Main_Loop():
#Sets the bool for the loop to run on
loop = True
#Game variables --
#Sets the player center position
player_pos_x = 255
player_pos_y = 255
#Sets the players X and y speed
move_a_speed = 0
move_d_speed = 0
move_w_speed = 0
move_s_speed = 0
move_x_speed = 0
move_y_speed = 0
#Makes the mouse invisible
pygame.mouse.set_visible(False)
#Starts the loop
while loop == True:
# -- Event Loop --
for event in pygame.event.get():
if event.type == pygame.QUIT:
Game_Quit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.QUIT:
done = True
elif event.key == pygame.K_a:
move_a_speed = -2
elif event.key == pygame.K_d:
move_d_speed = 2
elif event.key == pygame.K_w:
move_w_speed = -2
elif event.key == pygame.K_s:
move_s_speed = 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
move_a_speed = 0
elif event.key == pygame.K_d:
move_d_speed = 0
elif event.key == pygame.K_w:
move_w_speed = 0
elif event.key == pygame.K_s:
move_s_speed = 0
# -- Screen Clearing --
screen.fill(WHITE)
# -- Drawing Logic --
move_x_speed = move_a_speed + move_d_speed
move_y_speed = move_w_speed + move_s_speed
#Increments vertical and horizontal player position by horizontal
#and vertical speed
player_pos_x += move_x_speed
player_pos_y += move_y_speed
#Set the mouse X and Y to a variable
mouse_pos = pygame.mouse.get_pos()
#Set the angle needed to rotate
angle = 270-math.atan2(mouse_pos[1] - player_pos_x, mouse_pos[0] - player_pos_y) * 180 / math.pi
# should rotate the character to the mouse, but doesnt work properly towards the edge/while moving
#Transforms the image to the angle
player_rotate = pygame.transform.rotate(player_bright_green, angle)
rect = player_rotate.get_rect(center=(player_pos_x + 15, player_pos_y + 15))
# -- Drawing --
#blits the rotated player
screen.blit(player_rotate, rect)
#blits the weapon the player is currently using
Sword(player_bright_green_shortsword, player_pos_x, player_pos_y, 14, 25)
#blits the pointer at mouse - KEEP ABOVE ALL OTHER BLITS
screen.blit(pointer, [mouse_pos[0] - 50, mouse_pos[1] - 50])
#Screen Refresh
pygame.display.flip()
# -- Refresh Rate --
clock.tick(80)
fps = str(clock.get_fps())
pygame.display.set_caption(fps)
# -- Runs the game --
(我遗漏了整个菜单脚本,似乎没必要。) 我试过调整角度计算中的值,如果没有移动角色,这个游戏真的不可行。
我的最终目标是让角色与鼠标完美同步,或至少尽可能完美。