Gavin Jones
Gavin Jones

Reputation: 185

Convert Function to a Class Python, Beginner

Hi I am attempting to write my first Class by converting this function into a class.

# My Function

def left_click_first_Thompson(x, y, clicks=17):
    SetCursorPos(x, y)
    for i in range(clicks):
        mouse_event(2, 0, 0, 0, 0)
        mouse_event(4, 0, 0, 0, 0)

def left_click_M1A1_Thompson(x, y, clicks=2):
    SetCursorPos(x, y)
    for i in range(clicks):
        mouse_event(2, 0, 0, 0, 0)
        mouse_event(4, 0, 0, 0, 0)

"""Me Attempting to Turn it into a Class """

class AimMouse(object):

    # The Constructor to Instantiate the Objects
    def __init__(self, x, y, clicks):
        self.x = x
        self.y = y
        self.clicks = clicks

    def left_click_first_Thompson(self, x, y, clicks=17):
        SetCursorPos(x, y)
        for i in range(clicks):
            mouse_event(2, 0, 0, 0, 0)
            mouse_event(4, 0, 0, 0, 0)

    def left_click_M1A1_Thompson(self, x, y, clicks=2):
        SetCursorPos(x, y)
        for i in range(clicks):
            mouse_event(2, 0, 0, 0, 0)
            mouse_event(4, 0, 0, 0, 0)

Can I please get someone to proof read and correct ?

Thanks

Upvotes: 1

Views: 8868

Answers (1)

RecencyEffect
RecencyEffect

Reputation: 756

Edit: Since I just understood your idea now, here is some code to put you on the right track:

import ctypes

SetCursorPos = ctypes.windll.user32.SetCursorPos
mouse_event = ctypes.windll.user32.mouse_event

class Aimer:
    def __init__(self, clicks):
        self.clicks = clicks 

    def handle_click(self, x, y):
        SetCursorPos(x, y)
        for i in range(self.clicks):
            mouse_event(2, 0, 0, 0, 0)
            mouse_event(4, 0, 0, 0, 0)

thompsonAimer = Aimer(clicks=17) 
m1a1Aimer = Aimer(clicks=2)

Your code is correct, but you do not explain your motivation to turn this into a class.

A class is often used when the various functions need to share some state. For instance, let's say we want to fix the number of clicks for all function calls, and the functions do not receive such an argument. You set self.clicks = clicks in the constructor, and then in left_click... instead of clicks you use self.clicks.

The way it is now, it is pointless to store self.x, self.y and self.clicks because they are not being used.

If you are a beginner, I would not recommend focusing on classes, though it is good to have an idea of how they work.

Upvotes: 3

Related Questions