0

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.

  • A github link to the assets may be helpful – Jobo Fernandez Aug 12 '22 at 12:06
  • @JoboFernandez No! The code must be added completely in the question. Links to eternal resources tend to break or the content may change. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Rabbid76 Aug 12 '22 at 12:27
  • The asset folder is just two images and can be replaced with anything so it doesn’t really make a difference. The problem with the code lies somewhere in the movement section. – michael white Aug 13 '22 at 03:43

0 Answers0