├── README.md └── source code └── snake_game.py /README.md: -------------------------------------------------------------------------------- 1 | # Snake-game 2 | a snake game made with python language 3 | -------------------------------------------------------------------------------- /source code/snake_game.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import random 3 | 4 | GAME_WIDTH = 700 5 | GAME_HEIGHT = 700 6 | SPEED = 50 7 | SPACE_SIZE = 50 8 | BODY_PARTS = 3 9 | SNAKE_COLOR = "#00FF00" 10 | FOOD_COLOR = "#FF0000" 11 | BACKGROUND_COLOR = "#000000" 12 | 13 | 14 | class Snake: 15 | 16 | def __init__(self): 17 | self.body_size = BODY_PARTS 18 | self.coordinates = [] 19 | self.squares = [] 20 | 21 | for i in range(0, BODY_PARTS): 22 | self.coordinates.append([0, 0]) 23 | 24 | for x, y in self.coordinates: 25 | square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, tag="snake") 26 | self.squares.append(square) 27 | 28 | 29 | class Food: 30 | 31 | def __init__(self): 32 | 33 | x = random.randint(0, (GAME_WIDTH / SPACE_SIZE)-1) * SPACE_SIZE 34 | y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE 35 | 36 | self.coordinates = [x, y] 37 | 38 | canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food") 39 | 40 | 41 | def next_turn(snake, food): 42 | 43 | x, y = snake.coordinates[0] 44 | 45 | if direction == "up": 46 | y -= SPACE_SIZE 47 | elif direction == "down": 48 | y += SPACE_SIZE 49 | elif direction == "left": 50 | x -= SPACE_SIZE 51 | elif direction == "right": 52 | x += SPACE_SIZE 53 | 54 | snake.coordinates.insert(0, (x, y)) 55 | 56 | square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR) 57 | 58 | snake.squares.insert(0, square) 59 | 60 | if x == food.coordinates[0] and y == food.coordinates[1]: 61 | 62 | global score 63 | 64 | score += 1 65 | 66 | label.config(text="Score:{}".format(score)) 67 | 68 | canvas.delete("food") 69 | 70 | food = Food() 71 | 72 | else: 73 | 74 | del snake.coordinates[-1] 75 | 76 | canvas.delete(snake.squares[-1]) 77 | 78 | del snake.squares[-1] 79 | 80 | if check_collisions(snake): 81 | game_over() 82 | 83 | else: 84 | window.after(SPEED, next_turn, snake, food) 85 | 86 | 87 | def change_direction(new_direction): 88 | 89 | global direction 90 | 91 | if new_direction == 'left': 92 | if direction != 'right': 93 | direction = new_direction 94 | elif new_direction == 'right': 95 | if direction != 'left': 96 | direction = new_direction 97 | elif new_direction == 'up': 98 | if direction != 'down': 99 | direction = new_direction 100 | elif new_direction == 'down': 101 | if direction != 'up': 102 | direction = new_direction 103 | 104 | 105 | def check_collisions(snake): 106 | 107 | x, y = snake.coordinates[0] 108 | 109 | if x < 0 or x >= GAME_WIDTH: 110 | return True 111 | elif y < 0 or y >= GAME_HEIGHT: 112 | return True 113 | 114 | for body_part in snake.coordinates[1:]: 115 | if x == body_part[0] and y == body_part[1]: 116 | return True 117 | 118 | return False 119 | 120 | 121 | def game_over(): 122 | 123 | canvas.delete(ALL) 124 | canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2, 125 | font=('consolas',70), text="GAME OVER", fill="red", tag="gameover") 126 | 127 | 128 | window = Tk() 129 | window.title("Snake game") 130 | window.resizable(False, False) 131 | 132 | score = 0 133 | direction = 'down' 134 | 135 | label = Label(window, text="Score:{}".format(score), font=('consolas', 40)) 136 | label.pack() 137 | 138 | canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH) 139 | canvas.pack() 140 | 141 | window.update() 142 | 143 | window_width = window.winfo_width() 144 | window_height = window.winfo_height() 145 | screen_width = window.winfo_screenwidth() 146 | screen_height = window.winfo_screenheight() 147 | 148 | x = int((screen_width/2) - (window_width/2)) 149 | y = int((screen_height/2) - (window_height/2)) 150 | 151 | window.geometry(f"{window_width}x{window_height}+{x}+{y}") 152 | 153 | window.bind('', lambda event: change_direction('left')) 154 | window.bind('', lambda event: change_direction('right')) 155 | window.bind('', lambda event: change_direction('up')) 156 | window.bind('', lambda event: change_direction('down')) 157 | 158 | snake = Snake() 159 | food = Food() 160 | 161 | next_turn(snake, food) 162 | 163 | window.mainloop() 164 | --------------------------------------------------------------------------------