You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

6 months ago
import pygame
from .. import tools,setup
from .. import constants as C
6 months ago
from .powerup import create_powerup
6 months ago
class Brick(pygame.sprite.Sprite):
6 months ago
def __init__(self, x, y,brick_type,group,color=None,name = 'brick'):
6 months ago
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
6 months ago
self.brick_type = brick_type
self.group = group
self.name = name
6 months ago
bright_frame_rects = [(16,0,16,16),(48,0,16,16)]
dark_frame_rects = [(16,32,16,16),(48,32,16,16)] # 抠图
6 months ago
if not color:
6 months ago
self.frame_rects = bright_frame_rects
6 months ago
else:
6 months ago
self.frame_rects = dark_frame_rects
6 months ago
self.frames = []
for frame_rect in self.frame_rects:
6 months ago
self.frames.append(tools.get_image(setup.GRAPHICS['tile_set'],*frame_rect,(0,0,0),C.BRICK_MULTI)) # 遍历加载图片
6 months ago
self.frame_index = 0
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y # 设置坐标与图片
6 months ago
self.state = 'rest'
self.gravity = C.GRAVITY
def update(self):
self.current_time = pygame.time.get_ticks()
self.handel_states()
def handel_states(self):
if self.state == 'rest':
self.rest()
elif self.state == 'bumped':
self.bumped()
elif self.state == 'open':
self.open()
def rest(self):
pass
def go_bumped(self):
self.y_vel = -7
self.state = 'bumped'
def bumped(self):
self.rect.y += self.y_vel
self.y_vel += self.gravity
if self.rect.y > self.y + 10 : # 如果大于初始位置回弹
self.rect.y = self.y
if self.brick_type == 0:
self.state = 'rest'
elif self.brick_type == 1:
self.state = 'open'
else:
self.group.add(create_powerup(self.rect.centerx,self.rect.centery,self.brick_type))
def open(self):
self.frame_index = 1
self.image = self.frames[self.frame_index]