Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: donno/RollyTheSnake
base: 1e302f5dcd
...
head fork: donno/RollyTheSnake
compare: 44944cbbcb
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 29, 2012
@donno Added the mouth property to the snake.
Cleaned up some unused code and made it so it only starts off
with a head and a tail instead of 7 body parts and a head and tail.
00133c1
@donno Now calls the screenResize on the player class.
Added collision detection for a mouse and the snake's mouth.
44944cb
Showing with 48 additions and 11 deletions.
  1. +31 −10 engine/snake.py
  2. +17 −1 game.py
View
41 engine/snake.py
@@ -36,12 +36,14 @@ def drawSnake(screen, snake):
for joint in snake.all_joints:
if joint == head: continue
- pygame.draw.circle(screen, (0, 255,0), (joint.x, joint.y), 10, 0)
snake.drawSnake.drawBody(screen, (joint.x % screenRect.width, joint.y % screenRect.height), joint.rotation)
- pygame.draw.circle(screen, (255, 0,0), (head.x, head.y), 10, 0)
- snake.drawSnake.drawHead(screen, (head.x% screenRect.width, head.y% screenRect.height), head.rotation)
+ x = head.x% screenRect.width
+ y = head.y% screenRect.height
+ snake.drawSnake.drawHead(screen, (x% screenRect.width, y% screenRect.height), head.rotation)
+ # Disable this circle
+ pygame.draw.circle(screen, (255, 0,0), (x%screenRect.width, y%screenRect.width), 10, 0)
class Snake:
"""
@@ -53,18 +55,14 @@ class Snake:
SegmentDistance = 50
def __init__(self, position):
- # This includes head, segements and tail.
+ # This includes head, middle and tail.
self.all_joints = []
self.drawSnake = drawablesnake.DrawableSnake()
self.x, self.y = position
- self.new_segment()
- for x in xrange(0, 9):
- self.new_segment()
-
- # The segments that build up the head to the tail.
- self.segments = []
+ self.new_segment() # Head
+ self.new_segment() # Tail
# The velocity the snake is traveling in X and Y.
#don't use this
@@ -77,9 +75,20 @@ def __init__(self, position):
# the top speed
self.topspeed = 15
+ # The score the snake has.
+ self.score = 0
+
def new_segment(self):
self.all_joints.append(Joint(self.x, self.y, Snake.SegmentDistance))
+ def eat_mouse(self, safeToEat):
+ if safeToEat:
+ self.new_segment()
+ self.score += 5
+ else:
+ # loose a segment
+ pass
+
def move(self, theta):
self.direction += theta
@@ -91,6 +100,18 @@ def draw(self, screen):
# Replace this with the correct code / instance
drawSnake(screen, self)
+ def screenResize(self, size):
+ self.screenSize = size
+
+ @property
+ def mouth(self):
+ # screenResize should be called first...
+ head = self.all_joints[len(self.all_joints) -1]
+ width, height = self.screenSize
+ x = head.x% width
+ y = head.y% height
+ return (x, y)
+
def update(self, timeSinceLastUpdate):
dx = self.speed*math.cos(self.direction)
dy = self.speed*math.sin(self.direction)
View
18 game.py
@@ -62,6 +62,7 @@ def spawnMouse(self):
def onResize(self, size):
self.screenSize = size
+ self.player.screenResize(size)
def onEvent(self, event):
if event.type == pygame.KEYDOWN:
@@ -96,6 +97,8 @@ def onUpdate(self):
for mouse in self.mice:
mouse.update()
+ self.check_mice()
+
def onRender(self, screen):
# Clear the whole screen
screen.fill((0,0,0))
@@ -108,4 +111,17 @@ def onRender(self, screen):
mouse.draw(screen)
# Render the heads up display
- self.hud.draw(screen, 0)
+ self.hud.draw(screen, self.player.score)
+
+ # Check if the snake is colliding with any mice.
+ def check_mice(self):
+ aliveMice = []
+ for mouse in self.mice:
+ # Test if its colliding with the nose of the snake.
+ if mouse.rect.collidepoint(self.player.mouth):
+ # TODO: Handle zombie mouse.
+ self.player.eat_mouse(safeToEat=True)
+ else:
+ # This mouse is still alive.
+ aliveMice.append(mouse)
+ self.mice = aliveMice

No commit comments for this range

Something went wrong with that request. Please try again.