|
|

楼主 |
发表于 2017-7-1 22:59:11
|
显示全部楼层
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- __author__ = 'Eric_XU okbrady@sina.com'
- import random as r
- import os, msvcrt
- # 游戏界面大小
- s_Wide = 50
- s_Height = 20
- table_list = []
- tail = (1, int(s_Height/2))
- v = {'w': 1, 'a': 2, 's': 4, 'd': 3}
- t = [1, 1, 0.8, 0.6, 0.4, 0.3, 0.2]
- level = 3
- # 清屏程序
- def clear_screen():
- os.system('cls')
- # 打印标题
- def print_title():
- global s_Wide
- title1 = 'SNAKE'
- title2 = 'author: Eric XU okbrady@sina.com'
- print(title1.center(s_Wide))
- print(title2.center(s_Wide))
- # 原始界面的二维列表
- def get_table():
- global table_list
- table_list = []
- top_bot = []
- for i in range(s_Wide):
- top_bot.append('-')
- table_list.append(top_bot)
- for i in range(s_Height - 2):
- mid_line = ['|']
- for i in range(s_Wide - 2):
- mid_line.append(' ')
- mid_line.append('|')
- table_list.append(mid_line)
- table_list.append(top_bot)
- # 将食物与蛇加入二维列表
- def reflash():
- global table_list, snake, food
- get_table()
- b = snake.show_body()
- for i in b:
- temp_x = i[0]
- temp_y = i[1]
- table_list[temp_y][temp_x] = 'O'
- j = food.show_location()
- table_list[j[1]][j[0]] = '*'
- # 以字符串形式打印二维列表table_list
- def print_table():
- global table_list, s_Height
- for i in range(s_Height):
- temp = ''.join(table_list[i])
- print(temp)
- # 边框列表
- def edge_list():
- global s_Wide, s_Height
- edge = []
- for i in range(s_Wide):
- edge.extend([(i, 0), (i, s_Height - 1)])
- for j in range(1, s_Height - 1):
- edge.extend([(0, j), (s_Wide - 1, j)])
- return edge
- edge = edge_list()
- # 定义打印整个屏幕
- def print_screen():
- print_title()
- reflash()
- print_table()
- # 剩余可以用空格数
- def rest():
- global s_Wide, s_Height, snake
- rest_num = (s_Wide-2)*(s_Height-2) - len(snake.show_body())
- return rest_num
- class Snake:
- global s_Wide, s_Height, edge
- # 初始参数
- def __init__(self, b, d, s):
- self.body = b
- self.direction = d
- self.status = s
- # 返回蛇身体的列表
- def show_body(self):
- return(self.body)
- # 返回蛇的朝向
- def show_direction(self):
- return(self.direction)
- # 返回蛇的状态
- def show_status(self):
- return(self.status)
- # 返回蛇的尾部
- def show_tail(self):
- return(self.body[-1])
- # 测试是否死亡
- def test_status(self):
- b = self.show_body()
- head = b[0]
- l = len(b)
- corp = []
- for i in range(1, l):
- corp.append(b[i])
- if (head in edge_list()) or (head in corp):
- self.status = False
- else:
- pass
- # 定义移动
- # 吃可以分解为 move() + append(上一步的tail)
- def move(self):
- direct = self.show_direction()
- body_list = self.show_body()
- head = body_list[0]
- (head_x, head_y) = body_list[0]
- l = len(body_list)
- if direct == 'w':
- head_x = head[0]
- head_y = head[1] - 1
- elif direct == 'a':
- head_x = head[0] - 1
- head_y = head[1]
- elif direct == 's':
- head_x = head[0]
- head_y = head[1] + 1
- elif direct == 'd':
- head_x = head[0] + 1
- head_y = head[1]
- new_body = [(head_x, head_y)]
- new_body.extend(body_list[:l-1])
- self.body = new_body
- class Food:
- global snake, s_Wide, s_Height
- def __init__(self, l):
- self.location = l
- def show_location(self):
- return self.location
- def reborn(self):
- rest_num = rest()
- temp = r.randint(1, rest_num + 1)
- (temp_x, temp_y) = (0, 1)
- while (temp > 0):
- temp_x += 1
- if temp_x == s_Wide - 1:
- temp_x = 1
- temp_y += 1
- if (temp_x, temp_y) not in snake.show_body():
- temp -= 1
- self.location = (temp_x, temp_y)
- # 蛇的初始参数
- in_body = [(3, int(s_Height/2)), (2, int(s_Height/2)), (1, int(s_Height/2))] # 蛇的身体
- in_direction = 'w' # 头的朝向
- in_status = True # 死活
- # 定义一条蛇
- snake = Snake(in_body, in_direction, in_status)
- # 定义食物
- food = Food((1, 1))
- food.reborn()
- # 定义蛇是否吃到了食物
- def test_eat():
- global snake, food, tail
- if snake.show_body()[0] == food.show_location():
- food.reborn()
- (snake.show_body()).append(tail)
- # 获取字符
- def get_char():
- global snake, v
- dir_temp = snake.show_direction()
- unknow = msvcrt.getch()
- char = unknow.decode()
- if ((char in ['w', 'a', 's', 'd']) and (v[char] + v[dir_temp] != 5)):
- return char
- else:
- get_char()
- # 主体程序
- while snake.show_status():
- clear_screen()
- print_screen()
- key = get_char()
- snake.direction = str(key)
- tail = snake.show_tail()
- snake.move()
- test_eat()
- snake.test_status()
- print('Good job! You\'ve just killed yourself!')
复制代码 |
|