Some updates

This commit is contained in:
Thomas Forgione 2019-03-26 17:49:25 +01:00
parent 28e8aae4ee
commit 77174a8e42
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 18 additions and 9 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
__pycache__
*.pyc
ais

View File

@ -8,6 +8,8 @@ from tron.game import Game, PositionPlayer
from tron.window import Window
from tron.player import Direction, KeyboardPlayer, Mode
from ais.wallavoider import Ai
# 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.
@ -29,7 +31,7 @@ def main():
# 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.
PositionPlayer(1, KeyboardPlayer(Direction.RIGHT, Mode.ZQSD), [0, 0]),
PositionPlayer(1, Ai(), [0, 0]),
# We create a second player that will use the arrow keys.
PositionPlayer(2, KeyboardPlayer(Direction.LEFT, Mode.ARROWS), [width - 1, height - 1]),

View File

@ -34,6 +34,19 @@ class Player(object):
"""
return '/'.join(self.__module__.split('.')[:-1]) + '/' + name
def next_position(self, current_position, direction):
"""
Computes the next position if the player would go on towards a specific
direction.
"""
if direction == Direction.UP:
return (current_position[0] - 1, current_position[1])
if direction == Direction.RIGHT:
return (current_position[0], current_position[1] + 1)
if direction == Direction.DOWN:
return (current_position[0] + 1, current_position[1])
if direction == Direction.LEFT:
return (current_position[0], current_position[1] - 1)
def next_position_and_direction(self, current_position, id, map):
"""
@ -41,14 +54,7 @@ class Player(object):
depending on the current position.
"""
direction = self.action(map, id)
if direction == Direction.UP:
return ((current_position[0] - 1, current_position[1]), direction)
if direction == Direction.RIGHT:
return ((current_position[0], current_position[1] + 1), direction)
if direction == Direction.DOWN:
return ((current_position[0] + 1, current_position[1]), direction)
if direction == Direction.LEFT:
return ((current_position[0], current_position[1] - 1), direction)
return (self.next_position(current_position, direction), direction)
def action(self, map, id):
"""