1)pygame中有些模块有时是无效的,当无效时,其值为None,可以通过如下方法判断
if not pygame.font: print 'Warning, fonts disabled'
2)Surface.set_colorkey
设置Surface的透明色。当把 这个Surface blit到令一个Surface时候,和这个透明色颜色相同的像素会变成透明。具体使用方法参照API
3)如果是找不到音乐文件是,可以返回一个NoneSound,保证程序正常执行。
def load_sound(name): class NoneSound: def play(self): pass if not pygame.mixer: return NoneSound() fullname = os.path.join('data', name) try: sound = pygame.mixer.Sound(fullname) except pygame.error, message: print 'Cannot load sound:', wav raise SystemExit, message return sound4)可以用矩形自带的方法执行move,判断是否在矩形内等
newpos = self.rect.move((self.move, 0))
Rect.contains
5)取得Display的sruface
screen = pygame.display.get_surface()
pygame.display.update() - This updates the whole window (or the whole screen for fullscreen displays). pygame.display.flip() - This does the same thing, and will also do the right thing if you're using doublebuffered hardware acceleration, which you're not, so on to... pygame.display.update(a rectangle or some list of rectangles) - This updates just the rectangular areas of the screen you specify. 在游戏大的时候,一定要用第三种,只更新脏矩形,通常做法是做一个list,在每帧的刷新中只更新 the_dirty_rectangles,具体步骤如下 Blit a piece of the background over the sprite's current location, erasing it.xAppend the sprite's current location rectangle to a list called dirty_rects. Move the sprite. Draw the sprite at it's new location. Append the sprite's new location to my dirty_rects list. Call display.update(dirty_rects)
if (key.get_pressed[K_t] and key.get_pressed[K_f]):