如何将图像(播放器)旋转到鼠标方向? [英] How to rotate an image(player) to the mouse direction?

查看:43
本文介绍了如何将图像(播放器)旋转到鼠标方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在 pygame 中制作一个 2d 射击游戏,我想让我的玩家(Player_1)指向鼠标方向.我寻找了几个小时的解决方案并尝试了我能找到的所有解决方案,但没有一个有效,你能吗请帮助我 ?这是我的代码:

导入pygame、sys、os从 pygame.locals 导入 *os.environ['SDL_VIDEO_CENTERED'] = '1'pygame.init()#退出设置定义退出():pygame.quit()系统退出()定义事件():对于 pygame.event.get() 中的事件:if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):放弃()#IDS时钟=pygame.time.Clock()每秒帧数=120DS=pygame.display.set_mode((0,0), pygame.FULLSCREEN)pygame.display.set_caption("射击模拟器")W,H=DS.get_size()P_X=W/2-50P_Y=H/2-50#颜色Red=pygame.Color("#FF0000")Blue=pygame.Color("#0000FF")绿色=pygame.Color("#00FF00")Black=pygame.Color("#000000")White=pygame.Color("#FFFFFF")#IGT(在游戏中)Player_1=pygame.image.load("Img/Player_1.png").convert()def game_loop():为真:事件()DS.fill(白色)DS.blit(Player_1,(P_X,P_Y))pygame.display.flip()游戏循环()

我将非常感谢所有帮助.

解决方案

您必须计算从播放器到鼠标的向量的角度.通过 向右看,修正角度为 0:correction_angle = 0
正在抬头,校正角度为 90:correction_angle = 90
向左看,校正角度为 180:correction_angle = 180
往下看,修正角度为270:correction_angle = 270

使用

导入数学导入pygamepygame.init()窗口 = pygame.display.set_mode((300, 300))player = pygame.image.load("player.png").convert_alpha()# 0 - 图像向右看# 90 - 图像正在查找# 180 - 图像向左看# 270 - 图像向下看校正角度 = 90运行 = 真运行时:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误player_pos = window.get_rect().centerplayer_rect = player.get_rect(center = player_pos)mx,我的 = pygame.mouse.get_pos()dx, dy = mx - player_rect.centerx,我的 - player_rect.centery角度 = math.degrees(math.atan2(-dy, dx)) - Correction_anglerot_image = pygame.transform.rotate(玩家,角度)rot_image_rect = rot_image.get_rect(center = player_rect.center)window.fill((255, 255, 255))window.blit(rot_image, rot_image_rect.topleft)pygame.display.flip()pygame.quit()出口()

Im thinking about making a 2d shooting game in pygame and i want to make my player(Player_1) point to the mouse direction.I looked for a solution for hours and tried all solution i could find but none had worked so can you pls help me ? Here's my code:

import pygame, sys, os
from pygame.locals import *
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
#Exit settings
def quit():
  pygame.quit()
  sys.quit()
def events():
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            quit()

#IDS
CLOCK=pygame.time.Clock()
FPS=120
DS=pygame.display.set_mode((0,0), pygame.FULLSCREEN)
pygame.display.set_caption("Shooting simulator")
W,H=DS.get_size()
P_X=W/2-50
P_Y=H/2-50

#Colors
Red=pygame.Color("#FF0000")
Blue=pygame.Color("#0000FF")
Green=pygame.Color("#00FF00")
Black=pygame.Color("#000000")
White=pygame.Color("#FFFFFF")

#IGT(in game things)
Player_1=pygame.image.load("Img/Player_1.png").convert()

def game_loop():
  while True:
    events()

    DS.fill(White)
    DS.blit(Player_1,(P_X,P_Y))
    pygame.display.flip()
game_loop()

This is my player(Player_1)

I will really appreciate all help.

解决方案

You have to compute the angle of the vector from the player to the mouse. get the mouse position by pygame.mouse.get_pos() and the rectangle (pygame.Rect) around the player:

mx, my = pygame.mouse.get_pos()
player_rect = Player_1.get_rect(topleft=(P_X,P_Y))

Calculate the vector from the player to the mouse and compute the angle of vector by math.atan2. The y-axis needs to be reversed (-dy) as the y-axis is generally pointing up, but in the PyGame coordinate system the y-axis is pointing down.

dx, dy = mx - player_rect.centerx, player_rect.centery - my
angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

In addition, a correction angle must be deducted (- correction_angle). The correction angle depends on the Sprite. If the Sprite

is looking to the right, the correction angle is 0: correction_angle = 0
is looking up, the correction angle is 90: correction_angle = 90
is looking to the left, the correction angle is 180: correction_angle = 180
is looking down, the correction angle is 270: correction_angle = 270

Rotate the player with pygame.transform.rotate() by the angle around its center:
(See also How do I rotate an image around its center using Pygame?)

rot_image = pygame.transform.rotate(Player_1, angle)
rot_image_rect = rot_image.get_rect(center=player_rect.center)


Minimal example:

import math
import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
player = pygame.image.load("player.png").convert_alpha()

#   0 - image is looking to the right
#  90 - image is looking up
# 180 - image is looking to the left
# 270 - image is looking down
correction_angle = 90

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    player_pos  = window.get_rect().center
    player_rect = player.get_rect(center = player_pos)

    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - player_rect.centerx, my - player_rect.centery
    angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

    rot_image      = pygame.transform.rotate(player, angle)
    rot_image_rect = rot_image.get_rect(center = player_rect.center)

    window.fill((255, 255, 255))
    window.blit(rot_image, rot_image_rect.topleft)
    pygame.display.flip()

pygame.quit()
exit()

这篇关于如何将图像(播放器)旋转到鼠标方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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