class entity: location = types.vector(0,0) size = types.vector(0,0) disposable = False disposed = False def update(self, time, timePassed): 0 def draw(self, screen): 0 def getBoundingBox(self): return types.rectangle(self.location.x, self.location.y, self.size.x, self.size.y) def getCenterLocation(self): return self.location.add(self.size.multiplyScalar(0.5)) def dispose(self): self.disposable = True def doDispose(self): self.disposed = True def hitByBall(self, ball): 0 def setImage(self, image): self.image = image self.size = types.vector(image.get_width(), image.get_height())
def level1(screenSize): levelBlocks = [] spacing = 1 for y in range(3): for x in range(int((screenSize[0] - spacing) / (30 + spacing))): newBlock = blocks.GreenBlock() newBlock.location = types.vector(spacing + (x * (30 + spacing)), spacing + (y * (15 + spacing))) levelBlocks.append(newBlock) return levelBlocks
def update(self): time = pygame.time.get_ticks() timePassed = time - self.lastFrameTime self.handleEvents() self.updateEntities(time, timePassed) if len(self.blocks) == 0: self.nextLevel() if self.ballAttached: self.ball.location = self.paddle.location.add( types.vector((self.paddle.size.x / 2) - (self.ball.size.x / 2), -self.ball.size.y)) self.lastFrameTime = time
def loadLevel(self, levelIndex): self.entities = [] self.ballAttached = True self.paddle = entity.paddle() self.paddle.location = types.vector( (self.screenSize[0] / 2) - (self.paddle.size.x / 2), self.screenSize[1] - 20) self.entities.append(self.paddle) self.ball = Ball() self.ball.suspended = True self.entities.append(self.ball) blocks = levels.levels[levelIndex](self.screenSize) for block in blocks: self.blocks.add(block) self.entities.append(block)
def setImage(self, image): self.image = image self.size = types.vector(image.get_width(), image.get_height())
class Ball(entity.entity): vector = types.vector(2, -2).toUnit() velocity = 6 suspended = False def __init__(self): self.setImage(images.get('ball')) def update(self, time, timePassed): if self.suspended == False: velocityToApply = self.vector.multiplyScalar(self.velocity * (timePassed * 0.02)) self.handleVerticalCollisions(velocityToApply) self.handleHorizontalCollisions(velocityToApply) def handleVerticalCollisions(self, velocityToApply): if velocityToApply.y != 0: self.location.y += velocityToApply.y if self.handleVerticalEntityCollisions(velocityToApply): return else: self.handleVerticalViewCollisions() def handleVerticalEntityCollisions(self, velocity): collidingEntities = services.game.findEntitiesInRectangle( self.getBoundingBox(), self) if len(collidingEntities) > 0: collidingEntity = collidingEntities[0] if velocity.y > 0: self.location.y = collidingEntity.location.y - self.size.y elif velocity.y < 0: self.location.y = collidingEntity.location.y + collidingEntity.size.y self.vector.y = self.vector.y * -1 if isinstance(collidingEntity, entity.paddle): self.vector.x += collidingEntity.speed / 3.0 self.vector = self.vector.limitLength(5) collidingEntity.hitByBall(self) return True else: return False def handleVerticalViewCollisions(self): if self.location.y < 0: self.location.y = 0 self.vector.y = self.vector.y * -1 return True elif self.location.y + self.size.y > services.game.screenSize[1]: self.location.y = services.game.screenSize[1] - self.size.y self.vector.y = self.vector.y * -1 return True else: return False def handleHorizontalCollisions(self, velocityToApply): if velocityToApply.x != 0: self.location.x += velocityToApply.x if self.handleHorizontalEntityCollisions(velocityToApply): return if self.location.x < 0: self.location.x = 0 self.vector.x = self.vector.x * -1 elif self.location.x + self.size.y > services.game.screenSize[0]: self.location.x = services.game.screenSize[0] - self.size.x self.vector.x = self.vector.x * -1 def handleHorizontalEntityCollisions(self, velocity): collidingEntities = services.game.findEntitiesInRectangle( self.getBoundingBox(), self) if len(collidingEntities) > 0: collidingEntity = collidingEntities[0] if velocity.x > 0: self.location.x = collidingEntity.location.x - self.size.x self.vector.x = self.vector.x * -1 elif velocity.x < 0: self.location.x = collidingEntity.location.x + collidingEntity.size.x self.vector.x = self.vector.x * -1 collidingEntity.hitByBall(self) return True else: return False def draw(self, screen): screen.blit(self.image, (self.location.x, self.location.y))