-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.py
50 lines (41 loc) · 1.87 KB
/
food.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
import random
import numpy as np
from game_utils import Point, Direction
from game_settings import DIRECTIONS_QUANTITY, BLOCK_SIZE, SCREEN_W, SCREEN_H
class Food:
def __init__(self, position):
self.position = position
self.direction = random.choice([Direction.RIGHT, Direction.LEFT, Direction.UP, Direction.DOWN])
def move(self, action=None):
# [straight, right, left, no action]
clock_wise = [Direction.RIGHT, Direction.DOWN, Direction.LEFT, Direction.UP]
# If action is None, choose a random action
if action is None:
action = [0, 0, 0, 0]
action[random.randint(0, 3)] = 1 # Set one of the actions to 1 at random
# Current direction index
idx = clock_wise.index(self.direction)
if np.array_equal(action, [1, 0, 0, 0]):
new_dir = clock_wise[idx] # no change
elif np.array_equal(action, [0, 1, 0, 0]):
next_idx = (idx + 1) % DIRECTIONS_QUANTITY
new_dir = clock_wise[next_idx] # right turn r -> d -> l -> u
elif np.array_equal(action, [0, 0, 1, 0]):
next_idx = (idx - 1) % DIRECTIONS_QUANTITY
new_dir = clock_wise[next_idx] # left turn r -> u -> l -> d
elif np.array_equal(action, [0, 0, 0, 1]):
new_dir = self.direction # No action
else:
raise Exception('Unknown direction')
self.direction = new_dir
x = self.position.x
y = self.position.y
if self.direction == Direction.RIGHT:
x = min(x + BLOCK_SIZE, SCREEN_W - BLOCK_SIZE)
elif self.direction == Direction.LEFT:
x = max(x - BLOCK_SIZE, 0)
elif self.direction == Direction.DOWN:
y = min(y + BLOCK_SIZE, SCREEN_H - BLOCK_SIZE)
elif self.direction == Direction.UP:
y = max(y - BLOCK_SIZE, 0)
self.position = Point(x, y)