大富翁(简版Python)

编程入门 行业动态 更新时间:2024-10-24 04:50:55

<a href=https://www.elefans.com/category/jswz/34/1727345.html style=大富翁(简版Python)"/>

大富翁(简版Python)

大富翁游戏(简版Python代码)
这是自己做着玩的,有兴趣的朋友可以试着玩,有些地方偷懒了,请多多包涵
地图是一个CSV文件(Map.csv:有些地图参数需要去阅读代码后可以理解)
内容如下:
0,Start,0,0,0,3
1,Postmonth St,1,100,1000,0
2,Charless St,1,1000,1500,0
3,M23,1,1030,2000,0
4,Mount Waverley Police Station,2,0,0,-1
5,Mummery St,1,1000,200,0
6,Willam St,1,2000,1500,0
7,Mount Waverley Shopping Center,4,0,0,2
8,Bruce St,1,200,10000,0
9,Gallery PI,1,2000,200,0
10,Valley Rd,1,9000,100,0
11,Petrol Station,3,0,0,1
12,Will Ave,1,1000,1000,0
13,Bond St,1,4000,5000,0
14,Ophir Rd,1,2200,1000,0
15,Walmarie Dr,1,2000,3000,0
16,Waverley Private hospital,2,0,0,-1
17,The Hwy,1,100,200,0
18,Saving Ave,1,3000,500,0
19,Genoa Ct,1,4000,4000,0

import random
import csv
num=random.randint(1,6)def map_load(): #loading the game map from the map file(Map.csv)MapList=[]with open('Map.csv','r') as csvFile:reader = csv.reader(csvFile)#print(reader)for row in reader:Map_detail=row+['--','0']MapList.append(Map_detail)csvFile.close()return MapList
mapView=map_load()def CreatUser(UserNum):# Create the playersb=[]for i in range(UserNum):b.append([i,0,10000,0])return bdef SpecEx(Statu):#In the special area, the player will follow the area order that move front or back to another area, or stop one turnif(Statu=='-1'):print("you will stop on one turn!")return [0,-1]elif (Statu=='2'):luck=input("Try to get your Lucky(Y or N):")if(luck=='Y'):num=random.randint(1,6)if(num%2==0):result=random.randint(1,6)print("You are luck! Keep moving: "+str(result)+" Step")return [result,0]else:result=random.randint(1,6)print("Oh! Too bad You need back: "+str(result)+" Steps")return [-result,0]else:return [0,0]else:result=random.randint(1,6)print("You are luck! Keep moving: "+str(result)+" Step")return [result,0]def moveplayer_mapCheck(Num,i):# To public area, the player will decide to pay the money for buying the area, building house, loaning the area to pass baced on the different situationif mapView[Num][5]=='0':if mapView[Num][6]=='--':print("Locate: "+mapView[Num][1]+" Area Price/Building Price: "+mapView[Num][3]+"/"+mapView[Num][4])#For make sure the situation of this area (The Area Price and Building Price)yn=input("Do you want to buy this area(Y or N):")if yn=='Y' and Player[i][2]>=int(mapView[Num][3]):mapView[Num][6]=str(i)Player[i][2]=Player[i][2]-int(mapView[Num][3])elif yn=='Y' and Player[i][2]<int(mapView[Num][3]):print("Sorry you haven't enoug money!")else:passelif mapView[Num][6]==str(i):if(mapView[Num][7]=='0'):print("Locate: "+mapView[Num][1]+" Area Price/Building Price: "+mapView[Num][3]+"/"+mapView[Num][4])Byn=input("Do you want to bulid house(Y or N):")if Byn=='Y' and Player[i][2]>=int(mapView[Num][4]):Player[i][2]-=int(mapView[Num][4])mapView[Num][7]='1'elif Byn=='Y' and Player[i][2]<int(mapView[Num][4]):print("Sorry you haven't enoug money!")else:passelse:print("Locate: "+mapView[Num][1]+" Area Price/Building Price: "+mapView[Num][3]+"/"+mapView[Num][4])try_paid=input("Paid loan(L) or Paid the area(A):")if try_paid=='A' and Player[i][2]>=(int(mapView[Num][3])+int(mapView[Num][4]))*1.1 and mapView[Num][7]=='1':mapView[Num][7]='0'Player[i][2]-=(int(mapView[Num][4])+int(mapView[Num][4]))*1.1Player[int(mapView[Num][6])][2]+=(int(mapView[Num][4])+int(mapView[Num][4]))*1.1mapView[Num][6]=str(i)print("Paid is successful!")elif try_paid=='A' and Player[i][2]>=int(mapView[Num][3])*1.1 and mapView[Num][7]=='0':Player[i][2]-=int(mapView[Num][3])*1.1Player[int(mapView[Num][6])][2]+=int(mapView[Num][3])*1.1mapView[Num][6]=str(i)print("Paid is successful!")elif Player[i][2]>=(int(mapView[Num][3])+int(mapView[Num][4]))*0.01 and mapView[Num][7]=='1':Player[i][2]-=(int(mapView[Num][3])+int(mapView[Num][4]))*0.01Player[int(mapView[Num][6])][2]+=(int(mapView[Num][3])+int(mapView[Num][4]))*0.01elif Player[i][2]>=int(mapView[Num][3])*0.01 and mapView[Num][7]=='0':Player[i][2]-=int(mapView[Num][3])*0.01Player[int(mapView[Num][6])][2]+=int(mapView[Num][3])*0.01else:print("Your Game is over!!!")Player[i][3]=404else:res=SpecEx(mapView[Num][5])if res[0]==0:Player[i][3]=-1else:Player[i][1]=(Player[i][1]+20+res[0])%20moveplayer_mapCheck(Player[i][1],i)#The Game begin.
UserNum=int(input("Enter the user Number:"))# identify the number of the players
Player=CreatUser(UserNum)# Create the player setprint("-----------------Player------------------")#show the player
for a in range(UserNum):print("Player: "+str(Player[a][0])+" local: "+str(Player[a][1])+" Money: "+str(Player[a][2]))
x=1# the variable for control whether the game is over 
while x==1:#Make sure whether the game is overdata_rec=0for i in range(UserNum):n=0if Player[i][3]==0:while n==0:n=input("Play "+str(i)+" :Do you roll(yes/1 or No/0):")num=random.randint(1,6)#identify the number of the rollPlayer[i][1]=(num+int(Player[i][1]))%20moveplayer_mapCheck(Player[i][1],i)print("-----------------Player------------------")#show the location of the player in the mapfor i in range(UserNum):print("Player: "+str(Player[i][0])+" local: "+str(Player[i][1])+" Money: "+str(Player[i][2]))print("---------------MAP--------------------")#show the mapfor i in range(20):if(mapView[i][5]=='0'):print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Area Price/Building Price: "+mapView[i][3]+"/"+mapView[i][4]+" Belonger: "+mapView[i][6]+" is_built :"+mapView[i][7])elif(mapView[i][5]=='-1'):print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Please wait one turn")elif(mapView[i][5]=='1'):print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Please roll again")elif(mapView[i][5]=='2'):print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Good luck")else:print("Step: "+str(i)+" Locate: "+mapView[i][1]+" Thing: Starting")print("_____________________________________")elif Player[i][3]==-1:Player[i][3]=0else:passfor i in range(UserNum):if Player[i][3]==404:data_rec+=1if UserNum==data_rec+1:#make sure the game is over. Just one player is survive.x=0for i in range(UserNum):#make sure that who is Winnerif Player[i][3]!=404:print("Player "+str(i+1)+"You are Winner!")break

更多推荐

大富翁(简版Python)

本文发布于:2024-02-13 23:04:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大富翁   简版   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!