0

I am trying to make a Twitch plays Roller Coaster Tycoon 2 (for fun) with help from this site and a slightly modified version of this to click on the screen. What I want to do is: if a person writes for example !click 10 10, it will click at position 10 10, but I don't know how to do that. My dad said that I have to find a way to find those numbers and turn them in to a variable. I've already wrote a bit to turn the text into a variable.

Define clickMSG:

clickMSG = ""

Code to detect message:

if "!click" in msg:
    clickMSG = msg

How can I find those numbers and turn them into the variables xpos and ypos if, for example, the message is !click 50 10?

Community
  • 1
  • 1
bleuthoot
  • 75
  • 1
  • 7

2 Answers2

0

You can use a regular expression:

import re

regex = re.compile('.* (\d+) (\d+')

if "!click" in msg:
    clickMSG = msg
    xpos, ypos = map(int, regex.match(msg).groups())

. means any character, and * means zero or more times. Therefore, .* is just however many of whatever characters are there (in this case, "!click"). The \d means a digit, and + means one or more times. We add parentheses so that it becomes a group in the regex. We have two of those groups: '50' and '10'. We then convert them to integers with map(int, ...), and assign xpos and ypos to them.

zondo
  • 19,901
  • 8
  • 44
  • 83
0

You can do it without re (regular expression), here is my code:

msg = "!click 10 10"
clickMSG = ""

if "!click" in msg:
    clickMSG = msg.strip("!click ")  # removes "!click" from msg
    xpos, ypos = clickMSG.split()
    xpos = int(xpos)    # 
    ypos = int(ypos)    # converts string (e.g. "10") to number (e.g. 10)
    print(xpos)
    print(ypos)
ands
  • 1,926
  • 16
  • 27