repl.it
@jeffrobot/

Turtle_SnakeGame

Python (with Turtle)

No description

fork
loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import turtle
import random
import time
wn = turtle.Screen()

wn.setup(555,555)  #sets the screensize into 555x555 px

wn.bgcolor("orange")   #background color
pen = turtle.Turtle()
go_left = False
go_right = False
go_up = False
go_down = False
x = 0
#pressed key functions
def up():
  global x
  pen.penup()
  pen.pendown()
  x = 90

def right():
  global x
  pen.penup()
  pen.pendown()
  x = 0

def left():
  global x
  pen.penup()
  pen.pendown()
  x = 180

def down():
  global x
  pen.penup()
  pen.pendown()
  x = 270

#draw dot
def dotx():
  global x1
  global x2
  global y1
  global y2
  x1=random.randint(-225,225)
  y1=random.randint(-225,225)
  x2=list(range(x1-6,x1+6))
  y2=list(range(y1-6,y1+6))
  pen2.penup()
  pen2.goto(x1,y1)
  pen2.pendown()
  pen2.dot(20,"green")

#heading of the snake
def head():
  for i in range(1000):
    for ii in range(20):
      pen.seth(x)
      pen.fd(2)
      if pen.xcor()>=250.0 or pen.ycor()>=250.0 or pen.xcor()<=-250.0 or pen.ycor()<=-250.0:
          pen.clear()
          pen2.clear()
          pen4.write("GAME OVER")
          break
      elif (pen.xcor() in x2) and (pen.ycor() in y2):
        pen2.clear()
        pen4.write("EATEN",False,'center',font=('Arial',15,'normal'))
        dotx()
    if pen.xcor()>=250.0 or pen.ycor()>=250.0 or pen.xcor()<=-250.0 or pen.ycor()<=-250.0:
      pen.clear()
      pen2.clear()
      pen4.write("GAME OVER")
      break
    pen.clear()

#wn.color("white")
#wn.pensize(5)  #pensize

pen.shape('turtle')

#pen.hideturtle()


pen.delay(2)    #delay of animation
pen.speed(10)    #speed of animation

pen2=turtle.Turtle()  #dots
pen2.hideturtle()

pen4=turtle.Turtle()
pen4.hideturtle()
pen4.color("white")

#border
pen3=turtle.Turtle()
pen3.color("white")
pen3.pensize(3)
pen3.hideturtle()
pen3.speed(10)
pen3.penup()
pen3.goto(-250,-250)
pen3.pendown()
for p3 in range(4):
    pen3.fd(500)
    pen3.left(90)


#dots coordinates
x1=random.randint(-225,225)
y1=random.randint(-225,225)
x2=list(range(x1-6,x1+6))
y2=list(range(y1-6,y1+6))


dotx()  #call dots

#controls
wn.onkey(up,"Up")
wn.onkey(right,"Right")
wn.onkey(left,"Left")
wn.onkey(down,"Down")
wn.listen()

head()
result
console