Python 如何使此程序在启动前等待屏幕单击?

Python 如何使此程序在启动前等待屏幕单击?,python,turtle-graphics,Python,Turtle Graphics,我正在努力完成一门课程,我做得相当好。这是一个简单的python海龟图形游戏,您可以尝试避开毒点,并导航到一个正方形。但是,在用户单击屏幕之前,我的程序立即启动。我怎样才能解决这个问题?谢谢 我的代码: # This game involves avoiding red poison blobs while attempting to navigate to # a square. If you hit the blob, you begin to speed up, making it mor

我正在努力完成一门课程,我做得相当好。这是一个简单的python海龟图形游戏,您可以尝试避开毒点,并导航到一个正方形。但是,在用户单击屏幕之前,我的程序立即启动。我怎样才能解决这个问题?谢谢

我的代码:

# This game involves avoiding red poison blobs while attempting to navigate to
# a square. If you hit the blob, you begin to speed up, making it more difficult
# not to hit more. Additionally, you lose a point. If you reach the square you
# get a point.

import turtle
import math
import random

# screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.tracer(3)

# Draw border
pen1 = turtle.Turtle()
pen1.color("white")
pen1.penup()
pen1.setposition(-275,-275)
pen1.pendown()
pen1.pensize(5)
for side in range(4):
    pen1.forward(550)
    pen1.left(90)
pen1.hideturtle()

# player
player = turtle.Turtle()
player.color("dark green")
player.shape("turtle")
player.penup()

# poisonBlob
maxpoisonBlob = 15
poisonBlob = []

for a in range(maxpoisonBlob):
    poisonBlob.append(turtle.Turtle())
    poisonBlob[a].color("dark red")
    poisonBlob[a].shape("circle")
    poisonBlob[a].shapesize(4, 4, 4)
    poisonBlob[a].penup()
    poisonBlob[a].speed(0)
    poisonBlob[a].setposition(random.randint(-255, 255), random.randint(-255, 255))

maxfood = 1
food = []

for a in range(maxfood):
        food.append(turtle.Turtle())
        food[a].color("light blue")
        food[a].shape("square")
        food[a].penup()
        food[a].speed(0)
        food[a].setposition(random.randint(-240, 240), random.randint(-240, 240))

# speed variable
speed = 6.5

def turnleft():
    player.left(30)

def turnright():
    player.right(30)

def increasespeed():
    global speed
    speed += 1

def touchPoison(t1, t2):
    d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
    if d < 50:
        return True
    else:
        return False

def touchfood(z1, z2):
        d = math.sqrt(math.pow(z1.xcor()-z2.xcor(),2) + math.pow(z1.ycor()-z2.ycor(),2))
        if d < 20:
                return True
        else:
                return False


turtle.listen()
turtle.onkey(turnleft, "Left")
turtle.onkey(turnright, "Right")


def main():
    print("Help your turtle navigate red poison blobs while attempting to navigate to the\n"
          "food! If you hit the poison, you begin to speed up, making it more difficult\n"
          "not to hit more. Additionally, you lose a point. If you reach the square you\n"
          "get a point. To navigate, click the screen, and then use the right and left\n"
          "arrow keys. Quickly, your turtle is running away!")

    score = 0

    while True:
        player.forward(speed)

        # turtle boundary
        if player.xcor() > 260 or player.xcor() < -260:
            player.right(180)

        # turtle boundary
        if player.ycor() > 260 or player.ycor() < -260:
            player.right(180)

        # move poison
        for a in range(maxpoisonBlob):
            poisonBlob[a].forward(3)

           # Poison boundaries
            if poisonBlob[a].xcor() > 220 or poisonBlob[a].xcor() < -220:
                poisonBlob[a].right(180)

            # Poision boundaries
            if poisonBlob[a].ycor() > 220 or poisonBlob[a].ycor() < -220:
                poisonBlob[a].right(180)     

            # Poison touching
            if touchPoison(player, poisonBlob[a]):
                increasespeed()
                poisonBlob[a].setposition(random.randint(-230, 230), random.randint(-230, 230))
                poisonBlob[a].right(random.randint(0,360))
                score -= 1
                #Draw score
                pen1.undo()
                pen1.penup()
                pen1.hideturtle()
                pen1.setposition(-260, 280)
                scorestring = "Score: %s" %score
                pen1.write(scorestring, font=("Calibri", 12))

        for a in range(maxfood):

            #Positive Point Checking
            if touchfood(player, food[a]):
                food[a].setposition(random.randint(-230, 230), random.randint(-230, 230))
                score += 1
                #Draw Score
                pen1.undo()
                pen1.penup()
                pen1.hideturtle()
                pen1.setposition(-260, 280)
                scorestring = "Score: %s" %score
                pen1.write(scorestring, font=("Calibri", 12))

if __name__ == "__main__":
    main()
#此游戏包括在尝试导航到时避免红色毒药斑点
#正方形。如果你碰到了水滴,你就会开始加速,这会使它变得更加困难
#不要再打了。此外,你会失去一分。如果你到了广场,你就走了
#明白了。
进口海龟
输入数学
随机输入
#屏风
wn=tutle.Screen()
wn.bgcolor(“黑色”)
wn.示踪剂(3)
#划界
pen1=海龟。海龟()
钢笔1.颜色(“白色”)
pen1.penup()
pen1.设置位置(-275,-275)
pen1.pendown()
pen1.养老金(5)
对于范围内的侧面(4):
pen1.前进(550)
pen1.左(90)
pen1.hideturtle()
#玩家
玩家=乌龟。乌龟()
播放器颜色(“深绿色”)
玩家形状(“海龟”)
player.penup()
#毒斑
MaxBlob=15
毒液滴=[]
对于范围内的(MaxTownBlob):
toxinblob.append(turtle.turtle())
有毒斑点[a]。颜色(“暗红色”)
毒液滴[a]。形状(“圆”)
毒液滴[a].形状大小(4,4,4)
TownBlob[a].penup()
有毒水滴[a]。速度(0)
TownBlob[a].setposition(random.randint(-255,255),random.randint(-255,255))
maxfood=1
食物=[]
对于范围内的食品(maxfood):
food.append(海龟.海龟())
食物[a].颜色(“浅蓝色”)
食物[a]。形状(“正方形”)
食物[a].penup(食物)
食物[a]。速度(0)
食物[a].设置位置(random.randint(-240240),random.randint(-240240))
#变速器
速度=6.5
def左转()
球员。左(30)
def turnright():
玩家。右(30)
def递增速度():
全球速度
速度+=1
def接触毒物(t1、t2):
d=math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
如果d<50:
返回真值
其他:
返回错误
def触摸食品(z1、z2):
d=math.sqrt(math.pow(z1.xcor()-z2.xcor(),2)+math.pow(z1.ycor()-z2.ycor(),2))
如果d<20:
返回真值
其他:
返回错误
乌龟,听着
乌龟。钥匙(左转,“左”)
turtle.onkey(右转,“右”)
def main():
打印(“帮助您的海龟在尝试导航到目标位置时导航红色毒药斑点\n”
食物!如果你中了毒药,你会开始加速,这会使它更加困难\n
不要打得更多。此外,您会失去一分。如果您到达正方形,您将\n
获取点。要导航,请单击屏幕,然后使用左右键\n
“箭头键。快点,你的乌龟跑了!”
分数=0
尽管如此:
球员向前(速度)
#龟界
如果player.xcor()大于260或player.xcor()小于-260:
右(180)
#龟界
如果player.ycor()大于260或player.ycor()小于-260:
右(180)
#移毒
对于范围内的(MaxTownBlob):
有毒水滴[a]。前进(3)
#毒药边界
如果毒物Blob[a].xcor()>220或毒物Blob[a].xcor()<-220:
毒液滴[a]。右(180)
#泊松边界
如果TownBlob[a].ycor()>220或TownBlob[a].ycor()<-220:
毒液滴[a]。右(180)
#触毒
如果触摸毒药(玩家,毒药滴[a]):
增加速度
TownBlob[a].setposition(random.randint(-230230),random.randint(-230230))
TownBlob[a]。右(random.randint(0360))
分数-=1
#平局
pen1.undo()
pen1.penup()
pen1.hideturtle()
pen1.设置位置(-260280)
scorestring=“分数:%s”%Score
pen1.write(scorestring,font=(“Calibri”,12))
对于范围内的食品(maxfood):
#正点检查
如果触摸食物(玩家,食物[a]):
食物[a].设置位置(random.randint(-230230),random.randint(-230230))
分数+=1
#平局
pen1.undo()
pen1.penup()
pen1.hideturtle()
pen1.设置位置(-260280)
scorestring=“分数:%s”%Score
pen1.write(scorestring,font=(“Calibri”,12))
如果名称=“\uuuuu main\uuuuuuuu”:
main()

通常我们不会直接回答家庭作业问题,但我会给你一些指导

我不熟悉您正在使用的库,但基本思想是,您需要等到用户单击屏幕后才能继续。我的理解是,您希望在main中的print()函数之后执行此操作,以便用户必须阅读消息,然后单击以继续

快速搜索显示此链接:

这将详细介绍onscreenclick()事件。从您的onkey()方法来看,您已经熟悉绑定到事件,因此您应该能够使用这些知识绑定到onscreenclick()事件,然后您只需要稍微分解代码,以便在onscreenclick()事件发生之前它不会执行您的游戏


如果这还不够清楚,请帮助我了解您的困惑所在,我将进一步提供帮助。谢谢

您可以从定义一个函数开始,在该函数中调用“if”语句,然后使用
turtle.onscreenclick()
在其中插入新定义的函数,如下所示:

def Game(x, y):
    if __name__ == "__main__":
        main()

turtle.onscreenclick(Game)
确保在末尾插入所有这些

现在,将(x,y)添加到函数定义中的函数名称后的括号中,将为用户在图形窗口中单击的点指定相应的(x,y)坐标,从而通过单击屏幕的动作激活函数。