-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (103 loc) · 3.72 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
import pygame, sys, time, random
speed = 10
#window sizes
frame_size_x = 720
frame_size_y = 480
check_errors = pygame.init()
if (check_errors[1] > 0 ):
print("Error" + check_errors[1 ])
else:
print('Game Successfully initiialized')
# initialise game windows
pygame.display.set_caption("Snake Game")
game_window = pygame.display.set_mode((frame_size_x,frame_size_y))
# colors
black = pygame.Color(0,0,0)
white = pygame.Color(255,255,255)
red = pygame.Color(255,0,0)
green = pygame.Color(0,255,0)
blue = pygame.Color(0,0,255)
fps_controller = pygame.time.Clock()
#One snake square size
square_size = 20
def init_vars():
global head_pos, snake_body, food_pos, food_spawn, score, direction
direction= "RIGHT"
head_pos = [120,60]
snake_body = [[120,60]]
food_pos = [random.randrange(1, (frame_size_x // square_size )) * square_size,
random.randrange(1, (frame_size_y// square_size)) * square_size]
food_spawn = True
score = 0
init_vars()
def show_score(choice,color,font,size):
score_font = pygame.font.SysFont(font,size)
score_surface = score_font.render("Score: " + str(score), True, color)
score_rect = score_surface.get_rect()
if choice == 1:
score_rect.midtop = (frame_size_x / 10, 15)
else:
score_rect.midtop = (frame_size_x / 2, frame_size_y/1.25)
game_window.blit(score_surface, score_rect)
#Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if ( event.key == pygame.K_UP or event.key == ord("w")
and direction != "DOWN" ):
direction = "UP"
elif ( event.key == pygame.K_DOWN or event.key == ord("s")
and direction != "UP" ):
direction = "DOWN"
elif ( event.key == pygame.K_RIGHT or event.key == ord("d")
and direction != "LEFT" ):
direction = "RIGHT"
elif ( event.key == pygame.K_LEFT or event.key == ord("a")
and direction != "RIGHT" ):
direction = "LEFT"
if direction == "UP":
head_pos[1] -= square_size
elif direction == "DOWN":
head_pos[1] += square_size
elif direction == "LEFT":
head_pos[0] -= square_size
elif direction == "RIGHT":
head_pos[0] += square_size
if head_pos[0] < 0 :
head_pos[0] = frame_size_x - square_size
elif head_pos[0] > frame_size_x - square_size:
head_pos[0] = 0
elif head_pos[1] < 0:
head_pos[1] = frame_size_y - square_size
elif head_pos[1] > frame_size_y - square_size:
head_pos[1] = 0
# eating apple
snake_body.insert(0,list(head_pos))
if head_pos[0] == food_pos[0] and head_pos[1] == food_pos[1]:
score += 1
food_spawn = False
else :
snake_body.pop()
# spawn food
if not food_spawn:
food_pos = [random.randrange(1,(frame_size_x // square_size )) * square_size,
random.randrange(1, (frame_size_y// square_size)) * square_size]
food_spawn = True
# GFX
game_window.fill(black)
for pos in snake_body:
pygame.draw.rect(game_window, green, pygame.Rect(
pos[0] + 2, pos[1] + 2,
square_size - 2, square_size -2))
pygame.draw.rect(game_window,red, pygame.Rect(food_pos[0],
food_pos[1], square_size,square_size))
# game over conditions
for block in snake_body[1:]:
if head_pos[0]== block[0] and head_pos[1] == block[1]:
init_vars()
show_score(1, white,'consolas',20)
pygame.display.update()
fps_controller.tick(speed)