-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_game.py
127 lines (107 loc) · 4.13 KB
/
snake_game.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
# Simple Python program for a snake game with colorama
# install colorama
import random
import os
import time
import msvcrt
from colorama import init, Fore
init(autoreset=True)
class SnakeGame:
def __init__(self):
self.board_size = [15, 25]
self.snake_position = [[5, 5], [5, 4], [5, 3]]
self.direction = 'd'
self.apple_position = self.generate_apple_position()
self.score = 0
self.game_over = False
self.user_response_speed = 0.1
self.snake_speed = 0.2
def generate_apple_position(self):
while True:
position = [random.randint(1, self.board_size[0] - 2), random.randint(1, self.board_size[1] - 2)]
if position not in self.snake_position:
return position
def display_board(self):
os.system('cls' if os.name == 'nt' else 'clear')
print("Controls:")
print("- Press 'w' to move up")
print("- Press 's' to move down")
print("- Press 'a' to move left")
print("- Press 'd' to move right")
print("- Press 'q' to quit\n")
print(f"Score: {self.score}\n")
for i in range(self.board_size[0]):
for j in range(self.board_size[1]):
if i == 0 or i == self.board_size[0] - 1 or j == 0 or j == self.board_size[1] - 1:
print(Fore.YELLOW + " █ ", end="")
elif [i, j] in self.snake_position:
if [i, j] == self.snake_position[0]:
print(Fore.GREEN + " ⦿ ", end="")
else:
print(Fore.GREEN + " ● ", end="")
elif i == self.apple_position[0] and j == self.apple_position[1]:
print(Fore.RED + " 🍎 ", end="")
else:
print(" ", end="")
print()
def get_user_input(self):
if msvcrt.kbhit():
key = msvcrt.getch().decode('utf-8').lower()
if key == 'w':
self.direction = 'w'
elif key == 's':
self.direction = 's'
elif key == 'a':
self.direction = 'a'
elif key == 'd':
self.direction = 'd'
elif key == 'q':
self.game_over = True
def check_collision(self):
head = self.snake_position[0].copy()
if self.direction == 'w':
head[0] -= 1
elif self.direction == 's':
head[0] += 1
elif self.direction == 'a':
head[1] -= 1
elif self.direction == 'd':
head[1] += 1
if head == self.apple_position:
self.score += 1
self.snake_position.insert(0, head)
self.apple_position = self.generate_apple_position()
else:
self.snake_position.insert(0, head)
self.snake_position.pop()
if (
head in self.snake_position[1:] or
head[0] <= 0 or head[0] >= self.board_size[0] - 1 or
head[1] <= 0 or head[1] >= self.board_size[1] - 1
):
self.game_over = True
print(Fore.RED + "\nGame Over! You hit the wall or yourself.")
self.display_scorecard()
def display_scorecard(self):
print("\n" + Fore.CYAN + "-------- Scorecard --------")
print(Fore.MAGENTA + f"🎉 Congratulations! You scored {self.score} points. 🎉")
if self.score >= 10:
print(Fore.GREEN + "👑 Great job! You're a Snake Master! 👑")
elif self.score >= 5:
print(Fore.YELLOW + "🌟 Well done! You're becoming a skilled snake player. 🌟")
else:
print(Fore.RED + "💪 Keep practicing! You'll improve with time. 💪")
print("---------------------------")
def play_game(self):
input("Press a key to start...")
while not self.game_over:
self.display_board()
self.get_user_input()
self.check_collision()
time.sleep(self.snake_speed)
def main():
print(Fore.CYAN + "Welcome to Snake Game!\n")
snake_game = SnakeGame()
snake_game.play_game()
if __name__ == "__main__":
main()