The examples in this article share the specific code for implementing a guessing game in python for your reference. The specific content is as follows
To design a guessing game, the requirements are as follows:
1、 When the game starts, enter the player name
2、 You can choose 3 computer opponents (computer name is customized)
3、 The rules of the game are: 1, scissors 2, rock 3, cloth
4、 At the beginning of the game, the player vs. the computer, the player chooses one, the computer is randomly 1-3
5、 With statistics on the number of rounds (such as: which round is it now), statistics on winning and losing (how many rounds the player wins, how many rounds the computer wins)
The renderings are as follows:
code show as below:
import random #Import random numbers first
classgame(): #The class for creating a game is divided into two attributes, one player name and one computer name
def __init__(self,playername,computername):
self.playername=playername
self.computername=computername
self.playerscore=0 #Player score
self.comscore=0 #Computer score
self.sum=0 #Number of draws
def startgame(self):
newname=input("Please enter your name:")
self.playername=newname
dnname=input("Please choose your opponent: 1.Wang Yuan, 2.Wang Junkai, 3.Yi Yang Qianxi:")if dnname=="1":print("Your opponent is Wang Yuan")
self.computername="Wang Yuan"
elif dnname=="2":print("Your opponent is Wang Junkai")
self.computername ="Wang Junkai"
elif dnname=="3":print("Your opponent is Yi Yang Qianxi")
self.computername ="Yi Yang Qianxi"else:print("The input is wrong, the system thought you picked Wang Junkai randomly")
self.computername ="Wang Junkai"print("--------The guessing game starts,%s VS %s--------"%(self.playername, self.computername))print("The rules of the game are: 1.Scissors, 2.Stone, 3.cloth")while True:
player=int(input("Players please punch:"))
# Player punches
player = random.randint(1,3)if player ==1:print(" %s out of the scissors"% self.playername)
elif player ==2:print(" %s out of the stone"% self.playername)else:print(" %s out of cloth"% self.playername)
# Computer punches
computer=random.randint(1,3)if computer==1:print(" %s out of the scissors"%self.computername)
elif computer==2:print(" %s out of the stone"%self.computername)else:print(" %s out of cloth"%self.computername)if(player==computer):print("draw")
self.sum+=1elif(player==1 and computer==3)or(player==2 and computer==1)or(player==3 and computer==2):print("%s win this game"%self.playername)
self.playerscore+=1else:print("%s win this game"%self.computername)
self.comscore+=1print("Winning or losing statistics:%s VS %s"%(self.playername, self.computername),"Player%s victory%d times"%(self.playername, self.playerscore),"%s victory%d times"%(self.computername, self.comscore),"%d times平局"% self.sum)print("---------------------------------------------------")
tag =input("Whether to continue y/n :")if tag.lower()=='n':print("Winning or losing statistics:%s VS %s"%(self.playername, self.computername),"Player%s victory%d times"%(self.playername, self.playerscore),"%s victory%d times"%(self.computername, self.comscore),"%d times平局"% self.sum)break
lx=game("as","hfg") #The number of parameters passed in must be equal to the number of defined parameters
lx.startgame() #Call the method to start the game
More interesting classic mini game implementation topics, share with you:
C++ classic games summary
Python classic games summary
python tetris game collection
JavaScript classic games are constantly playing
Summary of classic java games
JavaScript classic games summary
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts