在python 2.7中制作简单的纸牌游戏

时间:2015-05-21 06:17:59

标签: python python-2.7

我正努力在学校的python中制作一个非常简单的纸牌游戏。它就像绘制两张牌一样简单,一张用于经销商,另一张用于用户,获得最高牌的人赢得了那一轮。每轮赢得用户分数上升1,当用户未通过一轮时,他/她的高分被记录到.py文件旁边的.txt文件中。我进入了一些可能非常简单的问题......一个是我为经销商和用户选择随机卡,并将其设置为变量,另一个是在scores.txt文件中记录得分。谢谢你的帮助!! 它可能有点乱,但我更担心它实际工作。 这是代码:

from sys import argv # imports the opening of two separate files
import random # imports the 'random' string
script, filename = argv # sets two variables, one on the py file and the other on the txt file
txt = open(filename) # opens the txt file

# card variables assigning values 
Two = 1
Three = 2
Four = 3
Five = 4
Six = 5
Seven = 6
Eight = 7
Nine = 8
Ten = 9
Jack = 10
Queen = 11
King = 12
Ace = 13
# chooses a random card 
score = 0 # score at game start
def scores(): # this will print out the current high scores.
    print "The current scores in file: %r" % filename
    print txt.read() # prints the open txt file

def start(): # this is sent after the scores. welcomes play to game.
    print "Welcome %s, the computer and you will both draw a card. Whoever gets the higher card wins" % name
    print "Try to in a row to go on the high scores!"
    print "Good luck %s" % name
    game() #starts the game

def game(): # begins the actual game
    print "The dealer draws a card..." 
    dealer = random.choice([Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace]) # gives the dealer a random card
    print "You draw a card..."
    user = random.choice([Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace]) # gives the user a random card
    if dealer > user: # if dealer wins 
        print "You lose!"
        if score > 1: # checks if score is better than 1, then writes score to txt doc
            filename.write("\n")
            filename.write(name "=" score)
            filename.close()
        else: # aren't good enough, game quits
            print "You didn't get a score over 1, you suck and aren't going to be on high scores."
            filename.close()
    elif user < dealer: # if user wins
        print "Nice! You won that round"
        score = score + 1 # adds 1 to current score
        game() # next round
    else:
        print "Um no idea what you did there, again?"
        game()

scores() # score def
name = raw_input("First off let's get your name. \n >") # asks the users name for recording high scores
start() # start def

2 个答案:

答案 0 :(得分:3)

1)打开文件:

txt = open(filename)

在python中有3种类型的打开文件。阅读,写作和追加。您可以使用&#39; r&#39;,&#39; w&#39;指定它们。和&#39; a&#39;作为开放函数的第二个参数:

txt = open(filename, 'r')

但是如果你想读写一个文件,比如在这个纸牌游戏中,你必须使用&#39; r +&#39; (或&#39; w +&#39;和&#39; a +&#39;)。然后你也可以读写而不是只读这个文件。

2)语法无效:

filename.write(name "=" score)

你应该写:

filename.write("%s=%d" % (name, score))

3)&#39;得分&#39;变量:

在游戏功能中出现错误:

    if score > 1: # checks if score is better than 1, then writes score to txt doc
UnboundLocalError: local variable 'score' referenced before assignment

您必须将得分变量设置为全局变量(在游戏功能中)。因此范围不仅用于游戏功能本身:

global score

4)&#34;游戏逻辑中的小错误&#34;:

在你的if-else语句中,你这样做:

if dealer > user:
    ...
elif user < dealer:
    ...
else

elif条件与if条件相同。您必须将其更改为:

if dealer > user:
    ...
elif user > dealer:
    ...
else

5)读写文件:

你写了例如:

filename.write("\n")

但文件名只是一个字符串。你必须使用open()函数返回的&t; txt&#39; -variable。

txt.write("\n")

完成所有这些更改后,代码如下所示:

from sys import argv # imports the opening of two separate files
import random # imports the 'random' string
script, filename = argv # sets two variables, one on the py file and the other on the txt file
txt = open(filename, 'r+') # opens the txt file

# card variables assigning values 
Two = 1
Three = 2
Four = 3
Five = 4
Six = 5
Seven = 6
Eight = 7
Nine = 8
Ten = 9
Jack = 10
Queen = 11
King = 12
Ace = 13
# chooses a random card 
score = 0 # score at game start
def scores(): # this will print out the current high scores.
    print "The current scores in file: %r" % filename
    print txt.read() # prints the open txt file

def start(): # this is sent after the scores. welcomes play to game.
    print "Welcome %s, the computer and you will both draw a card. Whoever gets the higher card wins" % name
    print "Try to in a row to go on the high scores!"
    print "Good luck %s" % name
    game() #starts the game

def game(): # begins the actual game
    global score
    print "The dealer draws a card..." 
    dealer = random.choice([Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace]) # gives the dealer a random card
    print "You draw a card..."
    user = random.choice([Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace]) # gives the user a random card
    if dealer > user: # if dealer wins 
        print "You lose!"
        if score > 1: # checks if score is better than 1, then writes score to txt doc
            txt.write("\n")
            txt.write("%s=%d" % (name, score))
            txt.close()
        else: # aren't good enough, game quits
            print "You didn't get a score over 1, you suck and aren't going to be on high scores."
            txt.close()
    elif user > dealer: # if user wins
        print "Nice! You won that round"
        score = score + 1 # adds 1 to current score
        game() # next round
    else:
        print "Um no idea what you did there, again?"
        game()

scores() # score def
name = raw_input("First off let's get your name. \n >") # asks the users name for recording high scores
start() # start def

我希望我能帮到你!

答案 1 :(得分:0)

如果要写入文件,则应以写入模式打开它:

txt = open(filename, mode='w')