简体   繁体   English

如何在一个简单的Python游戏中循环播放?

[英]How to loop in a simple Python Game?

I'm very new to Python but have written a simple Rock, Paper, Scissors game. 我是Python的新手,但是写了一个简单的Rock,Paper和Scissors游戏。

It's code is as follows: 它的代码如下:

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')
yesorno = str(yesorno)



if yesorno == str('Yes'):
    choices = ['Rock', 'Paper', 'Scissors']
    print('Ok')
    your_choice = raw_input('Choose Rock, Paper or Scissors: ')
    print('My turn...')

    my_choice = random.choice(choices)
    print('I choose,', my_choice)
    if your_choice == my_choice:
        print('We both choose the same, it is a draw.')
    elif your_choice == 'Rock' and my_choice == 'Paper':
        print('I win!')
    elif your_choice == 'Scissors' and my_choice == 'Paper':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Rock':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Scissors':
        print('I win!')
    elif your_choice == 'Rock' and my_choice == 'Scissors':
        print('You win!')
    elif your_choice == 'Scissors' and my_choice == 'Rock':
        print('I win!')

    again = raw_input('Would you like to play again?:')

    #this is where I would like to loop to the start depending on input.



else:
    print('Ok, maybe we can play later, bye.')

Now I can imagine this isn't even remotely elegant code and that there must be more precise ways to write what I am trying to do. 现在,我可以想象这甚至不是遥不可及的代码,而且必须有更精确的方法来编写我想做的事情。 (Please give any pointers you have time to). (请提供您有空的指针)。

My main concern is how to correctly insert a loop for after each game ends to either return to the second block ie. 我主要关心的是如何在每个游戏结束后正确插入循环以返回到第二个块,即。 the start or the game, or to simply end, based on the raw_input from the user. 根据用户的raw_input,开始游戏或结束游戏。

Many thanks for any help you can give. 非常感谢您提供的任何帮助。

There you go: 你去了:

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')
n = 0
while True:
    if 'y' in raw_input('Would you like to play Rock, Paper, Scissors?: ').lower() or n > 0:
        my_choice = random.choice(['rock', 'paper', 'scissors'])
        print('Ok')
        your_choice = raw_input('Choose Rock, Paper or Scissors: ').lower()
        print('My turn...')
        print('I choose,', my_choice)
        if your_choice == my_choice:
            print('We both choose the same, it is a draw.')
        elif your_choice in ['rock', 'scissors'] and my_choice == 'paper':
            print('I win!')
        elif your_choice == 'paper' and my_choice in ['rock', 'scissors']:
            print('You win!')
        elif your_choice == 'rock' and my_choice == 'scissors':
            print('You win!')
        elif your_choice == 'scissors' and my_choice == 'rock':
            print('I win!')
        n += 1
        if not 'y' in raw_input('Would you like to play again?:').lower():
            break

    #this is where I would like to loop to the start depending on input.

It's also case insensitive now, kind of annoying when the game exits or you lose because you accidentally forgot to write your answer starting with a capital letter. 现在它也不区分大小写,当游戏退出或失败时会很烦人,因为您不小心忘记了以大写字母开头的答案。 I also simplified it a little bit using 'in' 我也使用'in'简化了一点

I would insert the loop there: 我将在其中插入循环:

# ...

yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')

while yesorno == 'Yes':
    choices = ['Rock', 'Paper', 'Scissors']

    # ...

    yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')

print('Ok, maybe we can play later, bye.')
while True:

    <your code>

    if not 'y' in raw_input('Would you like to play again?: ').lower():
        break;

What I'd do is to remove the unnecessary variables and instead put the actual game in a function. 我要做的是删除不必要的变量,而是将实际游戏放入函数中。 This allows you to call it once WITHOUT asking if the player wants to replay, and then make a while loop where the user can decide if he wants to play on. 这使您可以调用一次,而无需询问播放器是否要重播,然后进行while循环,以便用户可以决定是否要继续播放。 I hope this helps. 我希望这有帮助。 I couldn't test this myself as I use Python 2.7, but aside from some potential syntax differences the code should work. 当我使用Python 2.7时,我无法自己对此进行测试,但是除了某些潜在的语法差异之外,该代码也应该可以工作。 I suggest something like this: 我建议是这样的:

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')
if yesorno == 'y':
    play()
def play():
        choices = ['Rock', 'Paper', 'Scissors']
        print('Ok')
        your_choice = raw_input('Choose Rock, Paper or Scissors: ')
        print('My turn...')

    my_choice = random.choice(choices)
    print('I choose,', my_choice)
    if your_choice == my_choice:
        print('We both choose the same, it is a draw.')
    elif your_choice == 'Rock' and my_choice == 'Paper':
        print('I win!')
    elif your_choice == 'Scissors' and my_choice == 'Paper':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Rock':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Scissors':
        print('I win!')
    elif your_choice == 'Rock' and my_choice == 'Scissors':
        print('You win!')
    elif your_choice == 'Scissors' and my_choice == 'Rock':
        print('I win!')
while True:
    play_again = raw_input('Play Again?(Y/N)')
    if play_again.lower() == 'y':
        play()
    elif play_again.lower() == 'n':
        print('Ok, maybe we can play later, bye.')
        break
    else:
        print('That isn\'t a valid option...')

I don't have time to clean it up, but here's a way you can fix your specific issue. 我没有时间清理它,但是这是解决特定问题的一种方法。 You wrap the game within a while loop that is always true, and exit when desired. 您将游戏包装在始终为true的while循环中,并在需要时退出。

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')

yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')

while(1):

    yesorno = str(yesorno)

    if yesorno == str('Yes'):
        choices = ['Rock', 'Paper', 'Scissors']
        print('Ok')
        your_choice = raw_input('Choose Rock, Paper or Scissors: ')
        print('My turn...')

        my_choice = random.choice(choices)
        print('I choose,', my_choice)
        if your_choice == my_choice:
            print('We both choose the same, it is a draw.')
        elif your_choice == 'Rock' and my_choice == 'Paper':
            print('I win!')
        elif your_choice == 'Scissors' and my_choice == 'Paper':
            print('You win!')
        elif your_choice == 'Paper' and my_choice == 'Rock':
            print('You win!')
        elif your_choice == 'Paper' and my_choice == 'Scissors':
            print('I win!')
        elif your_choice == 'Rock' and my_choice == 'Scissors':
            print('You win!')
        elif your_choice == 'Scissors' and my_choice == 'Rock':
            print('I win!')

        again = raw_input('Would you like to play again? [y/n]:')

        if again is "y":
            next
        else:
            print('Thanks for playing!\n')
            exit()

    else:
        print('Ok, maybe we can play later, bye.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM