这是一个使用tkinter和turtle的蛇和梯子脚本。
我的问题是,计数器(乌龟)没有超过23号方格,我不知道为什么。
代码很长但我在重要区域附近添加了评论 如果你想看看我的意思,你可以运行它
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 :(得分:0)
问题是因为乌龟将位置保持为float
所以xcor()
,ycor()
给出float()
值,print()
显示舍入值ie。 25.0
但实际值为25.000000000000063948846218409016728401184082031250000000
您必须使用int()
值,即。
x = int(p1.xcor())
y = int(p1.ycor())
并使用x
,y
来改变方向。
或使用单元格编号来改变方向。