从0开始用python写一个命令行小游戏(五)

news/2024/5/6 23:10:20

今天我时间有点紧张,所以不说废话了,直接进入正题。前作链接:

  1. 从0开始用python写一个命令行小游戏(一)
  2. 从0开始用python写一个命令行小游戏(二)
  3. 从0开始用python写一个命令行小游戏(三)
  4. 从0开始用python写一个命令行小游戏(四)

用户界面:第2.5步(第三步的前半步)

上次我们的Game类是这样的:

import game_obj as o

class Game:
    def __init__(self):
        o.sunlight = 50
        o.board = [0] * 10
        self.sunlight = o.sunlight
        self.board = o.board
        import json
        with open("level.json") as fr:
            self.steps = json.load(fr)
    def step(self):
        print("Sunlight: %d." % self.sunlight)
        print("Current state:")
        for obj in self.board:
            if isinstance(obj, o.GameObject):
                obj.step()
            print(obj, end='  ')

这个类离全自动还差这些元素:

  • 自动出现的僵尸;
  • 用户可控的植物;
  • 自动重复执行step()的方法。

下面就先解决前两个!

自动出现的僵尸

之前,我们已经有了配置文件。我们现在要做的就是每步都看看这一步有没有在配置文件中出现。

import game_obj as o

class Game:
    def __init__(self):
        o.sunlight = 50
        o.board = [0] * 10
        self.sunlight = o.sunlight
        self.board = o.board
        self.step_num = 0
        import json
        with open("level.json") as fr:
            self.steps = json.load(fr)
    def step(self):
        self.step_num += 1
        print("Sunlight: %d." % self.sunlight)
        print("Current state:")
        for obj in self.board:
            if isinstance(obj, o.GameObject):
                obj.step()
            print(obj, end='  ')
        if str(self.step_num) in self.steps.keys():
            action = self.steps[str(self.step_num)]
            if action == "zombie":
                o.Zombie(9)
            elif action == "exit zombie":
                o.Zombie(9, die_to_exit=True)

好!现在,游戏可以自动产生僵尸了。然后呢?

用户可控的植物

真正的植物大战僵尸游戏可以让玩家用鼠标控制游戏。由于这是命令行游戏,所以我们得用命令控制。我突然发现,居然还得编写处理命令的方法!

def process_command(self, commands):
    for command in commands:
        command_list = command.split()
        if command_list[0] == 'plant' and len(command_list) == 3:
            plant_type = command_list[1]
            try:
                pos = int(command_list[2])
            except ValueError:
                print("Invalid command.")
            else:
                if plant_type == 's':
                    o.Sunflower(pos)
                elif plant_type == 'p':
                    o.Peashooter(pos)
        else:
            print("Invalid command.")

好,用用它吧(当然,是在step()里面)!

def step(self):
    pass            # 同前
    first_command = input("next step: ")
    if first_command:
        commands = [first_command]
        next_command = 'some content'
        while next_command:
            next_command = input("        -: ")
            commands.append(next_command)
    else:
        commands = []
    self.process_command(commands)

后来我又知道,可以把不依赖实例的方法声明为@staticmethod,并把self参数去掉,于是把process_command改为:

@staticmethod
def process_command(commands):
    pass           # 同前

好了!至此,我们的三个需求只剩一个了,而这一个将会在第三步的后半步解决!欢迎继续关注!


http://www.niftyadmin.cn/n/3089502.html

相关文章

apache php编译安装,linux系统下apache+php环境编译安装配置过程记录

linux系统下apachephp环境编译安装配置过程记录来源:IDC机房作者:梦飞浏览量:252016-05-250第1章 环境说明1.1 系统说明Centos 6.2 (最小化安装)1.2 软件说明httpd-2.4.2.tar.gzapr-util-1.4.1.tar.gzapr-1.4.6.tar.gzpcre-8.13.tar.gzphp-5…

通过代码解决SharePoint列表视图权限分配问题【转】

最近被SharePoint列表视图分权限的问题困扰着,在网上搜到些解决方案,比较不错的是Bewise.SharePoint.SPViewPermissionSetting的解决方案,但是在修改视图权限配置的时候经常出问题,不知道是什么原因,后来从codeplex上找…

Zabbix - 配置服务器对第三方服务的监控

需求: 需要配置zabbix监控,使得zabbix服务器可以监控到另一台服务器上运行的第三方服务的状态,当状态异常时发送告警邮件。 限制:被监控的服务器不允许安装任意客户端,且该台服务器不能联通外网 对zabbix的各种监控方式…

【原】小软件开发心得(二)——推广、测试

正如上一篇文章最后所述,相比开发软件本身,推广软件技术含量似乎更高,而测试则是更耗体力的工作。 推广 让自己的软件被更多的人使用,这可能是很多开发者的心愿,而且这往往和经济利益什么的不搭界,纯粹是一…

php+html+转码,PHP解析html类库simple_html_dom的转码bug

这几天有在用simple_html_dom抓一些文章。不同网站的编码在国内基本上是gbk gb2312 utf-8。而以gb2312和utf-8居多。我这一版的simple_html_dom有一个方法 convert_text 是这个样子的。// PaperG - Function to convert the text from one character set to another if the two…

个人常用的linux/unix命令

私人mac配置环境变量文件位置:个人用户下: vim /etc/profile 建议环境变量文件位置: touch ~/.bash_profile 复制代码删除文件rm [options] [fileName] -r : 递归删除子目录 -f : 禁用确认 -i : 删除前逐一询问确认 -v : 显示删除细节 复制代码移动文件夹…

不畏将来

自己的人生道路,除了自己,没有任何人可以为之负责。去相信所有遇到的人都是人生中注定要遇到的人,遇到喜欢的人,就认真的爱吧,遇到不喜欢的人,也认真的对待,不辜负这番注定的缘分。人生中所有经…

第三讲 多重背包问题(对背包九讲的学习)

题目 有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。 基本思路: 对每个物品都考虑拿几个(这个很好理解) 递推式:f[i][…