-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake_Game_using_python.py
More file actions
146 lines (122 loc) · 4.53 KB
/
Snake_Game_using_python.py
File metadata and controls
146 lines (122 loc) · 4.53 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import pygame
import random
x = pygame.init()
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
# print(x)
# Creating Window
speed = 5
screen_width = 1200
screen_height = 600
screen_caption = "Snake Game"
gameWindow = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(screen_caption)
pygame.display.update()
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 55)
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [x, y])
# Creating Game loop
def plot_snake(gameWindow, black1, snk_list, Snake_size1):
for x, y in snk_list:
pygame.draw.rect(gameWindow, black1, [x, y, Snake_size1, Snake_size1])
def welcome():
exit_game = False
while not exit_game:
gameWindow.fill(white)
text_screen("Welcome To Snake Game ", black, 300, 275)
text_screen("Press Space Bar For Continue ", black, 300, 320)
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gameloop()
pygame.display.update()
clock.tick(60)
def gameloop():
# Game specific Variable
exit_game = False
game_over = False
score = 0
Snake_x = 45
Snake_y = 55
Snake_size = 10
velocity_x = 0
velocity_y = 0
fps = 30 # means frame per secound
food_x = random.randint(0, screen_width / 2)
food_y = random.randint(0, screen_height / 2)
snk_length = 1
snk_list = []
with open("highscore.txt", "r") as f:
highscore = f.read()
while not exit_game:
if game_over:
with open("highscore.txt", "w") as f:
f.write(str(highscore))
gameWindow.fill(white)
text_screen("Game Over! Press Enter To Continue ", red, 300, 280)
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
welcome()
else:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
velocity_x = speed
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = -speed
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = -speed
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = speed
velocity_x = 0
if event.key == pygame.K_q:
score += 10
Snake_x += velocity_x
Snake_y += velocity_y
if abs(food_x - Snake_x) < 8 and abs(food_y - Snake_y) < 8:
score = score + 10
# print("score=",score)
food_x = random.randint(0, screen_width / 2)
food_y = random.randint(0, screen_height / 2)
snk_length += 5
if score > int(highscore):
highscore = score
gameWindow.fill(white)
text_screen("Score: " + str(score) + " High Score:" + str(highscore), red, 5, 5)
pygame.draw.rect(gameWindow, red, [food_x, food_y, Snake_size, Snake_size])
head = []
head.append(Snake_x)
head.append(Snake_y)
snk_list.append(head)
# print(snk_list)
if len(snk_list) > snk_length:
del snk_list[0]
if head in snk_list[:-1]:
game_over = True
# pygame.draw.rect(gameWindow,black,[Snake_x,Snake_y,Snake_size,Snake_size])
if (Snake_x < 0) or (Snake_y < 0) or (Snake_x > screen_width) or (Snake_y > screen_height):
game_over = True
plot_snake(gameWindow, black, snk_list, Snake_size)
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
# gameloop()
welcome()