1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#box class
import random
import pygame
from pygame.locals import *
import game, gfx, snd
boximages = []
yboximages = []
def load_game_resources():
global boximages
for i in range(15):
img = gfx.load('box%02d.gif'%i)
boximages.append(img)
img = gfx.load('ybox%02d.gif'%i)
yboximages.append(img)
snd.preload('boxhit', 'yboxhit')
class Box:
def __init__(self, pos, touches):
self.rotate = random.randint(0, 89)
self.rotspeed = random.randint(2, 4)
if random.randint(0, 1):
self.rotspeed = -self.rotspeed
self.rect = boximages[0].get_rect().move(pos)
self.touches = touches
self.touching = 0
self.dead = 0
self.imglists = None, boximages, yboximages
def erase(self, background):
r = background(self.rect)
if self.dead:
gfx.dirty(r)
def draw(self, gfx):
frame = (self.rotate/6)%15
img = self.imglists[self.touches][frame]
r = gfx.surface.blit(img, self.rect)
gfx.dirty(r)
def tick(self, speedadjust):
self.rotate = self.rotate + self.rotspeed
def playercollide(self, rect):
if self.touching:
self.touching = self.rect.colliderect(rect)
return 0
elif self.rect.colliderect(rect):
self.touches -= 1
if self.touches:
self.touching = 1
snd.play('yboxhit')
return 0
snd.play('boxhit')
return 1
return 0
|