【问题标题】:How to create a while loop for a dice roll in python turtle?如何在 python turtle 中为掷骰子创建一个 while 循环?
【发布时间】:2020-07-11 23:56:17
【问题描述】:

我设置了两只乌龟,游戏的目标是看哪只乌龟能先到家。我必须使用掷骰子机制来确定这一点,但我不知道如何在使用 while 循环时添加一个。一旦乌龟 1 滚动它应该是乌龟 2 的回合。他们一直这样做,直到其中一只海龟到达他们的家。这就是游戏结束的时候。

import turtle
import random 

turtle.hideturtle()
turtle.penup()
turtle.setpos(300,80)
turtle.pendown()
turtle.speed(9)
turtle.circle(30)

turtle.hideturtle()
turtle.penup()
turtle.setpos(300,-125)
turtle.pendown()
turtle.speed(9)
turtle.circle(30)

p1 = turtle.Turtle()
p1.shape("turtle")

p2 = turtle.Turtle()
p2.shape("turtle")

def p1_start():
  p1.speed(9)
  p1.penup()
  p1.goto(-200,100)
  p1.pendown()

p1_start()
 
def p2_start():
  p2.speed(9)
  p2.penup()
  p2.goto(-200,-100)
  p2.pendown()

p2_start()

player1 = input("Player 1 enter name: ")
turtle.penup()
turtle.setpos(-207, 50)
turtle.write(player1, font=('Arial', 16, 'normal'))
turtle.hideturtle()

player2 = input("\nPlayer 2 enter name: ")
turtle.penup()
turtle.setpos(-207, -150)
turtle.write(player2, font=('Arial', 16, 'normal'))
turtle.hideturtle()

player1_color = raw_input("\nPlayer 1 enter color: ")
p1.color(player1_color)

player2_color = raw_input("\nPlayer 2 enter color: ")
p2.color(player2_color)

player1_dict = {"Name": player1, "Color": player1_color}
print("\nPLAYER 1 INFO: ")
print(player1_dict)

player2_dict = {"Name": player2, "Color": player2_color}
print("\nPLAYER 2 INFO: ")
print(player2_dict)

print('\n')

print('Player 1 is now rolling')
roll = int (random.randint (1,6))
if roll==1:
  print('The number on the die is',roll)
elif roll== 2:
  print('The number on the die is',roll)
elif roll == 3:
  print('The number on the die is',roll)
elif roll == 4:
  print('The number on the die is',roll)
elif roll == 5:
  print('The number on the die is',roll)
else: 
  print('The number on the die is',roll)
p1.forward(roll*20)

【问题讨论】:

    标签: python while-loop python-turtle


    【解决方案1】:

    我看到了一些问题:在解决大问题之前你添加了太多的细节——应该是相反的;您创建了一个数据结构来代表您的玩家,但最终您并没有真正使用它;你为固定数量的玩家设计了这个,而你真的应该为未知(但合理)数量的玩家设计它以保持你的代码诚实。

    让我们将您的代码拆开并重新组合以解决上述问题:

    from turtle import Screen, Turtle
    from random import randint
    
    NUMBER_PLAYERS = 3
    POLE = 300
    FONT = ('Arial', 16, 'normal')
    
    players = []
    
    for player in range(1, NUMBER_PLAYERS + 1):
        name = input("\nPlayer " + str(player) + " enter name: ")
        color = input("Player " + str(player) + " enter color: ")
    
        players.append({'Name': name, 'Color': color})
    
    for number, player in enumerate(players, 1):
        print("\nPlayer", number, "information:", player)
    
    screen = Screen()
    height = screen.window_height()
    lane_height = height / (NUMBER_PLAYERS + 1)  # divide the vertical space into lanes
    
    marker = Turtle()
    marker.hideturtle()
    marker.speed('fastest')
    
    for number, player in enumerate(players, 1):
        y_coordinate = height/2 - lane_height * number
    
        marker.penup()
        marker.setpos(-POLE, y_coordinate + 30)
        marker.write(player['Name'], align='center', font=FONT)
        marker.setpos(POLE, y_coordinate - 30)
        marker.pendown()
        marker.circle(30)
    
        tortoise = Turtle()
        tortoise.hideturtle()
        tortoise.shape('turtle')
        tortoise.speed('slowest')
        tortoise.penup()
        tortoise.goto(-POLE, y_coordinate)
        tortoise.pendown()
        tortoise.color(player['Color'])
        tortoise.showturtle()
    
        player['Turtle'] = tortoise
    
    while True:
        for number, player in enumerate(players, 1):
            y_coordinate = height/2 - lane_height * number
    
            print()
            print(player['Name'], 'is now rolling')
            roll = randint(1, 6)
            print('The number on the die is:', roll)
            player['Turtle'].forward(roll * 5)
    
            if player['Turtle'].distance(POLE, y_coordinate) < 30:
                print()
                print(player['Name'], "is the winner!")
                break
    
        else:  # no break
            continue
    
        break  # yes, this is a tricky loop -- take your time with it
    
    screen.exitonclick()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2018-02-27
      • 2014-05-12
      • 2014-02-28
      • 1970-01-01
      • 2012-11-27
      相关资源
      最近更新 更多