Skip to content

Commit 8c6e2d5

Browse files
Merge pull request seeditsolution#362 from baibhavtripathy/patch-2
Snake
2 parents a552576 + 57bc2b4 commit 8c6e2d5

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Snake

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
pip install pygame
2+
3+
import pygame
4+
import time
5+
pygame.init()
6+
7+
white = (255, 255, 255)
8+
black = (0, 0, 0)
9+
red = (255, 0, 0)
10+
11+
dis_width = 800
12+
dis_height = 600
13+
dis = pygame.display.set_mode((dis_width, dis_width))
14+
pygame.display.set_caption('Snake Game by Baibhav')
15+
16+
game_over = False
17+
18+
x1 = dis_width/2
19+
y1 = dis_height/2
20+
21+
snake_block=10
22+
23+
x1_change = 0
24+
y1_change = 0
25+
26+
clock = pygame.time.Clock()
27+
snake_speed=30
28+
29+
font_style = pygame.font.SysFont(None, 50)
30+
31+
def message(msg,color):
32+
mesg = font_style.render(msg, True, color)
33+
dis.blit(mesg, [dis_width/2, dis_height/2])
34+
35+
while not game_over:
36+
for event in pygame.event.get():
37+
if event.type == pygame.QUIT:
38+
game_over = True
39+
if event.type == pygame.KEYDOWN:
40+
if event.key == pygame.K_LEFT:
41+
x1_change = -snake_block
42+
y1_change = 0
43+
elif event.key == pygame.K_RIGHT:
44+
x1_change = snake_block
45+
y1_change = 0
46+
elif event.key == pygame.K_UP:
47+
y1_change = -snake_block
48+
x1_change = 0
49+
elif event.key == pygame.K_DOWN:
50+
y1_change = snake_block
51+
x1_change = 0
52+
53+
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
54+
game_over = True
55+
56+
x1 += x1_change
57+
y1 += y1_change
58+
dis.fill(white)
59+
pygame.draw.rect(dis, black, [x1, y1, snake_block, snake_block])
60+
61+
pygame.display.update()
62+
63+
clock.tick(snake_speed)
64+
65+
message("You lost",red)
66+
pygame.display.update()
67+
time.sleep(2)
68+
69+
pygame.quit()
70+
quit()

0 commit comments

Comments
 (0)