Wednesday, 18 September 2013

Call function without resetting variables

Call function without resetting variables

I am making a card game, when it gets to the end I want people to be able
to play more rounds but when it replays it must go through the variables
again.. resetting the score. I am looking for a way to fix it without a
whole new block of complicating code, I'm hoping I'm just missing a really
easy fix.
#!/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)

No comments:

Post a Comment