-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
129 lines (110 loc) · 4.01 KB
/
play.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
import pygame
import random
import time
from enum import Enum
from collections import namedtuple
class Direction(Enum):
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
Point = namedtuple("Point", "x, y")
pygame.display.init()
pygame.font.init()
font = pygame.font.Font(pygame.font.get_default_font(), 25)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 255, 0)
ORANGE = (255, 165, 0)
BLACK = (0, 0, 0)
BLOCK_SIZE = 20
SPEED = 15
class SnakeGameHuman:
def __init__(self, width=640, height=480):
self.w = width
self.h = height
self.display = pygame.display.set_mode((self.w, self.h))
pygame.display.set_caption("Snake Game - Human Playable")
self.clock = pygame.time.Clock()
self.reset()
def reset(self):
self.direction = Direction.RIGHT
self.head = Point(self.w / 2, self.h / 2)
self.snake = [self.head,
Point(self.head.x - BLOCK_SIZE, self.head.y),
Point(self.head.x - (2 * BLOCK_SIZE), self.head.y)]
self.score = 0
self.food = None
self._place_food()
self.frame_iteration = 0
def _place_food(self):
x = random.randint(0, (self.w - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (self.h - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
self.food = Point(x, y)
if self.food in self.snake:
self._place_food()
def play_step(self):
self.frame_iteration += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and self.direction != Direction.DOWN:
self.direction = Direction.UP
elif event.key == pygame.K_DOWN and self.direction != Direction.UP:
self.direction = Direction.DOWN
elif event.key == pygame.K_LEFT and self.direction != Direction.RIGHT:
self.direction = Direction.LEFT
elif event.key == pygame.K_RIGHT and self.direction != Direction.LEFT:
self.direction = Direction.RIGHT
self._move(self.direction)
self.snake.insert(0, self.head)
if self.is_collision() or self.frame_iteration > 100 * len(self.snake):
return True, self.score
if self.head == self.food:
self.score += 1
self._place_food()
else:
self.snake.pop()
self._update_ui()
self.clock.tick(SPEED)
return False, self.score
def is_collision(self, pt=None):
if pt is None:
pt = self.head
if pt.x > self.w - BLOCK_SIZE or pt.x < 0 or pt.y > self.h - BLOCK_SIZE or pt.y < 0:
return True
if pt in self.snake[1:]:
return True
return False
def _move(self, direction):
x = self.head.x
y = self.head.y
if direction == Direction.RIGHT:
x += BLOCK_SIZE
elif direction == Direction.LEFT:
x -= BLOCK_SIZE
elif direction == Direction.DOWN:
y += BLOCK_SIZE
elif direction == Direction.UP:
y -= BLOCK_SIZE
self.head = Point(x, y)
def _update_ui(self):
self.display.fill(BLACK)
for pt in self.snake[1:]:
pygame.draw.rect(self.display, GREEN, pygame.Rect(pt.x, pt.y, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(self.display, ORANGE, pygame.Rect(self.head.x, self.head.y, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(self.display, RED, pygame.Rect(self.food.x, self.food.y, BLOCK_SIZE, BLOCK_SIZE))
text = font.render("Score: " + str(self.score), True, WHITE)
self.display.blit(text, [0, 0])
pygame.display.flip()
if __name__ == "__main__":
game = SnakeGameHuman()
while True:
game_over, score = game.play_step()
if game_over:
break
time.sleep(0.1)
print(f'Final Score: {score}')
pygame.quit()