突发奇想学习pygamepython
记录一下遇到的问题吧~python3.x
1.pygame版本对应python版本必须一致,我用pygame对应的python3.2去试python3.4失败,不能识别,后来把python3.4删了重装才OK安全
2.在pycharm下debug一直失败,都卡在execfile的某一句,是由于你的.py文件里有中文,注释也算~多线程
在文件头加 # -*- coding: utf-8 -*- 便可解决学习
3.另外pygame.font.get_height和get_linesize反回的都不是字体的高度,字体原本高度是16字体
4.surface有不少操做手段,经常使用的有clip,bilt,subsurface,spa
5.在蚂蚁蜘蛛叶子那个代码里,python2.x和python3.x 的问题凸显得比较明显线程
源代码debug
def process(self, time_passed): time_passed_seconds = time_passed / 1000.0 for entity in self.entities.values(): entity.process(time_passed_seconds)
意思是遍历整个世界中的entities并执行每一个entity的动做(能够理解为process),可是在动做过程当中entity会修改entities(在代码中表现为若是entity离开了屏幕50像素就把这个entity从entites中除去),这样就形成了遍历过程当中对遍历对象的修改,是不安全的,好比你可能会获得一个已经不存在的对象(原本这种问题多线程里会多一点)code
解决方案是首先生成entites(是一个字典,entity_id: entity)的key的list,而后经过遍历这个list(tuple,元组)加上一些断定便可。
以下
for entity_id in (list)(self.entities.keys()): entity1=self.get(entity_id) if entity1 is not None: entity1.process(time_passed_seconds)
其中,原来的itervalues()被values()取代了。。还有xrange被range取代,这里要加上(list)强制转换的缘由暂时不明。。