Как улучшить код

copypaste_scripter

Известный
Автор темы
1,295
249
ВНИМАНИЕ! гавнокод

Привет. Это мой первый скрипт на питоне. Пишу для себя (правилами игры запрещено, но не чит). Как можно улучшить скрипт? Ато я знаю что пишу через жопы. По идее пофиг, т.к. и так работает, и "и так сойдет", но хочу для далнейшего прогресса как то повторные действия поменять. Жду ваших мнении.

Python:
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener

mouse = Controller()

from myscript import *

def on_press(F4):
    try:
        mouse.position = (1750, 50)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 445)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 660)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 780)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 850)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 920)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 990)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1895, 985)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 595)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 665)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 780)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 850)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 920)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 990)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1830, 50)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 605)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1579, 816)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 887)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 954)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1898, 998)
        time.sleep(0.2)
        mouse.click(Button.left, 22)
        time.sleep(0.2)
        mouse.position = (1578, 196)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 266)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 337)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 403)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 473)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 542)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 609)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 825)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 894)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 964)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1896, 998)
        time.sleep(0.2)
        mouse.click(Button.left, 21)
        time.sleep(0.2)
        mouse.position = (1578, 197)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1579, 261)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 381)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1579, 449)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 518)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 585)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 705)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 775)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1575, 845)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 914)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1898, 1004)
        time.sleep(0.2)
        mouse.click(Button.left, 13)
        time.sleep(0.2)
        mouse.position = (1577, 526)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 592)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 664)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1580, 784)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1577, 851)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1576, 920)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        mouse.position = (1578, 990)
        time.sleep(0.2)
        mouse.click(Button.left, 1)
        time.sleep(0.2)
        return False

    except AttributeError:
        return False

with Listener(on_press=on_press) as listener:
    listener.join()
 
  • Нравится
Реакции: pastow
Решение
Python:
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener

mouse = Controller()

# Coordinates for the mouse to click in a list of tuples (x, y)
coordinates = [
    (1750, 50), (1578, 445), (1578, 660), (1578, 780),
    (1578, 850), (1578, 920), (1578, 990), (1895, 985),
    (1578, 595), (1578, 665), (1578, 780), (1578, 850),
    (1578, 920), (1578, 990), (1830, 50), (1576, 605),
    (1579, 816), (1577, 887), (1577, 954), (1898, 998),
    (1578, 196), (1578, 266), (1576, 337), (1578, 403),
    (1577, 473), (1577, 542), (1576, 609), (1576, 825),
    (1578, 894), (1577, 964), (1896, 998), (1578, 197),
    (1579, 261), (1578, 381), (1579, 449), (1576, 518),
    (1577, 585), (1577, 705), (1577...

chapo

чопа сребдс // @moujeek
Модератор
8,934
11,700
я конечно не пидорас питонист, но мне кажется что это можно сделать на циклах
Python:
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener
from myscript import *

list = [
    [1750, 50],
    [1578, 445],
    [1578, 660],
    [1578, 780],
    [1578, 850],
    [1578, 920],
    [1578, 990],
    [1895, 985],
    [1578, 595],
    [1578, 665],
    [1578, 780],
    [1578, 850],
    [1578, 920],
    [1578, 990],
    [1830, 50],
    [1576, 605],
    [1579, 816],
    [1577, 887],
    [1577, 954],
    [1898, 998],
    [1578, 196],
    [1578, 266],
    [1576, 337],
    [1578, 403],
    [1577, 473],
    [1577, 542],
    [1576, 609],
    [1576, 825],
    [1578, 894],
    [1577, 964],
    [1896, 998],
    [1578, 197],
    [1579, 261],
    [1578, 381],
    [1579, 449],
    [1576, 518],
    [1577, 585],
    [1577, 705],
    [1577, 775],
    [1575, 845],
    [1578, 914],
    [1898, 1004],
    [1577, 526],
    [1576, 592],
    [1578, 664],
    [1580, 784],
    [1577, 851],
    [1576, 920],
    [1578, 990],
]
mouse = Controller()

def on_press(F4):
    try:
        for pizda in list:
            mouse.position = (pizda[0], pizda[1])
            time.sleep(0.2)
            mouse.click(Button.left, 1)
            time.sleep(0.2)
        return False

    except AttributeError:
        return False

with Listener(on_press=on_press) as listener:
    listener.join()
 
  • Нравится
Реакции: copypaste_scripter

copypaste_scripter

Известный
Автор темы
1,295
249
Python:
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener

mouse = Controller()

# Coordinates for the mouse to click in a list of tuples (x, y)
coordinates = [
    (1750, 50), (1578, 445), (1578, 660), (1578, 780),
    (1578, 850), (1578, 920), (1578, 990), (1895, 985),
    (1578, 595), (1578, 665), (1578, 780), (1578, 850),
    (1578, 920), (1578, 990), (1830, 50), (1576, 605),
    (1579, 816), (1577, 887), (1577, 954), (1898, 998),
    (1578, 196), (1578, 266), (1576, 337), (1578, 403),
    (1577, 473), (1577, 542), (1576, 609), (1576, 825),
    (1578, 894), (1577, 964), (1896, 998), (1578, 197),
    (1579, 261), (1578, 381), (1579, 449), (1576, 518),
    (1577, 585), (1577, 705), (1577, 775), (1575, 845),
    (1578, 914), (1898, 1004), (1577, 526), (1576, 592),
    (1578, 664), (1580, 784), (1577, 851), (1576, 920),
    (1578, 990)
]

# Function to perform the mouse clicks at the given coordinates
def click_at_positions(coords):
    for x, y in coords:
        mouse.position = (x, y)
        time.sleep(0.2)  # optional, you can adjust this delay
        mouse.click(Button.left, 1)

# Function to handle the key press event
def on_press(key):
    try:
        if key == Key.f4:  # Only trigger on F4 press
            click_at_positions(coordinates)  # Perform all the clicks
            return False  # Stops the listener after F4 press

    except AttributeError:
        return False

# Start the keyboard listener
with Listener(on_press=on_press) as listener:
    listener.join()

чат ЖОПАТЕ предлагает так, но блин у меня на 89, 133 и 177 линиях не 1 клик а разное количество

edit: может я буду делать как чапо предлагает, но указать заодно сколько кликов в таблице?
или второй вариант разделить скрипт на 4 частей
 

chapo

чопа сребдс // @moujeek
Модератор
8,934
11,700
Python:
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener

mouse = Controller()

# Coordinates for the mouse to click in a list of tuples (x, y)
coordinates = [
    (1750, 50), (1578, 445), (1578, 660), (1578, 780),
    (1578, 850), (1578, 920), (1578, 990), (1895, 985),
    (1578, 595), (1578, 665), (1578, 780), (1578, 850),
    (1578, 920), (1578, 990), (1830, 50), (1576, 605),
    (1579, 816), (1577, 887), (1577, 954), (1898, 998),
    (1578, 196), (1578, 266), (1576, 337), (1578, 403),
    (1577, 473), (1577, 542), (1576, 609), (1576, 825),
    (1578, 894), (1577, 964), (1896, 998), (1578, 197),
    (1579, 261), (1578, 381), (1579, 449), (1576, 518),
    (1577, 585), (1577, 705), (1577, 775), (1575, 845),
    (1578, 914), (1898, 1004), (1577, 526), (1576, 592),
    (1578, 664), (1580, 784), (1577, 851), (1576, 920),
    (1578, 990)
]

# Function to perform the mouse clicks at the given coordinates
def click_at_positions(coords):
    for x, y in coords:
        mouse.position = (x, y)
        time.sleep(0.2)  # optional, you can adjust this delay
        mouse.click(Button.left, 1)

# Function to handle the key press event
def on_press(key):
    try:
        if key == Key.f4:  # Only trigger on F4 press
            click_at_positions(coordinates)  # Perform all the clicks
            return False  # Stops the listener after F4 press

    except AttributeError:
        return False

# Start the keyboard listener
with Listener(on_press=on_press) as listener:
    listener.join()

чат ЖОПАТЕ предлагает так, но блин у меня на 89, 133 и 177 линиях не 1 клик а разное количество
ну тогда как вариант сделать примерно так
Python:
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener
from myscript import *

list = [
    [1750, 50, True], # True - сделать клик не 1 а 3 раза
    ...
]
mouse = Controller()

def on_press(F4):
    try:
        for pizda in list:
            mouse.position = (pizda[0], pizda[1])
            time.sleep(0.2)
            mouse.click(Button.left, 1)
            time.sleep(0.2)
            if pizda[2]:
                # тут типо еще клик или че там тебе надо
        return False

    except AttributeError:
        return False

with Listener(on_press=on_press) as listener:
    listener.join()
 
  • Нравится
Реакции: copypaste_scripter

copypaste_scripter

Известный
Автор темы
1,295
249
я помню кто то тут помог в тот раз тоже. когда писал скрипт для телефона в луа. но не смог правильно погуглить как сделать таблицу для питона. круто когда ты даун и не понимаешь что луа и питон и хоть что будет работать одинаково