ChickenSupreme
ChickenSupreme

Reputation: 23

I cannot call one of my functions unless I manually do so

I have found that the function is being called when I run the program. However, where I have 2 if statements on the same indent, the second if statement does not start. But, if I run result() from the console, it works perfectly.

This function, below, cannot be called in the program. No errors, just nothing. However, I can call it manually by typing result() into the Idle shell.

Here is the entire program:

import win32api, win32con
import time
import ImageGrab
import ImageOps
import os
from numpy import *
from PIL import Image

x_pad = 148
y_pad = 27

def start():
    #Clicks the random number generator's "Again!" button
    mousePos((1153, 404))
    leftClick()
    print "Click: 'generate random number' button"
    print "Wait for 'random number' to load..."
    print
    time.sleep(4)

    detectNum():

#Detects the colour of a pixel which is different for
#each number: 1, 2 or 3
def detectNum():
    b1 = (x_pad + 1,y_pad+1,x_pad+640,y_pad+480)
    im = ImageGrab.grab()

    #im.save(os.getcwd() + '\\Snap__' + str(int(time.time())) +'.png', 'PNG')
    #print im

    im = im
    pixelLocation = 1312,371
    pixelColour = im.getpixel(pixelLocation)
    print pixelColour

    if pixelColour == (167, 79, 0):
        print "1: rock"
        print
        mousePos((64, 310))
        leftClick()
        print"Click: 'rock' button"
        time.sleep(.1)

    elif pixelColour == (239, 199, 135):
        print "2: paper"
        print
        mousePos((175, 313))
        leftClick()
        print"Click: 'paper' button"
        time.sleep(.1)

    elif pixelColour == (255, 255, 255):
        print "3: scissors"
        print
        mousePos((377, 317))
        leftClick()
        print"Click: 'scissors' button"
        time.sleep(.1)

    #Clicks the "bet amount" field
    mousePos((348, 180))
    leftClick()
    print "Click: 'bet amount' field"
    time.sleep(.1)

    #Type in bet amount

    #Clicks "create battle" button
    mousePos((195, 429))
    leftClick()
    print "Click: 'create battle' button"
    print
    print "Wait for 'success' popup to appear..."
    print
    time.sleep(1)

    #Click the cross on "success" message
    mousePos((477, 306))
    leftClick()
    print "Click: 'success' popup cross"
    time.sleep(.1)

    #Clicks the second tab (shows "your battles")
    mousePos((119, 2))
    leftClick()
    print "Click: second tab"
    print
    time.sleep(.1)

    isBtnPrsnt()

#Checks if the "remove" button is present
#Thereby checking if the game is present
def isBtnPrsnt():
    box = (383,331,481,370)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    #im.save(os.getcwd() + '\\button__' + str(int(time.time())) + '.png', 'PNG')    
    #return a

    #The button is present
    if a == 19984:
        print "Button? Yes. Game is active."
        #Checks again if the button is no longer present
        time.sleep(.1)
        isBtnPrsnt()

    #The button is not present
    elif a == 4547:
        print "Button? No. Game is over."
        print
        #Clicks chrome on windows task bar
        mousePos((62, 716))
        leftClick()
        print "Click: chrome on task bar"
        time.sleep(.1)
        #Clicks second window (shows "recent wins")
        mousePos((190, 615))
        leftClick()
        print "Click: second window"
        time.sleep(.1)
        #Right clicks the page
        mousePos((201, 198))
        rightClick()
        print "RightClick: page"
        time.sleep(.1)
        #Clicks reload to update results
        mousePos((311, 259))
        leftClick()
        print "Click: 'reload' button"
        time.sleep(10)
        result()

def result():
    count = 0
    box = (380,200,760,230)
    image = ImageGrab.grab(box)
    for x in range(1,380):
        for y in range(1,30):
            px = image.getpixel((x, y))
            if px[1] == 118:
                count = count + 1
                #print count

            if x == 379:
                if count >= 20:
                    return "win"

                else:
                    #print
                    count = 0
                    box = (380,200,760,230)
                    image = ImageGrab.grab(box)
                    for x in range(1,380):
                        for y in range(1,30):
                            px = image.getpixel((x, y))
                            if px[1] == 68:
                                count = count + 1
                                #print count

                            if x == 379:
                                if count >= 20:
                                    return "loose"

                                else:
                                    return "draw"

def leftClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

def rightClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)

def leftDown():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    print 'left Down'

def leftUp():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    time.sleep(.1)
    print 'left release'

def mousePos(cord):
    win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1]))

#Enter "get_cords()" into the shell to print the cords of the mouse 
def getCords():
    x,y = win32api.GetCursorPos()
    x = x - x_pad
    y = y - y_pad
    print x,y

def main():
    pass

if __name__ == '__main__':
    main()

Upvotes: 2

Views: 84

Answers (1)

user3835277
user3835277

Reputation:

When you're in shell and you call methods and functions, the returned value is displayed if it is not None. So your program actually works, it's just you don't explicitly print the returned value of result(). When you don't run it in shell, it appears nothing has happened. When you do call result() in shell, the shell automatically displays the returned value.

To "fix" this, just print the returned value. I highly recommend you also work outside of the shell to understand how Python actually works.

An Example

Suppose you were in shell when the following code is executed:

>>> def a(n):
...     print n * n
...
>>> def b(n):
...     return n * n
...
>>> a(5)
25
>>> b(5)
25
>>> exit()

Notice how both functions seem to do the same thing. However, they are far different. The return type of a(n) is None, while the return type of b(n) is int. So if you were to run a program (not from shell) that calls a(5) and b(5), you'd only see one 25, not two. This is because you are not in shell so the value returned by b(5) is not automatically displayed.


Also, to run your program outside of shell, modify main() so start() is called, instead of calling pass (pass essentially means "do nothing").

Upvotes: 3

Related Questions