Note 12/20/2023 10:52:35 AM
import pyautogui
import time
import pygetwindow as gw
def find_position(image_path, confidence=0.4):
# Get BlueStacks window details
bluestacks = gw.getWindowsWithTitle('BlueStacks')[0]
bluestacks.activate()
window_x, window_y, _, _ = bluestacks.left, bluestacks.top, bluestacks.width, bluestacks.height
# Find the position of the image within the BlueStacks window
position = pyautogui.locateOnScreen(image_path, region=(window_x, window_y, bluestacks.width, bluestacks.height), confidence=confidence)
if position is None:
return None
# Calculate the center position of the found image
position_center = (position[0] + position[2] // 2, position[1] + position[3] // 2)
return position_center
def move_towards(destination, current_position):
# Calculate the differences in x and y coordinates
x_diff = destination[0] - current_position[0]
y_diff = destination[1] - current_position[1]
# Move the snake towards the food based on the differences
if x_diff > 0:
pyautogui.press('right')
elif x_diff < 0:
pyautogui.press('left')
elif y_diff > 0:
pyautogui.press('down')
elif y_diff < 0:
pyautogui.press('up')
def play_snake():
while True:
# Find the positions of the head and food
head_position = find_position(r"C:\Users\User\Desktop\bot\head.png", confidence=0.4)
food_position = find_position(r"C:\Users\User\Desktop\bot\food.png", confidence=0.4)
if head_position is None or food_position is None:
print("Head or food not found. Exiting...")
break
print('Head Center:', head_position)
print('Food Center:', food_position)
if head_position == food_position:
print("Snake reached the food. Exiting...")
break
# Move the snake towards the food
move_towards(food_position, head_position)
time.sleep(0.5)
# Start playing the snake game
play_snake()
Public Last updated: 2023-12-20 10:52:35 AM