play.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import pygame
  2. import argparse
  3. import time
  4. from os.path import exists, join, splitext
  5. from pygame.locals import QUIT
  6. from simulation.interface import Interface
  7. from simulation.board import Board
  8. from simulation.player import Player
  9. from simulation.logic.main import Blob_Manager
  10. def main():
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('--height', type=int, default=40,
  13. help='Board height resolution (default = 40)')
  14. parser.add_argument('--width', type=int, default=100,
  15. help='Board width resolution (default = 100)')
  16. parser.add_argument('-s', '--scale', type=int, default=10,
  17. help='Scaling from board resolution to window resolution (default = x10)')
  18. parser.add_argument('--init_from', type=str,
  19. help='Initialize game from a save. Pass the board filename')
  20. parser.add_argument('--save_dir', type=str, default="save/",
  21. help='Directory where saves are stored.')
  22. parser.add_argument('--computing_ratio', type=int, default=1,
  23. help='how many times computing loop is done before drawing GUI')
  24. args = parser.parse_args()
  25. default_dir = "simulation/default"
  26. player_file = join(default_dir, "player.json")
  27. blob_file = join(default_dir, "blob.json")
  28. if args.init_from is not None:
  29. board_file = join(args.save_dir, args.init_from)
  30. assert exists(board_file)
  31. root_name = splitext(board_file)[0]
  32. board = Board(args.width, args.height)
  33. board.load(args.save_dir + args.init_from)
  34. if exists(root_name + ".player.json"):
  35. player_file = root_name + ".player.json"
  36. if exists(root_name + ".blob.json"):
  37. blob_file = root_name + ".blob.json"
  38. else:
  39. board = Board(args.width, args.height)
  40. blob = Blob_Manager(board, blob_file)
  41. player = Player(board, blob, player_file)
  42. gui = Interface(board, player, blob, args.scale, args.save_dir)
  43. init_counter = 100
  44. timer = time.time()
  45. counter = init_counter
  46. init_computing_ratio = args.computing_ratio
  47. ended = False
  48. while not ended:
  49. computing_ratio = init_computing_ratio
  50. while computing_ratio > 0:
  51. if gui.play or gui.do_step:
  52. blob.move()
  53. board.next_turn()
  54. gui.do_step = False
  55. for event in pygame.event.get():
  56. if event.type == QUIT:
  57. ended = True
  58. else:
  59. gui.event_listener(event)
  60. computing_ratio -= 1
  61. gui.draw()
  62. pygame.time.wait(10)
  63. counter -= 1
  64. if counter == 0:
  65. timing = time.time() - timer
  66. print("Loop mean time : {:.3f}s per iteration".format(timing / init_counter))
  67. counter = init_counter
  68. timer = time.time()
  69. if __name__ == "__main__":
  70. main()