-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
125 lines (99 loc) · 3.07 KB
/
snake.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
import pygame
import random
pygame.init()
GAME_WIDTH = 500
GAME_HEIGHT = 500
SNAKECOLOR = (255, 153, 51)
FOODCOLOR = (51, 153, 255)
SNAKEBLOCK = 20
win = pygame.display.set_mode((GAME_WIDTH,GAME_HEIGHT))
pygame.display.set_caption("Anthony Kugel Snake Game")
clock = pygame.time.Clock()
run = True
font = pygame.font.SysFont(None, 30)
lost = False
score = 0
def message(msg, color, x, y):
mesg = font.render(msg, True, color)
win.blit(mesg, [int(x), int(y)])
class Snake:
def __init__(self, body):
self.body = body
self.x = body[0][0]
self.y = body[0][1]
self.size = SNAKEBLOCK
self.xDelta = 0
self.yDelta = 0
def draw(self):
for x in self.body:
pygame.draw.rect(win, SNAKECOLOR, [x[0], x[1], self.size, self.size])
def grow(self, x, y):
self.body.append([x, y])
def slither(self, x, y):
self.body.insert(0, [x, y])
self.body.pop()
class Food:
def __init__(self, food_x=0, food_y=0):
self.size = SNAKEBLOCK
self.location_x = food_x
self.location_y = food_y
def newfood(self):
randX = random.randint(0, GAME_WIDTH-self.size)
randXModulus = randX % SNAKEBLOCK
self.location_x = randX - randXModulus
randY = random.randint(0, GAME_HEIGHT-self.size)
randYModulus = randY % SNAKEBLOCK
self.location_y = randY - randYModulus
def drawFood(self):
pygame.draw.rect(win, FOODCOLOR, [self.location_x, self.location_y, self.size, self.size])
food = Food()
food.newfood()
snake = Snake([[20,20],[40,20],[60,20]])
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if snake.xDelta != SNAKEBLOCK:
snake.xDelta = -(SNAKEBLOCK)
snake.yDelta = 0
if keys[pygame.K_RIGHT]:
if snake.xDelta != -(SNAKEBLOCK):
snake.xDelta = SNAKEBLOCK
snake.yDelta = 0
if keys[pygame.K_UP]:
if snake.yDelta != SNAKEBLOCK:
snake.xDelta = 0
snake.yDelta = -(SNAKEBLOCK)
if keys[pygame.K_DOWN]:
if snake.yDelta != -(SNAKEBLOCK):
snake.xDelta = 0
snake.yDelta = SNAKEBLOCK
snake.x += snake.xDelta
snake.y += snake.yDelta
if snake.x > GAME_WIDTH or snake.x < 0 or snake.y > GAME_HEIGHT or snake.y < 0:
lost = True
run = False
break
if snake.body[0] in snake.body[1:] and score > 5:
lost = True
run = False
break
if food.location_x == snake.x and food.location_y == snake.y:
food.newfood()
score += 5
snake.grow(snake.x, snake.y)
snake.slither(snake.x, snake.y)
win.fill((0,0,0))
snake.draw()
food.drawFood()
message(f"{score}", (255, 0, 0), 10, 10)
pygame.display.update()
clock.tick(10)
if lost:
message(f"You lose! Your score is {score}", (255, 0, 0), GAME_WIDTH/4, GAME_HEIGHT/2)
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()
quit