Skip to content

Commit

Permalink
day11 First Blood and First Respawn
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhao Xin committed Nov 14, 2016
1 parent a28d385 commit 5626c1e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

用Python基于Pygame写的贪吃蛇,以及一个对Pygame再次封装的通用游戏类MyGame。

## 与视频教程对应的版本 VERSIONS WITH VIDEO TUTORIALS
## 视频教程对应版本列表 DAY by DAY TUTORIALS

[视频教程第1集 video tutorial 1](http://v.youku.com/v_show/id_XMTYzMzg5MzQ0NA==.html),
[视频教程第2集 video tutorial 2](http://v.youku.com/v_show/id_XMTYzNTU0ODA5Mg==.html)
Expand Down Expand Up @@ -37,5 +37,8 @@
- **Day 10** 第一口苹果 First Bite of Apple
[[源码 source](https://github.com/archtaurus/pysnake/tree/day10/src)],
[[视频教程 video tutorial](http://v.youku.com/v_show/id_XMTY1MTMwNjIyNA==.html)]
- **Day 11** 第一滴血,蛇的重生 First Blood and First Respawn
[[源码 source](https://github.com/archtaurus/pysnake/tree/day11/src)],
[[视频教程 video tutorial](http://v.youku.com/v_show/id_XMTY1MjY1MjMwMA==.html)]

**[回到最新版本](https://github.com/archtaurus/pysnake/tree/master)**
7 changes: 6 additions & 1 deletion src/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# 功能: define class Firld
# 许可: General Public License
# 作者: Zhao Xin (赵鑫) <[email protected]>
# 时间: 2016.07.20
# 时间: 2016.07.21

import pygame
from settings import *
Expand All @@ -29,6 +29,8 @@ def put_cell(self, cell):
def get_cell(self, x, y):
if 0 <= x < self.columns and 0 <= y < self.rows:
return self.cell_array[y][x]
else:
return OUT

def del_cell(self, x, y):
if 0 <= x < self.columns and 0 <= y < self.rows:
Expand All @@ -43,3 +45,6 @@ def draw(self):
CELL_SIZE, CELL_SIZE)
self.screen.fill(cell.color1, rect)
self.screen.fill(cell.color2, rect.inflate(-4, -4))

def clear(self):
self.cell_array = [[None] * self.columns for i in range(self.rows)]
7 changes: 5 additions & 2 deletions src/pysnake.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# 功能: main program
# 许可: General Public License
# 作者: Zhao Xin (赵鑫) <[email protected]>
# 时间: 2016.07.20
# 时间: 2016.07.21

import pygame
from settings import *
Expand All @@ -31,7 +31,9 @@ def __init__(self):
self.field = Field(self, COLUMNS, ROWS)
self.apple_counter = 0
self.apple = Apple(self)
self.snake = Snake(self, 0, 0, 5, RIGHT, 5, SNAKE_COLOR_SKIN,
self.snake = Snake(self, SNAKE_DEFAULT_X, SNAKE_DEFAULT_Y,
SNAKE_DEFAULT_BODY_LENGTH, RIGHT,
SNAKE_DEFAULT_SPEED, SNAKE_COLOR_SKIN,
SNAKE_COLOR_BODY, SNAKE_COLOR_HEAD)

# 控制按键设定
Expand All @@ -42,6 +44,7 @@ def __init__(self):
self.key_bind(KEY_RIGHT, self.snake.turn, direction=RIGHT)
self.key_bind(pygame.K_EQUALS, self.snake.speed_up)
self.key_bind(pygame.K_MINUS, self.snake.speed_down)
self.key_bind(KEY_RESPAWN, self.snake.respawn)


if __name__ == '__main__':
Expand Down
23 changes: 19 additions & 4 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
# 功能: define constants, configurations, etc.
# 许可: General Public License
# 作者: Zhao Xin (赵鑫) <[email protected]>
# 时间: 2016.07.20
# 时间: 2016.07.21

import pygame

# COLORS
BLACK = 0, 0, 0
WHITE = 255, 255, 255
DARK_GREY = 33, 33, 33
GREY = 127, 127, 127
LIGHT_GREY = 192, 192, 192
BACKGROUND_COLOR = BLACK
GRID_COLOR = DARK_GREY
APPLE_COLOR1 = 255, 127, 127
APPLE_COLOR2 = 255, 66, 66
SNAKE_COLOR_SKIN = 33, 255, 33
SNAKE_COLOR_BODY = 33, 192, 33
SNAKE_COLOR_HEAD = 192, 192, 33

# KEY SETTINGS
KEY_EXIT = pygame.K_ESCAPE
KEY_UP = pygame.K_UP
KEY_DOWN = pygame.K_DOWN
KEY_LEFT = pygame.K_LEFT
KEY_RIGHT = pygame.K_RIGHT
KEY_RESPAWN = pygame.K_r

# GAME SETTINGS
GAME_NAME = "pysnake"
Expand All @@ -35,3 +35,18 @@
COLUMNS, ROWS = SCREEN_WIDTH / CELL_SIZE, SCREEN_HEIGHT / CELL_SIZE
FPS = 60
UP, DOWN, LEFT, RIGHT = (0, -1), (0, 1), (-1, 0), (1, 0)
OUT = -1

# SNAKE DEFAULT SETTINGS
SNAKE_COLOR_SKIN = 33, 255, 33
SNAKE_COLOR_BODY = 33, 192, 33
SNAKE_COLOR_HEAD = 192, 192, 33
SNAKE_COLOR_SKIN_DEAD = LIGHT_GREY
SNAKE_COLOR_BODY_DEAD = GREY
SNAKE_COLOR_HEAD_DEAD = DARK_GREY
SNAKE_DEFAULT_X = 0
SNAKE_DEFAULT_Y = 0
SNAKE_DEFAULT_BODY_LENGTH = 5
SNAKE_DEFAULT_DIRECTION = RIGHT
SNAKE_DEFAULT_SPEED = 10
SNAKE_DEFAULT_LIVE = 100
33 changes: 32 additions & 1 deletion src/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# 功能: define class Snake
# 许可: General Public License
# 作者: Zhao Xin (赵鑫) <[email protected]>
# 时间: 2016.07.20
# 时间: 2016.07.21

from settings import *
from cell import Cell
Expand Down Expand Up @@ -47,6 +47,11 @@ def move(self):
meeting = self.field.get_cell(self.head.x + self.direction[0],
self.head.y + self.direction[1])

# 死亡审判
if meeting and meeting is not self.game.apple:
self.die()
return

# 判断吃了没有,吃了就不断尾巴了
if meeting is self.game.apple:
self.game.apple.drop()
Expand Down Expand Up @@ -84,3 +89,29 @@ def speed_up(self):
def speed_down(self):
if self.speed > 1:
self.speed -= 1

def die(self):
self.alive = False
print "游戏结束!!!"
for body_cell in self.body:
body_cell.color1 = SNAKE_COLOR_SKIN_DEAD
body_cell.color2 = SNAKE_COLOR_BODY_DEAD
self.head.color1 = SNAKE_COLOR_SKIN_DEAD
self.head.color2 = SNAKE_COLOR_HEAD_DEAD

def respawn(self):
# 复生
if not self.alive:
self.game.apple_counter = 0
self.field.clear() # 移除尸体
self.game.apple.drop()
self.head = Cell(SNAKE_DEFAULT_X, SNAKE_DEFAULT_Y,
self.skin_color, self.head_color)
self.field.put_cell(self.head)
tmp_body_cell = Cell(-1, -1, self.skin_color, self.body_color)
self.body = [tmp_body_cell] * SNAKE_DEFAULT_BODY_LENGTH
self.direction = SNAKE_DEFAULT_DIRECTION
self.new_direction = SNAKE_DEFAULT_DIRECTION
self.speed = SNAKE_DEFAULT_SPEED
self.alive = True
self.live = SNAKE_DEFAULT_LIVE

0 comments on commit 5626c1e

Please sign in to comment.