I am trying to get this dot to move when not selected on the program window and have the program continuously updating while selected on another application.
I am no expert and I am new to coding, any help would be great.
Here is what I have so far:
import pygame
import os
from pynput.keyboard import Listener
WIDTH, HEIGHT = 1280, 720
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("interchange map")
WHITE = (255, 255, 255)
FPS = 120
VEL = 1
player_width, player_height = 5, 5
player_image = pygame.image.load(
os.path.join('Assets', 'red circle.png'))
player = pygame.transform.scale(player_image, (player_width, player_height))
MAP = pygame.image.load(os.path.join('Assets', 'interchange background.jpg'))
# Mx, My = pygame.mouse.get_pos()
def draw_window(p1):
WIN.blit(MAP, (0, 0))
WIN.blit(player, (p1.x, p1.y))
pygame.display.update()
def on_press(key):
global k
k=str(key)
return False
def p1_movement(k, p1, ):
if k == 'a' and p1.x - VEL > 0: # left
p1.x -= VEL
if k == 'd' and p1.x + VEL < 990: # right
p1.x += VEL
if k == 'w' and p1.y - VEL > 0: # up
p1.y -= VEL
if k == 's' and p1.y + VEL < 990: # down
p1.y += VEL
print(k,p1)
def main(): # map loop
p1 = pygame.Rect(500, 500, player_width, player_height)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
with Listener(on_press=on_press) as l:
l.join()
draw_window(p1)
p1_movement(k, p1)
pygame.quit()
if __name__ == "__main__":
main()
I have tried to use pygames key code to match listener inputs but does not move it either. I can get it to run properly only using pygames inputs however it does not monitor for inputs when the window is not selected.
The only thing in the asset folders is two images. This code can be run and tested by simply creating a folder named assets that can be accessed with two pictures in it.