Pygame 简单的文字游戏

在本文中,我们将演示如何创建一个简单的猜谜游戏。游戏的目标是猜出正确的数字。

Pygame 猜数游戏示例

下面是程序运行的的例子,

$ python game.py
Guess the number below 10:
Guess: 5
Wrong guess
Guess: 2
Wrong guess
Guess: 1
Wrong guess
Guess: 7
Wrong guess
Guess: 

随机数

将要求用户猜测随机数。我们先选择随机数:

from random import randint
 
x = randint(1,9)

randint() 函数将选择 1 到 10 之间的伪随机数。然后我们必须继续,直到找到正确的数字:

guess = -1
 
print("Guess the number below 10:")
while guess != x:
    guess = int(raw_input("Guess: "))
 
    if guess != x:
        print("Wrong guess")
    else:
        print("Guessed correctly")

Python 猜猜游戏

下面的代码启动游戏:

from random import randint
 
x = randint(1,9)
guess = -1
 
print "Guess the number below 10:"
while guess != x:
    guess = int(raw_input("Guess: "))
 
    if guess != x:
        print("Wrong guess")
    else:
        print("Guessed correctly")

程序运行的例子,

Guess the number below 10:
Guess: 3
Wrong guess
Guess: 6
Wrong guess
..