Advertisement
Guest User

Untitled

a guest
Nov 7th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. import time
  2. import random
  3. players=[]
  4. tries=0
  5. #First, a log in system
  6. def login():
  7.     global tries
  8.     user=input('Username: ')
  9.     if user=='dice':
  10.         password=input('Password :')
  11.         if password=='game':
  12.             print('Welcome')
  13.         else:
  14.             print('Incorrect')
  15.             tries=tries+1
  16.             print(str(tries))
  17.             if tries==5:
  18.                 print('You have reached the maximum number of tries, you are now locked out for 5 minutes. Goodbye')
  19.                 time.sleep(300)
  20.                 login()
  21.             else:
  22.                 login()
  23.     else:
  24.         print('Incorrect')
  25.         tries=tries+1
  26.         print(str(tries))
  27.         if tries== 5:
  28.             print('You have reached the maximum number of tries, you are now locked out for 5 minutes. Goodbye')
  29.             time.sleep(300)
  30.             login()
  31.         else:
  32.             login()
  33.  
  34. #Now the dice roll
  35. def roll():
  36.     d3=0
  37.     dadd=0
  38.     dtotal=0
  39.     dfinal=0
  40.     input('Press Enter to Roll')
  41.     #this bit generates two random numbers and calls them dice1 and dice2
  42.     dice1=random.randint(1,6)
  43.     dice2=random.randint(1,6)
  44.     #this checks if the two dice are the same
  45.     if dice1==dice2:
  46.         input('You rolled a double! Press enter to roll dice 3 to add to your total!')
  47.         dice3=random.randint(1,6)
  48.         d3=d3+dice3
  49.         dadd=dadd+dice1+dice2+d3
  50.     #if they're not the same number...
  51.     else:
  52.         dadd=dadd+dice1+dice2
  53.         if dadd==3 or dadd==5 or dadd==7 or dadd==9 or dadd==11:
  54.             print('Oh No! You rolled an odd number! -5 points!')
  55.             dtotal=dtotal+dadd-5
  56.         else:
  57.             print('You rolled an even! +10 points!')
  58.             dtotal=dtotal+dadd+10
  59.     dfinal=dfinal+dadd+dtotal
  60.     #This next bit tests if the total score is less than zero
  61.     if dadd<0 or dtotal <0:
  62.         dadd=0
  63.         dtotal=0
  64.         print("Oh no! You've rolled 0 points! No points added to score!")
  65.     else:
  66.         print("You've rolled a total of "+str(dfinal)+" points. Added to total score")
  67.     return dfinal
  68.  
  69. def leaderboard():
  70.     file=open('leaderboard.txt','r')
  71.     for line in file:
  72.         temp=line.split(',')
  73.         players.append([temp[0],int(temp[1].strip('\n'))])
  74.     file.close()
  75.     #print(players)
  76.     sortedlist=sorted(players,key=lambda x:x[1], reverse=True)
  77.     #print(sortedlist)
  78.     print('Top 5 Players on leaderboard:')
  79.     for k in range(5):
  80.         print(sortedlist[k][0]+" scored "+str(sortedlist[k][1]))
  81.  
  82. def tiebreak():
  83.     input('Press enter to roll')    
  84.     tiedie=random.randint(1,6)
  85.     print('You rolled a '+str(tiedie)+'!')
  86.      
  87.  
  88. login()
  89. leaderboard()
  90.  
  91. p1turns=0
  92. p2turns=0
  93. p1score=0
  94. p2score=0
  95.  
  96. p1name=input('Player 1 Enter Name :')
  97. p2name=input('Player 2 Enter Name :')
  98. print('Welcome '+p1name+' and '+p2name+'.')
  99. time.sleep(1)
  100. print('In this game, each player gets 5 rolls, and each number you roll is a point (i.e roll a 5 = 5 points). Whoever has the highest score at the end wins! Each of your scores get put on a leaderboard at the end of the game. Top 5 players on the leaderboard will be shown in game')
  101. time.sleep(10)
  102. while p1turns<5 and p2turns<5:
  103.     print(p1name+"'s turn")
  104.     p1=roll()
  105.     p1score=p1score+p1
  106.     p1turns=p1turns+1
  107.     print(p1name+' is on '+str(p1score)+' points!')
  108.     time.sleep(2)
  109.     print()
  110.     print(p2name+"'s turn")
  111.     p2=roll()
  112.     p2score=p2score+p2
  113.     p2turns=p2turns+1
  114.     print(p2name+' is on '+str(p2score)+' points!')
  115.     time.sleep(2)
  116.     print()
  117. if p1turns==5 and p2turns==5:
  118.     if p1score>p2score:
  119.         print(str(p1name)+' has won the game! You got '+str(p1score)+' points and '+str(p2name)+' got '+str(p2score)+' points.')
  120.     elif p2score>p1score:
  121.         print(str(p2name)+' has won the game! You got '+str(p2score)+' points and '+str(p1name)+' got '+str(p1score)+' points.')
  122.     else:
  123.         print("It's a tie! You both got a score of "+str(p1score)+'!')
  124.  
  125.  
  126.  
  127. players.append([p1name,p1score])
  128. players.append([p2name,p2score])
  129. def save():
  130.     playerfile=open('leaderboard.txt','w')
  131.     for player in players:
  132.           playerfile.write(player[0]+","+str(player[1])+'\n')
  133.     playerfile.close()
  134.  
  135. save()
  136. print('Thanks for playing!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement