-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
148 lines (120 loc) · 4.31 KB
/
game.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
from elements import Food
from elements import Enemy
from elements import Player
import random
import numpy as np
import cv2
class Game:
nFoods = 10
nEnemies = 10
foods = []
enemies = []
size, x, y = 0, 0, 0
food_reward = 50
enemy_penalty = 300
idle_penalty = 5
steps = 0
def __init__(self, _canvas, _width, _height):
self.canvas = _canvas
self.width = _width
self.height = _height
self.oldscore = 0
self.player = None
def init_objects(self):
self.steps = 0
for enemy in self.enemies:
self.enemies.remove(enemy)
for food in self.foods:
self.foods.remove(food)
for i in range(self.nFoods):
while True:
flag = False
size = int(self.width * Food.sizeF)
x = random.uniform(int(size / 2.0)+1, self.width - int(size / 2.0)+1)
y = random.uniform(int(size / 2.0)+1, self.width - int(size / 2.0)+1)
for n in self.foods:
if abs(x - n.posX) < size+1 and abs(y - n.posY) < size+1:
flag = True
if not flag:
break
self.foods.extend([Food(x, y, self.width)])
for j in range(self.nEnemies):
while True:
flag = False
size = int(self.width * Enemy.sizeF)
x = random.uniform(int(size / 2.0)+1, self.width - int(size / 2.0)+1)
y = random.uniform(int(size / 2.0)+1, self.width - int(size / 2.0)+1)
for n in self.enemies:
if abs(x - n.posX) < size+1 and abs(y - n.posY) < size+1:
flag = True
for n in self.foods:
if abs(x - n.posX) < size+1 and abs(y - n.posY) < size+1:
flag = True
if not flag:
break
self.enemies.extend([Enemy(x, y, self.width)])
while True:
flag = False
# size = random.randint(0, 0.1)
size = int(self.width * Player.sizeF)
x = random.uniform(int(size / 2.0)+1, self.width - int(size / 2.0)+1)
y = random.uniform(int(size / 2.0)+1, self.width - int(size / 2.0)+1)
for n in self.foods:
if abs(x - n.posX) < size + 1 and abs(y - n.posY) < size + 1:
flag = True
for n in self.enemies:
if abs(x - n.posX) < size + 1 and abs(y - n.posY) < size + 1:
flag = True
if not flag:
break
self.player = Player(x, y, self.width)
def update(self, action):
angle = 0
if action == 0:
angle = 0
if action == 1:
angle = 0.25
if action == 2:
angle = 0.5
if action == 3:
angle = 0.75
if action == 4:
angle = 1
if action == 5:
angle = 1.25
if action == 6:
angle = 1.5
if action == 7:
angle = 1.75
collisions = self.player.checkcollision(angle)
deltax, deltay = self.player.moveplayer(angle, collisions)
self.foods, eaten_food = self.player.checkeatingfood(self.foods)
self.enemies, eaten_enemy = self.player.checkeatingenemy(self.enemies)
reward = 0
if eaten_food:
reward += self.food_reward
elif eaten_enemy:
reward -= self.enemy_penalty
else:
reward -= self.idle_penalty
finished = self.player.checkeatenallfood(self.foods)
if self.steps >= 10000:
finished = True
self.player.posX = deltax
self.player.posY = deltay
self.steps += 1
image = self.get_image()
imagenp = np.array(image, dtype=np.float32)[:, :, :3]
cv2.imshow("", np.array(image.resize((720, 720))))
cv2.waitKey(1)
return imagenp, reward, finished
def get_image(self):
return self.canvas.update_image(self.foods, self.enemies, self.player)
def reset(self):
self.x, self.y, self.size = 0, 0, 0
self.enemies = []
self.foods = []
self.init_objects()
image = self.get_image()
image = np.asarray(image, dtype=np.float32)[:, :, :3]
return image