亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python?Pygame如何實現經營類游戲

發布時間:2022-12-28 09:37:11 來源:億速云 閱讀:124 作者:iii 欄目:開發技術

本文小編為大家詳細介紹“Python Pygame如何實現經營類游戲”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Python Pygame如何實現經營類游戲”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

一、注意事項

1.python的字體要盡量小一點 否則視覺上會很難看,調9-11左右 而且一定一定要調Consolas字體 對于win7系統字符畫顯示可能有問題,因為程序使用制表符, 制表符在win7下存儲占2個字符 游戲是win10設計,設計時使用1個字符的制表符做圖畫。 

2.千萬不要Ctrl+C直接暴力停止游戲!要輸入“退出”,否則存檔不保存第二次進去無法加載存檔,正常退出會有file.close() 暴力退出不會close 。

3.不要輸錯任何一個東西!該寫數字的輸入字符串報錯,存檔同樣不保存!!

4.收錢最多有10分鐘上限,不會一直計時,計時從建造工作點開始算起。

二、運行環境

[開發環境]: 

python 3.6 運行代碼 

pycharm 2021 輔助敲代碼  第三方模塊 pip install 模塊名 

鏡像源安裝 pip install -i pypi.douban.com/simple/ +模塊名  +python 安裝包

三、代碼展示

主程序

#  特大都市  夢想小鎮
import random
import time as ctime
import os

# DATA
ROAD =           {"name":"road",           "level":1,  "cost":200,     "exp":1,  "people":False, "environment":0}
SMALLHOUSE =     {"name":"smallhouse",     "level":1,  "cost":12500,   "exp":5,  "people":(5,10,15), "environment":-5}
WOODENHOUSE =    {"name":"woodenhouse",    "level":2,  "cost":15000,   "exp":10, "people":(10,20,25), "environment":-10}
DOUBLECABIN =    {"name":"doublecabin",    "level":2,  "cost":35000,   "exp":15, "people":(30,50,80), "environment":-15}
BASICFARM   =    {"name":"basicfarm",      "level":1,  "cost":5000,    "exp":8,  "people":False, "earns":2000, "environment":-40}
EXTRAFARM   =    {"name":"extrafarm",      "level":2,  "cost":10000,   "exp":10, "people":False, "earns":4000, "environment":-80}
TREE1  =         {"name":"tree1",          "level":1,  "cost":1000,    "exp":3, "people":False, "environment":15}
SMALLLAKE  =     {"name":"smalllake",       "level":2,  "cost":500,    "exp":2, "people":False, "environment":10}
BAMBOO  =        {"name":"bamboo",          "level":3,  "cost":5000,    "exp":15, "people":False, "environment":40}
MARKET =         {"name":"market",          "level":3,  "cost":25000,    "exp":20, "people":False, "earns":7500, "environment":-20} 
DATA = [ROAD, SMALLHOUSE, WOODENHOUSE, DOUBLECABIN, BASICFARM, EXTRAFARM, TREE1, SMALLLAKE, BAMBOO, MARKET]

lv = [10, 15, 25, 50, 100, 200, 500, 1000]
CHA = {"road": 1, "smallhouse": 5, "woodenhouse": 10, "doublecabin": 15,
       "smallpark": 15, "basicfarm": 8, "extrafarm": 15, "tree1": 3}
earns = {"basicfarm":5000, "extrafarm":10000}
PEOPLE_BUILDINGS = ["smallhouse", "woodenhouse", "doublecabin"]
EARNING_BUILDINGS = ["basicfarm", "extrafarm", "market"]
ROADS = ["road1", "road2", "road3", "road4", "road5", "road6", "road7", "road8", "road9", "road10", "road11"]

#PICS2 [0]-empty [1]-smallhouse [2]-basicfarm [3]-woodenhouse [4]-doublecabin [5]-smallpark
#PICS2 [6]-road1 [7]-road2 [8]-road3 [9]-road4 [10]-road5 [11]-road6 [12]-road7
#PICS2 [13]-road8 [14]-road9 [15]-road10 [16]-road11
#PICS2 [17]-extrafarm [18]-tree1 [19]-bamboo [20]-water [21]-market
#4,5,6,7分別是上轉左,上轉右,下轉左,下轉右
# 4,5,6,7 left+up right+up left+down right+down
# 8,9,10,11 left+up+down right+up+down up+left+right down+left+right
PICS2 = [["┏━━━━━━━━━━━━━━┓", "┃              ┃","┃              ┃","┃              ┃",
         "┃              ┃","┃              ┃","┃              ┃","┗━━━━━━━━━━━━━━┛"],
         ["                ","   /^^^^^^^^\   ","/^^^^^^^^^^^^^^\\","┣┅┳┅┳━━━━┳┅┅┳┅┅┫",
         "┣┳┻┅┫    ┣━┳┻┳┅┫", "┣┻┅┳┫  ○ ┣┅┻┳┻┅┫","┣┅┳┻┫    ┣┅┳┻┳┅┫","┗┅┻┅┻┅┅┅┅┻┅┻┅┻┅┛"],
         ["┏━━━━━━━┳━━━━━━┓","┃□□□ □□□┃Basic ┃","┃□□□ □□□┃Farm  ┃","┃□□□ □□□┃      ┃",
         "┃□□□ □□□┃      ┃","┣━━━ ━━━┛      ┃","┃        2k/min┃","┗━━━━━━━━━━━━━━┛"],
         ["     ╔┉┉┉┉╗     ","  ╔╦┉╩┉┉┉┉╩┉╦╗  ","╔╦╬╬╦╦╦╦╦╦╦╦╬╬╦╗"," ╬╬ ╩ ╩  ╩ ╩ ╬╬ ",
         " ┋┋ ╔┉┉╗ ╔┉┉┉╣┋ "," ┋┋ ╠┉┉╣ ┋  ╔╣┋ "," ┋┋ ╚┉┉╝ ┋  ╚╣┋ ","╔╩╩┉┉┉┉┉┉╩┉┉┉╩╩╗"],
         ["----------------","|              |","|    Double    |","|    Cabin     |",
          "|              |","|              |","|              |","----------------"],
         ["----------------", "|              |", "|              |", "|  Small Park  |",
          "|              |", "|              |", "|              |", "----------------"],
         ["┃ ┃    ■■    ┃ ┃","┃ ┃    ■■    ┃ ┃","┃ ┃    ■■    ┃ ┃","┃ ┃    ■■    ┃ ┃",  # road 1
          "┃ ┃    ■■    ┃ ┃","┃ ┃    ■■    ┃ ┃","┃ ┃    ■■    ┃ ┃","┃ ┃    ■■    ┃ ┃"], 
         ["━━━━━━━━━━━━━━━━","━━━━━━━━━━━━━━━━","                "," ■■ ■■ ■■ ■■ ■■ ",  # road 2
          " ■■ ■■ ■■ ■■ ■■ ","                ","━━━━━━━━━━━━━━━━","━━━━━━━━━━━━━━━━"],
         ["  ┃    ■■    ┃  ","━━┛    ■■    ┗━━","       ■■       "," ■■ ■■ ■■ ■■ ■■ ",  # road 3
          " ■■ ■■ ■■ ■■ ■■ ","       ■■       ","━━┓    ■■    ┏━━","  ┃    ■■    ┃  "],
         ["━━━━━━━━━━━━━━━┓","━━━━━━━━━━━━━┓ ┃","             ┃ ┃"," ■■ ■■ ■■    ┃ ┃",  # 4
          " ■■ ■■ ■■    ┃ ┃","       ■■    ┃ ┃","━━┓    ■■    ┃ ┃","  ┃    ■■    ┃ ┃"], 
         ["┏━━━━━━━━━━━━━━━","┃ ┏━━━━━━━━━━━━━","┃ ┃             ","┃ ┃    ■■ ■■ ■■ ",  # 5
          "┃ ┃    ■■ ■■ ■■ ","┃ ┃    ■■       ","┃ ┃    ■■    ┏━━","┃ ┃    ■■    ┃  "],
         ["  ┃    ■■    ┃ ┃","━━┛    ■■    ┃ ┃","       ■■    ┃ ┃"," ■■ ■■ ■■    ┃ ┃",  #6
          " ■■ ■■ ■■    ┃ ┃","             ┃ ┃","━━━━━━━━━━━━━┛ ┃","━━━━━━━━━━━━━━━┛"],
         ["┃ ┃    ■■    ┃  ","┃ ┃    ■■    ┗━━","┃ ┃    ■■       ","┃ ┃    ■■ ■■ ■■ ",  # 7
          "┃ ┃    ■■ ■■ ■■ ","┃ ┃             ","┃ ┗━━━━━━━━━━━━━","┗━━━━━━━━━━━━━━━"],
         ["  ┃    ■■    ┃ ┃","━━┛    ■■    ┃ ┃","       ■■    ┃ ┃"," ■■ ■■ ■■    ┃ ┃", # 8
          " ■■ ■■ ■■    ┃ ┃","       ■■    ┃ ┃","━━┓    ■■    ┃ ┃","  ┃    ■■    ┃ ┃"],
         ["┃ ┃    ■■    ┃  ","┃ ┃    ■■    ┗━━","┃ ┃    ■■       ","┃ ┃    ■■ ■■ ■■ ", # 9
          "┃ ┃    ■■ ■■ ■■ ","┃ ┃    ■■       ","┃ ┃    ■■    ┏━━","┃ ┃    ■■    ┃  "],
         ["  ┃    ■■    ┃  ","━━┛    ■■    ┗━━","       ■■       "," ■■ ■■ ■■ ■■ ■■ ", # 10
          " ■■ ■■ ■■ ■■ ■■ ","                ","━━━━━━━━━━━━━━━━","━━━━━━━━━━━━━━━━"],
         ["━━━━━━━━━━━━━━━━","━━━━━━━━━━━━━━━━","                "," ■■ ■■ ■■ ■■ ■■ ", # 11
          " ■■ ■■ ■■ ■■ ■■ ","       ■■       ","━━┓    ■■    ┏━━","  ┃    ■■    ┃  "],
         ["┏━━━━━━━━━━━┳━━┓","┃□□□ □□□ □□□┃  ┃","┃□□□ □□□ □□□┃  ┃","┃□□□ □□□ □□□┃  ┃", # ExtraFarm
          "┃□□□ □□□ □□□┃  ┃","┣━━┛ ┗━━    ┛  ┃","┃FarmEX  4k/min┃","┗━━━━━━━━━━━━━━┛",],
         [" ┏━━━━━━━━━━━━┓ "," ┃  ┏┳━━━━┳┓  ┃ " ," ┃┏━┛┗┳━━┳┛┗━┓┃ " ," ┗┛   ┗┳┳┛   ┗┛ ", # Tree1
          "       ╋╋       ","       ╋╋       ","       ╋╋       ","       ┻┻       ",],
         ["      ╲┏┓╱      ","      ╲╋╋╱      ","  ╲┏┓╱╲╋╋╱      ","  ╲╋╋╱╲╋╋╱      ", # bamboo
          "  ╲╋╋╱╲╋╋╱ ╲┏┓╱ ","  ╲╋╋╱ ╋╋  ╲╋╋╱ ","   ╋╋  ╋╋  ╲╋╋╱ ","   ╋╋  ╋╋   ╋╋  "],
         ["┏━━━━━━━━━━━━━━┓","┃ ~~~ ~~   ~~  ┃","┃~~~ ~   ~~  ~~┃","┃  ~~  ~~ ~~  ~┃", # water
          "┃~ ~~~~  ~~    ┃","┃~~~   ~~~  ~~~┃","┃~~ ~ ~~~~~~  ~┃","┗━━━━━━━━━━━━━━┛"],
         ["  ╱╳╳╳╳╳╳╳╳╳╳╲  "," ╱╳╳╳╳╳╳╳╳╳╳╳╳╲ ","╱┳┻━━━━━━━━━━┻┳╲","┣┫   Market   ┣┫", # market
          "╋╈┳┳┳┳┳┳┳┳━━━┳╈╋","╋╈╈╈╈╈╈╈╈╈  ┏╈╈╋","╋╈╈╈╈╈╈╈╈╈  ┗╈╈╋","┻┻┻┻┻┻┻┻┻┻━━━┻┻┻"]] 


# 通過建筑名稱獲取建筑相關數據
def getdata(buildname, datatype):
    for data in DATA:
        if data["name"] == buildname:
            return data[datatype]

# 檢測是否已經有存檔存在
def checkloads():
    for load in os.listdir():
        if load.split(".")[-1] == "city":
            return True
    return False

# 獲得目錄下所有存檔
def getloads():
    loads = []
    for load in os.listdir():
        if load.split(".")[-1] == "city":
            loads.append(load)
    return loads

# 通過存檔還原地圖
def remap(s):
    resultmap = []
    new = []
    s1 = s.split("$")[:-1]
    for s2 in s1:
        s2 = s2[1:-1]
        tmp = s2.split(",")
        resultmap.append(tmp)
    for i in resultmap:
        jie = []
        for j in i:
            try:
                jie.append(int(j))
            except:
                jie.append(str(j[1:][1:-1]))
        new.append(jie)
    return new

# 通過存檔還原工作點
def rework(s):
    resultworkers = []
    parts = s.split("@")
    for part in parts:
        if part == "":
            continue
        resultworkers.append(part.split("#"))
    for i in range(0, len(resultworkers)):
        for j in range(0, len(resultworkers[i])):
            try:
                resultworkers[i][j] = float(resultworkers[i][j])
            except:
                pass
    return resultworkers[:-1]



# 玩家類
class Player():
    def __init__(self, name, type = "new"):
        self.type = type
        self.data = city.userdata.readlines()
        if self.type == "load":
            self.name = self.data[0].split()[-1]
            self.balance = int(self.data[1].split()[-1])
            self.level = int(self.data[2].split()[-1])
            self.exp = int(self.data[3].split()[-1])
            self.totalexp = int(self.data[4].split()[-1])
            self.nextexp = int(self.data[5].split()[-1])
        else:
            self.name = name
            self.balance = 50000
            self.level = 1
            self.exp = 0
            self.totalexp = 0
            self.nextexp = 10
            city.userdata.write("username: " + self.name + "\n")
            city.userdata.write("balance: " + str(self.balance) + "\n")
            city.userdata.write("level: " + str(self.level) + "\n")
            city.userdata.write("exp: " + str(self.exp) + "\n")
            city.userdata.write("totalexp: " + str(self.totalexp) + "\n")
            city.userdata.write("nextexp: " + str(self.nextexp) + "\n")


    def earn(self, value):
        self.balance += value

    def cost(self, value):
        if value <= self.balance:
            self.balance -= value
        else:
            return False

    def addexp(self, num):
        self.totalexp += num
        self.exp += num
        if self.exp < lv[self.level - 1]:
            print("你獲得了" + str(num) + "經驗!")
            return
        while self.exp >= lv[self.level - 1]:
            self.exp = self.exp - lv[self.level - 1]
            self.level += 1
            self.nextexp = lv[self.level - 1] - self.exp
            print("恭喜! 您升入等級" + str(self.level) + "!")

    def show(self):
        print("市長:" + self.name)
        print("金錢:" + str(self.balance))
        print("等級:" + str(self.level), " 經驗:" + str(self.exp))
        print()

# 城市工作經營類
class Worker():
    def __init__(self, name, type, money):
        T = ctime.time()
        self.name = name
        self.type = type
        self.oncemoney = money
        self.totaltime = 0 # 游戲總時長
        self.time = 0 # 距離上次獲得利潤時間
        self.starttime = T # 起始時間刻(每局常量)
        self.nowtime = T # 現在時間刻
        self.lastearntime = T # 上次盈利時間
        self.totalearn = 0 # 盈利總金額

    # 刷新現在的時間刻
    def update(self):
        self.nowtime = ctime.time()
        self.totaltime = int(self.nowtime - self.starttime)
        self.time = int(self.nowtime - self.lastearntime)

    # 盈利
    def earn(self):
        # 這里規定 單次收取的最大利潤不可以大于10min所獲得的利潤
        self.update()
        if int(self.time) < 60:
            return False
        self.lastearntime = ctime.time()
        earns = (int(self.time) // 60) * self.oncemoney
        if earns > 10 * self.oncemoney:
            earns = 10 * self.oncemoney
        self.totalearn += earns
        return earns



# 城市類
class City():
    def __init__(self, name, size = "8x8", type = "new", username = "/"):
        self.name = name
        self.height = int(size.split("x")[1])
        self.width = int(size.split("x")[0])
        self.map = [[j, i, "empty"] for i in range(1, self.height+1) for j in range(1, self.width+1)]
        self.environment = 100
        self.env_type = "perfect"
        self.people = 0
        self.people_env_list = [0, 0, 0] # 應當反映的人口數量和環境狀態的關系 bad-good-perfect 順序
        
        self.earn = 0
        self.housecount = 0
        self.workcount = 0
        self.parkcount = 0
        self.buildcount = 0

        self.isfull = False
        self.lasttime = 0
        self.workers = []

        self.filedata = []
        self.f, self.f2 = 0, 0
        if type == "new":
            self.f = open("{}.city".format(name), "w+")
            self.userdata = open("{}.user".format(name), "w+")
            self.log = open("{}.log".format(name), "a+")
            '''
            userdata.write("username: "+ username +"\n")
            userdata.write("balance: 50000\n")
            userdata.write("level: 1\n")
            userdata.write("exp: 0\n")
            userdata.write("totalexp: 0\n")
            userdata.write("nextexp: 0\n")
            '''
            self.writedata("創建存檔\n")
            self.f.write("cityname: " + self.name + "\n")
            self.f.write("height: " + str(self.height) + "\n")
            self.f.write("width: " + str(self.width) + "\n")
            self.f.write("environment: " + str(self.environment) + "\n")
            self.f.write("people: " + str(self.people) + "\n")
            self.f.write("people_env_list: " + str(self.people_env_list)[1:-1] + "\n")
            workers_data = self.workers_data_change()
            self.f.write("workers: " + str(workers_data) + "\n")  
            self.f.write("earn: " + str(self.earn) + "\n")
            self.f.write("housecount: " + str(self.housecount) + "\n")
            self.f.write("workcount: " + str(self.workcount) + "\n")
            self.f.write("parkcount: " + str(self.parkcount) + "\n")
            self.f.write("buildcount: " + str(self.buildcount) + "\n")
            self.f.write("citymap: " + str(self.map))

        elif type == "load":
            try:
                self.f = open("{}.city".format(name), "r+")
                self.userdata = open("{}.user".format(name), "r+")
                self.log = open("{}.log".format(name), "a+")

            except:
                print("錯誤!沒有找到存檔!")
        self.filedata = self.f.readlines()
        if type == "load":
            self.name = self.filedata[0].split()[-1]
            self.height = int(self.filedata[1].split()[-1])
            self.width = int(self.filedata[2].split()[-1])
            self.environment = int(self.filedata[3].split()[-1])
            self.people = int(self.filedata[4].split()[-1])
            self.people_env_list = self.filedata[5][17:-1].split(", ")
            for i in range(0, len(self.people_env_list)):
                self.people_env_list[i] = int(self.people_env_list[i])
            
            workerslist = rework(self.filedata[6].split(":")[-1])
            self.map = remap(self.filedata[12][9:])
            for worker in workerslist:
                tmpworker = Worker(worker[0], worker[1], worker[2])
                tmpworker.totaltime = worker[3]
                tmpworker.time = worker[4]
                tmpworker.starttime = worker[5]
                tmpworker.nowtime = worker[6]
                tmpworker.lastearntime = worker[7]
                tmpworker.totalearn = worker[8]
                self.workers.append(tmpworker)
            self.workcount = len(self.workers)

    # 建造建筑
    def build(self, what, where):
        w = self.changexy(where)
        for i in range(0, len(self.map)):
            if self.map[i][0] == int(w[0]) and self.map[i][1] == int(w[1]) and self.map[i][2] == "empty":
                self.map[i][2] = what
                break
        if what in PEOPLE_BUILDINGS:
            self.housecount += 1
            self.people_change(what, "increase")
        if what in EARNING_BUILDINGS:
            self.workcount += 1
            worker = Worker(str(len(self.workers) + 1), what, self.getplacedata(what, "earns"))
            self.workers.append(worker)

        self.changemap()
        self.writedata("在 {} 處建造 {} 建筑\n".format(where, what))
        self.show()

    # 道路建造
    def buildroad(self, where, isShow=True):
        w = self.changexy(where)
        x, y = w[0], w[1]
        if self.getplace(x, y) != "empty":
            return False

        self.map[self.getmapnum(x, y)][2] = "road3"
        self.roadmove(x, y, 1)

        # 判斷1:如果目標道路上下左右都沒有道路,建road3十字路口
        if self.getplace(x + 1, y) not in ROADS and self.getplace(x - 1, y) not in ROADS and self.getplace(x,
                                                                                                           y + 1) not in ROADS and self.getplace(
                x, y - 1) not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road3"
        else:
            self.roadcheck(x, y)

        self.writedata("在 {} 處建造道路\n".format(where))
        self.changemap()
        if isShow:
            self.show()

    # 更改.city的數據
    def changemap(self):
        self.f.close()
        os.remove("{}.city".format(self.name))
        ctime.sleep(0.1)
        self.f = 0
        self.f = open("{}.city".format(self.name), "w+")
        self.f.write("cityname: " + self.name + "\n")
        self.f.write("height: " + str(self.height) + "\n")
        self.f.write("width: " + str(self.width) + "\n")
        self.f.write("environment: " + str(self.environment) + "\n")
        self.f.write("people: " + str(self.people) + "\n")
        self.f.write("people_env_list: " + str(self.people_env_list)[1:-1] + "\n")
        workers_data = self.workers_data_change()
        self.f.write("workers: " + str(workers_data) + "\n")  
        self.f.write("earn: " + str(self.earn) + "\n")
        self.f.write("housecount: " + str(self.housecount) + "\n")
        self.f.write("workcount: " + str(self.workcount) + "\n")
        self.f.write("parkcount: " + str(self.parkcount) + "\n")
        self.f.write("buildcount: " + str(self.buildcount) + "\n")
        self.f.write("citymap: ")
        for i in self.map:
            self.f.write(str(i) + "$")

        self.f.seek(0, 0)
        self.filedata = self.f.readlines()


    # 變換坐標
    def changexy(self, string):
        s = string.split(",")
        return int(s[0]), int(s[1])

    # 更改.user的用戶信息(錢數,等級等等)
    def changeuserdata(self, name, balance, level, exp, totalexp, nextexp):
        self.userdata.close()
        self.userdata = open("{}.user".format(self.name), "w+")
        self.userdata.write("username: " + name + "\n")
        self.userdata.write("balance: " + str(balance) + "\n")
        self.userdata.write("level: " + str(level) + "\n")
        self.userdata.write("exp: " + str(exp) + "\n")
        self.userdata.write("totalexp: " + str(totalexp) + "\n")
        self.userdata.write("nextexp: " + str(nextexp) + "\n")
    
    def check(self):
        return self.height, self.width, self.map

    # 檢測坐標上是否是空位
    def checkempty(self, where):
        w = self.changexy(where)
        for i in range(0, len(self.map)):
            if self.map[i][0] == int(w[0]) and self.map[i][1] == int(w[1]) and self.map[i][2] == "empty":
                return True
            elif self.map[i][0] == int(w[0]) and self.map[i][1] == int(w[1]) and self.map[i][2] != "empty":
                return False

    # 環境狀態監測
    def checkenv(self):
        if self.environment < 0:
            self.env_type = "bad"
        elif 0 < self.environment and self.environment < 100:
            self.env_type = "good"
        elif self.environment > 100:
            self.env_type = "perfect"

    # 檢測坐標是否超出范圍
    def checkout(self, where):
        w = self.changexy(where)
        if w[0] <= self.width and w[1] <= self.height and w[0] > 0 and w[1] > 0:
            return True
        else:
            return False

    # 檢測城市是否滿格
    def checkplace(self):
        isOk = True
        for i in range(0, len(self.map)):
            if self.map[i][2] == "empty":
                self.isfull = False
                return
        self.isfull = True

    # 檢查工作點并返回工作點的信息列表
    def checkworkplace(self):
        res = []
        for i in self.workers:
            i.update()
            res.append(str(int(float(i.name))) + " " + i.type + " 工作時間:" + str(i.time) + "秒")
        return res

    # 商業性建筑的盈利
    def earnmoney(self, num):
        for i in self.workers:
            if int(i.name) == num:
                result = i.earn()
                if result == False:
                    return None
                else:
                    self.writedata("收取 {} 建筑的利潤 ¥{} \n".format(i.type, result))
                    return result

    # 獲取系統內部圖的序號
    def getmapnum(self, xray, yray):
        for i in range(0, len(self.map)):
            if self.map[i][0] == xray and self.map[i][1] == yray:
                return i
        return False

    # 獲取該位置上的建筑物
    def getplace(self, xray, yray):

        if xray == 0 or yray == 0 or xray > self.width or yray > self.height:
            return False
        
        for target in self.map:
            if target[0] == xray and target[1] == yray:
                return target[2]
        return False

    # 通過建筑名稱獲取建筑的所有信息 tag 表示信息種類
    def getplacedata(self, placename, tag):
        for data in DATA:
            if data["name"] == placename:
                return data[tag]
        return False
        
    # 動態更新人口
    def people_change(self, what = None, mode = "increase"):
        if what != None:
            peoples = self.getplacedata(what, "people")
            self.people_env_list[0] += (peoples[0] if mode == "increase" else -peoples[0])
            self.people_env_list[1] += (peoples[1] if mode == "increase" else -peoples[1])
            self.people_env_list[2] += (peoples[2] if mode == "increase" else -peoples[2])

        # 動態更新
        if self.env_type == "bad": self.people = self.people_env_list[0]
        elif self.env_type == "good": self.people = self.people_env_list[1]
        elif self.env_type == "perfect": self.people = self.people_env_list[2]

    # 拆除建筑
    def remove(self, where, isShow = True):
        w = self.changexy(where)
        removething = ""
        for i in range(0, len(self.map)):
            if self.map[i][0] == int(w[0]) and self.map[i][1] == int(w[1]):
                removething = self.map[i][2]
                self.map[i][2] = "empty"
                if removething in ROADS:
                     # 拆除的如果是道路,拆除后對其他道路的朝向重新檢測 
                    self.roadmove(int(w[0]), int(w[1]), 2)
                break
        if removething in PEOPLE_BUILDINGS:
            self.housecount -= 1
            self.people_change(removething, "decrease")
        self.changemap()
        
        self.writedata("在 {} 處拆除 {}\n".format(where, removething))
        if isShow:
            self.show()

    # 判斷一個道路四周的道路方向并加以調整
    def roadcheck(self, x, y):
        placesnear = [self.getplace(x-1, y-1), self.getplace(x, y-1), self.getplace(x+1, y-1),
                      self.getplace(x-1, y),   self.getplace(x, y),   self.getplace(x+1, y)  ,
                      self.getplace(x-1, y+1), self.getplace(x, y+1), self.getplace(x+1, y+1)]
        #print(x, y, self.getplace(x, y))
        # 前判斷:如果原點不是道路,直接返回
        if placesnear[4] not in ROADS:
            return
        # 判斷1:如果一方出現道路,那么將一方的道路改為單向道路
        if placesnear[1] in ROADS and placesnear[3] not in ROADS and placesnear[5] not in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road1"
        elif placesnear[3] in ROADS and placesnear[1] not in ROADS and placesnear[5] not in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road2"
        elif placesnear[5] in ROADS and placesnear[1] not in ROADS and placesnear[3] not in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road2"
        elif placesnear[7] in ROADS and placesnear[1] not in ROADS and placesnear[3] not in ROADS and placesnear[5] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road1"
        # 判斷2:雙連路口 
        elif placesnear[1] in ROADS and placesnear[3] in ROADS and placesnear[5] not in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road6"
        elif placesnear[1] in ROADS and placesnear[5] in ROADS and placesnear[3] not in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road7"
        elif placesnear[1] in ROADS and placesnear[7] in ROADS and placesnear[3] not in ROADS and placesnear[5] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road1"
        elif placesnear[3] in ROADS and placesnear[5] in ROADS and placesnear[1] not in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road2"
        elif placesnear[3] in ROADS and placesnear[7] in ROADS and placesnear[1] not in ROADS and placesnear[5] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road4"
        elif placesnear[5] in ROADS and placesnear[7] in ROADS and placesnear[1] not in ROADS and placesnear[3] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road5"
        # 判斷3:丁字路口
        elif placesnear[1] in ROADS and placesnear[3] in ROADS and placesnear[5] in ROADS and placesnear[7] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road10"
        elif placesnear[1] in ROADS and placesnear[3] in ROADS and placesnear[7] in ROADS and placesnear[5] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road8"
        elif placesnear[1] in ROADS and placesnear[5] in ROADS and placesnear[7] in ROADS and placesnear[3] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road9"
        elif placesnear[3] in ROADS and placesnear[5] in ROADS and placesnear[7] in ROADS and placesnear[1] not in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road11" 
        # 判斷4:十字路口
        elif placesnear[1] in ROADS and placesnear[3] in ROADS and placesnear[5] in ROADS and placesnear[7] in ROADS:
            self.map[self.getmapnum(x, y)][2] = "road3" 

    # 動態道路分析判斷
    def roadmove(self, x, y, time):
        # time 遞歸次數(拓展范圍) 
        #print(x, y, self.getplace(x, y), time)
        if time == -1:
            return
        #if self.getplace(x, y) not in ROADS :
            #return

        self.roadcheck(x, y)
        # 遞歸
        self.roadmove(x-1, y-1, time-1)
        self.roadmove(x, y-1, time-1)
        self.roadmove(x+1, y-1, time-1)
        self.roadmove(x-1, y, time-1)
        self.roadmove(x+1, y, time-1)
        self.roadmove(x-1, y+1, time-1)
        self.roadmove(x, y+1, time-1)
        self.roadmove(x+1, y+1, time-1)

    # 打印城市模型
    def show(self):
        def printlines(things):
            oneline = ""
            for line in range(0, 8):
                for i in things:
                    if i == "empty":
                        oneline += PICS2[0][line]
                    elif i == "smallhouse":
                        oneline += PICS2[1][line]
                    elif i == "basicfarm":
                        oneline += PICS2[2][line]
                    elif i == "woodenhouse":
                        oneline += PICS2[3][line]
                    elif i == "doublecabin":
                        oneline += PICS2[4][line]
                    elif i == "smallpark":
                        oneline += PICS2[5][line]
                    elif i == "road1":
                        oneline += PICS2[6][line]
                    elif i == "road2":
                        oneline += PICS2[7][line]
                    elif i == "road3":
                        oneline += PICS2[8][line]
                    elif i == "road4":
                        oneline += PICS2[9][line]
                    elif i == "road5":
                        oneline += PICS2[10][line]
                    elif i == "road6":
                        oneline += PICS2[11][line]
                    elif i == "road7":
                        oneline += PICS2[12][line]
                    elif i == "road8":
                        oneline += PICS2[13][line]
                    elif i == "road9":
                        oneline += PICS2[14][line]
                    elif i == "road10":
                        oneline += PICS2[15][line]
                    elif i == "road11":
                        oneline += PICS2[16][line]
                    elif i == "extrafarm":
                        oneline += PICS2[17][line]
                    elif i == "tree1":
                        oneline += PICS2[18][line]
                    elif i == "bamboo":
                        oneline += PICS2[19][line]
                    elif i == "smalllake":
                        oneline += PICS2[20][line]
                    elif i == "market":
                        oneline += PICS2[21][line]
                print(oneline)
                oneline = ""
        tmp, things = 0, []
        for i in range(0, len(self.map)):
            things.append(self.map[i][2])
            tmp += 1
            if tmp == self.width:
                printlines(things)
                tmp, things = 0, []
                
    # 打印城市信息
    def show_information(self):
        self.update()
        print("城市名稱:" + self.name)
        print("金錢:{} 等級 {} 距下一級還需 {} 經驗".format(player.balance, player.level, player.nextexp))
        print("人口:" + str(self.people))
        print("環境指數:" + str(self.environment), self.env_type.capitalize())
        print()

    # 有事沒事更新一下游戲各種數據~
    def update(self):
        player.nextexp = lv[player.level - 1] - player.exp # 更新下一等級所需經驗數
        self.checkenv() # 更新環境等級
        self.people_change()  # 依照環境更新人口
            
    # 改變Worker類的數據為可見的列表
    def workers_data_change(self):
        result = ""
        if self.workers == []:
            return []
        for each in self.workers:
            temp = str(each.name) + "#" + str(each.type) + "#" + str(each.oncemoney) + "#" +str(each.totaltime) + "#" +str(each.time) + "#" +str(each.starttime) + "#" +str(each.nowtime) + "#" +str(each.lastearntime) + "#" +str(each.totalearn)
            result += temp + "@"
        
        return result

    # 工作時間檢查
    def worktimecheck(self):
        res = self.checkworkplace()

    # 記錄玩家操作日志
    def writedata(self, thing):
        self.log.write(ctime.strftime("%Y-%m-%d %H:%M:%S") + " " + thing)

        
# 初始化城市
isRead = False
print("----------歡迎來到Megapolis!----------")
print("市長,我們該如何稱呼您:")
name = input()
if checkloads():
    loads = []
    print("系統檢測到您已經有存檔,存檔如下:")
    loads = getloads()
    print(loads)
    print("您想選擇打開存檔,還是新建一個城市?(打開:1/新建:2)")
    user = int(input())
    if user == 1:
        print("您想打開哪一個存檔?輸入序號:")
        num = int(input())
        load = loads[num - 1]
        print("正在加載中,請稍后......")
        isRead = True
    elif user == 2:
        print("您好, " + name + ", 現在,為您的城市創建一個新名稱:")
        cityname = input()
        print("您的城市大小是多少? (運用數字x數字的方式,例如 4x4, 8x8)")
        citysize = input()
        print("城市創建中,請稍后......")
else:
    print("您好, " + name + ", 現在,為您的城市創建一個新名稱:")
    cityname = input()
    print("您的城市大小是多少? (運用數字x數字的方式,例如 4x4, 8x8)")
    citysize = input()
    print("城市創建中,請稍后......")
# 創造并打印城市
if isRead:
    city = City(load.split(".")[0], "1x1", "load")
    # 創造玩家
    player = Player(name, type = "load")
else:
    city = City(cityname, citysize)
    player = Player(name)
city.show()
player.show()

def fast_road_building():
    print("您要把道路安放在哪些地方?(用2,3 2,4 2,5這樣的連續坐標來安置道路,坐標之間要加空格哦)")
    while True:
        try:
            wheres = input()
            if wheres == "退出":
                print("好的,已經退出~")
                return
            else:
                wheres = wheres.split()
        except:
            print("輸入有誤,請重新嘗試哦~")
        else:
            not_empty_places = []
            is_repeated = True if len(list(set(wheres))) != len(wheres) else False
            wheres = list(set(wheres))
            print(wheres)
            all_count = len(wheres)
            # 制作一個wheres的拷貝,方便完整遍歷where
            wheres_copy = wheres.copy()
            for where in wheres_copy:
                if not city.checkempty(where) or not city.checkout(where):
                    not_empty_places.append(where)
                    wheres.remove(where)

            rest_empty_places = (all_count - len(not_empty_places))
            total_price = 200 * rest_empty_places
            total_exp = 2 * rest_empty_places
            if is_repeated:
                print("您輸入了重復的坐標,已經自動幫您去除重復~")
            if len(wheres) == 0:
                print("您所描述的位置上均有建筑或位于城市區域之外,無法建造道路!")
                return
            elif len(not_empty_places) != 0:
                print("您所描述的位置中,{} 位置不能建造道路。剩余可建造的道路總價為¥{}".format(str(not_empty_places)[1:-1], total_price))
            else:
                print("建造這些道路需要¥{}".format(total_price))
            print("繼續建造嗎?(建造/不建造)")
            is_build = input()
            if is_build == "建造":
                player.cost(total_price)
                player.addexp(total_exp)
                city.changeuserdata(player.name, player.balance, player.level, player.exp, player.totalexp,
                                    player.nextexp)
                for where in wheres:
                    city.buildroad(where, isShow = False)
                city.show()
                player.show()
                return
            else:
                print("好的,已經取消建造!")
                return

def fast_removing():
    print("您要拆除哪些地方的建筑?(用2,3 2,4 2,5這樣的連續坐標來拆除建筑,坐標之間要加空格哦)")
    while True:
        try:
            wheres = input()
            if wheres == "退出":
                print("好的,已經退出~")
                return
            else:
                wheres = wheres.split()
        except:
            print("輸入有誤,請重新嘗試哦~")
        else:
            not_empty_places = []
            is_repeated = True if len(list(set(wheres))) != len(wheres) else False
            wheres = list(set(wheres))
            print(wheres)
            all_count = len(wheres)
            # 制作一個wheres的拷貝,方便完整遍歷where
            wheres_copy = wheres.copy()
            for where in wheres_copy:
                if city.checkempty(where) or not city.checkout(where):
                    not_empty_places.append(where)
                    wheres.remove(where)

            rest_empty_places = (all_count - len(not_empty_places))
            total_price = 200 * rest_empty_places
            total_exp = 2 * rest_empty_places
            if is_repeated:
                print("您輸入了重復的坐標,已經自動幫您去除重復~")
            if len(wheres) == 0:
                print("您所描述的位置上均沒有可拆除的建筑或位于城市區域之外,無法拆除!")
                return
            elif len(not_empty_places) != 0:
                print("您所描述的位置中,{} 位置無法拆除。".format(str(not_empty_places)[1:-1]))
            print("繼續拆除嗎?(拆除/不拆除)")
            is_build = input()
            if is_build == "拆除":
                for where in wheres:
                    city.remove(where, isShow = False)
                city.show()
                player.show()
                return
            else:
                print("好的,已經取消拆除!")
                return


def choose_building(housetypes, prices):
    l = len(housetypes)
    if housetypes == "road":
        if player.balance < prices:
            print("市長,您沒有足夠的金錢建造道路!\n")
            return
        elif city.isfull:
            print("市長,您的城市空位不足!\n")
            return
        else:
            print("您想要把道路安放在何處:(運用 x,y 例如 3,3)")
            while True:
                where = input()
                if where == "退出":
                    print("好的,已退出建造!")
                    break
                if not city.checkout(where):
                    print("市長,您不能把道路放在城市土地之外!")
                elif not city.checkempty(where):
                    print("市長,這塊地上已經有建筑了,請換一塊地!")
                else:
                    break

            player.cost(prices)
            player.addexp(2)
            city.changeuserdata(player.name, player.balance, player.level, player.exp, player.totalexp, player.nextexp)
            city.buildroad(where)
            player.show()
            return
    while True:
        print("請告訴我建筑物的序號(用0來停止):")
        num = int(input())
        if num > l or num < 0:
            print("沒有這個序號的建筑物,請重試!")
        elif num == 0:
            print("好的,已經退出選擇!")
            break
        else:
            if player.level < getdata(housetypes[num - 1], "level"):
                print("市長,您的等級不夠建造此建筑!\n")
                break
            if player.balance < prices[num - 1]:
                print("市長,您沒有足夠的金錢建造該建筑!\n")
                break
            elif city.isfull:
                print("市長,您的城市空位不足!\n")
                break
            else:
                print("您想要把該建筑安放在何處:(運用 x,y 例如 3,3)")
                while True:
                    where = input()
                    if not city.checkout(where):
                        print("市長,您不能把道路放在城市土地之外!")
                    elif not city.checkempty(where):
                        print("市長,這塊地上已經有建筑了,請換一塊地!")
                    else:
                        break

                player.cost(prices[num - 1])
                city.environment += int(getdata(housetypes[num - 1], "environment"))
                city.checkenv()
                city.build(housetypes[num - 1], where)
                player.addexp(city.getplacedata(housetypes[num - 1], "exp"))
                city.changeuserdata(player.name, player.balance, player.level, player.exp, player.totalexp, player.nextexp)
                player.show()
                break

# 主循環
while True:
    city.update()
    print("親愛的市長,您想做些什么?(建造/鏟除建筑/查看信息/盈利/退出)")
    users = input()
    if users == "建造":
        print("您想建造什么樣的建筑?(房屋/工作點/道路/綠化)")
        kind = input()
        if kind == "房屋":
            print("以下是一些種類的房屋:")
            print("1.小房子      價格:12500  需求等級:1 容納人數:5/10/15  環境指數:-5")
            print("2.精致木屋    價格:15000  需求等級:2 容納人數:10/20/25 環境指數:-10")
            print("3.雙層旅館    價格:35000  需求等級:2 容納人數:30/50/80 環境指數:-15")
            choose_building(["smallhouse", "woodenhouse", "doublecabin"],
                            [12500, 15000, 35000])

        elif kind == "工作點":
            print("以下是一些種類的工作點(利潤單位為元/分鐘):")
            print("1.基礎農場     價格:5000  需求等級:1 利潤:¥2000/min  環境指數:-40")
            print("2.大型農場     價格:15000 需求等級:2 利潤:¥4000/min  環境指數:-80")
            print("3.小商店       價格:25000 需求等級:3 利潤:¥7500/min  環境指數:-20")
            choose_building(["basicfarm", "extrafarm", "market"],
                            [5000, 15000, 25000])
        elif kind == "道路":
            print("道路的價格為¥200/1格")
            print("選擇快速建造道路模式嗎?(是/否)")
            is_fast_road_building = input()
            if is_fast_road_building == "是":
                fast_road_building()
            else:
                choose_building("road", 200)
        elif kind == "綠化":
            print("以下是不同種類的綠化:")
            print("1.樹木       價格:1000 需求等級:1 環境指數:+15")
            print("2.小型湖泊   價格:500 需求等級:2 環境指數:+10")
            print("3.竹林       價格:5000 需求等級:3 環境指數:+40")
            choose_building(["tree1", "smalllake", "bamboo"], [1000, 500, 5000])
        else:
            print("不好意思,您輸入的建筑類型還不存在哦~")
    elif users == "鏟除建筑":
        print("選擇快速拆除建筑模式嗎?(是/否)")
        is_fast_removing = input()
        if is_fast_removing == "是":
            fast_removing()
        else:
            print("輸入你想鏟除的建筑坐標:(運用 x,y 例如 3,3)")
            while True:
                where = input()
                if where == "退出":
                    print("好的,已經退出拆除界面!")
                    break
                if not city.checkout(where):
                    print("市長,您不能鏟除城市土地之外的建筑,請重新嘗試!")
                elif city.checkempty(where):
                    print("這塊土地上沒有建筑可以鏟除,請重新嘗試!")
                else:
                    city.remove(where)
                    player.show()
                    break

    elif users == "查看信息":
        city.show_information()
    elif users == "盈利":
        if city.workcount == 0:
            print("市長,您還沒有任何的工作點!")
        else:
            result = city.checkworkplace()
            print("您有以下幾個工作點:")
            print(result)
            print("您想要選擇哪一個? 請告訴我序號:")
            index = int(input())
            tmpmoney = city.earnmoney(int(result[index - 1].split()[0]))
            money = None if tmpmoney == None else int(tmpmoney)
            if money == None:
                print("等一下,市長!工人們還沒有生產完畢!")
            else:
                player.earn(money)
                city.changeuserdata(player.name, player.balance, player.level, player.exp, player.totalexp, player.nextexp)
                city.changemap()
                print("您獲得了 ¥" + str(money) + "!")
    elif users == "退出":
        print("歡迎下次再來, " + name + "!")
        break
    
city.f.close()
city.log.close()
city.userdata.close()

四、效果展示

這款游戲是經營類:可建造房屋、工作、綠化等。會產出、消耗相應的金錢等。

1)城市示咧

Python?Pygame如何實現經營類游戲

2)隨機截圖

Python?Pygame如何實現經營類游戲

讀到這里,這篇“Python Pygame如何實現經營類游戲”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

奉节县| 大足县| 镇康县| 固安县| 厦门市| 青田县| 四子王旗| 石渠县| 隆子县| 合川市| 烟台市| 左权县| 奇台县| 电白县| 德州市| 桦川县| 黔西县| 巴彦淖尔市| 亚东县| 成安县| 柳江县| 嘉义市| 方山县| 章丘市| 景德镇市| 洪湖市| 新蔡县| 昌都县| 嵩明县| 横山县| 毕节市| 咸阳市| 钟祥市| 南汇区| 湾仔区| 华蓥市| 乐山市| 仙桃市| 青铜峡市| 武隆县| 临夏市|