-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakegame.py
More file actions
108 lines (97 loc) · 3.46 KB
/
snakegame.py
File metadata and controls
108 lines (97 loc) · 3.46 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
import pygame
import random
import time
pygame.init()
black=(0,0,0)#snake
food=(139,69,19)#food
light=(250,235,215)#background
width,height=600,400
game_screen=pygame.display.set_mode((width,height))
pygame.display.set_caption("My Snake")
#snake and food initial positions
snake_x,snake_y=width/2,height/2
change_x=0
change_y=0
food_x=random.randrange(0,width)//10*10
food_y=random.randrange(0,height)//10*10
#game variables
clock=pygame.time.Clock()
snake_body=[(snake_x,snake_y)]
game_over=False
def display_snake_and_food():
global food_x
global food_y
global snake_x,snake_y
global game_over
snake_x=(snake_x+change_x)%width
snake_y=(snake_y+change_y)%height
#check if colides itself
if((snake_x, snake_y) in snake_body[1:]):
game_over=True
return
#add the new head of the snake
snake_body.append((snake_x,snake_y))
if(food_x==snake_x and food_y==snake_y):
food_x=random.randrange(0,width)//10*10
food_y=random.randrange(0,height)//10*10
else:
del snake_body[0]
game_screen.fill((light))
pygame.draw.rect(game_screen,(food),[food_x,food_y,10,10])
for(x,y) in snake_body:
pygame.draw.rect(game_screen,(black),[x,y,10,10])
pygame.display.update()
def show_game_over():
font = pygame.font.Font(pygame.font.get_default_font(),16)
text = font.render('GAME OVER. Play Again? (Enter Y or N)',True,(255,0,0))
text_rect = text.get_rect(center=(width /2, height /2))
game_screen.blit(text, text_rect)
pygame.display.update()
pygame.display.flip()
def reset_game():
global snake_x, snake_y, change_x, snake_y, food_x, food_y, snake_body, game_over
snake_x, snake_y = width /2, height/2
change_x, change_y =0, 0
food_x = random.randrange(0,width) // 10*10
food_y = random.randrange(0,height) // 10*10
snake_body = [(snake_x, snake_y)]
game_over = False
while True:
if game_over:
show_game_over()
pygame.event.clear()
waiting_for_input = True
while waiting_for_input:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y: # Restart the game
reset_game()
waiting_for_input = False
elif event.key == pygame.K_n: # Quit the game
pygame.quit()
quit()
else:
continue
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if(event.type==pygame.KEYDOWN):
if(event.key==pygame.K_LEFT):
change_x=-10
change_y=0
elif(event.key==pygame.K_RIGHT):
change_x=10
change_y=0
elif(event.key==pygame.K_UP):
change_y=-10
change_x=0
elif(event.key==pygame.K_DOWN):
change_x=0
change_y=10
display_snake_and_food()
clock.tick(9)