pygame Notebook
由于使用emacs-org进行编辑,为方便使用了英文
Table of Contents
- chimp (instance)
- sprites
- numpy (Numeric Python) intro
- surfarray
- making game
chimp (instance)
importing modules
import os, sys
import pygame
from pygame.locals import *
if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled')
os
, sys
modules can get independent on platforms.
pygame.locals
: commonly used constants and functions,
not necessary to include, just include pygame
is enough.
loading resources
image
def load_image(name, colorkey = None):
fullname = os.path.join("assets", name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print("cannot load image:", name)
raise SystemExit(message)
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0,))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
create a fullname, tryna load the img,
raise exception if failed, convert the img to the format
of the screen
to make it faster to blit,
set a transparent colorkey,
default the (0, 0,) point’s color,
return the img and its rect.
REACCEL
can boost up blitting but slow down modifying.
sound
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer:
return NoneSound()
fullname = os.path.join("assets", name)
try:
sound = pygame.mixer