用户:
孤僻的木叶龙982查看:0 回复:0 评论:0 创建时间:2020-10-16T22:44:38
【作品展示】

【作品介绍】
控制台版本的贪吃蛇,必须在命令行里运行。
【作品源代码】
import time
import os
import sys
import keyboard
import random
UP = -2
DOWN = 2
LEFT = -1
RIGHT = 1
XXS = 32
YYS = 16
snake_len = 4
snake_x = [3,2,1,0]
snake_y = [5,5,5,5]
food_x = 0
food_y = 0
old_direction = RIGHT
direction = RIGHT
eat = 0
direction_change = 0
key_get = 0
snake_cross = 0
mat = []
def clear():
os.system('cls')
def init_snake():
global mat
mat = [[' ']*YYS for i in range(XXS)]
def print_snake():
global food_x, food_y, snake_cross
clear()
init_snake()
for l in range(snake_len,0,-1):
x = snake_x[l-1]
y = snake_y[l-1]
if l==1 and mat[x][y]=='蛇':
snake_cross = 1
mat[x][y] = '蛇'
mat[food_x][food_y] = '豆'
for i in range(YYS):
for j in range(XXS):
print(mat[j][i],end='')
print('|')
print('--' * XXS)
def flash_snake():
global direction, old_direction, direction_change
for l in range(snake_len-1,0,-1):
snake_x[l] = snake_x[l-1]
snake_y[l] = snake_y[l-1]
if direction != -old_direction:
if direction == UP:
snake_x[0] = snake_x[1]
snake_y[0] = snake_y[1]-1
elif direction == DOWN:
snake_x[0] = snake_x[1]
snake_y[0] = snake_y[1]+1
elif direction == LEFT:
snake_x[0] = snake_x[1]-1
snake_y[0] = snake_y[1]
elif direction == RIGHT:
snake_x[0] = snake_x[1]+1
snake_y[0] = snake_y[1]
old_direction = direction
else:
direction = old_direction
if direction == UP:
snake_x[0] = snake_x[1]
snake_y[0] = snake_y[1]-1
elif direction == DOWN:
snake_x[0] = snake_x[1]
snake_y[0] = snake_y[1]+1
elif direction == LEFT:
snake_x[0] = snake_x[1]-1
snake_y[0] = snake_y[1]
elif direction == RIGHT:
snake_x[0] = snake_x[1]+1
snake_y[0] = snake_y[1]
direction_change = 0
'''
elif direction == 0:
if old_direction == UP:
snake_x[0] = snake_x[1]
snake_y[0] = snake_y[1]-1
elif old_direction == DOWN:
snake_x[0] = snake_x[1]
snake_y[0] = snake_y[1]+1
elif old_direction == LEFT:
snake_x[0] = snake_x[1]-1
snake_y[0] = snake_y[1]
elif old_direction == RIGHT:
snake_x[0] = snake_x[1]+1
snake_y[0] = snake_y[1]
'''
def snake_key_get(e):
global direction, old_direction,key_get,direction_change
if(key_get == 1):
for key in keyboard._pressed_events:
if key == 72:
direction = UP
direction_change = 1
return
elif key == 80:
direction = DOWN
direction_change = 1
return
elif key == 75:
direction = LEFT
direction_change = 1
return
elif key == 77:
direction = RIGHT
direction_change = 1
return
else:
pass
def flash_food():
global food_x, food_y
food_x = random.randint(1, XXS-2)
food_y = random.randint(1, YYS-2)
while mat[food_x][food_y] == '蛇' or mat[food_x][food_y] == '豆':
food_x = random.random(1, XXS-2)
food_y = random.random(1, YYS-2)
def eat_food():
global food_x, food_y, snake_len
if snake_x[0]==food_x and snake_y[0]==food_y:
snake_x.append(snake_x[-1])
snake_y.append(snake_y[-1])
snake_len+=1
return True
return False
keyboard.hook(snake_key_get)
init_snake()
flash_food()
print_snake()
while(True):
if snake_cross == 1:
print("打结了")
break
if eat == 1:
print("吃到了")
snake_cross = 0
if direction_change != 0 or eat != 0:
direction_change = 0
key_get = 1
time.sleep(0.3)
else:
direction_change = 0
key_get = 1
time.sleep(1)
key_get = 0
eat = 0
flash_snake()
if snake_x[0] >= XXS or snake_x[0] < 0:
print("碰壁了")
break
if snake_y[0]>=YYS or snake_y[0]<0:
print("碰壁了")
break
if eat_food():
flash_food()
eat = 1
print_snake()
input('回车结束')
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn