Skip to content

Instantly share code, notes, and snippets.

@clarkzjw
Last active October 23, 2016 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkzjw/95040fc0f505e08fc3b5c82877e46f7f to your computer and use it in GitHub Desktop.
Save clarkzjw/95040fc0f505e08fc3b5c82877e46f7f to your computer and use it in GitHub Desktop.
Telegram game LumberJack bot, require Python3 and PyAutoGUI, the coordinates are for the left side of 1366*768 screen.
from pyautogui import *
from time import sleep
def move(_pos, _now):
if _pos == 'left' and _now == 'left':
typewrite(['left'])
typewrite(['left'])
elif _pos == 'left' and _now == 'right':
typewrite(['right'])
typewrite(['right'])
elif _pos == 'right' and _now == 'right':
typewrite(['right'])
typewrite(['right'])
elif _pos == 'right' and _now == 'left':
typewrite(['left'])
typewrite(['left'])
width, height = size()
width *= 0.5
moveTo(333, 600)
click()
posY = [360, 260, 160]
posX = [276, 398]
q = ['left', 'left', 'left']
im = screenshot(region=(0, 0, width, height))
l0 = im.getpixel((posX[0], posY[0]))
l1 = im.getpixel((posX[0], posY[1]))
l2 = im.getpixel((posX[0], posY[2]))
r0 = im.getpixel((posX[1], posY[0]))
r1 = im.getpixel((posX[1], posY[1]))
r2 = im.getpixel((posX[1], posY[2]))
if l0[0] == 161 and l0[1] == 116 and l0[2] == 56:
q[0] = 'right'
pos = 'right'
else:
pos = 'left'
if l1[0] == 161 and l1[1] == 116 and l1[2] == 56:
q[1] = 'right'
if l2[0] == 161 and l2[1] == 116 and l2[2] == 56:
q[2] = 'right'
qlen = 3
while True:
if qlen == 3:
now = q[0]
move(pos, now)
now = q[1]
move(pos, now)
now = q[2]
move(pos, now)
qlen = 0
elif qlen == 0:
im = screenshot(region=(0, 0, width, height))
l0 = im.getpixel((posX[0], posY[0]))
l1 = im.getpixel((posX[0], posY[1]))
l2 = im.getpixel((posX[0], posY[2]))
r0 = im.getpixel((posX[1], posY[0]))
r1 = im.getpixel((posX[1], posY[1]))
r2 = im.getpixel((posX[1], posY[2]))
if l0[0] == 161 and l0[1] == 116 and l0[2] == 56:
q[0] = 'right'
else:
q[0] = 'left'
if l1[0] == 161 and l1[1] == 116 and l1[2] == 56:
q[1] = 'right'
else:
q[1] = 'left'
if l2[0] == 161 and l2[1] == 116 and l2[2] == 56:
q[2] = 'right'
else:
q[2] = 'left'
now = q[0]
move(pos, now)
now = q[1]
move(pos, now)
now = q[2]
move(pos, now)
sleep(0.1)
@clarkzjw
Copy link
Author

clarkzjw commented Oct 7, 2016

lumberjack

@monashva
Copy link

Can you please explain how to use it? i installed python 3.5 (32bit) and i also think i installed PyAutoGUI ( but not sure how to check if it is really installed, then i changed my resolution to 1366*768 and opened your program that i downloaded using IDLE and then opened the game in chrome using telegram app and put it on left and resizing the browser so that there was a no black corners and then hit f5 on IDLE but it dosent work properly.

@clarkzjw
Copy link
Author

@monashva Hi, there.

Well, let's make things easier step by step.

1. How to check whether PyAutoGUI is properly installed.
I am not sure which OS you are using. Let me assume you are using Windows. :(. Press Win+R, and type python to open an interactive Python interpreter. Then type import pyautogui. If any warning or error appears, PyAutoGUI is not properly installed.

To be continued, let's make sure PyAutoGUI is OK first

@clarkzjw
Copy link
Author

Well, it seems PyAutoGUI is not properly installed. Try this?

@monashva
Copy link

monashva commented Oct 20, 2016

OK done, i went back at Python and typed: import pyautogui and it just went back to the next line without saying anything or giving any error, I think it is OK then.right?

@clarkzjw
Copy link
Author

clarkzjw commented Oct 20, 2016

It seems PyAutoGUI is now properly installed.

So you are using Python IDLE. Well I didn't use IDLE much. From your description, it seems you are executing the code line by line?

I guess there might be some options to run the entire code at once? Here might be some help.

Or you can try to run the Python code as a file/script. If you are not familiar with that, this might be helpful.

@monashva
Copy link

any advice on how to run the game and also where and it what size the browser should be?

@clarkzjw
Copy link
Author

Well, the situation might vary depend on your OS and your display.

In line 21, moveTo(333, 600), moves the cursor to the restart button of the LumberJack game. So you can pay attention to your cursor's move after this line.

If your cursor did not move to the correct place of that button, you can modify the coordinates.

@clarkzjw
Copy link
Author

Once the cursor move to the correct place, aka, that restart button of LumberJack, the following should be automatic.

@monashva
Copy link

well now i just run your program by dabble clicking on your file "LumberJack.py" after that a black windows pops out and then i open the minimized browser and hit replay bottom and it just goes straight until he hit a branch and game over, it doesn't switch the sides .

@clarkzjw
Copy link
Author

Well you should leave your browser open on the left side, and then execute my code. It will automatically move cursor to the button and automatically hit that button.

@monashva
Copy link

monashva commented Oct 20, 2016

Ok i tested it and i just starts automatically meaning the courser location is right and also it hits the left bottom but it keep hitting it until the game is over and it doesn't change sides to avoid getting hit.

@clarkzjw
Copy link
Author

Well, try to modify the last line. Increase the time it sleeps, e.g. sleep(0.5). If it works, then decrease the time.

@monashva
Copy link

I did it but it just does the same and fail. it doesn't know that he have to switch sides it just keep hitting left.

@clarkzjw
Copy link
Author

Well, after click the button, I didn't change the position of the cursor. I just send position signals to the browser.

Did the browser window get focus?

@clarkzjw
Copy link
Author

Well, it is 00:10 in GMT+8 now. I'm gonna sleep. :(

If you still get trouble with it, please post them here. I will check them tomorrow. :)

@monashva
Copy link

well as the cursor hits the start bottom the browser will get focused i think.i dont know what you mean by being focused but if you mean be as active window as i explained when it hits the start bottom the the browser's window goes active.

@clarkzjw
Copy link
Author

Then it does not make any sense. It should have worked well.

@xkit97
Copy link

xkit97 commented Oct 23, 2016

Traceback (most recent call last):
File "E:\User\Xkit\LumberJack.py", line 29, in
im = screenshot(region=(0, 0, width, height))
NameError: name 'screenshot' is not defined

how can i solve it :(( ? thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment