0% found this document useful (0 votes)
46 views13 pages

Sodapdf

Uploaded by

kanishkkasera12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views13 pages

Sodapdf

Uploaded by

kanishkkasera12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Snake Game

Input:
# modules
import turtle
import time
import random

delay = 0.1
score = 0
high_score = 0

# Creating a window screen


wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
# the width and height can be put as user's choice
wn.setup(width=600, height=600)
wn.tracer(0)

# head
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"

# food
food = turtle.Turtle()
colors = random.choice(['red', 'green', 'black'])
shapes = random.choice(['square', 'triangle', 'circle'])
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)

pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))

# assigning key directions


def group():
if head.direction != "down":
head.direction = "up"

def godown():
if head.direction != "up":
head.direction = "down"

def goleft():
if head.direction != "right":
head.direction = "left"

def goright():
if head.direction != "left":
head.direction = "right"

def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)

wn.listen()
wn.onkeypress(group, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")

segments = []

# Main Gameplay
while True:
wn.update()
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or
head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if head.distance(food) < 20:
x = random.randint(-270, 270)
y = random.randint(-270, 270)
food.goto(x, y)

# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange") # tail colour
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
# Checking for head collisions with body segments
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()

score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24,
"bold"))
time.sleep(delay)

wn.mainloop()
Output:
Running the program:

Gameplay:
.

.
AI In Snake Game
Now we will see how AI can play this game by certain
processes and how will it be able to do it.
But first we will understand the domains the AI is related to
for that task.

Related Domains:
Computer Vision (CV):
Utilization:
Extracts relevant information from the game environment, including
the positions of the snake, food, and obstacles.
Enables the AI to perceive and interpret visual data, forming the basis
for decision-making and action selection.
Techniques:
Employs image processing algorithms for feature extraction, such as
edge detection and object segmentation.
Utilizes deep learning models, such as convolutional neural networks
(CNNs), for object recognition and pattern detection.
Enhancements:
Advanced CV techniques may include feature enhancement methods
to improve the clarity and accuracy of extracted information.
Integration of real-time video processing enables dynamic perception
and adaptation to changing game scenarios.

Reinforcement Learning (RL):


Learning Process:
RL algorithms enable the agent to learn optimal strategies through
trial and error by receiving feedback in the form of rewards or
penalties.
The AI iteratively improves its gameplay strategy to maximize
cumulative rewards over time.
Exploration vs. Exploitation:
Balances exploration of new strategies with exploitation of learned
behaviors to ensure effective adaptation to varying game conditions.
Techniques such as epsilon-greedy exploration or softmax action
selection are commonly used to manage this trade-off.
Model-Free vs. Model-Based:
RL approaches may be model-free, directly learning policies or value
functions from experience, or model-based, using learned models to
plan future actions.
Model-free methods like Q-learning are well-suited for environments
with complex dynamics, while model-based approaches offer more
efficient exploration in simpler environments.

Machine Learning (ML):


Feature Extraction:
ML techniques extract relevant features from raw game data,
enabling the AI to understand and interact with the environment
more effectively.
Feature engineering may involve hand-crafting domain-specific
features or using automated feature learning methods such as deep
feature extraction.
Model Training:
Trains models to predict future states or rewards based on observed
data, facilitating decision-making and policy optimization.
Techniques such as supervised learning, unsupervised learning, and
semi-supervised learning may be applied depending on the
availability of labeled data.
Policy Optimization:
Optimizes decision-making policies to maximize cumulative rewards
over time.
Reinforcement learning algorithms utilize ML techniques to learn
policies through interaction with the environment, updating policy
parameters based on observed rewards and state transitions.

Deep Learning (DL):


Feature Extraction with CNNs:
Utilizes convolutional neural networks (CNNs) for feature extraction
from game images, enabling the AI to perceive complex spatial
patterns.
CNN architectures such as ResNet or VGGNet may be employed to
extract hierarchical features from raw pixel data.
Function Approximation:
DL models approximate value functions or policy functions in RL
algorithms, enabling efficient decision-making in high-dimensional
state spaces.
Techniques like Deep Q-Networks (DQN) or Policy Gradient methods
leverage deep neural networks to represent complex decision-
making policies.
Representation Learning:
Learns hierarchical representations of game data, capturing abstract
features that facilitate effective decision-making and adaptation to
varying game scenarios.
Unsupervised learning techniques such as autoencoders or
generative adversarial networks (GANs) may be used for
representation learning tasks.
Now we will see how the AI will work in that case.

Working Of The AI:


Initialization:
The AI initializes the game environment by capturing the initial state
using computer vision (CV) techniques. It processes the raw visual
data to extract relevant features such as the positions of the snake,
food, and obstacles.
Utilizing advanced CV algorithms, the AI enhances feature detection
and segmentation accuracy to ensure a robust initialization of the
game state.
In parallel, the AI sets up the reinforcement learning (RL) model or
algorithm, initializing Q-values, neural network weights, or policy
parameters to begin learning from scratch.

Game Loop:
Entering a continuous loop, the AI observes the game state, selects
actions, interacts with the environment, and updates its internal
representation.
Through CV techniques, the AI processes real-time visual data to
track changes in the positions of game elements, detecting new
obstacles or food items, and dynamically adjusting its strategy based
on evolving game conditions.
This iterative process allows the AI to perceive and interpret the
game environment in real-time, informing its decision-making and
action selection.
Action Selection:
When selecting actions, the AI evaluates various factors including the
current game state, learned policies, and exploration-exploitation
trade-offs.
Leveraging advanced ML techniques such as ensemble learning or
Bayesian optimization, the AI improves action selection strategies to
effectively navigate complex game scenarios.
Additionally, the AI incorporates domain-specific knowledge or
heuristics to guide its decision-making process, enhancing its ability
to make informed choices.

Updating the Environment:


After selecting an action, the AI simulates the effects of the action in
the game environment and observes resulting state changes such as
the snake's movement, food consumption, and collisions with
obstacles.
Utilizing physics-based simulations or predictive models, the AI
accurately predicts the consequences of its actions, enabling it to
make informed decisions about its next moves.

Reward Calculation:
The AI calculates rewards based on observed state changes, assigning
values to different outcomes based on their desirability.
Positive rewards are granted for beneficial actions like eating food,
while negative rewards penalize detrimental actions such as colliding
with obstacles.
Advanced reward shaping techniques such as temporal difference
learning or reward shaping functions are employed to guide the AI's
learning process and incentivize desired behaviors more effectively.

Learning and Policy Update:


Using RL techniques, the AI updates its internal model or policy
based on observed rewards and state transitions, aiming to maximize
the cumulative reward over time.
Learning from past experiences, the AI refines its strategies through
mechanisms such as value iteration or policy gradient methods,
adapting its decision-making process to achieve better outcomes.

Termination and Evaluation:


The AI continues playing until a termination condition is met, such as
the snake colliding with an obstacle or achieving a predefined
objective.
Following termination, the AI evaluates its performance based on
various metrics such as final score, survival time, or achieved
objectives.
Utilizing advanced evaluation techniques such as multi-objective
optimization or Pareto efficiency analysis, the AI assesses its
performance across multiple dimensions, identifying trade-offs
between different objectives.

Iterative Improvement:
Throughout the gameplay process, the AI undergoes iterative
improvement through repeated training cycles and policy updates.
Learning from past experiences and refining its strategies, the AI
explores different decision-making approaches to achieve better
outcomes over time.
Employing advanced techniques such as meta-learning or transfer
learning, the AI accelerates the learning process and enhances its
generalization capabilities in challenging or dynamic environments.

You might also like