2017-09-13 10 views
1

現在、亀のグラフィックを使用してPythonで蛇のようなゲームを作成しようとしています。前のターン。順序が合っていないコードを実行しているように見えますが、何が起こっているのかは分かりません。亀のグラフィックで奇数の順序で実行されるコード

全体のコードは

import turtle 
import random 
import math 

x = 400 
y = 400 
global speed 
global points 
points = 0 
speed = 2 
posList = [''] 

# turns your character left 
def left(): 
    global speed 
    global posList 
    key = True 
    char.fd(speed) 
    char.left(90) 
    char.fd(speed) 

# turns your character left 
def right(): 
    global speed 
    global posList 
    key = True 
    char.fd(speed) 
    char.right(90) 
    char.fd(speed) 

# adds one box to the point counter 
def point(): 
    global points 
    points += 1 
    wall.speed(0) 
    wall.pendown() 
    wall.forward(50) 
    wall.seth(90) 
    wall.forward(10) 
    wall.seth(180) 
    wall.forward(50) 
    wall.seth(270) 
    wall.forward(10) 
    wall.penup() 
    wall.seth(90) 
    wall.forward(12) 
    wall.seth(0) 
    dot.setx(random.randint(-200,200)) 
    dot.sety(random.randint(-200,200)) 
    print(points) 

# checks if curren posisition is anywhere you have ever been 
def checkBracktrack(pos, poslist): 
    found = False 
    for thing in posList: 
     if thing == pos: 
      found=True 
    return found 

# creates the box that the game occurs in 
turtle.colormode(255) 
screen = turtle.Screen() 
dot = turtle.Turtle() 
dot.penup() 
dot.speed(0) 
dot.shape('turtle') 
dot.setx(random.randint(-200,200)) 
dot.sety(random.randint(-200,200)) 
wall = turtle.Turtle() 
wall.speed(0) 
wall.penup() 
wall.goto(x/2,y/2) 
wall.pendown() 
wall.seth(180) 
wall.forward(400) 
wall.seth(270) 
wall.forward(400) 
wall.seth(0) 
wall.forward(400) 
wall.seth(90) 
wall.forward(400) 
wall.seth(270) 
wall.forward(400) 
wall.seth(0) 
wall.penup() 
wall.forward(100) 
char = turtle.Turtle() 
x = 0 
y = 0 

# updates the position of the player turtle 
while True: 
    screen.onkey(left,"a") 
    screen.onkey(right,"d") 
    char.hideturtle() 
    char.forward(speed) 
    char.speed(0) 
    turtle.listen(xdummy=None, ydummy=None) 
    print(char.pos()) 
    print(posList[(len(posList)-1)]) 

    # checks if current position is the same as any position it has ever been in !this is the bit that is having problems! 
    if checkBracktrack(char.pos(),posList): 
     speed = 0 
     break 

    # checks if it is close enough to a point marker to 
    if char.ycor() in range(dot.ycor()-10,dot.ycor()+10) and char.xcor() in range(dot.xcor()-10,dot.xcor()+10): 
     point() 

    # checks if in the box 
    if char.ycor() not in range(-200,200) or char.xcor() not in range(-200,200): 
     speed = 0 

    # adds current location to the list 
    posList.append(char.pos()) 
    char.fd(speed) 


print('you travelled',len(posList),'pixels') 
print('collided with yourself') 
print(char.pos()) 
print(posList) 
name = input('quit') 
screen.mainloop() 

答えて

1

を下回っているあなたのコードを持つ多くの小さな問題があります:あなたはglobalを使用する際について再読する必要があります。 checkBracktrack()関数は引数としてposlistを取りますが、代わりにグローバルposListで動作します(大文字の誤字)。 fd()コールが余分にあり、1より大きいspeedがあるため、ピクセル移動距離計算が正しくありません。あなたの近接性テストは、タートルの.distance()メソッドを使用して大幅に単純化することができます。ゲームボード上のポイントを表示するためのコードはまったく動作しません。ループごとにonkey()を繰り返し呼び出すときは、各キーごとに1回だけ呼び出す必要があります。 checkBracktrack()関数に不要なループがあります。

私がコードで持っている最大の問題は、イベントベースのコードでは発生しないwhile True:です。私が書き換えられ、そして上記の問題だけでなく、他の人への対処、以下のコードを簡素化しました:

from turtle import Turtle, Screen 
from random import randint 

FONT = ('Arial', 24, 'normal') 
WIDTH, HEIGHT = 400, 400 
SPEED = 1 

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(points, font=FONT) 

    dot.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2)) 

def checkBracktrack(pos, poslist): 
    """ checks if current posiition is anywhere you have ever been """ 

    return pos in poslist 

def move_char(): 
    """ updates the position of the player turtle """ 

    over = 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 

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

    # checks if it is close enough to a point marker 
    if char.distance(dot) < 20: 
     point() 

    screen.ontimer(move_char, 10) 

points = 0 
posList = [] 

# 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) 
wall.speed('fastest') 
wall.penup() 
wall.goto(WIDTH/2, HEIGHT/2) 
wall.pendown() 

for _ in range(4): 
    wall.right(90) 
    wall.forward(400) 

wall.penup() 
wall.forward(100) 
wall.write("0", font=FONT) 

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

move_char() 

screen.mainloop() 

私の信念は、あなたの元の質問を促した問題は、コードを再加工する過程で固定しまったということです。

enter image description here

+0

あなたは私が持っていた主要なバグを修正するために管理しますが、あなたのコードの衝突検出は、私のテストでは信頼性が低いように思われます。これまでのあなたの助けてくれてありがとう –

関連する問題