Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让我的海龟为基础的游戏退出按钮的工作_Python_Turtle Graphics - Fatal编程技术网

Python 让我的海龟为基础的游戏退出按钮的工作

Python 让我的海龟为基础的游戏退出按钮的工作,python,turtle-graphics,Python,Turtle Graphics,我想在代码中创建一个退出按钮,但它不起作用。我更喜欢使用exit(),但如果exit()无法使用,则可以使用其他方法使其工作。另外,请向我解释一下你是如何修复它的——提前谢谢!我的代码: import turtle import math import random screen = turtle.Screen() width = 780 height = 630 screen.setup(width, height) screen.bgcolor("#202020") screen.titl

我想在代码中创建一个退出按钮,但它不起作用。我更喜欢使用
exit()
,但如果
exit()
无法使用,则可以使用其他方法使其工作。另外,请向我解释一下你是如何修复它的——提前谢谢!我的代码:

import turtle
import math
import random

screen = turtle.Screen()
width = 780
height = 630
screen.setup(width, height)
screen.bgcolor("#202020")
screen.title("SpaceWar")

border = turtle.Turtle()
border.color("white")
border.speed(0)
border.penup()
border.setposition(-375, -300)
border.pendown()
border.pensize(3)
for side in range(2):
    border.forward(750)
    border.left(90)
    border.forward(600)
    border.left(90)
border.hideturtle()

score = 0
text1 = turtle.Turtle()
text1.color("white")
text1.speed(0)
text1.penup()
text1.hideturtle()
text1.setposition(-30, 200)
text1.write("Score: ", False, font=("Arial", 50, "normal"), align="center")

text2 = turtle.Turtle()
text2.color("white")
text2.speed(0)
text2.hideturtle()

text3 = turtle.Turtle()
text3.color("red")
text3.speed(0)
text3.hideturtle()

text4 = turtle.Turtle()
text4.color("blue")
text4.speed(0)
text4.hideturtle()

button = turtle.Turtle()
button.color("white")
button.speed(0)
button.pensize(4)
button.penup()
button.hideturtle()

button_text = turtle.Turtle()
button_text.color("white")
button_text.speed(0)
button_text.penup()
button_text.hideturtle()

booster1 = turtle.Turtle()
booster1.shape("circle")
booster1.color("#1c77d9")
booster1.penup()
booster1.speed(0)
booster1.setposition(random.randint(-250, 250), random.randint(-250, 250))

booster2 = turtle.Turtle()
booster2.shape("circle")
booster2.color("#d6281c")
booster2.penup()
booster2.speed(0)
booster2.setposition(random.randint(-250, 250), random.randint(-250, 250))

decreaser = turtle.Turtle()
decreaser.shape("circle")
decreaser.color("green")
decreaser.penup()
decreaser.speed(0)
decreaser.setposition(random.randint(-250, 250), random.randint(-250, 250))

speed1 = 3
speed2 = 3

player1 = turtle.Turtle()
player1.shape("triangle")
player1.color("#1c77d9")
player1.shapesize(1.5)
player1.speed(0)
player1.penup()
player1.goto(-330, 0)

def forward():
    player1.fd(speed1)
    player2.fd(speed2)

def turn_right():
    player1.rt(45)

def turn_left():
    player1.lt(45)

turtle.listen()
turtle.onkeypress(turn_left, "a")
turtle.onkeypress(turn_right, "d")

player2 = turtle.Turtle()
player2.shape("triangle")
player2.color("#d6281c")
player2.shapesize(1.5)
player2.speed(0)
player2.penup()
player2.setheading(180)
player2.goto(330, 0)

def turn_right2():
    player2.rt(45)

def turn_left2():
    player2.lt(45)

def exit_button():
    for i in range(2):
        button.forward(300)
        button.left(90)
        button.forward(100)
        button.left(90)

def button_click(x, y):
    if x > -150 and x < 150 and y > -100 and y < 0:
        exit()

turtle.listen()
turtle.onkey(turn_left2, "Left")
turtle.onkey(turn_right2, "Right")

while True:
    forward()

    a = math.sqrt(math.pow(player1.xcor()-player2.xcor(), 2) + math.pow(player1.ycor()-player2.ycor(), 2))
    b = math.sqrt(math.pow(player1.xcor()-booster1.xcor(), 2) + math.pow(player1.ycor()-booster1.ycor(), 2))
    c = math.sqrt(math.pow(player2.xcor()-booster2.xcor(), 2) + math.pow(player2.ycor()-booster2.ycor(), 2))
    d = math.sqrt(math.pow(player1.xcor()-decreaser.xcor(), 2) + math.pow(player1.ycor()-decreaser.ycor(), 2))
    e = math.sqrt(math.pow(player2.xcor()-decreaser.xcor(), 2) + math.pow(player2.ycor()-decreaser.ycor(), 2))

    if a < 20:
        player1.setposition(-330, 0)
        player1.setheading(0)
        player2.setposition(330, 0)
        player2.setheading(180)
        score += 1
        text2.undo()
        text2.hideturtle()
        text2.penup()
        text2.setposition(70, 200)
        scorestring = "%s" %score
        text2.write(scorestring, False, font=("Arial", 50, "normal"))

    if b < 20:
        speed1 += 1
        booster1.setposition(random.randint(-250, 250), random.randint(-250, 250))

    if c < 20:
        speed2 += 1
        booster2.setposition(random.randint(-250, 250), random.randint(-250, 250))

    if d < 20:
        speed1 -= 1
        decreaser.setposition(random.randint(-250, 250), random.randint(-250, 250))

    if e < 20:
        speed2 -= 1
        decreaser.setposition(random.randint(-250, 250), random.randint(-250, 250))

    if speed1 == 7:
        speed1 -= 1

    if speed2 == 7:
        speed2 -= 1

    if player1.xcor() > 375 or player1.xcor() < -375:
        score += 1
        player1.setposition(-330, 0)
        player1.setheading(0)
        text2.undo()
        text2.hideturtle()
        text2.penup()
        text2.setposition(70, 200)
        scorestring = "%s" %score
        text2.write(scorestring, False, font=("Arial", 50, "normal"))

    if player1.ycor() > 300 or player1.ycor() < -300:
        score += 1
        player1.setposition(-330, 0)
        player1.setheading(0)
        text2.undo()
        text2.hideturtle()
        text2.penup()
        text2.setposition(70, 200)
        scorestring = "%s" %score
        text2.write(scorestring, False, font=("Arial", 50, "normal"))

    if player2.xcor() > 375 or player2.xcor() < -375:
        player2.right(180)

    if player2.ycor() > 300 or player2.ycor() < -300:
        player2.right(180)

    if score == 2:
        text3.write("Red Won!", False, font=("Arial", 70, "normal"), align="center")
        button_text.goto(-85, -100)
        button_text.write("Exit", False, font=("Arial", 70, "normal"))
        button.goto(-150, -100)
        button.pendown()

        booster1.hideturtle()
        booster2.hideturtle()
        decreaser.hideturtle()
        player1.hideturtle()
        player2.hideturtle()

        exit_button()

        turtle.listen()
        turtle.onscreenclick(button_click, 1)
导入海龟
输入数学
随机输入
screen=turtle.screen()
宽度=780
高度=630
屏幕设置(宽度、高度)
屏幕颜色(“202020”)
屏幕标题(“太空战”)
border=turtle.turtle()
边框颜色(“白色”)
边界。速度(0)
border.penup()
边框设置位置(-375,-300)
border.pendown()
边界。养老金(3)
对于范围内的侧面(2):
边界向前(750)
边框。左(90)
边界向前(600)
边框。左(90)
border.hideturtle()
分数=0
text1=海龟。海龟()
文本1.颜色(“白色”)
text1.速度(0)
text1.penup()
text1.hideturtle()
text1.设置位置(-30200)
text1.书写(“分数:”,False,font=(“Arial”,50,“正常”),align=“中心”)
text2=海龟。海龟()
文本2.颜色(“白色”)
text2.速度(0)
text2.hideturtle()
text3=海龟。海龟()
文本3.颜色(“红色”)
text3.速度(0)
text3.hideturtle()
text4=海龟。海龟()
文本4.颜色(“蓝色”)
text4.速度(0)
text4.hideturtle()
button=turtle.turtle()
按钮颜色(“白色”)
按钮。速度(0)
按钮。养老金(4)
button.penup()
button.hideturtle()
button_text=turtle.turtle()
按钮\文本颜色(“白色”)
按钮\文本速度(0)
按钮\u text.penup()
按钮\u text.hideturtle()
booster1=海龟。海龟()
助推器1.形状(“圆”)
助推器1.颜色(“1c77d9”)
booster1.penup()
助推器1.速度(0)
booster1.setposition(random.randint(-250250),random.randint(-250250))
booster2=海龟。海龟()
助推器2.形状(“圆”)
助推器2.颜色(“d6281c”)
booster2.penup()
助推器2.速度(0)
booster2.setposition(random.randint(-250250),random.randint(-250250))
decreaser=海龟。海龟()
减量器形状(“圆”)
减量器颜色(“绿色”)
减量器
减速器速度(0)
递减器设置位置(random.randint(-250250),random.randint(-250250))
速度1=3
速度2=3
player1=海龟。海龟()
player1.形状(“三角形”)
播放器1.颜色(“1c77d9”)
播放器1.形状大小(1.5)
播放器1.速度(0)
player1.penup()
玩家1。转到(-330,0)
def forward():
player1.fd(速度1)
player2.fd(速度2)
def右转()
播放者1.rt(45)
def左转()
播放者1.lt(45)
乌龟,听着
海龟。按键盘(向左拐,“a”)
海龟。按键盘(右转“d”)
player2=海龟。海龟()
player2.形状(“三角形”)
播放器2.颜色(“d6281c”)
播放器2.形状大小(1.5)
播放机2.速度(0)
player2.penup()
播放者2.设置标题(180)
玩家2。转到(330,0)
def右转2()
播放者2.rt(45)
def左转2()
播放器2.lt(45)
def exit_按钮():
对于范围(2)中的i:
按钮。前进(300)
按钮。左(90)
按钮。前进(100)
按钮。左(90)
def按钮单击(x,y):
如果x>-150和x<150和y>-100和y<0:
退出()
乌龟,听着
乌龟。上键(左转2,“左”)
乌龟。上键(右转2,“右”)
尽管如此:
前进()
a=math.sqrt(math.pow(player1.xcor()-player2.xcor(),2)+math.pow(player1.ycor()-player2.ycor(),2))
b=math.sqrt(math.pow(player1.xcor()-booster1.xcor(),2)+math.pow(player1.ycor()-booster1.ycor(),2))
c=math.sqrt(math.pow(player2.xcor()-booster2.xcor(),2)+math.pow(player2.ycor()-booster2.ycor(),2))
d=math.sqrt(math.pow(player1.xcor()-decreaser.xcor(),2)+math.pow(player1.ycor()-decreaser.ycor(),2))
e=math.sqrt(math.pow(player2.xcor()-decreaser.xcor(),2)+math.pow(player2.ycor()-decreaser.ycor(),2))
如果a<20:
播放器1.设置位置(-330,0)
播放器1.设置标题(0)
播放器2.设置位置(330,0)
播放者2.设置标题(180)
分数+=1
text2.undo()
text2.hideturtle()
text2.penup()
text2.设置位置(70200)
scorestring=“%s”%score
text2.write(scorestring,False,字体=(“Arial”,50,“normal”))
如果b<20:
速度1+=1
booster1.setposition(random.randint(-250250),random.randint(-250250))
如果c<20:
速度2+=1
booster2.setposition(random.randint(-250250),random.randint(-250250))
如果d<20:
速度1-=1
递减器设置位置(random.randint(-250250),random.randint(-250250))
如果e<20:
速度2-=1
递减器设置位置(random.randint(-250250),random.randint(-250250))
如果速度1==7:
速度1-=1
如果速度2==7:
速度2-=1
如果player1.xcor()大于375或player1.xcor()小于-375:
分数+=1
播放器1.设置位置(-330,0)
播放器1.设置标题(0)
text2.undo()
text2.hideturtle()
text2.penup()
text2.设置位置(70200)
scorestring=“%s”%score
text2.write(scorestring,False,字体=(“Arial”,50,“normal”))
如果player1.ycor()大于300或player1.ycor()小于-300:
分数+=1
播放器1.设置位置(-330,0)
播放器1.设置标题(0)
text2.undo()
text2.hideturtle()
text2.penup()
text2.设置位置(70200)
scorestring=“%s”%score
text2.write(scorestring,False,字体=(“Arial”,50,“normal”))
如果player2.xcor()大于375或player2.xcor()小于-375:
玩家2.右(180)
如果player2.ycor()大于300或player2.ycor()小于-300:
玩家2.右(180)
如果得分=2:
text3.write(“Red-Won!”,False,font=(“Arial”,70,“normal”),align=“center”)
按钮\文本转到(-85,-100)
按钮_text.write(“退出”,错误,字体=(“Arial”,70,“正常”))
按钮。转到(-150,-100)
button.pendown()
booster1.hideturtle()
booster2.hideturtle()
减少者。隐藏者()
player1.hideturtle()
player2.hideturtle()
退出按钮()
乌龟,听着
海龟。屏幕上单击(按钮\单击,1)

问题似乎在于,在设置exi后,您没有将
循环中中断出来,而设置为True:
循环
from turtle import Screen, Turtle
from math import sqrt
from random import randint

WIDTH, HEIGHT = 780, 630

SMALL_FONT = ('Arial', 25, 'normal')
LARGE_FONT_SIZE = 70
LARGE_FONT = ('Arial', LARGE_FONT_SIZE, 'normal')

def forward():
    player1.fd(speed1)
    player2.fd(speed2)

def turn_right():
    player1.rt(45)

def turn_left():
    player1.lt(45)

def turn_right2():
    player2.rt(45)

def turn_left2():
    player2.lt(45)

def button_click(x, y):
    if -150 < x < 150 and -100 < y < 0:
        exit()

def exit_button():
    button.begin_fill()

    for _ in range(2):
        button.forward(300)
        button.left(90)
        button.forward(100)
        button.left(90)

    button.end_fill()

    button_text.write("Exit", align='center', font=LARGE_FONT)

    screen.onscreenclick(button_click)

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.bgcolor('#202020')
screen.title("SpaceWar")

border = Turtle()
border.hideturtle()
border.color('white')
border.speed('fastest')
border.pensize(3)

border.penup()
border.setposition(15 - WIDTH/2 - 4, 15 - HEIGHT/2 + 4)  # 4 = adjustment for "chrome"
border.pendown()

for side in range(2):
    border.forward(WIDTH - 30)
    border.left(90)
    border.forward(HEIGHT - 30)
    border.left(90)

score = 0

text2 = Turtle()
text2.hideturtle()
text2.color('white')
text2.penup()
text2.setposition(70, 200)
scorestring = "Score: %s" % score
text2.write(scorestring, align='center', font=SMALL_FONT)

text3 = Turtle()
text3.hideturtle()
text3.color('red')

button = Turtle()
button.hideturtle()
button.color('white')
button.speed('fastest')
button.pensize(4)
button.penup()
button.goto(-150, -100)

button_text = Turtle()
button_text.hideturtle()
button_text.penup()
button_text.goto(0, -50 + -LARGE_FONT_SIZE/2)

booster1 = Turtle()
booster1.shape('circle')
booster1.color('#1c77d9')
booster1.speed('fastest')
booster1.penup()
booster1.setposition(randint(-250, 250), randint(-250, 250))

booster2 = Turtle()
booster2.shape('circle')
booster2.color('#d6281c')
booster2.speed('fastest')
booster2.penup()
booster2.setposition(randint(-250, 250), randint(-250, 250))

decreaser = Turtle()
decreaser.shape('circle')
decreaser.color('green')
decreaser.speed('fastest')
decreaser.penup()
decreaser.setposition(randint(-250, 250), randint(-250, 250))

speed1 = 3
speed2 = 3

player1 = Turtle()
player1.shape('triangle')
player1.color('#1c77d9')
player1.shapesize(1.5)
player1.speed('fastest')
player1.penup()
player1.setx(-330)

player2 = Turtle()
player2.shape('triangle')
player2.color('#d6281c')
player2.shapesize(1.5)
player2.speed('fastest')
player2.penup()
player2.setheading(180)
player2.setx(330)

screen.onkeypress(turn_left, 'a')
screen.onkeypress(turn_right, 'd')
screen.onkey(turn_left2, 'Left')
screen.onkey(turn_right2, 'Right')
screen.listen()

while True:
    forward()

    a = sqrt(pow(player1.xcor() - player2.xcor(), 2) + pow(player1.ycor() - player2.ycor(), 2))
    b = sqrt(pow(player1.xcor() - booster1.xcor(), 2) + pow(player1.ycor() - booster1.ycor(), 2))
    c = sqrt(pow(player2.xcor() - booster2.xcor(), 2) + pow(player2.ycor() - booster2.ycor(), 2))
    d = sqrt(pow(player1.xcor() - decreaser.xcor(), 2) + pow(player1.ycor() - decreaser.ycor(), 2))
    e = sqrt(pow(player2.xcor() - decreaser.xcor(), 2) + pow(player2.ycor() - decreaser.ycor(), 2))

    if a < 20:
        player1.setx(-330)
        player1.setheading(0)
        player2.setx(330)
        player2.setheading(180)
        score += 1
        text2.undo()
        scorestring = "Score: %s" % score
        text2.write(scorestring, align='center', font=SMALL_FONT)

    if b < 20:
        speed1 += 1
        booster1.setposition(randint(-250, 250), randint(-250, 250))

    if c < 20:
        speed2 += 1
        booster2.setposition(randint(-250, 250), randint(-250, 250))

    if d < 20:
        speed1 -= 1
        decreaser.setposition(randint(-250, 250), randint(-250, 250))

    if e < 20:
        speed2 -= 1
        decreaser.setposition(randint(-250, 250), randint(-250, 250))

    if speed1 == 7:
        speed1 -= 1

    if speed2 == 7:
        speed2 -= 1

    if not -375 <= player1.xcor() <= 375:
        score += 1
        player1.setx(-330)
        player1.setheading(0)
        text2.undo()
        scorestring = "Score: %s" % score
        text2.write(scorestring, align='center', font=SMALL_FONT)

    if not -300 <= player1.ycor() <= 300:
        score += 1
        player1.setx(-330)
        player1.setheading(0)
        text2.undo()
        scorestring = "Score: %s" % score
        text2.write(scorestring, align='center', font=SMALL_FONT)

    if not -375 <= player2.xcor() <= 375:
        player2.right(180)

    if not -300 <= player2.ycor() <= 300:
        player2.right(180)

    if score == 2:
        text3.write("Red Won!", align='center', font=LARGE_FONT)

        booster1.hideturtle()
        booster2.hideturtle()
        decreaser.hideturtle()
        player1.hideturtle()
        player2.hideturtle()

        exit_button()

        break

screen.mainloop()