2021-03-11 00:47:14 +01:00
|
|
|
# libraries
|
2021-03-10 14:01:17 +01:00
|
|
|
import pygame
|
2021-04-17 19:56:34 +02:00
|
|
|
import time
|
2021-03-14 18:27:12 +01:00
|
|
|
from pyglet.gl import * # for blocky textures
|
2021-03-10 14:01:17 +01:00
|
|
|
|
2021-03-11 00:47:14 +01:00
|
|
|
# other files of this project
|
2021-03-14 19:18:23 +01:00
|
|
|
import project_constants as const
|
2021-03-12 11:49:19 +01:00
|
|
|
import minefield as mf
|
2021-04-17 19:56:34 +02:00
|
|
|
import searching_algorithms.bfs as bfs
|
2021-04-17 15:29:36 +02:00
|
|
|
from display_assets import display_sapper
|
2021-03-11 00:47:14 +01:00
|
|
|
|
2021-03-10 14:01:17 +01:00
|
|
|
|
2021-03-12 09:55:59 +01:00
|
|
|
def main():
|
2021-03-10 14:01:17 +01:00
|
|
|
pygame.init()
|
2021-03-14 19:18:23 +01:00
|
|
|
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
|
2021-03-11 00:47:14 +01:00
|
|
|
|
2021-03-16 06:41:40 +01:00
|
|
|
# FPS clock
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
2021-03-11 18:51:43 +01:00
|
|
|
# for blocky textures
|
|
|
|
glEnable(GL_TEXTURE_2D)
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
|
|
|
|
2021-03-12 11:49:19 +01:00
|
|
|
# create an instance of Minefield, pass necessary data
|
2021-03-14 19:18:23 +01:00
|
|
|
minefield = mf.Minefield(const.MAP_RANDOM_10x10)
|
2021-03-11 00:47:14 +01:00
|
|
|
|
2021-04-17 19:56:34 +02:00
|
|
|
# get sequence of actions found by BFS algorythm
|
|
|
|
action_sequence = bfs.graphsearch(initial_state=bfs.State(row=minefield.agent.position[0],
|
|
|
|
column=minefield.agent.position[1],
|
|
|
|
direction=const.Direction.UP),
|
|
|
|
minefield=minefield)
|
|
|
|
|
2021-03-10 14:01:17 +01:00
|
|
|
running = True
|
2021-03-11 00:47:14 +01:00
|
|
|
while running:
|
2021-03-16 06:41:40 +01:00
|
|
|
# FPS control
|
|
|
|
clock.tick(const.V_FPS)
|
2021-03-11 00:47:14 +01:00
|
|
|
|
2021-03-14 18:27:12 +01:00
|
|
|
# ================ #
|
|
|
|
# === GRAPHICS === #
|
|
|
|
# ================ #
|
2021-03-11 00:47:14 +01:00
|
|
|
|
|
|
|
# background grid (fills frame with white, blits grid)
|
2021-03-14 19:18:23 +01:00
|
|
|
const.SCREEN.fill((255, 255, 255))
|
|
|
|
const.SCREEN.blit(
|
|
|
|
const.ASSET_BACKGROUND,
|
2021-03-11 00:47:14 +01:00
|
|
|
(
|
2021-03-14 19:18:23 +01:00
|
|
|
const.V_SCREEN_PADDING,
|
|
|
|
const.V_SCREEN_PADDING
|
2021-03-11 00:47:14 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# draw tiles and mines
|
2021-03-14 19:18:23 +01:00
|
|
|
minefield.draw(const.SCREEN)
|
2021-04-17 15:29:36 +02:00
|
|
|
|
|
|
|
# sapper
|
|
|
|
display_sapper(
|
|
|
|
minefield.agent.position[0],
|
|
|
|
minefield.agent.position[1],
|
2021-04-17 19:56:34 +02:00
|
|
|
minefield.agent.direction
|
2021-04-17 15:29:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# update graphics after blitting
|
2021-03-11 00:47:14 +01:00
|
|
|
pygame.display.update()
|
|
|
|
|
2021-04-17 19:56:34 +02:00
|
|
|
# make the next move from sequence of actions
|
|
|
|
if len(action_sequence):
|
|
|
|
action = action_sequence.pop(0)
|
|
|
|
if action == const.Action.ROTATE_LEFT:
|
|
|
|
minefield.agent.rotate_left()
|
|
|
|
elif action == const.Action.ROTATE_RIGHT:
|
|
|
|
minefield.agent.rotate_right()
|
|
|
|
elif action == const.Action.GO:
|
|
|
|
minefield.agent.go()
|
2021-03-14 18:27:12 +01:00
|
|
|
|
2021-04-17 19:56:34 +02:00
|
|
|
time.sleep(const.ACTION_INTERVAL)
|
2021-03-26 16:36:21 +01:00
|
|
|
|
2021-04-17 19:56:34 +02:00
|
|
|
else:
|
|
|
|
for event in pygame.event.get():
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-04-17 19:56:34 +02:00
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
running = False
|
2021-04-11 19:04:06 +02:00
|
|
|
|
2021-03-14 18:27:12 +01:00
|
|
|
|
2021-03-12 09:55:59 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|