-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsnake_human.py
183 lines (152 loc) · 5.8 KB
/
snake_human.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import pygame
import random
from collections import namedtuple
from pygame.locals import K_RIGHT,K_LEFT,K_UP,K_DOWN,QUIT
Position = namedtuple('Point', 'x, y')
class Direction:
right = 0
left = 1
up = 2
down = 3
class Snake:
def __init__(self,block_size):
self.blocks=[]
self.blocks.append(Position(20,15))
self.blocks.append(Position(19,15))
self.block_size = block_size
self.current_direction = Direction.right
self.image = pygame.image.load('snake.png')
def move(self):
if (self.current_direction == Direction.right):
movesize = (1, 0)
elif (self.current_direction == Direction.left):
movesize = (-1, 0)
elif (self.current_direction == Direction.up):
movesize = (0, -1)
else:
movesize = (0, 1)
head = self.blocks[0]
new_head = Position(head.x + movesize[0], head.y + movesize[1])
self.blocks.insert(0,new_head)
def handle_input(self):
keys = pygame.key.get_pressed()
if (keys[K_RIGHT] and self.current_direction != Direction.left):
self.current_direction = Direction.right
elif (keys[K_LEFT] and self.current_direction != Direction.right):
self.current_direction = Direction.left
elif(keys[K_UP] and self.current_direction != Direction.down):
self.current_direction = Direction.up
elif(keys[K_DOWN] and self.current_direction != Direction.up):
self.current_direction = Direction.down
self.move()
def draw(self,surface,frame):
for index, block in enumerate(self.blocks):
positon = (block.x * self.block_size,
block.y * self.block_size)
if index == 0:
src = (((self.current_direction * 2) + frame) * self.block_size,
0, self.block_size, self.block_size)
else:
src = (8 * self.block_size, 0, self.block_size, self.block_size)
surface.blit(self.image, positon, src)
class Berry:
def __init__(self,block_size):
self.block_size = block_size
self.image = pygame.image.load('berry.png')
self.position = Position(1, 1)
def draw(self,surface):
rect = self.image.get_rect()
rect.left = self.position.x * self.block_size
rect.top = self.position.y * self.block_size
surface.blit(self.image, rect)
class Wall:
def __init__(self,block_size):
self.block_size = block_size
self.map = self.load_map('map.txt')
self.image = pygame.image.load('wall.png')
def load_map(self,fileName):
with open(fileName,'r') as map_file:
content = map_file.readlines()
content = [list(line.strip()) for line in content]
return content
def draw(self,surface):
for row, line in enumerate(self.map):
for col, char in enumerate(line):
if char == '1':
position = (col*self.block_size,row*self.block_size)
surface.blit(self.image, position)
class Game:
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
def __init__(self,Width=640, Height=480):
pygame.init()
self.block_size = 16
self.Win_width , self.Win_height = (Width, Height)
self.Space_width = self.Win_width//self.block_size-2
self.Space_height = self.Win_height//self.block_size-2
self.surface = pygame.display.set_mode((self.Win_width, self.Win_height))
self.score = 0
self.frame = 0
self.running = True
self.Clock = pygame.time.Clock()
self.fps = 1
self.font = pygame.font.Font(None, 32)
self.snake = Snake(self.block_size)
self.berry = Berry(self.block_size)
self.wall = Wall(self.block_size)
self.position_berry()
def position_berry(self):
bx = random.randint(1, self.Space_width)
by = random.randint(1, self.Space_height)
self.berry.position = Position(bx, by)
if self.berry.position in self.snake.blocks:
self.position_berry()
# handle collision
def berry_collision(self):
head = self.snake.blocks[0]
if (head.x == self.berry.position.x and
head.y == self.berry.position.y):
self.position_berry()
self.score += 1
else:
self.snake.blocks.pop()
def head_hit_body(self):
head = self.snake.blocks[0]
if head in self.snake.blocks[1:]:
return True
return False
def head_hit_wall(self):
head = self.snake.blocks[0]
if (self.wall.map[head.y][head.x] == '1'):
return True
return False
def draw_data(self):
text = "score = {0}".format(self.score)
text_img = self.font.render(text, 1, Game.WHITE)
text_rect = text_img.get_rect(centerx=self.surface.get_width()/2, top=32)
self.surface.blit(text_img, text_rect)
def draw(self):
self.surface.fill(Game.BLACK)
self.wall.draw(self.surface)
self.berry.draw(self.surface)
self.snake.draw(self.surface,self.frame)
self.draw_data()
pygame.display.update()
# main loop
def play(self):
while self.running:
for event in pygame.event.get():
if event.type == QUIT:
self.running = False
self.frame = (self.frame + 1) % 2
self.snake.handle_input()
self.berry_collision()
if self.head_hit_wall() or self.head_hit_body():
print('Final Score', self.score)
self.running = False
self.draw()
self.Clock.tick(self.fps)
pygame.quit()
if __name__ == '__main__':
game = Game()
game.play()