Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import random
- players=[]
- tries=0
- #First, a log in system
- def login():
- global tries
- user=input('Username: ')
- if user=='dice':
- password=input('Password :')
- if password=='game':
- print('Welcome')
- else:
- print('Incorrect')
- tries=tries+1
- print(str(tries))
- if tries==5:
- print('You have reached the maximum number of tries, you are now locked out for 5 minutes. Goodbye')
- time.sleep(300)
- login()
- else:
- login()
- else:
- print('Incorrect')
- tries=tries+1
- print(str(tries))
- if tries== 5:
- print('You have reached the maximum number of tries, you are now locked out for 5 minutes. Goodbye')
- time.sleep(300)
- login()
- else:
- login()
- #Now the dice roll
- def roll():
- d3=0
- dadd=0
- dtotal=0
- dfinal=0
- input('Press Enter to Roll')
- #this bit generates two random numbers and calls them dice1 and dice2
- dice1=random.randint(1,6)
- dice2=random.randint(1,6)
- #this checks if the two dice are the same
- if dice1==dice2:
- input('You rolled a double! Press enter to roll dice 3 to add to your total!')
- dice3=random.randint(1,6)
- d3=d3+dice3
- dadd=dadd+dice1+dice2+d3
- #if they're not the same number...
- else:
- dadd=dadd+dice1+dice2
- if dadd==3 or dadd==5 or dadd==7 or dadd==9 or dadd==11:
- print('Oh No! You rolled an odd number! -5 points!')
- dtotal=dtotal+dadd-5
- else:
- print('You rolled an even! +10 points!')
- dtotal=dtotal+dadd+10
- dfinal=dfinal+dadd+dtotal
- #This next bit tests if the total score is less than zero
- if dadd<0 or dtotal <0:
- dadd=0
- dtotal=0
- print("Oh no! You've rolled 0 points! No points added to score!")
- else:
- print("You've rolled a total of "+str(dfinal)+" points. Added to total score")
- return dfinal
- def leaderboard():
- file=open('leaderboard.txt','r')
- for line in file:
- temp=line.split(',')
- players.append([temp[0],int(temp[1].strip('\n'))])
- file.close()
- #print(players)
- sortedlist=sorted(players,key=lambda x:x[1], reverse=True)
- #print(sortedlist)
- print('Top 5 Players on leaderboard:')
- for k in range(5):
- print(sortedlist[k][0]+" scored "+str(sortedlist[k][1]))
- def tiebreak():
- input('Press enter to roll')
- tiedie=random.randint(1,6)
- print('You rolled a '+str(tiedie)+'!')
- login()
- leaderboard()
- p1turns=0
- p2turns=0
- p1score=0
- p2score=0
- p1name=input('Player 1 Enter Name :')
- p2name=input('Player 2 Enter Name :')
- print('Welcome '+p1name+' and '+p2name+'.')
- time.sleep(1)
- 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')
- time.sleep(10)
- while p1turns<5 and p2turns<5:
- print(p1name+"'s turn")
- p1=roll()
- p1score=p1score+p1
- p1turns=p1turns+1
- print(p1name+' is on '+str(p1score)+' points!')
- time.sleep(2)
- print()
- print(p2name+"'s turn")
- p2=roll()
- p2score=p2score+p2
- p2turns=p2turns+1
- print(p2name+' is on '+str(p2score)+' points!')
- time.sleep(2)
- print()
- if p1turns==5 and p2turns==5:
- if p1score>p2score:
- print(str(p1name)+' has won the game! You got '+str(p1score)+' points and '+str(p2name)+' got '+str(p2score)+' points.')
- elif p2score>p1score:
- print(str(p2name)+' has won the game! You got '+str(p2score)+' points and '+str(p1name)+' got '+str(p1score)+' points.')
- else:
- print("It's a tie! You both got a score of "+str(p1score)+'!')
- players.append([p1name,p1score])
- players.append([p2name,p2score])
- def save():
- playerfile=open('leaderboard.txt','w')
- for player in players:
- playerfile.write(player[0]+","+str(player[1])+'\n')
- playerfile.close()
- save()
- print('Thanks for playing!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement