# 导入所需的模块
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# 绘制窗口
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("black")
# 用户可以自行调整窗口的长度与宽度
wn.setup(width=600, height=600)
wn.tracer(0)
# 蛇头
head = turtle.Turtle()
head.shape("circle")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# 游戏中的食物
food = turtle.Turtle()
food.speed(0)
food.shape('circle')
food.color("green")
food.penup()
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food.goto(x,y)
food2 = turtle.Turtle()
food2.speed(0)
food2.shape('circle')
food2.color("red")
food2.penup()
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food2.goto(x, y)
food3 = turtle.Turtle()
food3.speed(0)
food3.shape('circle')
food3.color("yellow")
food3.penup()
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food3.goto(x, y)
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("得分 : 0 最高得分 : 0", align="center",
font=("candara", 24, "bold"))
# 设定按键方向
def group():
if head.direction != "down":
head.direction = "up"
def godown():
if head.direction != "up":
head.direction = "down"
def goleft():
if head.direction != "right":
head.direction = "left"
def goright():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)
wn.listen()
wn.onkeypress(group, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")
segments = []
n2=0
n1=["red","orange","yellow","green","teal","blue","purple"]
# 游戏主程序
while True:
wn.update()
if head.xcor() > 930 or head.xcor() < -930 or head.ycor() > 500 or head.ycor() < -500:
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food.goto(x, y)
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food2.goto(x, y)
food.color("red")
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if head.distance(food) < 20:
# 添加分段
for ahdskagu in range(10):
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("circle")
if n2==7:
n2=0
new_segment.color(n1[n2])
n2=n2+1# 尾巴颜色
new_segment.penup()
segments.append(new_segment)
delay /= 1.3
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("得分 : {} 最高得分 : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food.goto(x, y)
if head.distance(food3) < 20:
# 添加分段
for ahdskagu in range(10):
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("circle")
if n2==7:
n2=0
new_segment.color(n1[n2])
n2=n2+1# 尾巴颜色
new_segment.penup()
segments.append(new_segment)
delay /= 1.3
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("得分 : {} 最高得分 : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food3.goto(x, y)
if head.distance(food2) < 20:
# 添加分段
for ahdskagu in range(10):
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("circle")
if n2==7:
n2=0
new_segment.color(n1[n2])
n2=n2+1# 尾巴颜色
new_segment.penup()
segments.append(new_segment)
delay /= 1.3
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("得分 : {} 最高得分 : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
x = random.randint(-600, 600)
y = random.randint(-300, 300)
food2.goto(x, y)
# 检查蛇身分段是否与蛇头相撞
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segment.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("得分 : {} 最高得分 : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
time.sleep(delay)
wn.mainloop()
11-24
2770
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交