pygame小记

突发奇想学习pygame

记录一下遇到的问题吧~

 

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,

 

5.在蚂蚁蜘蛛叶子那个代码里,python2.x和python3.x 的问题凸显得比较明显

源代码

    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中除去),这样就造成了遍历过程中对遍历对象的修改,是不安全的,比如你可能会得到一个已经不存在的对象(本来这种问题多线程里会多一点)

 

解决方案是首先生成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)强制转换的原因暂时不明。。

posted @ 2014-06-11 18:02  crowinhell  阅读(600)  评论(0编辑  收藏  举报