Python - Turtle协调在蛇式游戏中无法正常工作

时间:2017-09-19 00:17:00

标签: python debugging turtle-graphics

我正在使用类似于蛇的简单游戏来解决游戏问题。

正如你在图片中看到的那样,蛇本身重叠但没有像预期的那样发生碰撞。

enter image description here

与你自己的碰撞确实可以在某些时候发挥作用,但是它不可靠。

enter image description here

我已经尝试了一切我能想到的解决方法。

from turtle import Turtle, Screen
from random import randint

FONT = ('Arial', 24, 'normal')
WIDTH, HEIGHT = 400, 400
SPEED = 1
points = 0
posList = []
def left():
##    turns your character left
    char.left(90)

def right():
##    turns your character right
    char.right(90)

def point():
##    adds one box to the point counter
    global points

    points += 1

    wall.undo()
    wall.write(str(points) + ' score', font=FONT)

    dot.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2))
    dot.seth(randint(0,360))
def checkBracktrack(pos, poslist):
##    checks if current posiition is anywhere you have ever been
    return pos in poslist

def moveChar():
##    updates the position of the player turtle
    over = False
    change = False
    char.forward(SPEED)

    # checks if current position is the same as any position it has ever been at
    if checkBracktrack(char.pos(), posList):
        over = True

    # checks if in the box
    elif not (-200 <= char.ycor() <= 200 and -200 <= char.xcor() <= 200):
        over = True

    if over:
        print('you travelled', len(posList), 'pixels')
        return
    tempPos = char.pos()

    # adds current location to the list
    posList.append(tempPos)

    # checks if it is close enough to a point marker
    if char.distance(dot) < 20:
        point()
    # calls the moveChar function again
    screen.ontimer(moveChar, 1)

# creates the box in which the game occurs
screen = Screen()
screen.onkey(left, "a")
screen.onkey(right, "d")
screen.listen()

dot = Turtle('turtle')
dot.speed('fastest')
dot.penup()
dot.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2))

wall = Turtle(visible=False)
score = Turtle(visible=False)
wall.speed('fastest')
wall.penup()
wall.goto(WIDTH/2, HEIGHT/2)
wall.pendown()

for _ in range(4):
    wall.right(90)
    wall.forward(400)
try:
    with open('score.txt','r') as file:
        highScore = int(file.read())
except:
    with open('score.txt','w') as file:
        file.write('0')
    highScore = 0
wall.penup()
wall.forward(50)
wall.write("0" + ' score', font=FONT)
score.penup()
score.setpos(wall.pos())
score.seth(270)
score.fd(30)
score.write(str(highScore) + ' high score', font=FONT)
score.right(180)
score.fd(30)

char = Turtle(visible=False)
char.speed('fastest')

moveChar()

screen.mainloop()
if points > highScore:
    with open('score.txt','w') as file:
        file.write(str(points))
        print('new high score of ' + str(score) + ' saved')
print(posList)

1 个答案:

答案 0 :(得分:1)

我无法重现您的错误,但让我们尝试一下。由于乌龟坐标系是浮点数,让我们假设小错误正在进入你的位置的小数部分,阻止了这个测试的起作用:

return pos in poslist

所以,让我们将该行更改为:

return (int(pos[0]), int(pos[1])) in poslist

并更改moveChar()中的匹配行:

# adds current location to the list
posList.append((int(tempPos[0]), int(tempPos[1])))

现在我们只存储和测试位置的整数部分,因此任何小数分量都将被忽略。试试这个,让我们知道会发生什么。