Hi this is my first time posting on here searching for help. I am trying to create a minimap overlay for a fps that I play. I am able to get the minimap displayed on screen and I am able to get the player icon to display on the minimap but I cannot figure out how to rotate the direction of the player icon based on the mouse direction along the x axis and move the icon in that new direction. I have a few examples that I have tried to follow but I can either accomplish rotating the icon only or rotating the icon/direction with the keyboard and moving in that direction with the keyboard. Here is my current code and the examples that I have followed.
import pygame
import sys
import os
import win32api
import win32con
import win32gui
import keyboard
import time
import cv2
import numpy as np
import pyautogui as py
worldx = 1600 #960
worldy = 1024 #720
fps = 60 # frame rate
ani = 4 # animation cycles
main = True
BLUE = (25, 25, 200)
BLACK = (23, 23, 23)
WHITE = (254, 254, 254)
ALPHA = (0, 0, 0)
upper = (0,0)
lower = (100,100)
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.original_image = pygame.image.load('images/player2.png').convert()
self.original_image.convert_alpha() # optimise alpha
self.original_image.set_colorkey(ALPHA) # set alpha
self.image = self.original_image
## self.image = pygame.transform.rotate(self.image, -90)
self.rect = self.image.get_rect(center = (x, y))
self.velocity = 1
self.imgx = 100
self.imgy = 700
self.playerx = 189
self.playery = 189
def point_at(self, x, y):
direction = pygame.math.Vector2(x, y) - self.rect.center
angle = direction.angle_to((0, -1))
self.image = pygame.transform.rotate(self.original_image, angle)
self.rect = self.image.get_rect(center = self.rect.center)
def move(self, x, y):
self.rect.move(x * self.velocity, y * self.velocity)
self.imgx = self.imgx + x
self.imgy = self.imgy + y
def rotate (self, angle):
self.image = pygame.transform.rotozoom(self.original_image, angle, 1)
self.rect = self.image.get_rect(center = self.rect.center)
## return rotated_surface, rotated_rect
def Get_Game_Window():
hwnd = win32gui.FindWindow(None,"EscapeFromTarkov")
windowrect = win32gui.GetWindowRect(hwnd)
x = windowrect[0]
y = windowrect[1]
width = windowrect[2] - x
height = windowrect[3] - y
return x,y,width,height
def Track_Game():
win32gui.SetWindowPos(pygame.display.get_wm_info()['window'], -1, Get_Game_Window()[0], Get_Game_Window()[1],0,0,0x0001)
def on_press_reaction(event):
global miniMap
global fps
if event.name == 'm':
miniMap = not miniMap
if event.name == 'j':
fps += 1
print(fps)
if event.name == 'k':
fps -= 1
print(fps)
def cv2ImageToSurface(cv2Image):
if cv2Image.dtype.name == 'uint16':
cv2Image = (cv2Image / 256).astype('uint8')
size = cv2Image.shape[1::-1]
if len(cv2Image.shape) == 2:
cv2Image = np.repeat(cv2Image.reshape(size[1], size[0], 1), 3, axis = 2)
format = 'RGB'
else:
format = 'RGBA' if cv2Image.shape[2] == 4 else 'RGB'
cv2Image[:, :, [0, 2]] = cv2Image[:, :, [2, 0]]
surface = pygame.image.frombuffer(cv2Image.flatten(), size, format)
return surface.convert_alpha() if format == 'RGBA' else surface.convert()
keyboard.on_press(on_press_reaction)
clock = pygame.time.Clock()
pygame.init()
##world = pygame.display.set_mode([worldx, worldy])
world = pygame.display.set_mode((Get_Game_Window()[2], Get_Game_Window()[3]), pygame.NOFRAME)
img = cv2.imread(os.path.join('images', 'customs.png'))
img = cv2.resize(img, (3354,2016))
backdropbox = world.get_rect()
fuchsia = (255, 0, 128) # Transparency color
dark_red = (139, 0, 0)
white = (255,255,255)
color_green = (0, 255, 0)
hwnd = pygame.display.get_wm_info()["window"]
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY)
miniMap = False
player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)
while True:
clock.tick(60)
Track_Game()
world.fill(fuchsia)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP:
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
player.point_at( * pygame.mouse.get_pos())
## player.rotate(-py.position()[0])
keys = pygame.key.get_pressed()
player.move(keys[pygame.K_d] - keys[pygame.K_a], keys[pygame.K_s] - keys[pygame.K_w])
miniMapimg = img[player.imgy - 99:player.imgy + 289, player.imgx - 99:player.imgx + 289]
try:
miniMapimg = cv2ImageToSurface(miniMapimg)
if miniMap:
## img = img[player.rect.x - 100:player.rect.y + 100, player.rect.x - 100:player.rect.y + 100]
world.blit(miniMapimg, backdropbox)
all_sprites.draw(world)
## world.blit(playerIcon_rotated, backdropbox)
## player_list.draw(world) #draw player
except:
print("error")
player.imgx = 100
player.imgy = 100
pass
## all_sprites.draw(world)
pygame.display.flip()
clock.tick(fps)
Here is an example that I tried to follow but I cannot figure out how to get the same result with the mouse changing the direction instead of keys. I want to be able to rotate the player/direction infinitely left or right as long as the mouse is moving left or right.
import pygame, sys, os
import pyautogui as py
ALPHA = (0, 0, 0)
def rotate(surface, angle):
rotated_surface = pygame.transform.rotozoom(surface, angle, 1)
rotated_rect = rotated_surface.get_rect(center = (480,270))
return rotated_surface, rotated_rect
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([960,540])
playerIcon = pygame.image.load(os.path.join('images', 'player2.png')).convert()
##playerIcon = pygame.transform.scale(playerIcon, (211, 260))
##playerIcon = pygame.convert_alpha()
##playerIcon = pygame.set_colorkey(ALPHA)
playerIcon_rect = playerIcon.get_rect(center = (300, 300))
angle = 0
pygame.mouse.set_pos(0,0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
## x, y = pygame.mouse.get_pos()
x, y = py.position()
print(x,y)
angle = x
screen.fill((255,255,255))
playerIcon_rotated, playerIcon_rotated_rect = rotate(playerIcon, angle)
screen.blit(playerIcon_rotated, playerIcon_rotated_rect)
pygame.display.update()
clock.tick(60)
Please excuse my messy code as I am still learning but any help would be greatly appreciated!