我昨天刚刚开始使用龟图。我决定用乌龟图片做一个小蛇游戏。但是后来,我遇到了一些问题。 :( 问题是,每当我的蛇吃食物时我都无法升级游戏:(我的蛇的动作与典型的蛇游戏有点不一样......所以我在这里要求建议如何解决这些问题。所以请把你的想法借给我:D
好吧,这是我很长的代码..from turtle import *
import random
title("Snake Game") #title of the game
setup(555,555) #sets the screensize into 555x555 px
bgcolor("orange") #background color
#pressed key functions
def up():
penup()
pendown()
head(90)
def right():
penup()
pendown()
head(0)
def left():
penup()
pendown()
head(180)
def down():
penup()
pendown()
head(270)
#draw dot
def dotx():
pen2.penup()
pen2.goto(x1,y1)
pen2.pendown()
pen2.dot(20,"green")
#heading of the snake
def head(x):
for i in range(9999999):
for ii in range(20):
seth(x)
fd(2)
if xcor()>=250.0 or ycor()>=250.0 or xcor()<=-250.0 or ycor()<=-250.0:
clear()
pen2.clear()
pen4.write("GAME OVER")
break
elif (xcor() in x2) and (ycor() in y2):
pen2.clear()
pen4.write("EATEN",False,'center',font=('Arial',15,'normal'))
if xcor()>=250.0 or ycor()>=250.0 or xcor()<=-250.0 or ycor()<=-250.0:
clear()
pen2.clear()
pen4.write("GAME OVER")
break
clear()
color("white")
pensize(5) #pensize
shape('turtle')
#hideturtle()
delay(2) #delay of animation
speed(10) #speed of animation
pen2=Pen() #dots
pen2.hideturtle()
pen4=Pen()
pen4.hideturtle()
pen4.color("white")
#border
pen3=Pen()
pen3.color("white")
pen3.pensize(3)
pen3.hideturtle()
pen3.speed(10)
pen3.penup()
pen3.goto(-250,-250)
pen3.pendown()
for p3 in range(4):
pen3.fd(500)
pen3.left(90)
#dots coordinates
x1=random.randint(-225,225)
y1=random.randint(-225,225)
x2=list(range(x1-6,x1+6))
y2=list(range(y1-6,y1+6))
dotx() #call dots
#controls
onkey(up,"Up")
onkey(right,"Right")
onkey(left,"Left")
onkey(down,"Down")
listen()
答案 0 :(得分:0)
为了让蛇成长,我发现创建一个列表很容易,并使用pop和insert函数来获得所有蛇位置的常量列表。然后,您可以使用海龟邮票方法标记所有这些列表位置,以便它显示在所有位置。这是我前一段时间制作的乌龟蛇游戏的一个例子,所以你可以看到如何使用这些概念
pos.insert(0,[round((snake.xcor())),round((snake.ycor()))]) #round x turns the float value of position into integer
pos.pop(-1)
for x in range(1, len(pos)):
if pos[x] == pos[0]:
sys.exit()
elif pos[x]!= pos[0]:
pass;
#pop removes last element of list, used to update position
else: #insert inserts an element into list at position
pos.insert(0,[round((snake.xcor())),round((snake.ycor()))])