Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
Jan 29, 2012
Sean 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
Sean Now calls the screenResize on the player class.
Added collision detection for a mouse and the snake's mouth.
44944cb

Showing 2 changed files with 48 additions and 11 deletions. Show diff stats Hide diff stats

  1. +31 10 engine/snake.py
  2. +17 1 game.py
41 engine/snake.py
@@ -36,12 +36,14 @@ def drawSnake(screen, snake):
36 36 for joint in snake.all_joints:
37 37 if joint == head: continue
38 38
39   - pygame.draw.circle(screen, (0, 255,0), (joint.x, joint.y), 10, 0)
40 39 snake.drawSnake.drawBody(screen, (joint.x % screenRect.width, joint.y % screenRect.height), joint.rotation)
41 40
42   - pygame.draw.circle(screen, (255, 0,0), (head.x, head.y), 10, 0)
43   - snake.drawSnake.drawHead(screen, (head.x% screenRect.width, head.y% screenRect.height), head.rotation)
  41 + x = head.x% screenRect.width
  42 + y = head.y% screenRect.height
  43 + snake.drawSnake.drawHead(screen, (x% screenRect.width, y% screenRect.height), head.rotation)
44 44
  45 + # Disable this circle
  46 + pygame.draw.circle(screen, (255, 0,0), (x%screenRect.width, y%screenRect.width), 10, 0)
45 47
46 48 class Snake:
47 49 """
@@ -53,18 +55,14 @@ class Snake:
53 55 SegmentDistance = 50
54 56
55 57 def __init__(self, position):
56   - # This includes head, segements and tail.
  58 + # This includes head, middle and tail.
57 59 self.all_joints = []
58 60
59 61 self.drawSnake = drawablesnake.DrawableSnake()
60 62
61 63 self.x, self.y = position
62   - self.new_segment()
63   - for x in xrange(0, 9):
64   - self.new_segment()
65   -
66   - # The segments that build up the head to the tail.
67   - self.segments = []
  64 + self.new_segment() # Head
  65 + self.new_segment() # Tail
68 66
69 67 # The velocity the snake is traveling in X and Y.
70 68 #don't use this
@@ -77,9 +75,20 @@ def __init__(self, position):
77 75 # the top speed
78 76 self.topspeed = 15
79 77
  78 + # The score the snake has.
  79 + self.score = 0
  80 +
80 81 def new_segment(self):
81 82 self.all_joints.append(Joint(self.x, self.y, Snake.SegmentDistance))
82 83
  84 + def eat_mouse(self, safeToEat):
  85 + if safeToEat:
  86 + self.new_segment()
  87 + self.score += 5
  88 + else:
  89 + # loose a segment
  90 + pass
  91 +
83 92 def move(self, theta):
84 93 self.direction += theta
85 94
@@ -91,6 +100,18 @@ def draw(self, screen):
91 100 # Replace this with the correct code / instance
92 101 drawSnake(screen, self)
93 102
  103 + def screenResize(self, size):
  104 + self.screenSize = size
  105 +
  106 + @property
  107 + def mouth(self):
  108 + # screenResize should be called first...
  109 + head = self.all_joints[len(self.all_joints) -1]
  110 + width, height = self.screenSize
  111 + x = head.x% width
  112 + y = head.y% height
  113 + return (x, y)
  114 +
94 115 def update(self, timeSinceLastUpdate):
95 116 dx = self.speed*math.cos(self.direction)
96 117 dy = self.speed*math.sin(self.direction)
18 game.py
@@ -62,6 +62,7 @@ def spawnMouse(self):
62 62
63 63 def onResize(self, size):
64 64 self.screenSize = size
  65 + self.player.screenResize(size)
65 66
66 67 def onEvent(self, event):
67 68 if event.type == pygame.KEYDOWN:
@@ -96,6 +97,8 @@ def onUpdate(self):
96 97 for mouse in self.mice:
97 98 mouse.update()
98 99
  100 + self.check_mice()
  101 +
99 102 def onRender(self, screen):
100 103 # Clear the whole screen
101 104 screen.fill((0,0,0))
@@ -108,4 +111,17 @@ def onRender(self, screen):
108 111 mouse.draw(screen)
109 112
110 113 # Render the heads up display
111   - self.hud.draw(screen, 0)
  114 + self.hud.draw(screen, self.player.score)
  115 +
  116 + # Check if the snake is colliding with any mice.
  117 + def check_mice(self):
  118 + aliveMice = []
  119 + for mouse in self.mice:
  120 + # Test if its colliding with the nose of the snake.
  121 + if mouse.rect.collidepoint(self.player.mouth):
  122 + # TODO: Handle zombie mouse.
  123 + self.player.eat_mouse(safeToEat=True)
  124 + else:
  125 + # This mouse is still alive.
  126 + aliveMice.append(mouse)
  127 + self.mice = aliveMice

No commit comments for this range

Something went wrong with that request. Please try again.