Нели обнови решението на 14.05.2013 15:52 (преди над 11 години)
+#import pygame
+#from pygame.locals import *
+from random import randint, shuffle, choice
+
+
+class WorldObject:
+ pass
+
+
+class Cell:
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject) or contents is None:
+ self.contents = contents
+ else:
+ raise TypeError
+
+ def contents(self):
+ return self.contents
+
+ def is_empty(self):
+ return self.contents is None
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.cells = [[Cell()] * width] * width
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, index):
+ if index < 0 or index > self.width:
+ raise IndexError
+ else:
+ return self.cells[index]
+
+ def __setitem__(self, index, item):
+ if index < 0 or index > self.width:
+ raise IndexError
+ else:
+ self.cells[index] = item
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x, self.y = x, y
+
+ def __add__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x + other.x, self.y + other.y)
+ else:
+ raise TypeError
+
+ def __sub__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x - other.x, self.y - other.y)
+ else:
+ raise TypeError
+
+ def __mul__(self, other):
+ if isinstance(other, Number):
+ return Vec2D(self.x * other, self.y * other)
+ else:
+ raise TypeError
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class PythonPart(WorldObject):
+ pass
+
+
+class PythonHead(PythonPart):
+ pass
+
+
+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.coords = coords
+ self.size = size
+ self.direction = direction
+ self.head = PythonHead()
+ self.parts = [PythonPart()] * size
+ x, y = coords
+ self.world[x][y] = Cell(self.head)
+
+ def move(self, direction):
+ self.coords += direction
+ x, y = self.coords
+ if x < 0 or x > self.world.width or y < 0 or y > self.world.width:
+ raise Death
+ self.world[x][y] = Cell(self.head)
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class Death(Exception):
+ pass
+
+
+class Game:
+ CAPTION_TEXT = 'Pythons bite!'
+ WORLD_WIDTH = 300
+ SIZE = WORLD_WIDTH * 2, WORLD_WIDTH * 2
+ BACKGROUND_COLOR = 154, 205, 50
+ SNAKE_COLOR = 110, 139, 61
+ PYTHON_SIZE = 3
+ DIRECTIONS = [Python.UP, Python.DOWN, Python.LEFT, Python.DOWN]
+ running = True
+
+ def __init__(self):
+ pygame.init()
+ world = World(Game.WORLD_WIDTH)
+ direction = choice(self.DIRECTIONS)
+ python = Python(world, Vec2D(10, 10), self.PYTHON_SIZE, direction)
+ screen = pygame.display.set_mode(Game.SIZE)
+ pygame.display.set_caption(Game.CAPTION_TEXT)
+
+ # Fill background
+ background = pygame.Surface(screen.get_size())
+ background = background.convert()
+ background.fill(Game.BACKGROUND_COLOR)
+
+ # Display snakes
+ font = pygame.font.Font(None, 30)
+ text = font.render("@@##########", 1, self.SNAKE_COLOR)
+ textpos = text.get_rect()
+ textpos.centerx = background.get_rect().centerx
+ background.blit(text, textpos)
+
+ # Blit everything to the screen
+ screen.blit(background, (0, 0))
+ pygame.display.flip()
+
+ # Event loop
+ while Game.running:
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ return
+
+ screen.blit(background, (0, 0))
+ pygame.display.flip()
+
+
+#game = Game()
Готино е че ползваш PyGame, но имаш доста грешки.
- няма такова нещо
Number
по подразбиране в Питон - питоните ти са безсмъртни
- нямаш отрицание на вектори.
Vec2D(1, 1) == -Vec2D(-1, -1)