-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysnake.py
177 lines (155 loc) · 4.23 KB
/
pysnake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# coding=utf-8
# author Cool_Breeze
import ctypes
import msvcrt
from time import sleep
from os import system
from random import randrange
from collections import deque
from threading import Thread
# 位置信息结构体
class point(ctypes.Structure):
_fields_ = [('x', ctypes.c_short),
('y', ctypes.c_short)]
def gotoXYPrint(coord, char):
global MAINHANDLE
global HEADICON
global FOODICON
ctypes.windll.kernel32.SetConsoleCursorPosition(
MAINHANDLE, coord)
if char == HEADICON:
ctypes.windll.kernel32.SetConsoleTextAttribute(
MAINHANDLE, 14)
elif char == FOODICON:
ctypes.windll.kernel32.SetConsoleTextAttribute(
MAINHANDLE, 12)
else:
ctypes.windll.kernel32.SetConsoleTextAttribute(
MAINHANDLE, 11)
print(char, end='', flush=True)
# 边框
def side():
for n in range(WIDTH-1):
gotoXYPrint(point(n,0), '+')
gotoXYPrint(point(n,HIGHT-1), '+')
for n in range(HIGHT-1):
gotoXYPrint(point(0,n), '+')
gotoXYPrint(point(WIDTH-1,n), '+')
def createFood():
global SNAKE
global FOODPOINT
off = False
while True:
x = randrange(1, WIDTH-1)
y = randrange(1, HIGHT-1)
for n in SNAKE:
if n.x == x and n.y == y:
continue
else:
FOODPOINT.x = x
FOODPOINT.y = y
gotoXYPrint(FOODPOINT, FOODICON)
off = True
if off: break
def createSnake():
global SNAKE
x, y = WIDTH//2, HIGHT//2
for n in range(3):
t = point(x+n, y)
SNAKE.append(t)
gotoXYPrint(t, HEADICON)
def update():
for i in SNAKE:
gotoXYPrint(i, HEADICON)
def _exit(info):
input(info)
exit()
def collision():
global SNAKE
head = SNAKE[0]
count = 0
for n in SNAKE:
count += 1
if count == 1: continue
if n.x == head.x and n.y == head.y:
_exit('游戏结束!')
if head.x == 0 or head.y == 0 or \
head.x == WIDTH-1 or head.y == HIGHT-1:
_exit('游戏结束!')
def moveSnake():
'''
K == ←
M == →
H == ↑
P == ↓
'''
global DIRECTION
global SNAKE
global FOODPOINT
if DIRECTION == 'K':
SNAKE.appendleft(point(SNAKE[0].x-1, SNAKE[0].y))
elif DIRECTION == 'M':
SNAKE.appendleft(point(SNAKE[0].x+1, SNAKE[0].y))
elif DIRECTION == 'H':
SNAKE.appendleft(point(SNAKE[0].x, SNAKE[0].y-1))
elif DIRECTION == 'P':
SNAKE.appendleft(point(SNAKE[0].x, SNAKE[0].y+1))
# 其他按键不做任何动作
else: return None
collision()
# 是否吃到食物
if SNAKE[0].x != FOODPOINT.x or SNAKE[0].y != FOODPOINT.y:
gotoXYPrint(SNAKE.pop(), ' ')
else:
createFood()
update()
# 长按加速
def Speed():
global SPEED
global ORDKEY
global DIRECTION
while True:
sleep(0.001)
if msvcrt.kbhit():
inputKey = msvcrt.getwch()
# 特殊按键
if inputKey == '\000' or inputKey == '\xe0':
inputKey = msvcrt.getwch()
if DIRECTION == 'K' and inputKey == 'M': continue
elif DIRECTION == 'M' and inputKey == 'K': continue
elif DIRECTION == 'H' and inputKey == 'P': continue
elif DIRECTION == 'P' and inputKey == 'H': continue
if DIRECTION == inputKey:
if SPEED >= 0.02:
SPEED -= 0.02
else:
DIRECTION = inputKey
SPEED = 0.1
HIGHT = 26
WIDTH = 60
MAINHANDLE = ctypes.windll.kernel32.GetStdHandle(-11)
SNAKE = deque([])
HEADICON = 'O'
FOODICON = '$'
FOODPOINT = point()
DIRECTION = 'K'
system(f'mode con cols={WIDTH} lines={HIGHT}')
system("title 贪吃蛇游戏")
# 隐藏光标
ctypes.windll.kernel32.SetConsoleCursorInfo(
MAINHANDLE, ctypes.byref(point(1,0)))
SPEED = 0.1
ORDKEY = None
def main():
global DIRECTION
global SPEED
side()
createSnake()
createFood()
InputThead = Thread(target=Speed, daemon=True)
InputThead.start()
while True:
moveSnake()
sleep(SPEED)
if __name__ == '__main__':
main()