forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGame
81 lines (61 loc) · 1.85 KB
/
SnakeGame
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
import pygame
import random
# Check for modules of pygame
pygame.init()
# Defining RGB color
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
# Creating Screen
screen_width = 900
screen_height = 600
gameWindow = pygame.display.set_mode((screen_height, screen_height))
pygame.display.set_caption('Snake Game')
# this is used to pygameDisplay
pygame.display.update()
# Game Specific Variables
clock = pygame.time.Clock()
exit_game = False
game_over = False
snake_x = 45
snake_y = 55
snake_size = 11
score = 0
food_size = 10
init_vel = 5
velocity_x = 0
velocity_y = 0
fps = 60
food_x = random.randint(20, int(screen_width / 2))
food_y = random.randint(20, int(screen_height / 2))
while not exit_game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
velocity_x = init_vel
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = - init_vel
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = - init_vel
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = init_vel
velocity_x = 0
if abs(snake_x - food_x) < 6 and abs(snake_y - food_y) < 6:
score += 1
print('SCORE: ', score * 10)
food_x = random.randint(20, int(screen_width / 2))
food_y = random.randint(20, int(screen_height / 2))
snake_x = snake_x + velocity_x
snake_y = snake_y + velocity_y
gameWindow.fill(white)
pygame.draw.rect(gameWindow, black, [snake_x, snake_y, snake_size, snake_size])
pygame.draw.rect(gameWindow, red, [food_x, food_y, food_size, food_size])
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()