Skip to content

Commit 6a0c4d9

Browse files
committed
011 snake game
1 parent a21b231 commit 6a0c4d9

File tree

5 files changed

+382
-0
lines changed

5 files changed

+382
-0
lines changed

011_snake_game.md

+265
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
# 10 gobang
2+
3+
## Requirements
4+
5+
1. Run the code in console using command line.
6+
2. It'll open a Python window to play the snake game.
7+
8+
## What will we practice in this project?
9+
10+
- turtle diagram
11+
- key event handle
12+
- if condition
13+
- timer
14+
- object clone
15+
- list
16+
17+
## A reference code
18+
19+
```python
20+
from turtle import *
21+
from random import randint
22+
23+
gz = 22
24+
bc = gz * 8
25+
26+
screen = Screen()
27+
screen.bgcolor("darkblue")
28+
29+
food = Turtle()
30+
food.color("red")
31+
food.shape("square")
32+
food.up()
33+
food.speed(0)
34+
food.goto(randint(-bc+gz, bc-gz)//gz*gz, randint(-bc+gz, bc-gz)//gz*gz)
35+
36+
liner = Turtle()
37+
liner.speed(0)
38+
liner.up()
39+
liner.goto(-bc, bc)
40+
liner.color("white")
41+
liner.pensize(5)
42+
liner.down()
43+
for i in range(4):
44+
liner.fd(bc*2)
45+
liner.rt(90)
46+
liner.ht()
47+
48+
score = 0
49+
judge = Turtle()
50+
judge.speed(0)
51+
judge.up()
52+
judge.goto(0, bc+20)
53+
judge.color("white")
54+
judge.write("得分:{}".format(score), align="center", font=("Kai", 20, "bold"))
55+
judge.ht()
56+
57+
head = Turtle()
58+
head.speed(0)
59+
head.up()
60+
head.color("cyan")
61+
head.shape("square")
62+
63+
snake = []
64+
snake.append(head)
65+
for i in range(2):
66+
body = head.clone()
67+
body.color("white")
68+
body.goto(head.xcor()+(i+1)*gz, head.ycor())
69+
snake.append(body)
70+
71+
d = [-1, 0]
72+
def move():
73+
last = snake.pop()
74+
first = snake[0]
75+
first.color("white")
76+
last.goto(first.xcor() + d[0] * gz, first.ycor() + d[1] * gz)
77+
last.color("cyan")
78+
snake.insert(0, last)
79+
80+
if snake[0].xcor() == food.xcor() and snake[0].ycor() == food.ycor():
81+
food.ht()
82+
food.goto(randint(-bc+gz, bc-gz)//gz*gz, randint(-bc+gz, bc-gz)//gz*gz)
83+
food.st()
84+
body = snake[-1].clone()
85+
snake.append(body)
86+
global score
87+
score += 1
88+
judge.clear()
89+
judge.write("得分:{}".format(score), align="center", font=("Kai", 20, "bold"))
90+
91+
if (snake[0].xcor() > -bc and snake[0].xcor() < bc) and \
92+
(snake[0].ycor() > -bc and snake[0].ycor() < bc):
93+
screen.ontimer(move, 500)
94+
95+
def up():
96+
global d
97+
if d[1] != -1:
98+
d = [0, 1]
99+
def down():
100+
global d
101+
if d[1] != 1:
102+
d = [0, -1]
103+
def left():
104+
global d
105+
if d[0] != 1:
106+
d = [-1, 0]
107+
def right():
108+
global d
109+
if d[0] != -1:
110+
d = [1, 0]
111+
112+
screen.onkey(up, "Up")
113+
screen.onkey(down, "Down")
114+
screen.onkey(left, "Left")
115+
screen.onkey(right, "Right")
116+
117+
screen.listen()
118+
119+
move()
120+
121+
```
122+
123+
## Run the demo
124+
125+
Please save the Python as snake.py and run it in console:
126+
127+
```
128+
python snake.py
129+
```
130+
131+
![Chanllenge 10](images/011_snake.png)
132+
133+
----
134+
135+
# 贪吃蛇小游戏
136+
137+
## 项目需求
138+
139+
- 直接在控制台使用命令行运行
140+
- 运行之后出现贪吃蛇小游戏
141+
142+
## 项目练习
143+
144+
- turtle
145+
- 键盘事件响应
146+
- 条件语句
147+
- 定时器
148+
- 对象克隆
149+
- 列表
150+
151+
## 项目参考代码
152+
153+
```python
154+
from turtle import *
155+
from random import randint
156+
157+
gz = 22
158+
bc = gz * 8
159+
160+
screen = Screen()
161+
screen.bgcolor("darkblue")
162+
163+
food = Turtle()
164+
food.color("red")
165+
food.shape("square")
166+
food.up()
167+
food.speed(0)
168+
food.goto(randint(-bc+gz, bc-gz)//gz*gz, randint(-bc+gz, bc-gz)//gz*gz)
169+
170+
liner = Turtle()
171+
liner.speed(0)
172+
liner.up()
173+
liner.goto(-bc, bc)
174+
liner.color("white")
175+
liner.pensize(5)
176+
liner.down()
177+
for i in range(4):
178+
liner.fd(bc*2)
179+
liner.rt(90)
180+
liner.ht()
181+
182+
score = 0
183+
judge = Turtle()
184+
judge.speed(0)
185+
judge.up()
186+
judge.goto(0, bc+20)
187+
judge.color("white")
188+
judge.write("得分:{}".format(score), align="center", font=("Kai", 20, "bold"))
189+
judge.ht()
190+
191+
head = Turtle()
192+
head.speed(0)
193+
head.up()
194+
head.color("cyan")
195+
head.shape("square")
196+
197+
snake = []
198+
snake.append(head)
199+
for i in range(2):
200+
body = head.clone()
201+
body.color("white")
202+
body.goto(head.xcor()+(i+1)*gz, head.ycor())
203+
snake.append(body)
204+
205+
d = [-1, 0]
206+
def move():
207+
last = snake.pop()
208+
first = snake[0]
209+
first.color("white")
210+
last.goto(first.xcor() + d[0] * gz, first.ycor() + d[1] * gz)
211+
last.color("cyan")
212+
snake.insert(0, last)
213+
214+
if snake[0].xcor() == food.xcor() and snake[0].ycor() == food.ycor():
215+
food.ht()
216+
food.goto(randint(-bc+gz, bc-gz)//gz*gz, randint(-bc+gz, bc-gz)//gz*gz)
217+
food.st()
218+
body = snake[-1].clone()
219+
snake.append(body)
220+
global score
221+
score += 1
222+
judge.clear()
223+
judge.write("得分:{}".format(score), align="center", font=("Kai", 20, "bold"))
224+
225+
if (snake[0].xcor() > -bc and snake[0].xcor() < bc) and \
226+
(snake[0].ycor() > -bc and snake[0].ycor() < bc):
227+
screen.ontimer(move, 500)
228+
229+
def up():
230+
global d
231+
if d[1] != -1:
232+
d = [0, 1]
233+
def down():
234+
global d
235+
if d[1] != 1:
236+
d = [0, -1]
237+
def left():
238+
global d
239+
if d[0] != 1:
240+
d = [-1, 0]
241+
def right():
242+
global d
243+
if d[0] != -1:
244+
d = [1, 0]
245+
246+
screen.onkey(up, "Up")
247+
screen.onkey(down, "Down")
248+
screen.onkey(left, "Left")
249+
screen.onkey(right, "Right")
250+
251+
screen.listen()
252+
253+
move()
254+
255+
```
256+
257+
## 测试运行
258+
259+
将代码保存为snake.py,然后在控制台运行:
260+
261+
```
262+
python snake.py
263+
```
264+
265+
![挑战10](images/011_snake.png)

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/009_Open
3636

3737
## 010 Gobang game
3838
https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/010_gobang.md
39+
40+
## 011 Snake game
41+
https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/011_snake_game.md

code/10/gobang.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from turtle import *
2+
3+
win = False
4+
5+
speed(0)
6+
bgcolor("lightgreen")
7+
yanse="black"
8+
gz=40
9+
10+
judge = Turtle()
11+
judge.up()
12+
judge.goto(-460, 330)
13+
judge.write("Next", font=("Arial", 40, "bold"))
14+
judge.color(yanse)
15+
judge.goto(-420, 300)
16+
judge.dot(30)
17+
18+
for i in range(19):
19+
up()
20+
goto(-gz*9, gz*(9-i))
21+
down()
22+
fd(gz*18)
23+
bk(gz*18)
24+
25+
rt(90)
26+
27+
for i in range(19):
28+
up()
29+
goto(-gz*(9-i), gz*9)
30+
down()
31+
fd(gz*18)
32+
bk(gz*18)
33+
34+
pensize(5)
35+
for i in range(4):
36+
fd(gz*18)
37+
rt(90)
38+
39+
# m = [[0] * 19 for i in range(19)]
40+
m =[
41+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
42+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
43+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
44+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
45+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
46+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
47+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
48+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
49+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
50+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
51+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
52+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
53+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
54+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
55+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
56+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
57+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
58+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
59+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
60+
]
61+
62+
def check(i, j):
63+
global win
64+
g = [0] * 8
65+
fw = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1))
66+
for index in range(8):
67+
d = fw[index]
68+
next_i = i + d[0]
69+
next_j = j + d[1]
70+
while next_i in range(19) and next_j in range(19) and m[next_i][next_j] == m[i][j]:
71+
g[index] = g[index] + 1
72+
next_i = next_i + d[0]
73+
next_j = next_j + d[1]
74+
75+
for index in range(4):
76+
if g[index] + g[index + 4] + 1 >= 5:
77+
win = True
78+
goto(0, 0)
79+
if yanse == "black":
80+
write('Black Win', font=('Arial', 100, ''), align='center')
81+
else:
82+
write('White Win', font=('Arial', 100, ''), align='center')
83+
break
84+
85+
def play(x, y):
86+
if not win:
87+
global yanse
88+
global gz
89+
color(yanse)
90+
up()
91+
x = round(x/gz)*gz
92+
y = round(y/gz)*gz
93+
i = int(9 - y / gz)
94+
j = int(x / gz + 9)
95+
96+
if i >= 0 and i <= 18 and j >=0 and j<=18:
97+
if m[i][j] == 0:
98+
goto(x, y)
99+
dot(30)
100+
101+
if yanse == "black":
102+
m[i][j] = 1
103+
check(i, j)
104+
yanse="white"
105+
else:
106+
m[i][j] = 2
107+
check(i, j)
108+
yanse="black"
109+
110+
judge.color(yanse)
111+
judge.dot(30)
112+
113+
onscreenclick(play, 1)
114+
done()

code/11/snake.py

Whitespace-only changes.

images/011_snake.png

45.2 KB
Loading

0 commit comments

Comments
 (0)