0% found this document useful (0 votes)
20 views4 pages

Snake Game - Task2

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)
20 views4 pages

Snake Game - Task2

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/ 4

Snake game

To run and execute the provided Snake game code using the turtle module, you can follow these
steps:

Ensure you have Python installed on your computer.

Save the provided code into a Python file, e.g., snake_game.py.

Run the Python file using a Python interpreter.

Here's the complete code, formatted correctly for execution:

from turtle import *

from random import randrange

from freegames import square, vector

food = vector(0, 0)

snake = [vector(10, 0)]

aim = vector(0, -10)

def change(x, y):

"Change snake direction."

aim.x = x

aim.y = y

def inside(head):

"Return True if head inside boundaries."

return -200 < head.x < 190 and -200 < head.y < 190

def move():

"Move snake forward one segment."

head = snake[-1].copy()

head.move(aim)
if not inside(head) or head in snake:

square(head.x, head.y, 9, 'red')

update()

return

snake.append(head)

if head == food:

print('Snake:', len(snake))

food.x = randrange(-15, 15) * 10

food.y = randrange(-15, 15) * 10

else:

snake.pop(0)

clear()

for body in snake:

square(body.x, body.y, 9, 'black')

square(food.x, food.y, 9, 'green')

update()

ontimer(move, 100)

setup(420, 420, 370, 0)

hideturtle()

tracer(False)

listen()

onkey(lambda: change(10, 0), 'Right')

onkey(lambda: change(-10, 0), 'Left')

onkey(lambda: change(0, 10), 'Up')

onkey(lambda: change(0, -10), 'Down')

move()
done()

(**to execute open cmd - pip install freegames )

Instructions to Run the Code:

Save the Code:

Save the code in a file named snake_game.py.

Run the Code:

Open a terminal or command prompt and navigate to the directory where you saved snake_game.py.
Then run the following command:

bash

Copy code

python snake_game.py

Explanation:

Import Statements:

from turtle import *: Imports all necessary functions from the turtle module.

from random import randrange: Imports the randrange function from the random module.

from freegames import square, vector: Imports the square and vector functions from the freegames
module.

Global Variables:

food, snake, aim: Define the food position, the snake's body, and the snake's direction.

Functions:

change(x, y): Changes the snake's direction based on user input.

inside(head): Checks if the snake's head is within the game boundaries.

move(): Moves the snake, checks for collisions, updates the snake's body, and redraws the game
screen.

Game Setup:

setup(): Sets up the game window.


hideturtle(), tracer(False): Hides the turtle cursor and turns off automatic screen updates for
smoother animation.

listen(): Sets up keyboard listeners for controlling the snake.

onkey(): Maps arrow keys to change the snake's direction.

move(): Starts the game by calling the move function.

done(): Keeps the window open.

This will start the Snake game, and you can control the snake using the arrow keys. The snake will
grow as it eats the food, and the game will end if the snake collides with the boundaries or itself.

You might also like