【问题标题】:How do I make this program wait for a screen click before starting?如何让这个程序在启动前等待屏幕点击?
【发布时间】:2015-12-10 03:33:46
【问题描述】:

我正在尝试完成一个课程的课程,而且我做得很好。这是一个简单的 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()

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    一般我们尽量不直接回答作业问题,但我会给你一些指导。

    我不熟悉您正在使用的库,但基本想法是您需要等到用户单击屏幕才能继续。我的理解是你想在 main 中的 print() 函数之后这样做,这样用户必须阅读消息然后单击继续。

    快速搜索发现此链接:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates

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

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

    【讨论】:

      【解决方案2】:

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

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

      确保在最后插入所有这些!

      现在,在函数定义中的函数名称后的括号中添加 (x, y) 会为用户在图形窗口中单击的点分配其对应的 (x, y) 坐标,从而激活函数到期到点击屏幕的动作。我成功地做到了,所以我可以向你保证它有效!我希望这有帮助! :)

      【讨论】:

        猜你喜欢
        • 2020-06-11
        • 2022-01-25
        • 2018-09-30
        • 2016-10-11
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多