0
スネークに似た単純なゲームでいくつかのゲームの問題が発生しています。Python - スネークスタイルのゲームでタートルの座標が正しく機能しない
あなたが写真で見ることができるように、ヘビは重なり合っていますが、想定されているように衝突していません。
あなたが見ることができるように自己との衝突は、時間の一部を動作しませんが、それは信頼できるものではありません。
私はこの問題を解決すると考えることができるすべてを試してみました。
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)
あなたはバグを修正できました。ありがとうございました! –