0

Im trying to set player controlled character in the game constantly face the mouse. I managed to get the angle working well except that towards the edges of the screen, as in the surface the object is blited onto, it seems to get progressively less and less accurate. it rotates allot less and seems to want to face the center of the screen. In the middle of the screen the object rotates almost perfectly.

Edit: It doesnt actually seem to want to face the center, it wants to face the original point that it blited into, rather then the place it is currently in.

# 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 --

(i left out the whole menu script it seemed unnecessary.) Ive tried adjusting the values in the angle calculation and this game isnt really going to be viable without a moving character.

My end goal is to have the character perfectly synced to the mouse, or atleast as perfect as possible.

  • Consider creating a [mcve] that focuses on your actual problem. It's hard to debug code that you can't run, and the code you posted won't run without those 2 dozen PNG files. FWIW, the usual convention when calling `math.atan2` is to give the Y delta then the X delta, eg `math.atan2(mouse_pos[0] - player_pos_y, mouse_pos[1] - player_pos_x)`, but I guess subtracting from 270° compensates for that. Also, the `math` library has functions to convert between radians and degrees, you don't need to do that `* 180 / math.pi` stuff. – PM 2Ring May 23 '16 at 18:01
  • sorry, im new to coding still and extremely new to this website. – Karker Carnesir May 24 '16 at 17:14

0 Answers0