π₯ Create a Python AI That Plays the Google Dino Game Automatically! [Full Code]
Demo :
Click Video πππ
Want to build an AI bot that plays the Google Chrome Dino Game automatically? π In this tutorial, we’ll use Python, OpenCV, and PyAutoGUI to detect obstacles and make the AI jump at the right time. π¦⚡ Get the full source code and step-by-step guide to set it up on your PC!
✅ Full Python Code Included
✅ GUI Version for Easy Control
✅ Completely Automated Gamepla
Code :
π Install Dependencies First: cmd => pip install pyautogui opencv-python numpy
import pyautogui
import cv2
import numpy as np
import time
import threading
import tkinter as tk
from tkinter import messagebox
# Define region of interest (game area)
GAME_REGION = (300, 400, 600, 150)
bot_running = False # Control flag
def is_obstacle_present():
screen = pyautogui.screenshot(region=GAME_REGION)
img = np.array(screen)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
obstacle = np.sum(threshold) > 500000 # Adjust sensitivity
return obstacle
def start_bot():
global bot_running
bot_running = True
status_label.config(text="Status: Running", fg="green")
messagebox.showinfo("Dino AI", "Bot started! Switch to the game window.")
def bot_logic():
time.sleep(3) # Give user time to switch to game
while bot_running:
if is_obstacle_present():
pyautogui.press("space") # Jump
thread = threading.Thread(target=bot_logic)
thread.start()
def stop_bot():
global bot_running
bot_running = False
status_label.config(text="Status: Stopped", fg="red")
messagebox.showinfo("Dino AI", "Bot stopped!")
# GUI Setup
root = tk.Tk()
root.title("Dino AI Bot")
root.geometry("300x200")
root.configure(bg="#222")
title_label = tk.Label(root, text="Chrome Dino AI Bot", font=("Arial", 14, "bold"), fg="white", bg="#222")
title_label.pack(pady=10)
status_label = tk.Label(root, text="Status: Stopped", font=("Arial", 12), fg="red", bg="#222")
status_label.pack()
start_button = tk.Button(root, text="Start Bot", font=("Arial", 12), command=start_bot, bg="green", fg="white")
start_button.pack(pady=5)
stop_button = tk.Button(root, text="Stop Bot", font=("Arial", 12), command=stop_bot, bg="red", fg="white")
stop_button.pack(pady=5)
root.mainloop()
Comments
Post a Comment