用Python和Pygame写游戏从入门到精通(16)
admin
2023-07-31 00:36:28
0

经历了长年的艰苦卓绝的披星戴月的惨绝人寰的跋山涉水,我们终于接近了AI之旅的尾声(好吧,实际上我们这才是刚刚开始)。这一次真正展示一下这几回辛勤工作的结果,最后的画面会是这个样子:

下面给出完整代码(注意需要gameobjects库才可以运行,参考之前的向量篇):

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 SCREEN_SIZE = (640, 480)NEST_POSITION = (320, 240)ANT_COUNT = 20NEST_SIZE = 100. import pygamefrom pygame.locals import * from random import randint, choicefrom gameobjects.vector2 import Vector2 class State(object):    def __init__(self, name):        self.name = name    def do_actions(self):        pass    def check_conditions(self):        pass    def entry_actions(self):        pass    def exit_actions(self):        pass         class StateMachine(object):    def __init__(self):        self.states = {}        self.active_state = None     def add_state(self, state):        self.states[state.name] = state     def think(self):        if self.active_state is None:            return        self.active_state.do_actions()        new_state_name = self.active_state.check_conditions()        if new_state_name is not None:            self.set_state(new_state_name)     def set_state(self, new_state_name):        if self.active_state is not None:            self.active_state.exit_actions()        self.active_state = self.states[new_state_name]        self.active_state.entry_actions() class World(object):    def __init__(self):        self.entities = {}        self.entity_id = 0        self.background = pygame.surface.Surface(SCREEN_SIZE).convert()        self.background.fill((255, 255, 255))        pygame.draw.circle(self.background, (200, 255, 200), NEST_POSITION, int(NEST_SIZE))     def add_entity(self, entity):        self.entities[self.entity_id] = entity        entity.id = self.entity_id        self.entity_id += 1     def remove_entity(self, entity):        del self.entities[entity.id]     def get(self, entity_id):        if entity_id in self.entities:            return self.entities[entity_id]        else:            return None     def process(self, time_passed):        time_passed_seconds = time_passed / 1000.0        for entity in self.entities.values():            entity.process(time_passed_seconds)     def render(self, surface):

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...