Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
f4aa9741e3 | |||
|
3591fedfa2 | ||
050a0359b0 | |||
c6cc763fe1 | |||
08ea0e945e | |||
cb9b8e6b2d | |||
|
dfa408d5fa | ||
|
ebaf97d82c | ||
8666a7dbf9 | |||
952e8663b4 | |||
5f7fea24fb | |||
297bd19336 | |||
bab75274dc | |||
681bf08424 | |||
c8f0dc76b6 | |||
e31364b21c | |||
162e2df890 | |||
8d73a85707 | |||
b6ba817d55 | |||
5c1a1605b8 |
1
.gitignore
vendored
@ -150,3 +150,4 @@ cython_debug/
|
|||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
.idea/
|
.idea/
|
||||||
|
/algorithms/neural_network/data/
|
||||||
|
Before Width: | Height: | Size: 814 B |
Before Width: | Height: | Size: 820 B |
Before Width: | Height: | Size: 789 B |
Before Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 760 B |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 725 B |
@ -1 +0,0 @@
|
|||||||
{}
|
|
@ -1 +0,0 @@
|
|||||||
{}
|
|
@ -1 +0,0 @@
|
|||||||
{}
|
|
@ -10,23 +10,39 @@ from common.constants import DEVICE, BATCH_SIZE, NUM_EPOCHS, LEARNING_RATE, SETU
|
|||||||
|
|
||||||
class NeuralNetwork(pl.LightningModule):
|
class NeuralNetwork(pl.LightningModule):
|
||||||
def __init__(self, numChannels=3, batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE, num_classes=4):
|
def __init__(self, numChannels=3, batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE, num_classes=4):
|
||||||
super().__init__()
|
super(NeuralNetwork, self).__init__()
|
||||||
self.layer = nn.Sequential(
|
self.conv1 = nn.Conv2d(numChannels, 24, (3, 3), padding=1)
|
||||||
nn.Linear(36*36*3, 300),
|
self.relu1 = nn.ReLU()
|
||||||
nn.ReLU(),
|
self.maxpool1 = nn.MaxPool2d((2, 2), stride=2)
|
||||||
nn.Linear(300, 4),
|
self.conv2 = nn.Conv2d(24, 48, (3, 3), padding=1)
|
||||||
nn.LogSoftmax(dim=-1)
|
self.relu2 = nn.ReLU()
|
||||||
)
|
self.fc1 = nn.Linear(48*18*18, 800)
|
||||||
|
self.relu3 = nn.ReLU()
|
||||||
|
self.fc2 = nn.Linear(800, 400)
|
||||||
|
self.relu4 = nn.ReLU()
|
||||||
|
self.fc3 = nn.Linear(400, 4)
|
||||||
|
self.logSoftmax = nn.LogSoftmax(dim=1)
|
||||||
|
|
||||||
self.batch_size = batch_size
|
self.batch_size = batch_size
|
||||||
self.learning_rate = learning_rate
|
self.learning_rate = learning_rate
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
|
x = self.conv1(x)
|
||||||
|
x = self.relu1(x)
|
||||||
|
x = self.maxpool1(x)
|
||||||
|
x = self.conv2(x)
|
||||||
|
x = self.relu2(x)
|
||||||
x = x.reshape(x.shape[0], -1)
|
x = x.reshape(x.shape[0], -1)
|
||||||
x = self.layer(x)
|
x = self.fc1(x)
|
||||||
|
x = self.relu3(x)
|
||||||
|
x = self.fc2(x)
|
||||||
|
x = self.relu4(x)
|
||||||
|
x = self.fc3(x)
|
||||||
|
x = self.logSoftmax(x)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def configure_optimizers(self):
|
def configure_optimizers(self):
|
||||||
optimizer = SGD(self.parameters(), lr=self.learning_rate)
|
optimizer = Adam(self.parameters(), lr=self.learning_rate)
|
||||||
return optimizer
|
return optimizer
|
||||||
|
|
||||||
def training_step(self, batch, batch_idx):
|
def training_step(self, batch, batch_idx):
|
||||||
|
@ -10,44 +10,8 @@ from torch.optim import Adam
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import pytorch_lightning as pl
|
import pytorch_lightning as pl
|
||||||
from pytorch_lightning.callbacks import EarlyStopping
|
from pytorch_lightning.callbacks import EarlyStopping
|
||||||
|
import torchvision.transforms.functional as F
|
||||||
|
from PIL import Image
|
||||||
def train(model):
|
|
||||||
model = model.to(DEVICE)
|
|
||||||
model.train()
|
|
||||||
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
|
|
||||||
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
|
|
||||||
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
|
|
||||||
test_loader = DataLoader(testset, batch_size=BATCH_SIZE, shuffle=True)
|
|
||||||
|
|
||||||
criterion = nn.CrossEntropyLoss()
|
|
||||||
optimizer = Adam(model.parameters(), lr=LEARNING_RATE)
|
|
||||||
|
|
||||||
for epoch in range(NUM_EPOCHS):
|
|
||||||
for batch_idx, (data, targets) in enumerate(train_loader):
|
|
||||||
data = data.to(device=DEVICE)
|
|
||||||
targets = targets.to(device=DEVICE)
|
|
||||||
|
|
||||||
scores = model(data)
|
|
||||||
loss = criterion(scores, targets)
|
|
||||||
|
|
||||||
optimizer.zero_grad()
|
|
||||||
loss.backward()
|
|
||||||
|
|
||||||
optimizer.step()
|
|
||||||
|
|
||||||
if batch_idx % 4 == 0:
|
|
||||||
print("epoch: %d loss: %.4f" % (epoch, loss.item()))
|
|
||||||
|
|
||||||
print("FINISHED TRAINING!")
|
|
||||||
torch.save(model.state_dict(), "./learnednetwork.pth")
|
|
||||||
|
|
||||||
print("Checking accuracy for the train set.")
|
|
||||||
check_accuracy(train_loader)
|
|
||||||
print("Checking accuracy for the test set.")
|
|
||||||
check_accuracy(test_loader)
|
|
||||||
print("Checking accuracy for the tiles.")
|
|
||||||
check_accuracy_tiles()
|
|
||||||
|
|
||||||
|
|
||||||
def check_accuracy_tiles():
|
def check_accuracy_tiles():
|
||||||
@ -95,12 +59,13 @@ def check_accuracy_tiles():
|
|||||||
|
|
||||||
|
|
||||||
def what_is_it(img_path, show_img=False):
|
def what_is_it(img_path, show_img=False):
|
||||||
image = read_image(img_path, mode=ImageReadMode.RGB)
|
image = Image.open(img_path).convert('RGB')
|
||||||
if show_img:
|
if show_img:
|
||||||
plt.imshow(plt.imread(img_path))
|
plt.imshow(image)
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
image = SETUP_PHOTOS(image).unsqueeze(0)
|
image = SETUP_PHOTOS(image).unsqueeze(0)
|
||||||
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_3/checkpoints/epoch=8-step=810.ckpt')
|
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_20/checkpoints/epoch=3-step=324.ckpt')
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
model.eval()
|
model.eval()
|
||||||
@ -108,18 +73,53 @@ def what_is_it(img_path, show_img=False):
|
|||||||
return ID_TO_CLASS[idx]
|
return ID_TO_CLASS[idx]
|
||||||
|
|
||||||
|
|
||||||
CNN = NeuralNetwork()
|
def check_accuracy(tset):
|
||||||
|
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_23/checkpoints/epoch=3-step=324.ckpt')
|
||||||
|
num_correct = 0
|
||||||
|
num_samples = 0
|
||||||
|
model = model.to(DEVICE)
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
for photo, label in tset:
|
||||||
|
photo = photo.to(DEVICE)
|
||||||
|
label = label.to(DEVICE)
|
||||||
|
|
||||||
|
scores = model(photo)
|
||||||
|
predictions = scores.argmax(dim=1)
|
||||||
|
num_correct += (predictions == label).sum()
|
||||||
|
num_samples += predictions.size(0)
|
||||||
|
|
||||||
|
print(f'Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}%')
|
||||||
|
|
||||||
|
|
||||||
trainer = pl.Trainer(accelerator='gpu', devices=1, auto_scale_batch_size=True, callbacks=[EarlyStopping('val_loss')], max_epochs=NUM_EPOCHS)
|
def check_accuracy_data():
|
||||||
#trainer = pl.Trainer(accelerator='gpu', devices=1, auto_lr_find=True, max_epochs=NUM_EPOCHS)
|
|
||||||
|
|
||||||
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
|
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
|
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
|
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
|
||||||
test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
|
test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
|
||||||
|
|
||||||
|
print("Accuracy of train_set:")
|
||||||
|
check_accuracy(train_loader)
|
||||||
|
print("Accuracy of test_set:")
|
||||||
|
check_accuracy(test_loader)
|
||||||
|
|
||||||
|
#CNN = NeuralNetwork()
|
||||||
|
#common.helpers.createCSV()
|
||||||
|
|
||||||
|
#trainer = pl.Trainer(accelerator='gpu', callbacks=EarlyStopping('val_loss'), devices=1, max_epochs=NUM_EPOCHS)
|
||||||
|
#trainer = pl.Trainer(accelerator='gpu', devices=1, auto_lr_find=True, max_epochs=NUM_EPOCHS)
|
||||||
|
|
||||||
|
#trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
|
#testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
|
#train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
|
||||||
|
#test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
|
||||||
#trainer.fit(CNN, train_loader, test_loader)
|
#trainer.fit(CNN, train_loader, test_loader)
|
||||||
#trainer.tune(CNN, train_loader, test_loader)
|
#trainer.tune(CNN, train_loader, test_loader)
|
||||||
check_accuracy_tiles()
|
|
||||||
print(what_is_it('../../resources/textures/sand.png', True))
|
|
||||||
|
#print(what_is_it('../../resources/textures/grass2.png', True))
|
||||||
|
|
||||||
|
#check_accuracy_data()
|
||||||
|
|
||||||
|
#check_accuracy_tiles()
|
||||||
|
@ -3,6 +3,7 @@ from torch.utils.data import Dataset
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
from torchvision.io import read_image, ImageReadMode
|
from torchvision.io import read_image, ImageReadMode
|
||||||
from common.helpers import createCSV
|
from common.helpers import createCSV
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
class WaterSandTreeGrass(Dataset):
|
class WaterSandTreeGrass(Dataset):
|
||||||
@ -15,7 +16,8 @@ class WaterSandTreeGrass(Dataset):
|
|||||||
return len(self.img_labels)
|
return len(self.img_labels)
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
def __getitem__(self, idx):
|
||||||
image = read_image(self.img_labels.iloc[idx, 0], mode=ImageReadMode.RGB)
|
image = Image.open(self.img_labels.iloc[idx, 0]).convert('RGB')
|
||||||
|
|
||||||
label = torch.tensor(int(self.img_labels.iloc[idx, 1]))
|
label = torch.tensor(int(self.img_labels.iloc[idx, 1]))
|
||||||
|
|
||||||
if self.transform:
|
if self.transform:
|
||||||
|
@ -6,7 +6,7 @@ GAME_TITLE = 'WMICraft'
|
|||||||
WINDOW_HEIGHT = 800
|
WINDOW_HEIGHT = 800
|
||||||
WINDOW_WIDTH = 1360
|
WINDOW_WIDTH = 1360
|
||||||
FPS_COUNT = 60
|
FPS_COUNT = 60
|
||||||
TURN_INTERVAL = 500
|
TURN_INTERVAL = 200
|
||||||
|
|
||||||
GRID_CELL_PADDING = 5
|
GRID_CELL_PADDING = 5
|
||||||
GRID_CELL_SIZE = 36
|
GRID_CELL_SIZE = 36
|
||||||
@ -77,19 +77,17 @@ BAR_HEIGHT_MULTIPLIER = 0.1
|
|||||||
|
|
||||||
|
|
||||||
#NEURAL_NETWORK
|
#NEURAL_NETWORK
|
||||||
LEARNING_RATE = 0.13182567385564073
|
LEARNING_RATE = 0.000630957344480193
|
||||||
BATCH_SIZE = 64
|
BATCH_SIZE = 64
|
||||||
NUM_EPOCHS = 50
|
NUM_EPOCHS = 9
|
||||||
|
|
||||||
DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
||||||
print("Using ", DEVICE)
|
print("Using ", DEVICE)
|
||||||
CLASSES = ['grass', 'sand', 'tree', 'water']
|
CLASSES = ['grass', 'sand', 'tree', 'water']
|
||||||
|
|
||||||
SETUP_PHOTOS = transforms.Compose([
|
SETUP_PHOTOS = transforms.Compose([
|
||||||
transforms.Resize(36),
|
|
||||||
transforms.CenterCrop(36),
|
|
||||||
transforms.ToPILImage(),
|
|
||||||
transforms.ToTensor(),
|
transforms.ToTensor(),
|
||||||
|
transforms.Resize((36, 36)),
|
||||||
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
|
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
|
||||||
])
|
])
|
||||||
|
|
||||||
|
1001
learning/dataset_tree_1000.csv
Normal file
@ -26,7 +26,7 @@ def parse_idx_of_opp_or_monster(s: str) -> int:
|
|||||||
|
|
||||||
class DecisionTree:
|
class DecisionTree:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
data_frame = pd.read_csv('learning/dataset_tree.csv', delimiter=';')
|
data_frame = pd.read_csv('learning/dataset_tree_1000.csv', delimiter=';')
|
||||||
unlabeled_goals = data_frame['goal']
|
unlabeled_goals = data_frame['goal']
|
||||||
self.goals_label_encoder = LabelEncoder()
|
self.goals_label_encoder = LabelEncoder()
|
||||||
self.goals = self.goals_label_encoder.fit_transform(unlabeled_goals)
|
self.goals = self.goals_label_encoder.fit_transform(unlabeled_goals)
|
||||||
|
@ -45,6 +45,7 @@ class Game:
|
|||||||
# create level
|
# create level
|
||||||
level.create_map()
|
level.create_map()
|
||||||
stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red)
|
stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red)
|
||||||
|
level.setup_stats(stats)
|
||||||
|
|
||||||
print_numbers_flag = False
|
print_numbers_flag = False
|
||||||
running = True
|
running = True
|
||||||
|
@ -10,7 +10,7 @@ class KnightsQueue:
|
|||||||
def dequeue_knight(self):
|
def dequeue_knight(self):
|
||||||
if self.both_teams_alive():
|
if self.both_teams_alive():
|
||||||
knight = self.queues[self.team_idx_turn].popleft()
|
knight = self.queues[self.team_idx_turn].popleft()
|
||||||
if knight.max_hp <= 0:
|
if knight.health_bar.current_hp <= 0:
|
||||||
return self.dequeue_knight()
|
return self.dequeue_knight()
|
||||||
else:
|
else:
|
||||||
self.queues[self.team_idx_turn].append(knight)
|
self.queues[self.team_idx_turn].append(knight)
|
||||||
|
136
logic/level.py
@ -31,6 +31,15 @@ class Level:
|
|||||||
|
|
||||||
self.knights_queue = None
|
self.knights_queue = None
|
||||||
|
|
||||||
|
self.stats = None
|
||||||
|
|
||||||
|
def setup_stats(self, stats):
|
||||||
|
self.stats = stats
|
||||||
|
|
||||||
|
def add_points(self, team, points_to_add):
|
||||||
|
if self.stats is not None:
|
||||||
|
self.stats.add_points(team, points_to_add)
|
||||||
|
|
||||||
def create_map(self):
|
def create_map(self):
|
||||||
self.map = import_random_map()
|
self.map = import_random_map()
|
||||||
self.setup_base_tiles()
|
self.setup_base_tiles()
|
||||||
@ -92,11 +101,119 @@ class Level:
|
|||||||
self.map[row_index][col_index] = castle
|
self.map[row_index][col_index] = castle
|
||||||
self.list_castles.append(castle)
|
self.list_castles.append(castle)
|
||||||
|
|
||||||
|
#def attack_knight(self, knights_list, positions, current_knight):
|
||||||
|
# op_pos_1 = current_knight.position[0] - 1, current_knight.position[1]
|
||||||
|
# positions.append(op_pos_1)
|
||||||
|
# op_pos_2 = current_knight.position[0], current_knight.position[1] - 1
|
||||||
|
# positions.append(op_pos_2)
|
||||||
|
# op_pos_3 = current_knight.position[0] + 1, current_knight.position[1]
|
||||||
|
# positions.append(op_pos_3)
|
||||||
|
# op_pos_4 = current_knight.position[0], current_knight.position[1] + 1
|
||||||
|
# positions.append(op_pos_4)
|
||||||
|
# for some_knight in knights_list:
|
||||||
|
# for some_position in positions:
|
||||||
|
# if (some_knight.position == some_position and some_knight.team != current_knight.team):
|
||||||
|
# some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
# if some_knight.health_bar.current_hp == 0:
|
||||||
|
# some_knight.kill()
|
||||||
|
# positions.clear()
|
||||||
|
|
||||||
|
def attack_knight_left(self, knights_list, current_knight):
|
||||||
|
position_left = current_knight.position[0] - 1, current_knight.position[1]
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_left and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp <= 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_left:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_left:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
|
|
||||||
|
def attack_knight_right(self, knights_list, current_knight):
|
||||||
|
position_right = current_knight.position[0] + 1, current_knight.position[1]
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_right and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp == 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_right:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_right:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
|
def attack_knight_up(self, knights_list, current_knight):
|
||||||
|
position_up = current_knight.position[0], current_knight.position[1] - 1
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_up and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp == 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_up:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_up:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
|
def attack_knight_down(self, knights_list, current_knight):
|
||||||
|
position_down = current_knight.position[0], current_knight.position[1] + 1
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_down and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp == 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_down:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_down:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
def handle_turn(self):
|
def handle_turn(self):
|
||||||
current_knight = self.knights_queue.dequeue_knight()
|
current_knight = self.knights_queue.dequeue_knight()
|
||||||
|
knights_list = self.list_knights_red + self.list_knights_blue
|
||||||
print("next turn " + current_knight.team)
|
print("next turn " + current_knight.team)
|
||||||
knight_pos_x = current_knight.position[0]
|
knight_pos_x = current_knight.position[0]
|
||||||
knight_pos_y = current_knight.position[1]
|
knight_pos_y = current_knight.position[1]
|
||||||
|
positions = []
|
||||||
|
|
||||||
goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight,
|
goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight,
|
||||||
monsters=self.list_monsters,
|
monsters=self.list_monsters,
|
||||||
@ -104,6 +221,9 @@ class Level:
|
|||||||
if current_knight.team_alias() == 'k_r' else self.list_knights_red,
|
if current_knight.team_alias() == 'k_r' else self.list_knights_red,
|
||||||
castle=self.list_castles[0])
|
castle=self.list_castles[0])
|
||||||
|
|
||||||
|
if (len(self.list_knights_blue) == 0 or len(self.list_knights_red) == 0):
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
if len(goal_list) == 0:
|
if len(goal_list) == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -116,6 +236,19 @@ class Level:
|
|||||||
return
|
return
|
||||||
|
|
||||||
next_action = action_list.pop(0)
|
next_action = action_list.pop(0)
|
||||||
|
|
||||||
|
#if current_knight.health_bar.current_hp != 0:
|
||||||
|
#self.attack_knight(knights_list, positions, current_knight)
|
||||||
|
|
||||||
|
if current_knight.direction.name == UP:
|
||||||
|
self.attack_knight_up(knights_list, current_knight)
|
||||||
|
elif current_knight.direction.name == DOWN:
|
||||||
|
self.attack_knight_down(knights_list, current_knight)
|
||||||
|
elif current_knight.direction.name == RIGHT:
|
||||||
|
self.attack_knight_right(knights_list, current_knight)
|
||||||
|
elif current_knight.direction.name == LEFT:
|
||||||
|
self.attack_knight_left(knights_list, current_knight)
|
||||||
|
|
||||||
if next_action == TURN_LEFT:
|
if next_action == TURN_LEFT:
|
||||||
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
|
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
|
||||||
current_knight.rotate_left()
|
current_knight.rotate_left()
|
||||||
@ -148,3 +281,6 @@ class Level:
|
|||||||
# update and draw the game
|
# update and draw the game
|
||||||
self.sprites.draw(self.screen)
|
self.sprites.draw(self.screen)
|
||||||
self.sprites.update()
|
self.sprites.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,8 +16,7 @@ class Castle(pygame.sprite.Sprite):
|
|||||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||||
self.rect = self.image.get_rect(center=position_in_px)
|
self.rect = self.image.get_rect(center=position_in_px)
|
||||||
self.max_hp = 80
|
self.max_hp = 80
|
||||||
self.current_hp = random.randint(1, self.max_hp)
|
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
||||||
self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.health_bar.update()
|
self.health_bar.update()
|
||||||
|
@ -7,8 +7,11 @@ from common.helpers import parse_cord
|
|||||||
from logic.health_bar import HealthBar
|
from logic.health_bar import HealthBar
|
||||||
|
|
||||||
|
|
||||||
def load_knight_textures():
|
def load_knight_textures(team):
|
||||||
random_index = random.randint(1, 4)
|
if team == "blue":
|
||||||
|
random_index = 3
|
||||||
|
else:
|
||||||
|
random_index = 4
|
||||||
states = [
|
states = [
|
||||||
pygame.image.load(f'resources/textures/knight_{random_index}_up.png').convert_alpha(), # up = 0
|
pygame.image.load(f'resources/textures/knight_{random_index}_up.png').convert_alpha(), # up = 0
|
||||||
pygame.image.load(f'resources/textures/knight_{random_index}_right.png').convert_alpha(), # right = 1
|
pygame.image.load(f'resources/textures/knight_{random_index}_right.png').convert_alpha(), # right = 1
|
||||||
@ -24,7 +27,7 @@ class Knight(pygame.sprite.Sprite):
|
|||||||
super().__init__(group)
|
super().__init__(group)
|
||||||
|
|
||||||
self.direction = Direction.DOWN
|
self.direction = Direction.DOWN
|
||||||
self.states = load_knight_textures()
|
self.states = load_knight_textures(team)
|
||||||
|
|
||||||
self.image = self.states[self.direction.value]
|
self.image = self.states[self.direction.value]
|
||||||
self.position = position
|
self.position = position
|
||||||
@ -33,11 +36,11 @@ class Knight(pygame.sprite.Sprite):
|
|||||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||||
|
|
||||||
self.team = team
|
self.team = team
|
||||||
self.max_hp = random.randint(7, 12)
|
self.max_hp = random.randint(9, 13)
|
||||||
self.attack = random.randint(4, 7)
|
self.attack = random.randint(2, 4)
|
||||||
self.defense = random.randint(1, 4)
|
self.defense = random.randint(1, 4)
|
||||||
self.points = 1
|
self.points = 1
|
||||||
self.health_bar = HealthBar(screen, self.rect, current_hp=random.randint(1, self.max_hp), max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
||||||
|
|
||||||
def rotate_left(self):
|
def rotate_left(self):
|
||||||
self.direction = self.direction.left()
|
self.direction = self.direction.left()
|
||||||
|
@ -22,14 +22,13 @@ class Monster(pygame.sprite.Sprite):
|
|||||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||||
self.position = position
|
self.position = position
|
||||||
self.max_hp = random.randrange(15, 25)
|
self.max_hp = random.randrange(15, 20)
|
||||||
self.current_hp = random.randint(1, self.max_hp)
|
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp,
|
||||||
self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp,
|
|
||||||
calculate_xy=True, calculate_size=True)
|
calculate_xy=True, calculate_size=True)
|
||||||
self.attack = random.randrange(2, 10)
|
self.attack = random.randrange(4, 6)
|
||||||
if self.image == monster_images[0]:
|
if self.image == monster_images[0]:
|
||||||
self.max_hp = 20
|
self.max_hp = 20
|
||||||
self.attack = 9
|
self.attack = 6
|
||||||
self.points = 10
|
self.points = 10
|
||||||
elif self.image == monster_images[1]:
|
elif self.image == monster_images[1]:
|
||||||
self.max_hp = 15
|
self.max_hp = 15
|
||||||
|
16
ui/stats.py
@ -23,6 +23,8 @@ class Stats:
|
|||||||
pygame.Rect(self.x + 210, self.y + 210, 100, 15),
|
pygame.Rect(self.x + 210, self.y + 210, 100, 15),
|
||||||
current_hp=sum([knight.get_current_hp() for knight in self.list_knights_red]),
|
current_hp=sum([knight.get_current_hp() for knight in self.list_knights_red]),
|
||||||
max_hp=sum([knight.get_max_hp() for knight in self.list_knights_red]))
|
max_hp=sum([knight.get_max_hp() for knight in self.list_knights_red]))
|
||||||
|
self.blue_team_points = 0
|
||||||
|
self.red_team_points = 0
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
|
||||||
@ -50,12 +52,16 @@ class Stats:
|
|||||||
|
|
||||||
# texts
|
# texts
|
||||||
draw_text('Rycerze: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue
|
draw_text('Rycerze: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue
|
||||||
draw_text('Fortece: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) # red
|
|
||||||
|
|
||||||
draw_text('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18)
|
draw_text('Rycerze: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 215, self.y + 240, 18)
|
||||||
draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 215, self.y + 270, 18)
|
|
||||||
|
|
||||||
# points
|
# points
|
||||||
pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))
|
pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))
|
||||||
draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True)
|
draw_text('PUNKTY: ' + str(self.blue_team_points), FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True)
|
||||||
draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True)
|
draw_text('PUNKTY: ' + str(self.red_team_points), FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True)
|
||||||
|
|
||||||
|
def add_points(self, team, points):
|
||||||
|
if team == "blue":
|
||||||
|
self.blue_team_points += points
|
||||||
|
else:
|
||||||
|
self.red_team_points += points
|
||||||
|