Merge branch 'master' into python2
This commit is contained in:
commit
0ff6f7632d
22
tron/game.py
22
tron/game.py
|
@ -6,6 +6,7 @@ from time import sleep
|
|||
from enum import Enum
|
||||
|
||||
from tron.map import Map
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
class Winner(Enum):
|
||||
PLAYER_ONE = 1
|
||||
|
@ -126,15 +127,20 @@ class Game:
|
|||
dies.
|
||||
"""
|
||||
|
||||
self.history[-1].player_one_direction = self.pps[0].player.direction
|
||||
self.history[-1].player_two_direction = self.pps[1].player.direction
|
||||
|
||||
map_clone = self.map()
|
||||
|
||||
# Set previous heads to body
|
||||
for pp in self.pps:
|
||||
map_clone[pp.position[0], pp.position[1]] = pp.body()
|
||||
|
||||
# Play next move
|
||||
for id, pp in enumerate(self.pps):
|
||||
(pp.position, pp.player.direction) = pp.player.next_position_and_direction(pp.position, id + 1, self.history[-1].map)
|
||||
|
||||
# Update history with newly played move
|
||||
self.history[-1].player_one_direction = self.pps[0].player.direction
|
||||
self.history[-1].player_two_direction = self.pps[1].player.direction
|
||||
|
||||
# Manage the events
|
||||
if window:
|
||||
import pygame
|
||||
|
@ -147,13 +153,8 @@ class Game:
|
|||
for pp in self.pps:
|
||||
pp.player.manage_event(event)
|
||||
|
||||
player_directions = []
|
||||
|
||||
for (id, pp) in enumerate(self.pps):
|
||||
|
||||
(pp.position, direction) = pp.player.next_position_and_direction(pp.position, id + 1, self)
|
||||
player_directions.append(direction)
|
||||
|
||||
# Check boundaries
|
||||
if pp.position[0] < 0 or pp.position[1] < 0 or \
|
||||
pp.position[0] >= self.width or pp.position[1] >= self.height:
|
||||
|
@ -172,7 +173,6 @@ class Game:
|
|||
# Append to history
|
||||
self.history.append(HistoryElement(map_clone, None, None))
|
||||
|
||||
|
||||
def main_loop(self, window = None):
|
||||
"""
|
||||
Loops until the game is finished
|
||||
|
@ -190,10 +190,6 @@ class Game:
|
|||
if window:
|
||||
sleep(0.1)
|
||||
|
||||
for (id, pp) in enumerate(self.pps):
|
||||
if pp.alive:
|
||||
pp.player.direction = pp.player.action(self, id) or pp.direction
|
||||
|
||||
self.next_frame(window)
|
||||
|
||||
for pp in self.pps:
|
||||
|
|
|
@ -25,8 +25,8 @@ class Player(object):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
def next_position_and_direction(self, current_position, id, game):
|
||||
direction = self.action(game, id)
|
||||
def next_position_and_direction(self, current_position, id, map):
|
||||
direction = self.action(map, id)
|
||||
if direction == Direction.UP:
|
||||
return ((current_position[0] - 1, current_position[1]), direction)
|
||||
if direction == Direction.RIGHT:
|
||||
|
@ -36,7 +36,7 @@ class Player(object):
|
|||
if direction == Direction.LEFT:
|
||||
return ((current_position[0], current_position[1] - 1), direction)
|
||||
|
||||
def action(self, game, id):
|
||||
def action(self, map, id):
|
||||
"""
|
||||
This function is called each time to ask the player his action.
|
||||
|
||||
|
@ -121,7 +121,7 @@ class KeyboardPlayer(Player):
|
|||
if event.key == self.down():
|
||||
self.direction = Direction.DOWN
|
||||
|
||||
def action(self, game, id):
|
||||
def action(self, map, id):
|
||||
"""
|
||||
Returns the direction of the tron.
|
||||
"""
|
||||
|
@ -136,5 +136,5 @@ class ConstantPlayer(Player):
|
|||
super(ConstantPlayer, self).__init__()
|
||||
self.direction = direction
|
||||
|
||||
def action(self, game, id):
|
||||
def action(self, map, id):
|
||||
return self.direction
|
||||
|
|
Loading…
Reference in New Issue