repl.it
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
126
127
#Works only in Python 3.6.4
import random
import turtle

turtle.Screen().bgcolor("black")

snake = turtle.Turtle()
snake.shape("square")
snake.color("green")
snake.speed(0)
snake.penup()
snake.ht()

energy = turtle.Turtle()
energy.shape("square")
energy.speed(0)
energy.color("yellow")
energy.penup()
energy.ht()

game_started = False

start = turtle.Turtle()
start.color("white")
start.write("Press Space to Start", align="center",font=("Arial",16,"bold"))
start.penup()
start.ht()

score = turtle.Turtle()
score.speed(0)
score.ht()

def outside_window():
  left_wall =  -turtle.window_width()/2
  right_wall = turtle.window_width()/2
  top_wall = turtle.window_height()
  bottom_wall = -turtle.window_height()/2
  (x,y) = snake.pos()

  outside = \
    x < left_wall or \
    x > right_wall or \
    y > top_wall or \
    y < bottom_wall
  return outside

def game_over():
  snake.color("black")
  energy.color("black")
  score.clear()
  turtle.penup()
  turtle.ht()
  gameover = turtle.Turtle()
  gameover.color("white")
  gameover.write("Game Over", align="center",font=("Arial",16,"bold"))

def display_score(current_score):
  score.clear()
  score.penup()
  x = (turtle.window_width()/2)-50
  y = (turtle.window_height()/2)-50
  score.color("white")
  score.write(str(current_score),align="right",font=("Arial",16,"bold"))

def place_energy():
  energy.ht()
  energy.setx(random.randint(-200,200))
  energy.sety(random.randint(-200,200))
  energy.st()

def start_game():
  global game_started
  if game_started:
    return
 
  game_started = True
  score = 0
  start.ht()
  snake.st()
  snake.color("green")
  start.clear()

  snake_speed = 2
  snake_length = 3
  snake.shapesize(1,snake_length,1)

  display_score(score)
  place_energy()

  while True:
    snake.forward(snake_speed)

    if snake.distance(energy) < 20:
      place_energy()
      snake_length += 1
      snake.shapesize(1,snake_length,1)
      snake_speed += 1
      score+=10
      display_score(score)
    if outside_window():
      game_over()
      break

def move_up():
  if snake.heading() == 0 or snake.heading() == 180:
    snake.setheading(90)

def move_down():
  if snake.heading() == 0 or snake.heading() == 180:
    snake.setheading(270)

def move_right():
  if snake.heading() == 90 or snake.heading() == 270:
    snake.setheading(0)

def move_left():
  if snake.heading() == 90 or snake.heading() == 270:
    snake.setheading(180)

turtle.onkey(move_up,"Up")
turtle.onkey(move_down,"Down")
turtle.onkey(move_right,"Right")
turtle.onkey(move_left,"Left")
turtle.onkey(start_game,"space")
turtle.listen()
turtle.mainloop()
result
console
?