-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
188 lines (161 loc) · 4.92 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
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
184
185
186
187
188
import pygame as pyg
import numpy as np
import os
from board import *
from button import *
from displayer import *
from enums import *
pyg.init()
font = pyg.font.Font(None, 30)
class Game():
def __init__(self):
self.surface = pyg.display.set_mode((600, 400))
pyg.display.set_caption('Random Dice Wars Emulator')
icon = pyg.image.load('img/AppIcon.png')
pyg.display.set_icon(icon)
Dice.set_font_and_surface(font, self.surface)
Button.set_font_and_surface(font, self.surface)
Displayer.set_font_and_surface(font, self.surface)
self.gridsize = Board.gridsize
self.dicesize = Board.dicesize
self.board = Board()
self.init_all()
self.tick = 0
self.tick_wave_ratio = 13
self.update_ui()
def reset(self):
self.tick = 0
Board.reset_game()
def play_step(self, action):
# action should be an int which maps to the
# value of Action class im myEnum.py
self.tick += 1
if self.tick % self.tick_wave_ratio == 0:
print('='*15)
Board.wave += 1
idx = np.sum([Board.wave >= wave for wave in Board.SP_level_wave])
Board.SP += Board.SP_list[idx]
# Step 1, collect user input
for event in pyg.event.get():
if event.type == pyg.QUIT:
pyg.quit()
quit()
reward = 0
game_over = False
skill_reward = Board.check_skill()
reward += skill_reward
# Step 2, execute action from Agent
print(Action(action).name)
if action == Action.Pass.value:
# Agent choose to do nothing
reward += Reward.pass_.value
success = True
elif action == Action.Summon.value:
# Agent choose to summon a new dice
success = Board.summon_dice()
if success:
reward += Reward.summon_success.value
else:
reward += Reward.summon_fail.value
# elif action in range(Action.Upg_1.value, Action.Upg_5.value+1):
# # Agent choose to upgrade dice
# dice_type = Action(action).name
# dice_type = int(dice_type.split('_')[1])
# success = Board.lvlup_dice(dice_type)
# if success:
# reward += Reward.upg_success.value
# else:
# reward += Reward.upg_fail.value
elif action in range(Action.Merge_0_1.value, Action.Merge_13_14.value+1):
# Agent choose to merge dice
action_str = Action(action).name
_, loc_a, loc_b = action_str.split('_')
loc_a = int(loc_a)
loc_b = int(loc_b)
success = Board.merge_dice(loc_a, loc_b)
if not success:
success = Board.merge_dice(loc_b, loc_a)
if success:
reward += Reward.merge_success.value
else:
reward += Reward.merge_fail.value
print('Success:', success)
# Step 3, check if game over
if Board.wave > 30:
game_over = True
reward += self.cal_reward()
return game_over, reward
# Step 4, update UI
self.update_ui()
reward += self.cal_reward()
# Step 5, return reward for this step
return game_over, reward
def cal_reward(self):
score = 0
a = 5
b = 0.2
c = 1
for dice in Board.dice_list:
dt = dice.dice_type
score += a*pow(dice.dice_star, 3) + b*Board.dice_lvl[dt]
return score
def update_ui(self):
self.surface.fill(pyg.Color("black"))
self.update()
self.draw()
pyg.display.flip()
def update(self):
Board.update()
for dp in self.displayer_list:
dp.update()
for btn in self.BtnList:
btn.update()
def draw(self):
Board.draw()
for dp in self.displayer_list:
dp.draw()
for btn in self.BtnList:
btn.draw()
def init_all(self):
self.init_btn()
self.init_displayer()
def init_btn(self):
self.BtnList = []
# Create Summon button
path = 'img/Summon.png'
image = pyg.image.load(path)
image = pyg.transform.scale(image, (self.dicesize-5, self.dicesize-5))
x = self.gridsize*5.2 + 50
y = self.gridsize*0.5 + 50
h = self.gridsize*2.3
btn = SummonBtn(image, x, y, self.dicesize+5, h)
self.BtnList.append(btn)
# Create Reset button
path = 'img/Reset.png'
image = pyg.image.load(path)
image = pyg.transform.scale(image, (30, 30))
x = self.gridsize*5.2 + 50
y = self.gridsize*3.2 + 50
h = self.gridsize*0.6
btn = ResetBtn(image, x, y, self.dicesize+5, h)
self.BtnList.append(btn)
for i in range(1, 6):
x = self.gridsize*(i-1) + 50
y = self.gridsize*3.4 + 50
h = self.gridsize*0.6
btn = LvlUpBtn(i, x, y, self.dicesize+5, h)
self.BtnList.append(btn)
def init_displayer(self):
self.displayer_list = []
# Create Wave display button
x = self.gridsize*1.4 + 50
y = self.gridsize*0
h = self.gridsize*0.4
wd = WaveDisplayer(x, y, self.dicesize*2.4, h)
self.displayer_list.append(wd)
# Create SP display button
x = self.gridsize*5.2 + 50
y = self.gridsize*0
h = self.gridsize*0.4
sd = SPDisplayer(x, y, self.dicesize*1, h)
self.displayer_list.append(sd)