-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
180 lines (135 loc) · 4.97 KB
/
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pygame
import random
import os
pygame.init()
# Handling High Score if it doesnt exit
if not os.path.exists("high_score"):
f = open("high_score", "w")
f.write("0")
f.close()
# Colors
red = (255, 0, 0)
black = (0, 0, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)
navy_blue = (0, 0, 83)
# Screen Size
screen_width = 800
screen_height = 500
# Creating Window
gameWindow = pygame.display.set_mode((screen_width, screen_height))
# Game Title
pygame.display.set_caption('The Snakes Game')
pygame.display.update()
# Some Constants
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 30)
fps = 60
def textOnScreen(text, color, pos_x, pos_y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [pos_x, pos_y])
def plotSnakeOnScreen(gameWindow, color, snake_list, snake_size):
for x, y in snake_list:
pygame.draw.rect(gameWindow, color, [x, y, snake_size, snake_size])
# Home Screen
def homeScreen():
exit_home = False
while not exit_home:
gameWindow.fill(navy_blue)
textOnScreen("Welcome to The Snakes Game!!! Press ENTER to continue", yellow, 10, 10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_home = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
exit_home = True
gameLoop()
if event.key == pygame.K_ESCAPE:
exit_home = True
pygame.display.update()
clock.tick(fps)
# Game Loop
def gameLoop():
f = open("high_score")
high_score = f.read()
f.close()
if high_score == "":
high_score = 0
else:
high_score = int(high_score)
# Game Specific Variables
exit_game = False
game_over = False
snake_x = 400
snake_y = 250
snake_size = 10
initial_velocity = 5
velocity_x = 0
velocity_y = 0
food_x = random.randint(int(screen_width / 6), int(5 * screen_width / 6))
food_y = random.randint(int(screen_height / 6), int(5 * screen_height / 6))
food_size = 5.0
snake_list = []
snake_length = 1
score = 0
# Actual Loop
while not exit_game:
if game_over:
gameWindow.fill(black)
textOnScreen(f"Game Over, Your score is {score}!!! Press ENTER to Continue, or ESC to exit", red, 10, 10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
gameLoop()
if event.key == pygame.K_ESCAPE:
exit_game = True
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and velocity_x == 0:
velocity_x = initial_velocity
velocity_y = 0
if event.key == pygame.K_LEFT and velocity_x == 0:
velocity_x = -initial_velocity
velocity_y = 0
if event.key == pygame.K_UP and velocity_y == 0:
velocity_x = 0
velocity_y = -initial_velocity
if event.key == pygame.K_DOWN and velocity_y == 0:
velocity_x = 0
velocity_y = initial_velocity
if event.key == pygame.K_ESCAPE:
exit_game = True
snake_x += velocity_x
snake_y += velocity_y
if abs(snake_x - food_x) < 10 and abs(snake_y - food_y) < 10:
score += 10
food_x = random.randint(int(screen_width / 6), int(5 * screen_width / 6))
food_y = random.randint(int(screen_height / 6), int(5 * screen_height / 6))
snake_length += 2
if score > high_score:
high_score = score
with open("high_score", "w") as f:
f.write(str(high_score))
gameWindow.fill(black)
scoreOnScreenText = "Score: " + str(score) + ", High Score: " + str(high_score)
textOnScreen(scoreOnScreenText, yellow, 10, 10)
pygame.draw.circle(gameWindow, red, (food_x, food_y), food_size)
head = [snake_x, snake_y]
snake_list.append(head)
if len(snake_list) > snake_length:
del snake_list[0]
if head in snake_list[:-1]:
game_over = True
if snake_x < 0 or snake_x > screen_width or snake_y < 0 or snake_y > screen_height:
game_over = True
plotSnakeOnScreen(gameWindow, green, snake_list, snake_size)
pygame.display.update()
clock.tick(fps)
homeScreen()
pygame.quit()
quit()