I'm looking for a way to execute a spacebar click within a fullscreen game whenever a certain image appears.
My problem is that out of the game the code works perfectly, but when I run the game and leave it on screen the script doesn't work.
I searched for some solutions with a "pydirectinput" library, but the problem persists.
I'm new to Python and I don't know exactly how to get around this.
from turtle import width
from pyautogui import *
import pyautogui
import pydirectinput
while True:
if pyautogui.locateOnScreen('barra.png', region=(700, 700, 500, 500), confidence=0.8) != None:
print("Press backspace!")
pydirectinput.press('space')
Related
Hello I am programming a program to automate mouse presses on a program for certain pixels but I don't want a second program to come in the way with that click, my program is going to look for a green pixel and click it on a certain part of the screen, but if there is another program/image in the way that is green I don't want it to click on that
I just want it to click on the process/program I want it to click on, and not click on the screen
If anyone could give me some tips on this, that would be helpful
To get the focused window (do not click if this is focused) use:
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))
Do this on your windows and then do if statements to stop clicking on pixels.
But to click on the pixels you can use win32con and win32api:
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
To get the pixels and click on them use PyAutoGui
import pyautogui
from pyautogui import *
width = 1920
hight = 1080
while WindowIsFocused:
pic = pyautogui.screenshot()
for x in range(0, width, 1):
for y in range(0, hight, 1):
r, g, b = pic.getpixel((x, y))
if r == 252:
if g == 200:
if b == 118:
click(x,y)
print("Clicked")
Set width and hight to your screen resolution and WindowIsFocused to True if the window you want is focused. Use an extra function for that (the function should run constantly).
I hope that I could help. For any questions ask me. :)
Im sorry im not familiar with keys interacting with programs, but i made a little research and found a library called PyWin32 that should statisfy your need. You can search for its documentation or try your luck by finding videos on this particullar library on youtube.
Anyways, hopefully this helped you getting set in the right direction, and feel free to ask any question
I have the following code:
import turtle
my_pen = turtle.Turtle()
window = turtle.Screen()
window.setup(width=1000, height=1000)
window.title('Tutorial')
my_pen.color("red")
my_pen.penup()
my_pen.goto(0, 0)
window.delay(1000)
my_pen.pendown()
window.delay(100)
my_pen.forward(100)
my_pen.left(90)
my_pen.forward(100)
my_pen.left(90)
turtle.mainloop()
When I try to run the code, the window opens and closes immediately. I am using the newest python 3.9, and I am using the newest PyCharm Community
I will attach a video of me running it and the window closing immediately
Two things: first, I don't know that the delay() method is doing anything for you, it doesn't add anything in my environment, so I'd leave it out until you've debugged the rest of the code.
Second, your code works fine for me. So I suggest you stop looking at the code and look at the environment in which you're running it. You're not simply running Python at the console but rather using some sort of IDE (Idle?). If so, you should include that information in your question.
A simple code clean up for testing purposes:
from turtle import Screen, Turtle
window = Screen()
window.setup(width=1000, height=1000)
window.title('Tutorial')
my_pen = Turtle()
my_pen.color("red")
my_pen.forward(100)
my_pen.left(90)
my_pen.forward(100)
my_pen.left(90)
window.mainloop()
I have a game written with a Pygame loop and it currently draws everything happening in the pygame window. I now want to integrate this window into a larger PySimpleGUI window to have nice functionality around the game. Is this possible?
I tried to use the code from here. The problem is that I get an error like this which comes from VIDEODRIVER at line 25:
pygame.error: windib not available
I changed this to 'windows' but then the Pygame window is separated from the PySimpleGUI as a different window.
Can I make the pygame loop as a window INSIDE the PySimpleGUI? Thank you.
It looks like the detached window is an open, unresolved issue with pygame 2.
If you're able to downgrade to pygame 1.9.6, the linked demo works as expected on Windows after changing the line 25 to:
os.environ['SDL_VIDEODRIVER'] = 'windows' as described.
As said there,
This line only work on windows:
os.environ['SDL_VIDEODRIVER'] = 'windib'
So make a code to skip it when the os is not Windows.
import platform
if platform.system == "Windows":
os.environ['SDL_VIDEODRIVER'] = 'windib'
I am using pygame to program a simple behavioral test. I'm running it on my macbook pro and have almost all the functionality working. However, during testing I'll have a second, external monitor that the subject sees and the laptop monitor. I'd like to have the game so up fullscreen on the external monitor and not on the laptop's monitor so that I can monitor performance. Currently, the start of the file looks something like:
#! /usr/bin/env python2.6
import pygame
import sys
stdscr = curses.initscr()
pygame.init()
screen = pygame.display.set_mode((1900, 1100), pygame.RESIZABLE)
I was thinking of starting the game in a resizable screen, but that OS X has problems resizing the window.
Pygame doesn't support two displays in a single pygame process(yet). See the question here and developer answer immediately after, where he says
Once SDL 1.3 is finished then pygame will get support for using multiple windows in the same process.
So, your options are:
Use multiple processes. Two pygame instances, each maximized on its own screen, communicating back and forth (you could use any of: the very cool python multiprocessing module, local TCP, pipes, writing/reading files, etc)
Set the same resolution on both of your displays, and create a large (wide) window that spans them with your information on one half and the user display on the other. Then manually place the window so that the user side is on their screen and yours is on the laptop screen. It's hacky, but might a better use of your time than engineering a better solution ("If it's studpid and it works, it ain't stupid" ;).
Use pyglet, which is similar to pygame and supports full screen windows: pyglet.window.Window(fullscreen=True, screens[1])
Good luck.
I do not know if you can do this in OS X, but this is worth mentioning for the Windows users out there, if you just want to have your program to run full screen on the second screen and you are on windows, just set the other screen as the main one.
The setting can be found under Rearrange Your Displays in settings.
So far for me anything that I can run on my main display can run this way, no need to change your code.
I did something silly but it works.
i get the number of monitors with get_monitors()
than i use SDL to change the pygame window's display position by adding to it the width of the smallest screen, to be sure that the window will be positionned in the second monitor.
from screeninfo import get_monitors
numberOfmonitors = 0
smallScreenWidth = 9999
for monitor in get_monitors():
#getting the smallest screen width
smallScreenWidth = min(smallScreenWidth, monitor.width)
numberOfmonitors += 1
if numberOfmonitors > 1:
x = smallScreenWidth
y = 0
#this will position the pygame window in the second monitor
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
#you can check with a small window
#screen = pygame.display.set_mode((100,100))
#or go full screen in second monitor
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#if you want to do other tasks on the laptop (first monitor) while the pygame window is being displayed on the second monitor, you shoudn't use fullscreen but instead get the second monitor's width and heigh using monitor.width and monitor.height, and set the display mode like
screen = pygame.display.set_mode((width,height))
display = pyglet.canvas.get_display()
display = display.get_screens()
win = pyglet.window.Window(screen=display[1])
------------------------------------------------------
screen=display[Номер монитора]
------------------------------------------------------
display = pyglet.canvas.get_display()
display = display.get_screens()
print(display) # Все мониторы которые есть
When i run this pygame program, a window is supposed to pop up, but nothing happens. Am i doing something wrong?
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
The problem here is that the program ends and closes the window. You should create some sort of game loop to keep the window open. An easy fix is to use an infinite while loop after the last line.
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
while True:
pass