-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakey.py
149 lines (120 loc) ยท 4.92 KB
/
snakey.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
# Author: Tanvi Roy
# Release Date: 31st March 2020
#====================================================================
import pygame, random, time, sys
from pygame.locals import *
pygame.mixer.pre_init(44100,-16,1,512)
pygame.mixer.init()
pygame.init()
Clock = pygame.time.Clock()
#====================================================================
# dimensions of the display screen
display_height = 600
display_width = 800
snake_size = 10
snake_speed = 17
# colors
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
black = (0,0,0)
font_style = pygame.font.SysFont("comicsans", 32)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("It\'s Snakey Time")
#====================================================================
def message(msg, color):
msg = font_style.render(msg, True, color)
display.blit(msg, [display_width / 5.5, display_height / 2])
def snakey(snake_size, snake_count):
for x in snake_count:
pygame.draw.rect(display, green, [x[0], x[1], snake_size, snake_size])
def main(): # game loop
game_over = False
game_close = False
x1 = display_width/2
y1 = display_height/2
x1_change = 0
y1_change = 0
snake_count = []
snake_length = 1
score = 0
sound_eat = pygame.mixer.Sound('yummy.wav')
sound_die = pygame.mixer.Sound('end.wav')
foodx = round(random.randrange(0, display_width - (snake_size * 3)) / 10.0) * 10.0
foody = round(random.randrange(0, display_height - (snake_size * 3)) / 10.0) * 10.0
while not game_over:
while game_close == True:
message("You Lost! Press C to Play Again or Q to Quit Game", white)
pygame.display.update()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_q:
game_over = True
print("Your final score is:", score)
pygame.quit()
sys.exit()
if event.key == K_c:
main()
if event.type == QUIT:
game_over = True
print("Your final score is:", score)
pygame.quit()
sys.exit()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT and x1_change != snake_size:
x1_change = -snake_size
y1_change = 0
elif event.key == K_RIGHT and x1_change != -snake_size:
x1_change = snake_size
y1_change = 0
elif event.key == K_DOWN and y1_change != -snake_size:
y1_change = snake_size
x1_change = 0
elif event.key == K_UP and y1_change != snake_size:
y1_change = -snake_size
x1_change = 0
if event.type == QUIT:
game_over = True
print("Your final score is:", score)
pygame.quit()
sys.exit()
x1 += x1_change
y1 += y1_change
display.fill(black)
pygame.draw.line(display, red, (0,0),(0,display_height),10)
pygame.draw.line(display, red, (0,display_height),(display_width,display_height),10)
pygame.draw.line(display, red, (display_width,display_height),(display_width,0),10)
pygame.draw.line(display, red, (display_width,0),(0,0),10)
pygame.draw.rect(display, green, [x1, y1, snake_size, snake_size])
# snake movement
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_count.append(snake_head)
if len(snake_count) > snake_length:
del snake_count[0]
snakey(snake_size, snake_count) # calls snake function
# eat randomly appearing food and grow
pygame.draw.rect(display, red, [foodx, foody, snake_size, snake_size])
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, display_width - (snake_size * 3)) / 10.0) * 10.0
foody = round(random.randrange(0, display_height - (snake_size * 3)) / 10.0) * 10.0
sound_eat.play()
snake_length += 1
score += 1
# boundaries
if x1 >= display_width or x1 < 0 or y1 >= display_height or y1 < 0:
game_close = True
sound_die.play()
pygame.display.update()
Clock.tick(snake_speed)
pygame.quit()
quit()
#====================================================================
# _____ ____ __ __ _ _ ___
# | |____| | \ | \ / |
# | | | | \| \/ _|_
#
main()