forked from aliakbar09a/AI_plays_snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arena.py
60 lines (54 loc) · 1.94 KB
/
Arena.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
import random
import pygame
import colors as col
class Arena:
def __init__(self, width, height, block):
self.height = height
self.width = width
self.block = block
self.food = (0, 0)
def setup_background(self, screen, color):
screen.fill(color)
def setup(self, screen, color_bg, color):
self.setup_background(screen, color_bg)
# building the horizontal walls
l = self.block
for x in range(0, self.width, l):
y = 0
pygame.draw.rect(screen, color, (x, y, l, l), 1)
pygame.draw.rect(screen, color, (x+3, y+3, l-6, l-6))
y = self.height - l
pygame.draw.rect(screen, color, (x, y, l, l), 1)
pygame.draw.rect(screen, color, (x+3, y+3, l-6, l-6))
# building the vertical walls
for y in range(l, self.height-l, l):
x = 0
pygame.draw.rect(screen, color, (x, y, l, l), 1)
pygame.draw.rect(screen, color, (x+3, y+3, l-6, l-6))
x = self.width - l
pygame.draw.rect(screen, color, (x, y, l, l), 1)
pygame.draw.rect(screen, color, (x+3, y+3, l-6, l-6))
return screen
def newFood(self, list):
'''
list = snake body parts position list
'''
found = False
size = self.block
while not found:
x = random.randint(2*size, self.width - 2*size)
x = x - (x % size)
y = random.randint(2*size, self.height - 2*size)
y = y - (y % size)
i = 0
while i < len(list):
if x == list[i][0] and y == list[i][1]:
break
i = i + 1
if i == len(list):
found = True
self.food = (x, y)
return self.food
def drawFood(self, screen, color):
pygame.draw.rect(screen, color, (self.food[0], self.food[1], self.block, self.block))
return screen