How to rotate an image(player) to the mouse direct

2020-02-07 07:05发布

问题:

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.

回答1:

Get the mouse position by pygame.mouse.get_pos() and the rectangle (pygame.Rect) around the player:

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

Calculate the angle of the players center point to the mouse position by math.atan2:

angle = math.degrees(math.atan2(player_rect.centery - mouse_pos[1],
                                mouse_pos[0]-player_rect.centerx)) + 90

Rotate the player around its center by pygame.transform.rotate():
(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)

Furthermore, use .convert_alpha() for per pixel alpha.

See the example:

import math
import pygame

pygame.init()
DS = pygame.display.set_mode((640, 480))

Player_1=pygame.image.load("Img/Player_1.png").convert_alpha()

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

    DS.fill((255, 255, 255))

    mouse_pos   = pygame.mouse.get_pos()
    player_pos  = DS.get_rect().center
    player_rect = Player_1.get_rect(center = player_pos)

    angle = math.degrees(math.atan2(player_rect.centery - mouse_pos[1],
                                    mouse_pos[0]-player_rect.centerx)) + 90

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

    pygame.display.flip()



回答2:

Here is a simple block of code that you can use to help. It uses some trigonometry and assumes the following are defined before: elsie is the player

position = pygame.mouse.get_pos()
    angle = math.atan2(position[1] - (elsiepos[1] + 32), position[0] - (elsiepos[0] + 26))
    elsierot = pygame.transform.rotate(elsie, 360 - angle * 57.29)
    elsiepos1 = (elsiepos[0] - elsierot.get_rect().width / 2, elsiepos[1] - elsierot.get_rect().height / 2)
    screen.blit(elsierot, elsiepos1)

This block should come directly after you detect keys, and should come before drawing the player, in this case, elsie.



标签: python pygame