summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Box.py8
-rw-r--r--Game.py42
-rw-r--r--GameView.py18
-rw-r--r--Snake.py24
4 files changed, 69 insertions, 23 deletions
diff --git a/Box.py b/Box.py
new file mode 100644
index 0000000..0b8f432
--- /dev/null
+++ b/Box.py
@@ -0,0 +1,8 @@
+
+from Vec import Vec2
+
+class Box:
+ pos = None
+
+ def __init__(self, pos: Vec2):
+ self.pos = pos
diff --git a/Game.py b/Game.py
index 07150e0..6f6a063 100644
--- a/Game.py
+++ b/Game.py
@@ -1,6 +1,7 @@
from Snake import Snake
from Vec import Vec2
+from Box import Box
class Walls:
"""Contains the walls of a game.
@@ -40,21 +41,29 @@ class Walls:
class Game:
"""Class responsible for the main logic of a game.
For a game, this will probably be a singleton."""
+
+ snake = None
+ boxes = []
+ walls = None
- snake = Snake([Vec2(6, 6), Vec2(6, 7), Vec2(6,8), Vec2(7,8)])
- walls = Walls.fromString("""###############
-# #
-# #
-# #
-# #
-# #
-# #
+ def __init__(self):
+ self.snake = Snake([Vec2(6, 6), Vec2(6, 7), Vec2(6,8), Vec2(7,8)], self)
+ _box1 = Box(Vec2(11, 4))
+ _box2 = Box(Vec2(3, 10))
+ self.boxes = [_box1, _box2]
+ self.walls = Walls.fromString("""###############
# #
+# f #
# #
+# b #
# #
+# h #
+# t #
+# tt #
# #
+# b #
# ### #
-# #
+# f #
###############""")
def width(self): return self.walls.width()
@@ -64,5 +73,18 @@ class Game:
self.snake.move()
def isLost(self):
- return self.snake.headInTail() or self.walls.wallAt(self.snake.head())
+ return self.snake.hasCollided
+
+ def enter(self, pos: Vec2, inDir: Vec2) -> bool:
+ boxAt = next(filter(lambda box: box.pos == pos, self.boxes), None)
+ if self.walls.wallAt(pos):
+ return False
+ elif boxAt != None:
+ if self.enter(pos + inDir, inDir):
+ boxAt.pos = pos + inDir
+ return True
+ else:
+ return False
+ else:
+ return True
diff --git a/GameView.py b/GameView.py
index 241914c..50785a7 100644
--- a/GameView.py
+++ b/GameView.py
@@ -1,5 +1,6 @@
from Game import Game
+from Box import Box
import pygame
class GameView:
@@ -29,16 +30,24 @@ class GameView:
surface.fill("black")
- for cell in self.game.snake.cells:
- pygame.draw.rect(surface, "red", pygame.Rect(
+ for cell in self.game.walls.walls():
+ pygame.draw.rect(surface, "white", pygame.Rect(
cell.x*self.cellWidth,
cell.y*self.cellWidth,
self.cellWidth,
self.cellWidth
))
- for cell in self.game.walls.walls():
- pygame.draw.rect(surface, "white", pygame.Rect(
+ for box in self.game.boxes:
+ pygame.draw.rect(surface, "brown", pygame.Rect(
+ box.pos.x*self.cellWidth,
+ box.pos.y*self.cellWidth,
+ self.cellWidth,
+ self.cellWidth
+ ))
+
+ for cell in self.game.snake.cells:
+ pygame.draw.rect(surface, "red", pygame.Rect(
cell.x*self.cellWidth,
cell.y*self.cellWidth,
self.cellWidth,
@@ -46,6 +55,7 @@ class GameView:
))
+
def update(self, time):
if (self._previousTick == None) or (self._previousTick + self._tickTime <= time):
self._previousTick = time
diff --git a/Snake.py b/Snake.py
index 81c3b0b..c53af73 100644
--- a/Snake.py
+++ b/Snake.py
@@ -1,23 +1,29 @@
import Vec
+from Vec import Vec2
+import Game
class Snake:
cells = []
heading = Vec.up
+ game = None
+ hasCollided = False
- def __init__(self, cells):
+ def __init__(self, cells: list[Vec2], game: Game):
self.cells = cells
+ self.game = game
def move(self):
- self.cells.pop()
- self.cells.insert(0, self.cells[0] + self.heading)
-
- def headInTail(self):
- res = False
- for i in range(1, len(self.cells)):
- res = res or (self.cells[0] == self.cells[i])
- return res
+ nextPos = self.cells[0] + self.heading
+ if self.game.enter(nextPos, self.heading):
+ if nextPos in self.cells[1:len(self.cells)-1]:
+ self.hasCollided = True
+ else:
+ self.cells.pop()
+ self.cells.insert(0, nextPos)
+ else:
+ self.hasCollided = True
def head(self):
return self.cells[0]