Рейхан обнови решението на 15.05.2013 03:06 (преди над 11 години)
+from Error import Error
+
+class Error(Exception):
+ """Base class for exception in Python Game"""
+ pass
+
+class Death(Error):
+ """ Death python exception"""
+ pass
+
+class Vec2D:
+ """ This class modles a 2 dimensional vector"""
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, value):
+ return Vec2D(self.x + value.x, self.y + value.y)
+
+ def __sub__(self, value):
+ return Vec2D(self.x - value.x, self.y - value.y)
+
+ def __mul__(self, value):
+ return Vec2D( self.x * value, self.y * value)
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __str__(self):
+ return "({}, {})".format(self.x, self.y)
+
+class WorldObject(object):
+ """ This class models the world object"""
+ def __str__(self):
+ return ".."
+
+class Food(WorldObject):
+ """ This class describes the Food"""
+ def __init__(self, energy):
+ self.energy = energy
+
+ def __str__(self):
+ return ":{}".format(self.energy)
+
+class PythonPart(WorldObject):
+ """ This class models the parts of the python body"""
+ def __str__(self):
+ return "##"
+
+class PythonHead(PythonPart):
+ """This class models the head of the python"""
+ def __str__(self):
+ return "@@"
+
+class Cell(object):
+ """ This class models the cell of the world"""
+ def __init__(self, content=None):
+ self.contents = content
+ if content is None:
+ self.isEmpty = True
+ else:
+ self.isEmpty = False
+
+ def is_empty(self):
+ return self.isEmpty
+
+ def __str__(self):
+ return "{}".format(self.contents)
+
+class World:
+ def __init__(self, width):
+ self.width = width
+ self.cellsMatrix = list()
+ for index in range(0,width):
+ cellsArray = list()
+ for secondIndex in range(0,width):
+ cellsArray.append(Cell(WorldObject))
+ self.cellsMatrix.append(cellsArray)
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, key):
+ try:
+ if(key >= 0 and key < self.width):
+ return self.cellsMatrix[key]
+ else:
+ raise IndexError
+ except IndexError:
+ raise
+
+ def __setitem__(self, key, value):
+ try:
+ if(key >= 0 and key < self.width):
+ self.cellsMatrix[key] = value
+ else:
+ raise IndexError
+ except IndexError:
+ raise
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, -1)
+ DOWN = Vec2D(0, 1)
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.energy = 0
+ self.size = size
+ self.direction = direction
+ self.currentPosition = coords
+ self.pythonPositions = list()
+ self.pythonPositions.append(coords)
+
+ self.world[coords.x][coords.y] = Cell(PythonHead)
+ for index in range(1, size + 1):
+ self.world[coords.x - index * direction.x][coords.y - index * direction.y] = Cell(PythonPart)
+ self.pythonPositions.append(Vec2D(coords.x - index * direction.x, coords.y - index * direction.y))
+ self.pythonPositions.reverse()
+
+ def move(self, direction):
+ try:
+ if self.currentPosition.x + direction.x < 0 or self.currentPosition.x + direction.x >= len(self.world):
+ raise Death
+ elif self.currentPosition.y + direction.y < 0 or self.currentPosition.y + direction.y >= len(self.world):
+ raise Death
+ elif (self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is PythonPart or
+ self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is PythonHead):
+ raise Death
+ elif direction == self.LEFT and self.direction == self.RIGHT:
+ raise ValueError
+ elif direction == self.RIGHT and self.direction == self.LEFT:
+ raise ValueError
+ elif direction == self.UP and self.direction == self.DOWN:
+ raise ValueError
+ elif direction == self.DOWN and self.direction == self.UP:
+ raise ValueError
+ elif self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is WorldObject:
+ self.world[self.pythonPositions[0].x][self.pythonPositions[0].y] = Cell(WorldObject)
+ self.pythonPositions.pop()
+ self.pythonPositions.append(Vec2D(self.currentPosition.x + direction.x, self.currentPosition.y + direction.y))
+ self.world[self.pythonPositions[-1].x][self.pythonPositions[-1].y] = Cell(PythonHead)
+ self.world[self.pythonPositions[-2].x][self.pythonPositions[-2].y] = Cell(PythonPart)
+ self.direction = direction
+ elif type(self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents) is Food:
+ self.energy += self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents.energy
+ self.pythonPositions.append(Vec2D(self.currentPosition.x + direction.x, self.currentPosition.y + direction.y))
+ self.world[self.pythonPositions[-1].x][self.pythonPositions[-1].y] = Cell(PythonHead)
+ self.world[self.pythonPositions[-2].x][self.pythonPositions[-2].y] = Cell(PythonPart)
+ self.direction = direction
+
+ except Death:
+ raise
+ except ValueError:
+ raise
+
+ def __str__(self):
+ output = ""
+ for index in range (0, len(self.world)):
+ for secondIndex in range (0, len(self.world)):
+ if type(self.world[secondIndex][index].contents) is Food:
+ output += ":{}".format(self.world[secondIndex][index].contents.energy)
+ elif self.world[secondIndex][index].contents is PythonPart:
+ output += "##"
+ elif self.world[secondIndex][index].contents is PythonHead:
+ output += "@@"
+ elif self.world[secondIndex][index].contents is WorldObject:
+ output += ".."
+ else:
+ output += ".."
+ output += "\n"
+ output += "Energy is {}".format(self.energy)
+ return output
Готино е че използваш pygame но нямаме pygame на сайта. По-добре го махни, защото решението ти не работи така.
И намери начин да смалиш тези дългите редове!