python自动玩2048

2048 是一个简单的游戏,通过箭头向上、下、左、右移动滑块,让滑块合并。 实际上,你可以通过一遍一遍的重复“上、右、下、左”模式,获得相当高的分数。 编写一个程序,打开 https://gabrielecirulli.github.io/2048/上的游戏,不断发送上、右、 下、左按键,自动玩游戏。 
代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()
browser.get('https://gabrielecirulli.github.io/2048/')
time.sleep(3)
keyList = [Keys.UP, Keys.RIGHT, Keys.DOWN, Keys.LEFT]

browser.find_element_by_link_text('New Game').click()
count = 0

while True:
	over = browser.find_elements_by_link_text('Try again')
	if len(over) > 0:
		break

	htmlElem = browser.find_element_by_tag_name('html')
	htmlElem.send_keys(keyList[count % 4])
	count += 1

time.sleep(5)
browser.quit()

猜你喜欢

转载自blog.csdn.net/dongyu1703/article/details/82218117