pygame笔记(更新中
pygame Notebook
由于使用emacs-org進行編輯,為方便使用了英文
Table of Contents
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): passif not pygame.mixer:return NoneSound()fullname = os.path.join("assets", name)try:sound = pygame.mixer.Sound(fullname)except pygame.error as message:print("cannot load sound", wav)raise SystemExit(message)return soundgame object classes
fist
class Fist(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image, self.rect = load_image("fist.bmp", -1)self.punching = 0def update(self):pos = pygame.mouse.get_pos()self.rect.midtop = posif self.punching:self.rect.move_ip(5, 10)def punch(self, target):if not self.punching:self.punching = 1hitbox = self.rect.inflate(-5, -5)return hitbox.colliderect(target.rect)def unpunch(self):self.punching = 0use one of the sprite drawing Group classes.
These classes can draw sprites that have image and rect
attribute.
By simply changing these two attributes, the renderer will
draw the current image at the current position.
All sprites have an update() method, called once per frame.
should put in code that moves and updates the variables
for the sprite.
chimp
class Chimp(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image, self.rect = load_image("chimp.bmp", -1)screen = pygame.display.get_surface()self.area = screen.get_rect()self.rect.topleft = 10, 10self.move = 9self.dizzy = 0def update(self):if self.dizzy():self._spin()else:self._walk()def _walk(self):newpos = self.rect.move((self.move, 0))if not self.area.contains(newpos):if self.rect.left < self.area.left or \self.rect.right > self.area.right:self.move = -self.movenewpos = self.rect.move((self.move, 0))self.image = pygame.transform.flip(self.image, 1, 0)self.rect = newposdef _spin(self):center = self.rect.centerself.dizzy += 12if self.dizzy >= 360:self.dizzy = 0self.image = self.originalelse:rotate = pygame.transform.rotateself.image = rotate(self.original, self.dizzy)self.rect = self.image.get_rect(center = center)def punched(self):if not self.dizzy:self.dizzy = 1self.original = self.imagerotate was made as a local reference,
to make that line shorter.
ALWAYS ROTATE FROM THE ORIGINAL PICTURE!!!
'cause each rotation will cost a loss of quality and size.
– when to make a copy?
– when first punched.
init everything
pygame.init() screen = pygame.display.set_mode((468, 60)) pygame.display.set_caption("Chimp Fever") pygame.mouse.set_visible(0)init(): initialize all the pygame modules imported.
display: if not set, pygame’ll choose the best
color depth.
mouse.set_visible(0): set invisible
create the background
create a single surface to represent the background
create the surface
background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250, 250, 250))put text on the background
get text rendered onto the background surface
if pygame.font:font = pygame.font.Font(None, 36)text = font.render("Pummel The Chimp, And Win $$$", 1, (1, 10, 10))textpos = text.get_rect(centerx = background.get_width() / 2)background.blit(text, textpos)None can be replaced by a TrueType font.
render: create a new surface with appropriate size
1 to tell the renderer to use antialiased text
display the background (after the setup)
screen.blit(background, (0, 0)) pygame.display.flip()flip: handles the entire window area
both single-buffered and double-buffered surfaces
- Note taken on
double-buffering:
在圖形圖像顯示過程中,計算機從顯示緩沖區(qū)取數(shù)據(jù)然后顯示,
很多圖形的操作都很復(fù)雜需要大量的計算,
很難訪問一次顯示緩沖區(qū)就能寫入待顯示的完整圖形數(shù)據(jù),
通常需要多次訪問顯示緩沖區(qū),每次訪問時寫入最新計算的圖形數(shù)據(jù)。
而這樣造成的后果是一個需要復(fù)雜計算的圖形,
你看到的效果可能是一部分一部分地顯示出來的,造成很大的閃爍不連貫。
而使用雙緩沖,可以使你先將計算的中間結(jié)果存放在另一個緩沖區(qū)中,
待全部的計算結(jié)束,該緩沖區(qū)已經(jīng)存儲了完整的圖形之后,
再將該緩沖區(qū)的圖形數(shù)據(jù)一次性復(fù)制到顯示緩沖區(qū)。
prepare game obj
create all the objects
whiff_sound = load_sound("whiff.wav") punch_sound = load_sound("punch.wav") chimp = Chimp() fist = Fist() allsprites = pygame.sprite.RenderPlain((fist, chimp)) clock = pygame.time.Clock()allsprites: a sprite Group named RenderPlain.
RenderPlain can draw all the sprites it contains
to the screen.
clock: used in the mainloop to control the
framerate
main loop
while 1:clock.tick(60)no more than 60 frames per second
handle input events
work with the event queue
for event in pygame.event.get():if event.type == QUIT:returnelif event.type == KEYDOWN and event.key == K_ESCAPEreturnelif event.type == MOUSEBUTTONDOWN:if fist.punch(chimp):punch_sound.play()chimp.punched()else:whiff_sound.play()elif event.type == MOUSEBUTTONUP:fist.unpunch()K_ESCAPE: ESC.
update all the sprites
allsprites.update()sprite groups have an update() method,
which simply calls the update() for all the sprites
it contains.
draw the entire scene
screen.blit(background, (0, 0)) allsprites.draw(screen) pygame.display.flip()no need to do anything cleaning up
sprites
remember to Sprite.__init__(self), or respond:
AttributeError: 'mysprite' instance has no attribute '_Sprite__g'use group
- Note taken on
get some methods here
belong to a group -> alive.
not belong to any group -> cleared up.
kill(): remove the sprite from all the groups it belonged to.
sprites(): return a iteratable object shows all the sprites
a group has.
update(): update all.
group types
Group
GroupSingle
contain at most 1 sprites, when added to, forget the old one.
RenderPlain
every sprite has a image and a rect.
has a draw() method that draw all the sprites on a surface
or a screen.
RenderClear
derived from RenderPlain.
add a method clear(): clear sprites from the screen
with background.
RenderUpdates
derived from RenderClear, Cadillac of rendering Groups.
the draw() method will return a list of rects of all sprites
showing all the area that is occupied.
collision detection
spritecollide(sprite, group, dokill) -> list
check for collisions between a single sprite
and a sprite group.
require a rect attribute for all the sprites used.
dokill: (boolean)
whether to call kill() on all crashed sprites.
return a list of all the sprites in the group that collides.
groupcollide(group1, group2, dokill1, dokill2) -> dictionary
dokill1: if true, kill all the crashed sprites in group1.
dokill2: if true, kill all the crashed sprites in group2.
dict: key is sprites in group1 that crashed.
its value is sprites in group2 that the key crashed
create my own group class
to get extra features.
numpy (Numeric Python) intro
deal with array, holds an array of fixed size,
and all elements inside an array are of the same type.
mathematical operations
apply to all elements in an array. (element-wise operations)
but only when able to apply the operation.
ex.
>>> array((1, 2, 3)) + array((4, 5, 6)) array([5, 7, 9])>>> array((1, 2, 3)) + array((3, 4)) Traceback (most recent call last):File "<stdin>", line 1, in <module> ValueError: operands could not be broadcast together with shapes (3,) (2,) # different size arrays cannot be added up.array slice
same to python list.
sliced-out array still refers to the original array.
so if when changing the sliced-out array,
the original one changes, too.
2d array slice
use , to separate different dimention’s indices,
and for each dimention’s index,
almost same to 1d scenario, but can use : to show “all”.
ex.
>>> b = array(((1, 2, 3), (3, 4, 5))) >>> b[0, 1] 2 >>> b[1, :] array([3, 4, 5]) >>> b[1] array([3, 4, 5]) >>> b[:, :2] array([[1, 2], [3, 4]])surfarray
be sure to import: use exception ImportError
try: import numpy as npimport pygame.surfarray as surfarray except ImportError:raise ImportErrer, "NumPy and Surfarray are required"making game
總結(jié)
以上是生活随笔為你收集整理的pygame笔记(更新中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVP结合(RecycleView,Re
- 下一篇: 微信html5小游戏源码70种