Skip to content

Commit 163ed03

Browse files
authored
Snake Game (#37)
* Spam Bot It will spam anything u save in spam.txt, on WhatsApp Instagram etc * Snake Game This is the retro snake game I made , it uses turtle, randrange, square and vector library , for the implementation I made three functions change, inside and move responsible for changing direction, to check if it is within boundaries and to move respectively, and onkey is also used to define to change the snake's direction to left right up and down * Rock Paper Scissors Game A simple rock paper scissor game program , random library used to use randInt, the user inputs what he has to choose between rock paper scissor which are labelled as 1, 2,0 respectively and random integer generated for the computer and if u win lose or match drawn , the message is displayed accordingly. * Delete rock_paper_scissors.py
1 parent 720f753 commit 163ed03

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

snake.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from turtle import *
2+
import turtle
3+
from random import randrange
4+
from freegames import square, vector
5+
6+
turtle.bgcolor("black")
7+
food = vector(0, 0)
8+
snake = [vector(10, 0)]
9+
aim = vector(0, -10)
10+
11+
def change(x, y):
12+
"Change snake direction."
13+
aim.x = x
14+
aim.y = y
15+
16+
def inside(head):
17+
"Return True if head inside boundaries."
18+
return -250 < head.x < 230 and -250 < head.y < 230
19+
20+
def move():
21+
"Move snake forward one segment."
22+
head = snake[-1].copy()
23+
head.move(aim)
24+
25+
if not inside(head) or head in snake:
26+
square(head.x, head.y, 9, 'red')
27+
update()
28+
return
29+
30+
snake.append(head)
31+
32+
if head == food:
33+
print('Snake:', len(snake))
34+
food.x = randrange(-15, 15) * 10
35+
food.y = randrange(-15, 15) * 10
36+
else:
37+
snake.pop(0)
38+
39+
clear()
40+
41+
for body in snake:
42+
square(body.x, body.y, 9, 'green')
43+
44+
square(food.x, food.y, 9, 'red')
45+
update()
46+
ontimer(move, 100)
47+
48+
49+
hideturtle()
50+
tracer(False)
51+
listen()
52+
onkey(lambda: change(10, 0), 'Right')
53+
onkey(lambda: change(-10, 0), 'Left')
54+
onkey(lambda: change(0, 10), 'Up')
55+
onkey(lambda: change(0, -10), 'Down')
56+
move()
57+
done()

0 commit comments

Comments
 (0)