Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpysnake.py
More file actions
Latest commit
177 lines (155 loc) · 4.23 KB
/
Copy pathpysnake.py
File metadata and controls
177 lines (155 loc) · 4.23 KB
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
importctypes
importmsvcrt
fromtimeimportsleep
fromosimportsystem
fromrandomimportrandrange
fromcollectionsimportdeque
fromthreadingimportThread
# 位置信息结构体
classpoint(ctypes.Structure):
_fields_= [('x', ctypes.c_short),
('y', ctypes.c_short)]
defgotoXYPrint(coord, char):
globalMAINHANDLE
globalHEADICON
globalFOODICON
ctypes.windll.kernel32.SetConsoleCursorPosition(
MAINHANDLE, coord)
ifchar==HEADICON:
ctypes.windll.kernel32.SetConsoleTextAttribute(
MAINHANDLE, 14)
elifchar==FOODICON:
ctypes.windll.kernel32.SetConsoleTextAttribute(
MAINHANDLE, 12)
else:
ctypes.windll.kernel32.SetConsoleTextAttribute(
MAINHANDLE, 11)
print(char, end='', flush=True)
# 边框
defside():
forninrange(WIDTH-1):
gotoXYPrint(point(n,0), '+')
gotoXYPrint(point(n,HIGHT-1), '+')
forninrange(HIGHT-1):
gotoXYPrint(point(0,n), '+')
gotoXYPrint(point(WIDTH-1,n), '+')
defcreateFood():
globalSNAKE
globalFOODPOINT
off=False
whileTrue:
x=randrange(1, WIDTH-1)
y=randrange(1, HIGHT-1)
forninSNAKE:
ifn.x==xandn.y==y:
continue
else:
FOODPOINT.x=x
FOODPOINT.y=y
gotoXYPrint(FOODPOINT, FOODICON)
off=True
ifoff: break
defcreateSnake():
globalSNAKE
x, y=WIDTH//2, HIGHT//2
forninrange(3):
t=point(x+n, y)
SNAKE.append(t)
gotoXYPrint(t, HEADICON)
defupdate():
foriinSNAKE:
gotoXYPrint(i, HEADICON)
def_exit(info):
input(info)
exit()
defcollision():
globalSNAKE
head=SNAKE[0]
count=0
forninSNAKE:
count+=1
ifcount==1: continue
ifn.x==head.xandn.y==head.y:
_exit('游戏结束!')
ifhead.x==0orhead.y==0or \
head.x==WIDTH-1orhead.y==HIGHT-1:
_exit('游戏结束!')
defmoveSnake():
'''
K == ←
M == →
H == ↑
P == ↓
'''
globalDIRECTION
globalSNAKE
globalFOODPOINT
ifDIRECTION=='K':
SNAKE.appendleft(point(SNAKE[0].x-1, SNAKE[0].y))
elifDIRECTION=='M':
SNAKE.appendleft(point(SNAKE[0].x+1, SNAKE[0].y))
elifDIRECTION=='H':
SNAKE.appendleft(point(SNAKE[0].x, SNAKE[0].y-1))
elifDIRECTION=='P':
SNAKE.appendleft(point(SNAKE[0].x, SNAKE[0].y+1))
# 其他按键不做任何动作
else: returnNone
collision()
# 是否吃到食物
ifSNAKE[0].x!=FOODPOINT.xorSNAKE[0].y!=FOODPOINT.y:
gotoXYPrint(SNAKE.pop(), ' ')
else:
createFood()
update()
# 长按加速
defSpeed():
globalSPEED
globalORDKEY
globalDIRECTION
whileTrue:
sleep(0.001)
ifmsvcrt.kbhit():
inputKey=msvcrt.getwch()
# 特殊按键
ifinputKey=='\000'orinputKey=='\xe0':
inputKey=msvcrt.getwch()
ifDIRECTION=='K'andinputKey=='M': continue
elifDIRECTION=='M'andinputKey=='K': continue
elifDIRECTION=='H'andinputKey=='P': continue
elifDIRECTION=='P'andinputKey=='H': continue
ifDIRECTION==inputKey:
ifSPEED>=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
defmain():
globalDIRECTION
globalSPEED
side()
createSnake()
createFood()
InputThead=Thread(target=Speed, daemon=True)
InputThead.start()
whileTrue:
moveSnake()
sleep(SPEED)
if__name__=='__main__':
main()