pytron/play.py

57 lines
1.9 KiB
Python
Raw Normal View History

2019-03-22 14:46:58 +01:00
#!/usr/bin/env python
2018-10-23 16:27:27 +02:00
2019-03-13 22:26:39 +01:00
# Import pygame without printing anything on the terminal
2019-03-22 14:46:58 +01:00
import pygame
2018-10-23 16:27:27 +02:00
2019-03-13 18:21:04 +01:00
from tron.map import Map
from tron.game import Game, PositionPlayer
from tron.window import Window
2019-03-13 22:26:39 +01:00
from tron.player import Direction, KeyboardPlayer, Mode
# This script shows how to create a game with human players and play it interactively.
# It shows how to create the game, to setup interactive controls for users, and
# to run the game while rendering it on a window with a reasonnable framerate.
2018-10-23 16:27:27 +02:00
def main():
2019-03-13 22:26:39 +01:00
# Initialize the game engine
2018-10-23 16:27:27 +02:00
pygame.init()
2019-03-13 22:26:39 +01:00
# Prepare the size for the game.
# Those values may be good if you want to play, they might not be so good
# to train your AI. Decreasing them will make the learning faster.
2019-03-27 15:19:54 +01:00
width = 10
height = 10
2018-10-23 16:27:27 +02:00
2019-03-13 22:26:39 +01:00
# Create a game from its size and its players
2018-10-23 16:27:27 +02:00
game = Game(width, height, [
2019-03-13 22:26:39 +01:00
# We create two PositionPlayer for each player of the game.
# The first one has the id 1, and will use keyboard interaction, with a
# default direction that will be to the right, and that will use the Z,
# Q, S and D keys.
# The last array defines the initial position of the player.
2019-03-26 18:06:37 +01:00
PositionPlayer(1, KeyboardPlayer(Direction.RIGHT, Mode.ZQSD), [0, 0]),
2019-03-13 22:26:39 +01:00
# We create a second player that will use the arrow keys.
PositionPlayer(2, KeyboardPlayer(Direction.LEFT, Mode.ARROWS), [width - 1, height - 1]),
2018-10-23 16:27:27 +02:00
])
2019-03-13 22:26:39 +01:00
# Create a window for the game so the players can see what they're doing.
2019-03-13 20:45:36 +01:00
window = Window(game, 10)
2018-10-23 16:27:27 +02:00
# Hide mouse
pygame.mouse.set_visible(False)
2019-03-13 22:26:39 +01:00
# Run the game.
2018-10-23 16:27:27 +02:00
game.main_loop(window)
2019-03-13 22:26:39 +01:00
# Once the game is finished, if game.winner is None, it means it's a draw
# Otherwise, game.winner will tell us which player has won the game.
2019-03-13 20:45:36 +01:00
if game.winner is None:
print("It's a draw!")
else:
print('Player {} wins!'.format(game.winner))
2018-10-23 16:27:27 +02:00
if __name__ == '__main__':
main()