diversified agents speed on different terrain types; temporarily disabled GameOver screen
This commit is contained in:
parent
9918bf64d8
commit
7fe9e0958c
32
agent.py
32
agent.py
@ -1,6 +1,6 @@
|
|||||||
import project_constants as const
|
import project_constants as const
|
||||||
import json
|
import json
|
||||||
from pygame import transform, Surface, SRCALPHA
|
from pygame import transform
|
||||||
|
|
||||||
|
|
||||||
# Class of our agent, initialization of it
|
# Class of our agent, initialization of it
|
||||||
@ -15,7 +15,8 @@ class Agent:
|
|||||||
with open(json_path) as json_data:
|
with open(json_path) as json_data:
|
||||||
data = json.load(json_data)
|
data = json.load(json_data)
|
||||||
self.row, self.column = data["agents_initial_state"]["position"].split(",")
|
self.row, self.column = data["agents_initial_state"]["position"].split(",")
|
||||||
self.position = [int(self.row), int(self.column)]
|
self.row, self.column = int(self.row), int(self.column)
|
||||||
|
self.position = [self.row, self.column]
|
||||||
self.on_screen_coordinates = const.get_tile_coordinates(tuple(self.position))
|
self.on_screen_coordinates = const.get_tile_coordinates(tuple(self.position))
|
||||||
# self.direction = const.Direction()
|
# self.direction = const.Direction()
|
||||||
self.direction = const.Direction(data["agents_initial_state"]["direction"])
|
self.direction = const.Direction(data["agents_initial_state"]["direction"])
|
||||||
@ -24,8 +25,8 @@ class Agent:
|
|||||||
self.rotating_left = False
|
self.rotating_left = False
|
||||||
self.rotating_right = False
|
self.rotating_right = False
|
||||||
|
|
||||||
def update_and_draw(self, window, delta_time):
|
def update_and_draw(self, window, delta_time, minefield):
|
||||||
self.update(delta_time)
|
self.update(delta_time, minefield)
|
||||||
self.draw(window)
|
self.draw(window)
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
@ -37,20 +38,33 @@ class Agent:
|
|||||||
|
|
||||||
window.blit(transform.rotate(const.ASSET_SAPPER, self.rotation_angle), rotating_rect)
|
window.blit(transform.rotate(const.ASSET_SAPPER, self.rotation_angle), rotating_rect)
|
||||||
|
|
||||||
def update(self, delta_time):
|
def update(self, delta_time, minefield):
|
||||||
self.new_action = self.going_forward + self.rotating_left * 2 + self.rotating_right * 4
|
self.new_action = self.going_forward + self.rotating_left * 2 + self.rotating_right * 4
|
||||||
|
|
||||||
|
# counting next agents terrain tile
|
||||||
|
# heading either up or down
|
||||||
|
if const.Direction(self.direction).value % 2 == 0 and self.going_forward:
|
||||||
|
next_row = min(9, self.row + const.Direction(self.direction).value - 1)
|
||||||
|
next_column = self.column
|
||||||
|
|
||||||
|
# heading either left or right
|
||||||
|
else:
|
||||||
|
next_row = self.row
|
||||||
|
next_column = min(9, self.column - const.Direction(self.direction).value + 2)
|
||||||
|
|
||||||
|
value_modifier = minefield.matrix[next_row][next_column].cost.value
|
||||||
|
|
||||||
if self.going_forward:
|
if self.going_forward:
|
||||||
direction = const.Direction(self.direction).value
|
direction = const.Direction(self.direction).value
|
||||||
x, y = self.on_screen_coordinates
|
x, y = self.on_screen_coordinates
|
||||||
|
|
||||||
# heading either up or down
|
# heading either up or down
|
||||||
if direction % 2 == 0:
|
if direction % 2 == 0:
|
||||||
self.on_screen_coordinates = (x, y + (direction - 1) * const.V_TILE_SIZE * delta_time)
|
self.on_screen_coordinates = (x, y + (direction - 1) * const.V_TILE_SIZE * delta_time / value_modifier)
|
||||||
|
|
||||||
# heading either left or right
|
# heading either left or right
|
||||||
else:
|
else:
|
||||||
self.on_screen_coordinates = (x - (direction - 2) * const.V_TILE_SIZE * delta_time, y)
|
self.on_screen_coordinates = (x - (direction - 2) * const.V_TILE_SIZE * delta_time / value_modifier, y)
|
||||||
|
|
||||||
elif self.rotating_right:
|
elif self.rotating_right:
|
||||||
self.rotation_angle -= delta_time * 90 % 360
|
self.rotation_angle -= delta_time * 90 % 360
|
||||||
@ -91,15 +105,19 @@ class Agent:
|
|||||||
if self.direction == const.Direction.RIGHT:
|
if self.direction == const.Direction.RIGHT:
|
||||||
temp = self.go_right()
|
temp = self.go_right()
|
||||||
self.position[1] = temp[1]
|
self.position[1] = temp[1]
|
||||||
|
self.column += 1
|
||||||
elif self.direction == const.Direction.LEFT:
|
elif self.direction == const.Direction.LEFT:
|
||||||
temp = self.go_left()
|
temp = self.go_left()
|
||||||
self.position[1] = temp[1]
|
self.position[1] = temp[1]
|
||||||
|
self.column -= 1
|
||||||
elif self.direction == const.Direction.UP:
|
elif self.direction == const.Direction.UP:
|
||||||
temp = self.go_up()
|
temp = self.go_up()
|
||||||
self.position[0] = temp[0]
|
self.position[0] = temp[0]
|
||||||
|
self.row -= 1
|
||||||
elif self.direction == const.Direction.DOWN:
|
elif self.direction == const.Direction.DOWN:
|
||||||
temp = self.go_down()
|
temp = self.go_down()
|
||||||
self.position[0] = temp[0]
|
self.position[0] = temp[0]
|
||||||
|
self.row += 1
|
||||||
|
|
||||||
def go_right(self):
|
def go_right(self):
|
||||||
|
|
||||||
|
65
main.py
65
main.py
@ -1,6 +1,5 @@
|
|||||||
# libraries
|
# libraries
|
||||||
import pygame
|
import pygame
|
||||||
import time
|
|
||||||
from pyglet.gl import * # for blocky textures
|
from pyglet.gl import * # for blocky textures
|
||||||
import random
|
import random
|
||||||
# other files of this project
|
# other files of this project
|
||||||
@ -13,6 +12,23 @@ import searching_algorithms.a_star as a_star
|
|||||||
from display_assets import blit_graphics
|
from display_assets import blit_graphics
|
||||||
from project_constants import HIGHLIGHT, INPUT_ROW, INPUT_COLUMN, RANDOM_BUTTON, OK_BUTTON
|
from project_constants import HIGHLIGHT, INPUT_ROW, INPUT_COLUMN, RANDOM_BUTTON, OK_BUTTON
|
||||||
from ui.ui_components_list import UiComponentsList
|
from ui.ui_components_list import UiComponentsList
|
||||||
|
|
||||||
|
|
||||||
|
def _get_next_agent_position(action, agent):
|
||||||
|
# counting next agents terrain tile
|
||||||
|
# heading either up or down
|
||||||
|
if const.Direction(agent.direction).value % 2 == 0 and action == const.Action.GO:
|
||||||
|
next_row = min(9, agent.row + const.Direction(agent.direction).value - 1)
|
||||||
|
next_column = agent.column
|
||||||
|
|
||||||
|
# heading either left or right
|
||||||
|
else:
|
||||||
|
next_row = agent.row
|
||||||
|
next_column = min(9, agent.column - const.Direction(agent.direction).value + 2)
|
||||||
|
|
||||||
|
return next_row, next_column
|
||||||
|
from project_constants import HIGHLIGHT, INPUT_ROW, INPUT_COLUMN, RANDOM_BUTTON, OK_BUTTON
|
||||||
|
from ui.ui_components_list import UiComponentsList
|
||||||
from ui.text_box import TextBox
|
from ui.text_box import TextBox
|
||||||
from ui.button import Button
|
from ui.button import Button
|
||||||
|
|
||||||
@ -28,6 +44,10 @@ def main():
|
|||||||
# FPS clock
|
# FPS clock
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# in-game turn number
|
||||||
|
turn = 0
|
||||||
|
last_turn = 0
|
||||||
|
|
||||||
# create an instance of Minefield, pass necessary data
|
# create an instance of Minefield, pass necessary data
|
||||||
minefield = mf.Minefield(const.MAP_RANDOM_10x10)
|
minefield = mf.Minefield(const.MAP_RANDOM_10x10)
|
||||||
|
|
||||||
@ -64,13 +84,13 @@ def main():
|
|||||||
# graphics (from display_assets)
|
# graphics (from display_assets)
|
||||||
blit_graphics(minefield)
|
blit_graphics(minefield)
|
||||||
|
|
||||||
agent.update_and_draw(const.SCREEN, 0)
|
agent.update_and_draw(const.SCREEN, 0, minefield)
|
||||||
|
|
||||||
# drawing gui
|
# drawing gui
|
||||||
ui_components_list.run_all(const.SCREEN, pygame.mouse.get_pos(), events)
|
ui_components_list.run_all(const.SCREEN, pygame.mouse.get_pos(), events)
|
||||||
|
|
||||||
# highlighting chosen tile destination (if exists)
|
# highlighting chosen tile destination (if exists)
|
||||||
if not (INPUT_ROW.empty() or INPUT_COLUMN.empty()):
|
if not(INPUT_ROW.empty() or INPUT_COLUMN.empty()):
|
||||||
row = min(9, int(INPUT_ROW.get_input()))
|
row = min(9, int(INPUT_ROW.get_input()))
|
||||||
column = min(9, int(INPUT_COLUMN.get_input()))
|
column = min(9, int(INPUT_COLUMN.get_input()))
|
||||||
const.SCREEN.blit(HIGHLIGHT, const.get_tile_coordinates((row, column)))
|
const.SCREEN.blit(HIGHLIGHT, const.get_tile_coordinates((row, column)))
|
||||||
@ -80,7 +100,7 @@ def main():
|
|||||||
|
|
||||||
ui_components_list.switch_selected_objects_with_arrow_keys(events, pygame.mouse.get_pos())
|
ui_components_list.switch_selected_objects_with_arrow_keys(events, pygame.mouse.get_pos())
|
||||||
|
|
||||||
OK_BUTTON.set_flags(is_active=(not (INPUT_ROW.empty() or INPUT_COLUMN.empty())))
|
OK_BUTTON.set_flags(is_active=(not(INPUT_ROW.empty() or INPUT_COLUMN.empty())))
|
||||||
|
|
||||||
if OK_BUTTON.is_clicked(pygame.mouse.get_pos(), events) or OK_BUTTON.enter_pressed(events):
|
if OK_BUTTON.is_clicked(pygame.mouse.get_pos(), events) or OK_BUTTON.enter_pressed(events):
|
||||||
in_menu = False
|
in_menu = False
|
||||||
@ -106,8 +126,15 @@ def main():
|
|||||||
direction=minefield.agent.direction),
|
direction=minefield.agent.direction),
|
||||||
minefield=minefield, tox=row, toy=column)
|
minefield=minefield, tox=row, toy=column)
|
||||||
|
|
||||||
action = None
|
# initializing variables
|
||||||
in_game_timer = 0
|
in_game_timer = 0
|
||||||
|
action = None
|
||||||
|
action_cost = 1
|
||||||
|
next_row, next_column = agent.row, agent.column
|
||||||
|
|
||||||
|
# =================== #
|
||||||
|
# ==== GAME LOOP ==== #
|
||||||
|
# =================== #
|
||||||
|
|
||||||
# =================== #
|
# =================== #
|
||||||
# ==== GAME LOOP ==== #
|
# ==== GAME LOOP ==== #
|
||||||
@ -115,6 +142,10 @@ def main():
|
|||||||
|
|
||||||
while running and not in_menu and not is_game_over:
|
while running and not in_menu and not is_game_over:
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
running = False
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
@ -128,7 +159,7 @@ def main():
|
|||||||
blit_graphics(minefield)
|
blit_graphics(minefield)
|
||||||
const.SCREEN.blit(HIGHLIGHT, const.get_tile_coordinates((row, column)))
|
const.SCREEN.blit(HIGHLIGHT, const.get_tile_coordinates((row, column)))
|
||||||
|
|
||||||
agent.update_and_draw(const.SCREEN, action_delta_time)
|
agent.update_and_draw(const.SCREEN, action_delta_time, minefield)
|
||||||
|
|
||||||
# drawing ui components so they don't "disappear"
|
# drawing ui components so they don't "disappear"
|
||||||
ui_components_list.draw_all(const.SCREEN, pygame.mouse.get_pos())
|
ui_components_list.draw_all(const.SCREEN, pygame.mouse.get_pos())
|
||||||
@ -136,10 +167,16 @@ def main():
|
|||||||
# updating graphics
|
# updating graphics
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
# make the next move from sequence of actions
|
# update turn
|
||||||
if in_game_timer >= const.ACTION_INTERVAL and any(action_sequence):
|
if in_game_timer >= const.ACTION_INTERVAL:
|
||||||
in_game_timer -= const.ACTION_INTERVAL
|
in_game_timer %= const.ACTION_INTERVAL
|
||||||
|
turn += 1
|
||||||
minefield.next_turn()
|
minefield.next_turn()
|
||||||
|
action_cost = 1 if action != const.Action.GO else minefield.matrix[next_row][next_column].cost.value
|
||||||
|
|
||||||
|
# make the next move from sequence of actions
|
||||||
|
if turn - last_turn >= action_cost and any(action_sequence):
|
||||||
|
last_turn = turn
|
||||||
|
|
||||||
agent.take_action(action)
|
agent.take_action(action)
|
||||||
|
|
||||||
@ -147,13 +184,16 @@ def main():
|
|||||||
|
|
||||||
agent.animate(action)
|
agent.animate(action)
|
||||||
|
|
||||||
elif in_game_timer >= const.ACTION_INTERVAL:
|
next_row, next_column = _get_next_agent_position(action, agent)
|
||||||
|
|
||||||
|
elif turn - last_turn >= action_cost:
|
||||||
agent.take_action(action)
|
agent.take_action(action)
|
||||||
|
|
||||||
agent.update_and_draw(const.SCREEN, delta_time)
|
agent.update_and_draw(const.SCREEN, delta_time, minefield)
|
||||||
agent.reset_actions()
|
agent.reset_actions()
|
||||||
|
|
||||||
in_menu = True
|
in_menu = True
|
||||||
|
|
||||||
if not any([x.is_selected for x in ui_components_list.ui_components]):
|
if not any([x.is_selected for x in ui_components_list.ui_components]):
|
||||||
INPUT_ROW.set_is_selected(True)
|
INPUT_ROW.set_is_selected(True)
|
||||||
|
|
||||||
@ -168,6 +208,9 @@ def main():
|
|||||||
if y.mine.timer == 0:
|
if y.mine.timer == 0:
|
||||||
is_game_over = True
|
is_game_over = True
|
||||||
|
|
||||||
|
# temporarily disabling game over screen
|
||||||
|
is_game_over = False
|
||||||
|
|
||||||
while running and is_game_over:
|
while running and is_game_over:
|
||||||
clock.tick(const.V_FPS)
|
clock.tick(const.V_FPS)
|
||||||
const.SCREEN.fill((255, 255, 255))
|
const.SCREEN.fill((255, 255, 255))
|
||||||
|
@ -43,7 +43,7 @@ class Minefield:
|
|||||||
mine = self.matrix[row][column].mine
|
mine = self.matrix[row][column].mine
|
||||||
|
|
||||||
if mine is not None and isinstance(mine, TimeMine):
|
if mine is not None and isinstance(mine, TimeMine):
|
||||||
mine.timer = max(0, mine.starting_time - self.turn)
|
mine.timer = max(0, mine.starting_time - int(self.turn / 4))
|
||||||
|
|
||||||
# ================ #
|
# ================ #
|
||||||
# === MOVEMENT === #
|
# === MOVEMENT === #
|
||||||
|
@ -22,7 +22,7 @@ V_NAME_OF_WINDOW = "MineFusion TM"
|
|||||||
DIR_ASSETS = os.path.join("resources", "assets")
|
DIR_ASSETS = os.path.join("resources", "assets")
|
||||||
V_FPS = 60
|
V_FPS = 60
|
||||||
|
|
||||||
ACTION_INTERVAL = 0.6 # interval between two actions in seconds
|
ACTION_INTERVAL = 0.3 # interval between two actions in seconds
|
||||||
|
|
||||||
V_TILE_SIZE = 60
|
V_TILE_SIZE = 60
|
||||||
V_GRID_VER_TILES = 10 # vertical (number of rows)
|
V_GRID_VER_TILES = 10 # vertical (number of rows)
|
||||||
|
Loading…
Reference in New Issue
Block a user