Ejemplo n.º 1
0
    def update(self, time_passed):
        """Update methods does:
        1) Check for mouse hover on the LevelExit area;
        if this is true, the sprite alpha value is changed and the area became a little visibile
        2) Check for hero movement on the exit. If this is true then raise the LEVEL_CHANGE_EVENT event
        """
        GameSprite.update(self, time_passed)
        if cblocals.global_controlsEnabled:
            # Mouse curson
            if self.physical_rect.collidepoint(pygame.mouse.get_pos()):
                if not self._focus:
                    self.image.set_alpha(50)
                    utils.changeMouseCursor(cblocals.IMAGE_CURSOR_CHANGELEVEL_TYPE)
                    utils.drawCursor(cblocals.screen, pygame.mouse.get_pos())
                    self._focus = True
            else:
                if self._focus:
                    self.image.set_alpha(0)
                    if cblocals.global_mouseCursorType==cblocals.IMAGE_CURSOR_CHANGELEVEL_TYPE:
                        utils.changeMouseCursor(None)
                    self._focus = False

            # Change level
            if self.to_level and self.physical_rect.colliderect(self.currentLevel.hero.physical_rect) and \
                        self.currentLevel.timeIn > 5.:
                event = pygame.event.Event(cblocals.LEVEL_CHANGE_EVENT, {'exit':self, })
                pygame.event.post(event)
Ejemplo n.º 2
0
 def enablePresentationMode(self):
     """Disable all keys and buttons that user can press to do action in the game.
     Exception for ESC key and SPACE.
     """
     cblocals.global_controlsEnabled = False
     cblocals.globals["text_tips"] = False
     cblocals.globals["points"] = False
     utils.changeMouseCursor(None)
Ejemplo n.º 3
0
 def update(self, time_passed):
     """TODO: open/close animation will be handled here"""
     GameSprite.update(self, time_passed)
     if cblocals.global_controlsEnabled:
         # Mouse curson
         if self.collide_rect.collidepoint(pygame.mouse.get_pos()):
             if not self._focus:
                 self.image.set_alpha(200)
                 utils.changeMouseCursor(cblocals.IMAGE_CURSOR_OPENDOOR_TYPE)
                 utils.drawCursor(cblocals.screen, pygame.mouse.get_pos())
                 self._focus = True
         else:
             if self._focus:
                 self.image.set_alpha(255)
                 if cblocals.global_mouseCursorType==cblocals.IMAGE_CURSOR_OPENDOOR_TYPE:
                     utils.changeMouseCursor(None)
                 self._focus = False
Ejemplo n.º 4
0
def game():
    clock = pygame.time.Clock()
    screen = cblocals.screen

    hero = character.PlayingCharacter("Boscolo", ("hero_sword1_vest1.png","hero_vest1.png"), (),
                                      realSize=(18,25), weaponInAndOut=True, sightRange=300)
    hero.setBrain(HeroStateMachine)
    hero.setCombatValues(2, 13)

    level = loadLevelByName("The South Bridge", hero)
    level.enablePresentation('funny-intro')
    
    pygame.display.set_caption("Cheese Boys (alpha release) %s - %s" % (cblocals.__version__, level.name))

    console_area = pygame.Surface( cblocals.CONSOLE_SCREEN_SIZE, flags=SRCALPHA|HWSURFACE, depth=32 )
    console_area.set_alpha(255)
    console_area.fill( (20,20,20) )
    console_area.blit(cblocals.default_font_big.render("This will be the", True, (255, 255, 255)), (2,0) )
    console_area.blit(cblocals.default_font_big.render("console/command", True, (255, 255, 255)), (2,20) )
    console_area.blit(cblocals.default_font_big.render("area", True, (255, 255, 255)), (2,40) )
    
    charas = level['charas']
    enemies = level['enemies']
    physical = level['physical']
    tippable = level['tippable']
    speech = level['speech']
    while True:
        # ******* EVENTS LOOP BEGIN *******
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            
            if event.type==KEYDOWN:
                pressed_keys = pygame.key.get_pressed()

                if not level.presentation and pressed_keys[K_0]:
                    hero.shout("Hey!")


                # ******* DEBUG *******
                if pressed_keys[K_d] and pressed_keys[K_LSHIFT]:
                    import pdb;pdb.set_trace()
                elif pressed_keys[K_d]:
                    level.computeGridMap()
                    print "\n"
                    print level.grid_map
                elif pressed_keys[K_c]:
                    print str(hero.navPoint.compute_path())
                # ******* END DEBUG *******


                if level.presentation is not None:
                    if pressed_keys[K_RIGHT]:
                        cblocals.game_speed = cblocals.game_speed*2
                        logging.info("Game speed changed to %s" % cblocals.game_speed)
                    elif pressed_keys[K_LEFT] and cblocals.game_speed>1.:
                        cblocals.game_speed = cblocals.game_speed/2
                        logging.info("Game speed changed to %s" % cblocals.game_speed)

                if pressed_keys[K_F1]:
                    cblocals.FULLSCREEN = not cblocals.FULLSCREEN
                    screen = handleFullScreen()

                if pressed_keys[K_ESCAPE]:
                    if level.presentation is not None:
                        # TODO: the ESC key to exit presentation is ugly, just move to a "safe" next part in the presentation...
                        cblocals.game_speed = 128.
                    else:
                        game_over()

            if cblocals.global_controlsEnabled:
                # No mouse control during presentations
                
                if event.type==MOUSEBUTTONDOWN or cblocals.global_leftButtonIsDown:
                    mouse_pos = pygame.mouse.get_pos()
                    if utils.checkPointIsInsideRectType(mouse_pos, ( (0,0),cblocals.GAME_SCREEN_SIZE ) ):
                        logging.debug("Click on %s (%s on level)" % (mouse_pos, level.transformToLevelCoordinate(mouse_pos)))
                        lb, cb, rb = pygame.mouse.get_pressed()
                        if lb and not cblocals.global_leftButtonIsDown:
                            cblocals.global_leftButtonIsDown = True
                        if lb:
                            cblocals.global_lastMouseLeftClickPosition = mouse_pos
                        elif rb:
                            cblocals.global_lastMouseRightClickPosition = mouse_pos
                if event.type==MOUSEBUTTONUP:
                    cblocals.global_leftButtonIsDown = False
    
                if event.type==cblocals.ATTACK_OCCURRED_EVENT:
                    print "Attack from %s" % event.character.name
                    hit_list = charas.rectCollisionWithCharacterHeat(event.character, event.attack.rect)
                    for hit in hit_list:
                        attackRes = event.character.roll_for_hit(hit)
                        if attackRes in module_th0.TH0_ALL_SUCCESSFUL:
                            print "  hit %s" % hit.name
                            hit.generatePhysicalAttackEffect(attacker=event.character, criticity=attackRes)
                        else:
                            print "  missed %s" % hit.name

            if event.type==cblocals.SHOUT_EVENT:
                logging.info('%s shouted "%s" from position %s.' % (event.character.name,
                                                              event.text,
                                                              event.position))

            # Sprite collision event
            if event.type==cblocals.SPRITE_COLLISION_EVENT:
                GameSprite.manageCollisions(event.source, event.to)

            # Change current level
            if event.type==cblocals.LEVEL_CHANGE_EVENT:
                exit = event.exit
                level = loadLevelByName(exit.to_level, hero)
                level.topleft = exit.nextTopleft
                level.timeIn=0.
                hero.position = exit.start_position
                hero.navPoint.set(exit.firstNavPoint)
                utils.changeMouseCursor(None)
                charas = level['charas']
                enemies = level['enemies']
                physical = level['physical']
                tippable = level['tippable']
                speech = level['speech']
                break

            # Trigger fired
            if event.type==cblocals.TRIGGER_FIRED_EVENT:
                trigger = event.trigger
                sprite_trigging = event.sprite
                trigger.getResult()
            

        # ******* EVENTS LOOP END *******

        time_passed_mm = clock.tick()
        time_passed =  time_passed_mm / 1000. * cblocals.game_speed
        cblocals.game_time = pygame.time.get_ticks()
        if pygame.key.get_pressed()[K_LCTRL]:            
            time_passed = 0
        else:
            cblocals.playing_time+= time_passed_mm
        
        # Level text overlay
        if level.update_text(time_passed):
            level.draw(screen)
            pygame.display.update(pygame.Rect( (0,0), cblocals.GAME_SCREEN_SIZE ))
            continue
        
        # Presentation
        if level.presentation is not None:
            commands = level.presentation.update(time_passed)
            if commands:
                for command in commands:
                    exec command
            elif commands is None:
                logging.info("Presentation: presentation is ended")
                level.presentation = None

        level.update(time_passed)        
        level.draw(screen)
        
        if cblocals.global_controlsEnabled:
            level.normalizeDrawPositionBasedOn(hero, time_passed)
        elif level.screenReferenceSprite:
            level.normalizeDrawPositionBasedOn(level.screenReferenceSprite, time_passed)

        if cblocals.DEBUG:
            physical.drawCollideRect(screen)
            physical.drawMainRect(screen) 
            physical.drawPhysicalRect(screen)
            charas.drawNavPoint(screen)

        charas.drawAttacks(screen, time_passed)

        if cblocals.DEBUG:
            charas.drawHeatRect(screen)

        # points
        if cblocals.globals['points']:
            for displayable in [x for x in charas.sprites() if hero.can_see_list.get(x.UID(),False)]:
                displayable.drawPointsInfos(screen)

        # textTips
        if cblocals.globals['text_tips']:
            for displayable in [x for x in tippable.sprites() if x.getTip() and hero.can_see_list.get(x.UID(),False)]:
                level.displayTip(screen, displayable)

        # Mouse cursor hover an enemy
        # BBB: can I check this in the enemy update method?
        if cblocals.global_controlsEnabled:
            for enemy in enemies.sprites():
                if enemy.physical_rect.collidepoint(pygame.mouse.get_pos()) and hero.can_see_list.get(enemy.UID(),False):
                    hero.seeking = enemy
                    utils.changeMouseCursor(cblocals.IMAGE_CURSOR_ATTACK_TYPE)
                    break
            else:
                if cblocals.global_mouseCursorType==cblocals.IMAGE_CURSOR_ATTACK_TYPE:
                    utils.changeMouseCursor(None)
                hero.seeking = None

        if cblocals.global_mouseCursorType:
            utils.drawCursor(screen, pygame.mouse.get_pos())

        # darkness
        if cblocals.SHADOW:
            level.blitShadow(screen, hero)

        # speechs
        speech.draw(screen)

        screen.blit(console_area, (cblocals.GAME_SCREEN_SIZE[0],0) )
        # fps:
        if cblocals.SHOW_FPS:
            utils.show_fps(screen, clock.get_fps())

        pygame.display.update()