summaryrefslogtreecommitdiffstats
path: root/snake.py
blob: f3d6c39d032bdc1813af6f6b5e2a9a2fadfb7bc2 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import pygame
import random

#Generate Apple
def generate_apple(body):
  empty_space = list(range(0,400))
  for part in body:
    empty_space.remove(part)
    
  apple_location = random.choice(empty_space)
  return apple_location

#Collision Check
def collision_check(body, body_copy):
  if body[0] < 0 or body[0] > 399:
    return True
  elif body[0] % 20 == 0 and body_copy[0] % 20 == 19:
    return True
  elif body[0] % 20 == 19 and body_copy[0] % 20 == 0:
    return True
  elif body[0] in body_copy:
    return True
  else:
    return False
  
#Gameover
def gameover():
  gameover = True
  font = pygame.font.Font(None, 50)
  gameover_msg = font.render("Gameover", True, (0, 0, 0), None)
  try_again_msg = font.render("Play again?", True, (0, 0, 0))
  main_menu_msg = font.render("Main Menu", True, (0, 0, 0))
  gameover_msg_rect = gameover_msg.get_rect(center = (250, 225))
  try_again_msg_rect = try_again_msg.get_rect(center = (250, 275))
  main_menu_msg_rect = main_menu_msg.get_rect(center = (250, 325))
  
  while gameover == True:
    pygame.display.update()
    screen.blit(gameover_msg, gameover_msg_rect)
    screen.blit(try_again_msg, try_again_msg_rect)
    screen.blit(main_menu_msg, main_menu_msg_rect)

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
      
      if event.type == pygame.MOUSEBUTTONDOWN and main_menu_msg_rect.collidepoint(event.pos):
        return False
      elif event.type == pygame.MOUSEBUTTONDOWN and try_again_msg_rect.collidepoint(event.pos):
        gameover = False
        

#Player Win
def congrats(head, head_color):
  font = pygame.font.Font(None, 100)
  msg = font.render("YOU WIN!", True, (0, 0, 0))
  msg_rect = msg.get_rect(center = (250, 250))

  while True:
    pygame.display.update()
    screen.fill((0, 255, 0))
    pygame.draw.rect(screen, head_color, head)
    screen.blit(msg, msg_rect)

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()

#How To Play
def how_to_play_menu(title_font, small_font, text_color):
  title = title_font.render("How To Play", True, text_color)
  line1 = small_font.render("You already know how snake works.", True, text_color)
  line2 = small_font.render("WASD or arrow keys to move.", True, text_color)
  line3 = small_font.render("The dark green square represents the snakes head.", True, text_color)
  line4 = small_font.render("The red square is the food.", True, text_color)

  title_rect = title.get_rect(center = (250, 150))
  line1_rect = line1.get_rect(center = (250, 200))
  line2_rect = line2.get_rect(center = (250, 230))
  line3_rect = line3.get_rect(center = (250, 290))
  line4_rect = line4.get_rect(center = (250, 350))

  example_snake_head = pygame.Rect(210, 250, 25, 25)
  example_snake_body = pygame.Rect(235, 250, 50, 25)
  example_apple = pygame.Rect(235, 310, 25, 25)

  return_text = title_font.render("Return to main menu", True, text_color)
  return_text_rect = return_text.get_rect(center = (250, 450))
  
  in_menu = True
  while in_menu == True:
    pygame.display.update()
    screen.fill((255, 253, 181))
    screen.blit(title, title_rect)
    screen.blit(line1, line1_rect)
    screen.blit(line2, line2_rect)
    screen.blit(line3, line3_rect)
    screen.blit(line4, line4_rect)
    pygame.draw.rect(screen, (0, 220, 0), example_snake_head)
    pygame.draw.rect(screen, (0, 255, 0), example_snake_body)
    pygame.draw.rect(screen, (255, 0, 0), example_apple)
    screen.blit(return_text, return_text_rect)

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
      
      if event.type == pygame.MOUSEBUTTONDOWN and return_text_rect.collidepoint(event.pos):
        in_menu = False
    
  
#Main Menu
def main_menu():
  text_color = (0, 0, 0)
  title_font = pygame.font.Font(None, 80)
  button_font = pygame.font.Font(None, 50)
  small_font = pygame.font.Font(None, 28)

  title = title_font.render("Snake", True, text_color)
  play = button_font.render("Play", True, text_color)
  how_to_play = button_font.render("How to Play", True, text_color)
  quit = button_font.render("Quit", True, text_color)
  credit = small_font.render("By Hy Lac Nguyen", True, text_color)

  title_rect = title.get_rect(center = (250, 100))
  play_rect = play.get_rect(center = (250, 250))
  how_to_play_rect = how_to_play.get_rect(center = (250, 300))
  quit_rect = quit.get_rect(center = (250, 350))
  credit_rect = credit.get_rect(center = (250, 150))
  
  in_menu = True
  while in_menu == True:
    pygame.display.update()
    screen.fill((255, 253, 181))
    screen.blit(title, title_rect)
    screen.blit(play, play_rect)
    screen.blit(how_to_play, how_to_play_rect)
    screen.blit(quit, quit_rect)
    screen.blit(credit, credit_rect)

    mouse = pygame.mouse.get_pos()
    
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
      
      if event.type == pygame.MOUSEBUTTONDOWN:
        #Play button click
        if play_rect.right > mouse[0] > play_rect.left and play_rect.top < mouse[1] < play_rect.bottom:
          in_menu = False
          break
          
        elif how_to_play_rect.right > mouse[0] > how_to_play_rect.left and how_to_play_rect.top < mouse[1] < how_to_play_rect.bottom:
          how_to_play_menu(button_font, small_font, text_color)

        elif quit_rect.collidepoint(event.pos):
          pygame.quit()
        
  
########################

pygame.init()

#Display Settings
screen_size = (500, 500)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("'Snake' By Hy Lac")

#Player
grid = []
for y in range(20):
  for x in range(20):
    grid.append(pygame.Rect(x*25, y*25, 25, 25))

move_event = pygame.event.custom_type()
player_color = (0, 255, 0)
player_head_color = (0, 220, 0)

#Snake body is made up of various points on the grid
body = [209, 210, 211]

#Apple
apple_color = (255, 0, 0)
apple_location = generate_apple(body)

#Player direction values :: 1 = up, 2 = down, 3 = left, 4 = right
player_direction = 0
pygame.time.set_timer(move_event, 350)

#Main Menu
main_menu()

#Main Loop
running = True
while running == True:
  #Basic Update
  pygame.display.update()
  screen.fill((255, 255, 255))
  for i in range(len(grid)):
    if i in body: 
      pygame.draw.rect(screen, player_color, grid[i])
    else:
      pygame.draw.rect(screen, (255,253,181), grid[i])

  pygame.draw.rect(screen, apple_color, grid[apple_location])
  pygame.draw.rect(screen, player_head_color, grid[body[0]])
        
  for event in pygame.event.get():
    #Quitting the game
    if event.type == pygame.QUIT:
      running = False
      break
      
    #Direction/User Input
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_UP or event.key == pygame.K_w:
        player_direction = 1
        
      elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
        player_direction = 2
        
      elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
        player_direction = 3

      elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
        player_direction = 4

    #Movement
    if event.type == move_event and player_direction > 0:
      body_copy = body[:]
      
      if player_direction == 1:
        body[0] -= 20
      elif player_direction == 2:
        body[0] += 20
      elif player_direction == 3:
        body[0] -= 1
      elif player_direction == 4:
        body[0] += 1

      #Collision check
      if collision_check(body, body_copy) == True:
        replay = gameover()
        if replay == False:
          main_menu()
        player_direction = 0
        body = [209, 210, 211]
        continue
        
      #Eating Apple
      if body[0] == apple_location:
        body.insert(1, body_copy[0])
        
        if len(body) == 400:
          congrats(grid[body[0]], player_head_color)
          
        apple_location = generate_apple(body)
      #Actual Movement
      for i in range(1, len(body)):
        body[i] = body_copy[i-1]

pygame.quit()