forked from archtaurus/pysnake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhao Xin
committed
Nov 14, 2016
1 parent
a28d385
commit 5626c1e
Showing
5 changed files
with
66 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 * | ||
|
@@ -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: | ||
|
@@ -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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
# 功能: main program | ||
# 许可: General Public License | ||
# 作者: Zhao Xin (赵鑫) <[email protected]> | ||
# 时间: 2016.07.20 | ||
# 时间: 2016.07.21 | ||
|
||
import pygame | ||
from settings import * | ||
|
@@ -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) | ||
|
||
# 控制按键设定 | ||
|
@@ -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__': | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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() | ||
|
@@ -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 |