pygame小記

突發奇想學習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)強制轉換的緣由暫時不明。。

相關文章
相關標籤/搜索