2016-11-22 2 views
-5

これはtkinterとturtleを使ったヘビとはしごのスクリプトです。タートルが動いていない

私の問題は、カウンター(亀)が正方形23を越えて移動しない理由です。

コードは非常に長いですが、重要な領域の近くにコメントを追加しました。あなたは私が何を意味するか見たい場合には亀がそうfloatxcor()としての地位を維持しますので、あなたはそれを実行することができます

import webbrowser 
from tkinter import * 
import random 
import time 
x = Tk() 
totalp1 = 1 
currentp1 = 0 
totalp2 = 1 
currentp2 = 0 






import turtle 
import time 
a = 0 
turtle.speed(0) 
p1=turtle.Turtle() 
p1.color("blue") 
p2=turtle.Turtle() 
turtle.penup() 
turtle.goto(-550,-350) 
turtle.pendown() 
p2.shape("circle") 
turtle.pensize(2) 
p1.shape("circle") 
p1.resizemode("user") 
p1.shapesize(1, 1, 1) 
p1.penup() 
p2.penup() 
p1.hideturtle() 
p2.hideturtle() 

popo =0 



def square(): 
    global popo 
    popo += 1 
    turtle.forward(100) 
    turtle.left(90) 
    turtle.forward(100) 
    turtle.left(90) 
    turtle.forward(100) 
    turtle.left(90) 
    turtle.forward(100) 
    turtle.left(90) 
    turtle.penup() 
    turtle.forward(75) 
    turtle.left(90) 
    turtle.forward(75) 
    turtle.write(str(popo), font=("Comic Sans MS", 10)) 
    turtle.left(180) 
    turtle.forward(75) 
    turtle.left(90) 
    turtle.forward(25) 
    turtle.pendown() 
def forwardrow(): 
    turtle.penup() 
    turtle.forward(200) 
    turtle.pendown() 
    for i in range(7): 
     square() 
    turtle.left(90) 
    turtle.forward(100) 
    turtle.left(90) 
    turtle.forward(100) 
    turtle.right(180) 

def backrow(): 
    for i in range(7): 
     square() 
     turtle.forward(-200) 
    turtle.penup() 
    turtle.forward(-200) 
    turtle.left(-90) 
    turtle.forward(-100) 
    turtle.left(-90) 
    turtle.forward(-100) 
    turtle.right(180) 
    turtle.pendown() 

def row(): 
    for i in range(7): 
     square() 


def grid(): 

     forwardrow() 

     backrow() 

     forwardrow() 

     backrow() 

     forwardrow() 

     backrow() 

     forwardrow() 
     turtle.penup() 
     turtle.goto(-351, -250) 
     turtle.pendown() 
     turtle.pencolor("white") 
     turtle.left(180) 
     turtle.forward(100) 

     turtle.penup() 
     turtle.goto(-351, -50) 
     turtle.pendown() 
     turtle.pencolor("white") 
     turtle.left(180) 
     turtle.forward(-100) 

     turtle.penup() 
     turtle.goto(-351, 150) 
     turtle.pendown() 
     turtle.pencolor("white") 
     turtle.left(180) 
     turtle.forward(100) 
     p1.goto(-300, -275) 
     p2.goto(-300, -325) 
     turtle.pensize(8.76) 
     turtle.color('green') 
     turtle.penup() 
     turtle.goto(175,-25) 
     turtle.write(str('ladder')) 
     turtle.pendown() 
     turtle.goto(75,175) 
     turtle.penup() 
     turtle.goto(275,-225) 
     turtle.write(str('ladder')) 
     turtle.pendown() 
     turtle.goto(-125,-25) 
     turtle.penup() 
     turtle.goto(-225,-325) 
     turtle.write(str('ladder')) 
     turtle.pendown() 
     turtle.goto(-125,75) 
     turtle.color('red') 
     turtle.penup() 
     turtle.goto(175,275) 
     turtle.write(str('snake')) 
     turtle.pendown() 
     turtle.goto(-225,-225) 
     turtle.penup() 
     turtle.goto(-325,75) 
     turtle.write(str('snake')) 
     turtle.pendown() 
     turtle.goto(-225,-125) 
     turtle.penup() 
     turtle.goto(-325,175) 
     turtle.write(str('snake')) 
     turtle.pendown() 
     turtle.goto(75,-25) 
     turtle.penup() 

     p1.showturtle() 
     p2.showturtle() 

#THIS IS FOR PLAYER ONE-------------------------------- 
def p1goto(): 
    global totalp1 
    global currentp1 
    while currentp1!=0: 
     print (currentp1) 
     currentp1-=1 
     if (p1.xcor() == 200) and (p1.ycor() == 25): 
      p1.forward(100) 
     if (p1.xcor() == -300) and (p1.ycor() == -275): 
      p1.setheading(0) 
      p1.forward(100) 
     elif (p1.xcor() == 300) and (p1.ycor() in [-275, -75, 125, 325]): 
      p1.setheading(90) 
      p1.forward(100) 
     elif (p1.xcor() == -300) and (p1.ycor() in [-175, 25, 225]): 
      p1.setheading(90) 
      p1.forward(100) 
     elif (p1.ycor() in [-275, -75, 125, 325]) and (p1.xcor() != 300): 
      p1.setheading(0) 
      p1.forward(100) 
     elif (p1.ycor() in [-175, 25, 175, 225, 75]) and (p1.xcor() != -300): 
      print("I Ran") 
      p1.setheading(180) 
      p1.forward(100) 
    print (p1.pos()) 

def p1check(): 
    global totalp1 
    if totalp1 == 2: 
     totalp1 = 31 

    elif totalp1 == 8: 
     totalp1 = 26 
     p1.goto() 
    elif totalp1 == 23: 
     totalp1 = 38 
    elif totalp1 == 48: 
     totalp1 = 13 
    elif totalp1 == 42: 
     totalp1 = 24 
    elif totalp1 == 29: 
     totalp1 = 16 

#THIS IS FOR PLAYER TWO-------------------------------- 
def p2goto(): 
    global totalp2 
    global currentp2 
    while currentp2!=0: 
     currentp2-=1 
     if (p2.xcor() == -300) and (p2.ycor() == -275): 
      p2.setheading(0) 
      p2.forward(100) 
      print("yep1") 
     elif (p2.xcor() == 300) and (p2.ycor() in [-325, -125, 75, 275]): 
      p2.setheading(90) 
      p2.forward(100) 
      print("yep2") 
     elif (p2.xcor() == -300) and (p2.ycor() in [-225, -25, -175]): 
      p2.setheading(90) 
      p2.forward(100) 
      print("yep3") 
     elif (p2.ycor() in [-325, -125, 75, 275, 125]) and (p2.xcor() != 300): 
      p2.setheading(0) 
      p2.forward(100) 
      print("yep4") 
     elif (p2.ycor() in [-225, -25, 175]) and (p2.xcor() != -300): 
      print("yep5") 
      p2.setheading(180) 
      p2.forward(100) 
    print (p2.pos()) 





def clicked(): 
    nam1.pack_forget() 
    name1.pack_forget() 
    nam2.pack_forget() 
    name2.pack_forget() 
    btn.pack_forget() 
    grid() 
    rollingp1() 

nam1 = Label(x, text = "  Player 1 Name  ", bg = "#6DE248") 
name1 = Entry(x, bg = "#8486A4") 
nam2 = Label(x, text = "  Player 2 Name  ", bg = "#487EE2") 
name2 = Entry(x, bg = "#8486A4") 
btn = Button(x, text = "-->", command=clicked, width= 10, height = 3, bg = "#E24C48") 

nam1.pack() 

name1.pack() 

nam2.pack() 

name2.pack() 

btn.pack() 

x.resizable(0,0) 

def rollingp1(): 
    global totalp2 
    global totalp1 
    global currentp1 
    a=random.randint(1,6) 
    currentp1 = a 
    totalp1 += currentp1 

    def show(): 
     global totalp1 
     global totalp2 
     w = str(totalp1) 
     print(name1.get() + " is on the " + w + "th square") 
     p1goto() 
     if (totalp2 < 49) and (totalp1 < 49): 
      close() 
      rollingp2() 
     elif totalp1 >= 49: 
      p1win() 
     elif totalp2 >= 49: 
      p2win() 

    def close(): 
     playerroll.pack_forget() 
     message.pack_forget() 
     roll.pack_forget() 


    playerroll = Label(x, text = name1.get() + " rolled : " + str(a)) 
    message = Label(x, text = name1.get() + "'s Turn", font =("Comic Sans MS", 17, "bold")) 
    message.pack() 
    roll = Button(x, text="""Click 
    To 
    Roll...""", width = 10, height = 3, font=("Comic Sans MS", 22, "bold"), bg="#E24C48", command=show) 
    roll.pack() 
    x.resizable(0,0) 
    x.title("Dice") 
    def p1win(): 
     playerroll.pack_forget() 
     message.pack_forget() 
     roll.pack_forget() 
     webbrowser.open("https://www.youtube.com/watch?v=3GwjfUFyY6M&feature=youtu.be&t=42") 

    def p2win(): 
     playerroll2.pack_forget() 
     message2.pack_forget() 
     roll2.pack_forget() 
     webbrowser.open("https://www.youtube.com/watch?v=3GwjfUFyY6M&feature=youtu.be&t=42") 




def rollingp2(): 
    global totalp1 
    global totalp2 
    global currentp2 
    a2=random.randint(1,6) 
    currentp2 = a2 
    totalp2 += currentp2 

    def show2(): 
     global totalp1 
     global totalp2 
     print(name2.get() +" is on the " + str(totalp2) +"th square") 
     p2goto() 
     if (totalp2 < 49) and (totalp1 < 49): 
      print("it ran") 
      close2() 
      rollingp1() 
     elif totalp1 >= 49: 
      p1win() 
     elif totalp2 >= 49: 
      p2win() 
    def close2(): 
     playerroll2.pack_forget() 
     message2.pack_forget() 
     roll2.pack_forget() 

    playerroll2 = Label(x, text = name2.get() + " rolled : " + str(a2) + """ 
    They are on the """ + str(totalp2)) 
    message2 = Label(x, text =name2.get() + "'s Turn", font =("Comic Sans MS", 17, "bold")) 

    message2.pack() 
    roll2 = Button(x, text="""Click 
    To 
    Roll...""", width = 10, height = 3, font=("Comic Sans MS", 22, "bold"), bg="#E24C48", command=show2) 
    roll2.pack() 
    x.resizable(0,0) 
    def p1win(): 
     playerroll.pack_forget() 
     message.pack_forget() 
     roll.pack_forget() 

     webbrowser.open("https://www.youtube.com/watch?v=3GwjfUFyY6M&feature=youtu.be&t=42") 


    def p2win(): 
     playerroll2.pack_forget() 
     message2.pack_forget() 
     roll2.pack_forget() 

     webbrowser.open("https://www.youtube.com/watch?v=3GwjfUFyY6M&feature=youtu.be&t=42") 





turtle.exitonclick() 
x.mainloop() 
+0

[デバッグヘルプ](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Prune

+0

ようこそStackOverflow。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 *最小限のコードを投稿して問題を正確に記述するまで、効果的にあなたを助けることはできません。 – Prune

+0

@Pruneニースリンク。私はそれをブックマークする必要があります。私は "rubber duck debugging"というフレーズが好きだった。ありがとう。 –

答えて

0

問題があり、ycor()float()値とprint()ショーは値ieを丸め与えます。 25.0ですが、実際の値は25.000000000000063948846218409016728401184082031250000000

です。int()という値を使用する必要があります。

x = int(p1.xcor()) 
    y = int(p1.ycor()) 

と方向を変更するxyを使用しています。

または方向を変更するにはセル番号を使用します。

+0

ありがとう、それはプレイヤー1では動作しますが、p2では動作しません。 –

+0

'print()'を使って変数の値を見て問題を見つけます。 'print("%。54f "%p2.ycor())'。 'p2.ycor()'は 'p1.ycor()'と少し異なり、 '25.000 ...'ではなく '24.999 ...'があり、 '24.99 'に' int(24.9999) 'が付いています。 'int()'の代わりに 'round()'を使います(または方向を変えるにはセル番号を使います)。 – furas

関連する問題