Решение на Питоните хапят! от Нели Хатева

Обратно към всички решения

Към профила на Нели Хатева

Резултати

  • 0 точки от тестове
  • 4 бонус точки
  • 4 точки общо
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)

Код

import pygame
from pygame.locals import *
from random import randint, 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, int) or isinstance(other, float):
return Vec2D(self.x * other, self.y * other)
else:
raise TypeError
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
else:
return False
def __ne__(self, other):
if isinstance(other, Vec2D):
return self.x != other.x or self.y != other.y
else:
return False
def __hash__(self):
return hash(id(self))
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)
OPOSITE = {LEFT: RIGHT, RIGHT: LEFT, UP: DOWN, DOWN: UP}
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
x, y = coords
self.parts = []
self.head = PythonHead()
self.world[x][y] = Cell(self.head)
coordinates = Vec2D(x, y)
for i in range(0, size):
coordinates += self.OPOSITE[direction]
x, y = coordinates
python_part = PythonPart()
self.parts.append(python_part)
self.world[x][y] = Cell(python_part)
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
if not self.world[x][y].is_empty():
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
FOOD_COLOR = 255, 0, 0
PYTHONS_NUMBER = 5
PYTHON_SIZE = 3
FOOD_CELLS = 11
DIRECTIONS = [Python.UP, Python.DOWN, Python.LEFT, Python.DOWN]
running = True
def play(self):
pygame.init()
world = World(Game.WORLD_WIDTH)
pythons = []
food = []
for i in range(0, Game.PYTHONS_NUMBER):
direction = choice(self.DIRECTIONS)
row = randint(0, Game.WORLD_WIDTH)
col = randint(0, Game.WORLD_WIDTH)
coords = Vec2D(row, col)
pythons.append(Python(world, coords, self.PYTHON_SIZE, direction))
for i in range(0, Game.FOOD_CELLS):
energy = randint(0, 10)
food.append(Food(energy))
screen = pygame.display.set_mode(Game.SIZE)
pygame.display.set_caption(Game.CAPTION_TEXT)
# Event loop
while Game.running:
for event in pygame.event.get():
if event.type == QUIT:
return
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(Game.BACKGROUND_COLOR)
# Display snakes
font = pygame.font.Font(None, 30)
for python in pythons:
text = font.render("@@######", 1, self.SNAKE_COLOR)
textpos = text.get_rect()
textpos.centerx = python.coords.x * 2
textpos.centery = python.coords.y * 2
background.blit(text, textpos)
try:
python.move(Python.LEFT)
except Death:
pass
screen.blit(background, (0, 0))
pygame.display.flip()
#Game().play()

Лог от изпълнението

File "lib/language/python/runner.py", line 99, in main
    test = imp.load_source('test', test_module)
  File "/opt/python3.3/lib/python3.3/imp.py", line 109, in load_source
    return _LoadSourceCompatibility(name, pathname, file).load_module(name)
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1023, in load_module
  File "<frozen importlib._bootstrap>", line 1004, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 869, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/tmp/d20130606-14014-4ecyz5/test.py", line 3, in <module>
    from solution import *
  File "/tmp/d20130606-14014-4ecyz5/solution.py", line 1, in <module>
    import pygame

История (3 версии и 3 коментара)

Нели обнови решението на 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()

Нели обнови решението на 15.05.2013 15:04 (преди над 11 години)

-#import pygame
-#from pygame.locals import *
-from random import randint, shuffle, choice
+import pygame
+from pygame.locals import *
+from random import randint, 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:
+ if index < 0 or index > self.width - 1:
raise IndexError
else:
return self.cells[index]
def __setitem__(self, index, item):
- if index < 0 or index > self.width:
+ if index < 0 or index > self.width - 1:
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):
+ if isinstance(other, int) or isinstance(other, float):
return Vec2D(self.x * other, self.y * other)
else:
raise TypeError
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __eq__(self, other):
+ if isinstance(other, Vec2D):
+ return self.x == other.x and self.y == other.y
+ else:
+ return False
+
+ def __ne__(self, other):
+ if isinstance(other, Vec2D):
+ return self.x != other.x or self.y != other.y
+ else:
+ return False
+
+ def __hash__(self):
+ return hash(id(self))
+
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)
+ OPOSITE = {LEFT: RIGHT, RIGHT: LEFT, UP: DOWN, DOWN: UP}
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.head = PythonHead()
self.world[x][y] = Cell(self.head)
+ coordinates = Vec2D(x, y)
+ #for i in range(0, size):
+ #coordinates += self.OPOSITE[direction]
+ #x, y = coords
+ #self.world[x][y] = Cell(PythonPart())
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
+ #if not self.world[x][y].is_empty():
+ #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
+ FOOD_COLOR = 255, 0, 0
+ PYTHONS_NUMBER = 5
PYTHON_SIZE = 3
+ FOOD_CELLS = 11
DIRECTIONS = [Python.UP, Python.DOWN, Python.LEFT, Python.DOWN]
running = True
- def __init__(self):
+ def play(self):
pygame.init()
world = World(Game.WORLD_WIDTH)
- direction = choice(self.DIRECTIONS)
- python = Python(world, Vec2D(10, 10), self.PYTHON_SIZE, direction)
+ pythons = []
+ food = []
+ for i in range(0, Game.PYTHONS_NUMBER):
+ direction = choice(self.DIRECTIONS)
+ row = randint(0, Game.WORLD_WIDTH)
+ col = randint(0, Game.WORLD_WIDTH)
+ coords = Vec2D(row, col)
+ pythons.append(Python(world, coords, self.PYTHON_SIZE, direction))
+ for i in range(0, Game.FOOD_CELLS):
+ energy = randint(0, 10)
+ food.append(Food(energy))
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
+ # Fill background
+ background = pygame.Surface(screen.get_size())
+ background = background.convert()
+ background.fill(Game.BACKGROUND_COLOR)
+ # Display snakes
+ font = pygame.font.Font(None, 30)
+ for python in pythons:
+ text = font.render("@@######", 1, self.SNAKE_COLOR)
+ textpos = text.get_rect()
+ textpos.centerx = python.coords.x * 2
+ textpos.centery = python.coords.y * 2
+ background.blit(text, textpos)
+ try:
+ python.move(Python.LEFT)
+ except Death:
+ pass
screen.blit(background, (0, 0))
pygame.display.flip()
-#game = Game()
+#Game().play()

Нели обнови решението на 15.05.2013 15:28 (преди над 11 години)

import pygame
from pygame.locals import *
from random import randint, 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 - 1:
+ 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 - 1:
+ 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, int) or isinstance(other, float):
return Vec2D(self.x * other, self.y * other)
else:
raise TypeError
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
else:
return False
def __ne__(self, other):
if isinstance(other, Vec2D):
return self.x != other.x or self.y != other.y
else:
return False
def __hash__(self):
return hash(id(self))
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)
OPOSITE = {LEFT: RIGHT, RIGHT: LEFT, UP: DOWN, DOWN: UP}
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
x, y = coords
+ self.parts = []
self.head = PythonHead()
self.world[x][y] = Cell(self.head)
coordinates = Vec2D(x, y)
- #for i in range(0, size):
- #coordinates += self.OPOSITE[direction]
- #x, y = coords
- #self.world[x][y] = Cell(PythonPart())
+ for i in range(0, size):
+ coordinates += self.OPOSITE[direction]
+ x, y = coordinates
+ python_part = PythonPart()
+ self.parts.append(python_part)
+ self.world[x][y] = Cell(python_part)
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
- #if not self.world[x][y].is_empty():
- #raise Death
+ if not self.world[x][y].is_empty():
+ 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
FOOD_COLOR = 255, 0, 0
PYTHONS_NUMBER = 5
PYTHON_SIZE = 3
FOOD_CELLS = 11
DIRECTIONS = [Python.UP, Python.DOWN, Python.LEFT, Python.DOWN]
running = True
def play(self):
pygame.init()
world = World(Game.WORLD_WIDTH)
pythons = []
food = []
for i in range(0, Game.PYTHONS_NUMBER):
direction = choice(self.DIRECTIONS)
row = randint(0, Game.WORLD_WIDTH)
col = randint(0, Game.WORLD_WIDTH)
coords = Vec2D(row, col)
pythons.append(Python(world, coords, self.PYTHON_SIZE, direction))
for i in range(0, Game.FOOD_CELLS):
energy = randint(0, 10)
food.append(Food(energy))
screen = pygame.display.set_mode(Game.SIZE)
pygame.display.set_caption(Game.CAPTION_TEXT)
# Event loop
while Game.running:
for event in pygame.event.get():
if event.type == QUIT:
return
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(Game.BACKGROUND_COLOR)
# Display snakes
font = pygame.font.Font(None, 30)
for python in pythons:
text = font.render("@@######", 1, self.SNAKE_COLOR)
textpos = text.get_rect()
textpos.centerx = python.coords.x * 2
textpos.centery = python.coords.y * 2
background.blit(text, textpos)
try:
python.move(Python.LEFT)
except Death:
pass
screen.blit(background, (0, 0))
pygame.display.flip()
#Game().play()