我正在制作一款纸牌游戏,当它结束时我希望人们能够玩更多的回合但是当它重播时它必须再次经历变量..重置分数。我正在寻找一种无需全新的复杂代码块即可修复它的方法,我希望我只是错过了一个非常简单的修复程序。
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
import time
def play_game():
# Variables
acard = int()
bcard = int()
apoints = int()
bpoints = int()
# Repeat
repeat = True
# Hand out cards
print 'Cards have been served'
input('\nPress Enter to Reveal')
# Cards Turn
time.sleep(0.5)
t = time.time()
acard = int(str(t - int(t))[2:]) % 13
print '\nYour card value is ' + str(acard)
time.sleep(0.1)
t = time.time()
bcard = int(str(t - int(t))[2:]) % 13
# Number Check & Point Assign
time.sleep(2)
if acard > 5:
apoints += 1
print '\nYour points have increased by one, your total is now ' \
+ str(apoints)
if acard < 5:
apoints -= 1
print '\nYour points have decreased by one, your total is now ' \
+ str(apoints)
if bcard > 5:
bpoints += 1
print '\nYour opponent got ' + str(bcard) \
+ ', their points have increased by one,\ntheir total is now ' \
+ str(bpoints)
if bcard < 5:
bpoints -= 1
print '\nYour opponent got ' + str(bcard) \
+ ', their points have decreased by one,\ntheir total is now ' \
+ str(bpoints)
# Card Reset
bcard = 0
acard = 0
# Shuffle
input('\nPress enter to shuffle deck')
print '\nCards being shuffled'
time.sleep(1)
print '.'
time.sleep(1)
print '.'
time.sleep(1)
print '.'
print 'Cards have been shuffled'
global enda
global endb
enda = apoints
endb = bpoints
# Loop
time.sleep(1)
answer = 'y'
while answer.lower() == 'y':
play_game()
answer = input('\nDo you wish to play again? (Y/N)')
# Scores
if enda > endb:
print '\nYou Win!'
print '\nScores'
print 'You: ' + str(enda) + ' Opponent: ' + str(endb)
if endb > enda:
print '\nYou Lost!'
print '\nScores'
print 'You: ' + str(enda) + ' Opponent: ' + str(endb)
请您参考如下方法:
我可能会使用面向对象的方式来编写类级变量,但如果您想使代码尽可能接近现在的代码,您可以将分数变量移到方法之外:
apoints = 0
bpoints = 0
def play_game():
global apoints
global bpoints
...
