|
| 1 | +import turtle as t |
| 2 | +import random |
| 3 | + |
| 4 | +w = 500 # Width of box |
| 5 | +h = 500 # Height of box |
| 6 | +food_size = 10 # Size of food |
| 7 | +delay = 100 # in millisecond |
| 8 | + |
| 9 | +# Values by which snake will move in direction when given direction |
| 10 | +offsets = { |
| 11 | + "up": (0, 20), |
| 12 | + "down": (0,-20), |
| 13 | + "left": (-20,0), |
| 14 | + "right": (20,0) |
| 15 | +} |
| 16 | + |
| 17 | +global SCORE |
| 18 | +SCORE = 0 |
| 19 | +# Default postion of game scene |
| 20 | + |
| 21 | +def reset(): |
| 22 | + global snake, snake_dir, food_position, pen |
| 23 | + |
| 24 | + snake = [[0,0],[0,20],[0,40],[0,60],[0,80]] |
| 25 | + snake_dir = "up" #default snake direction |
| 26 | + food_position = get_random_food_position() |
| 27 | + food.goto(food_position) # render food on scene |
| 28 | + move_snake() |
| 29 | + |
| 30 | +def move_snake(): |
| 31 | + global snake_dir,SCORE |
| 32 | + |
| 33 | + new_head = snake[-1].copy() |
| 34 | + new_head[0] = snake[-1][0] + offsets[snake_dir][0] |
| 35 | + new_head[1] = snake[-1][1] + offsets[snake_dir][1] |
| 36 | + |
| 37 | + if new_head in snake[:-1]: |
| 38 | + print(SCORE) |
| 39 | + reset() |
| 40 | + else: |
| 41 | + snake.append(new_head) |
| 42 | + |
| 43 | + |
| 44 | + if not food_collision(): |
| 45 | + snake.pop(0) |
| 46 | + |
| 47 | + if snake[-1][0] > w / 2: |
| 48 | + snake[-1][0] -= w |
| 49 | + elif snake[-1][0] < - w / 2: |
| 50 | + snake[-1][0] += w |
| 51 | + elif snake[-1][1] > h / 2: |
| 52 | + snake[-1][1] -= h |
| 53 | + elif snake[-1][1] < -h / 2: |
| 54 | + snake[-1][1] += h |
| 55 | + |
| 56 | + pen.clearstamps() |
| 57 | + |
| 58 | + for segment in snake: |
| 59 | + pen.goto(segment[0], segment[1]) |
| 60 | + pen.stamp() |
| 61 | + |
| 62 | + screen.update() |
| 63 | + t.ontimer(move_snake, delay) |
| 64 | + |
| 65 | +# If snake collides with food |
| 66 | +def food_collision(): |
| 67 | + global food_position, SCORE |
| 68 | + if get_distance(snake[-1], food_position) < 20: |
| 69 | + SCORE += 10 |
| 70 | + food_position = get_random_food_position() |
| 71 | + food.goto(food_position) |
| 72 | + return True |
| 73 | + return False |
| 74 | + |
| 75 | +# Random position for food |
| 76 | +def get_random_food_position(): |
| 77 | + x = random.randint(- w / 2 + food_size, w / 2 - food_size) |
| 78 | + y = random.randint(- h / 2 + food_size, h / 2 - food_size) |
| 79 | + return (x, y) |
| 80 | + |
| 81 | + |
| 82 | +def get_distance(pos1, pos2): |
| 83 | + x1, y1 = pos1 |
| 84 | + x2, y2 = pos2 |
| 85 | + distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5 |
| 86 | + return distance |
| 87 | + |
| 88 | +# Control |
| 89 | +def go_up(): |
| 90 | + global snake_dir |
| 91 | + if snake_dir != "down": |
| 92 | + snake_dir = "up" |
| 93 | +def go_down(): |
| 94 | + global snake_dir |
| 95 | + if snake_dir != "up": |
| 96 | + snake_dir = "down" |
| 97 | +def go_left(): |
| 98 | + global snake_dir |
| 99 | + if snake_dir != "right": |
| 100 | + snake_dir = "left" |
| 101 | +def go_right(): |
| 102 | + global snake_dir |
| 103 | + if snake_dir != "left": |
| 104 | + snake_dir = "right" |
| 105 | + |
| 106 | +#define screen setup |
| 107 | +screen = t.Screen() |
| 108 | +screen.setup(w, h) |
| 109 | +screen.title("Snake Game") |
| 110 | +screen.bgcolor("lightgrey") |
| 111 | +screen.tracer(0) |
| 112 | + |
| 113 | +#define snake setup |
| 114 | +pen = t.Turtle("square") |
| 115 | +pen.penup() |
| 116 | + |
| 117 | +#define food setup |
| 118 | +food = t.Turtle() |
| 119 | +food.shape("circle") |
| 120 | +food.color("red") |
| 121 | +food.shapesize(food_size / 20) |
| 122 | +food.penup() |
| 123 | + |
| 124 | +#define control setup |
| 125 | +screen.listen() |
| 126 | +screen.onkey(go_up, "Up") |
| 127 | +screen.onkey(go_right, "Right") |
| 128 | +screen.onkey(go_down, "Down") |
| 129 | +screen.onkey(go_left, "Left") |
| 130 | + |
| 131 | +reset() |
| 132 | +t.done() |
0 commit comments