当前位置: 首页>>代码示例>>Python>>正文


Python Background.add_object方法代码示例

本文整理汇总了Python中background.Background.add_object方法的典型用法代码示例。如果您正苦于以下问题:Python Background.add_object方法的具体用法?Python Background.add_object怎么用?Python Background.add_object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在background.Background的用法示例。


在下文中一共展示了Background.add_object方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MainLoop

# 需要导入模块: from background import Background [as 别名]
# 或者: from background.Background import add_object [as 别名]
class MainLoop(object):
    """ The main event loop for the game"""

    def __init__(self, file_name):
        pygame.init()
        self.display_surf = pygame.display.set_mode((WINDOW_SIZE_X, WINDOW_SIZE_Y), 0, 32)
        pygame.display.set_caption("Dinosaurs Evolved")

        self.change = False  # Used to see if the board needs to be redrawn
        self.down_keys = Set()  # The keys that the user is currently holding down
        self.player = Player()
        self.background = Background(file_name)
        self.loop()

    def change_movement(self, event):
        """ When a key is pushed down or let up, change the down_keys list """
        if event.type == KEYDOWN:
            self.down_keys.add(event.key)
        elif event.type == KEYUP:
            self.down_keys.remove(event.key)

    def bounds_check(self, x, y):
        """ Make sure the player can no leave the playable surface """
        background_rect = pygame.Rect(x + OFFSET_X, y + OFFSET_Y, self.background.map.PLAYABLE_DIMENSION_X,
                                      self.background.map.PLAYABLE_DIMENSION_Y)
        player_rect = pygame.Rect(WINDOW_SIZE_X / 2, WINDOW_SIZE_Y / 2, PLAYER_DIMENSION_X, PLAYER_DIMENSION_Y)
        return background_rect.contains(player_rect)

    def object_check(self, x, y):
        """ Make sure the player can not run over any object on the map """
        player_rect = pygame.Rect(x, y, PLAYER_DIMENSION_X, PLAYER_DIMENSION_Y)
        for current_object in self.background.all_objects:
            object_rect = pygame.Rect(current_object.x, current_object.y, current_object.surface_width,
                                      current_object.surface_height)
            if player_rect.colliderect(object_rect):
                if isinstance(current_object, GenericScenery):
                    food_qty = current_object.get_food()
                    if food_qty > 0:
                        print("NOM  NOM")
                        self.player.eat_food(food_qty)
                        text = Eating(current_object)
                        logging.info("Added text: {0}".format(str(text)))
                        self.background.add_object(text)
                        self.change = True
                if isinstance(current_object, GenericAnimal):
                    player_attack = self.player.attack()
                    enemy_attack = current_object.attack()
                    player_dead = self.player.take_damage(enemy_attack)
                    enemy_dead = current_object.take_damage(player_attack)
                    print 'Player {0} {1} enemy {2} {3}'.format(self.player.get_health(), player_dead,
                                                                current_object.get_health(), enemy_dead)
                    logging.info(
                        'Attack! Player {0} alive {1} enemy {2} alive {3}'.format(self.player.get_health(), player_dead,
                                                                                  current_object.get_health(),
                                                                                  enemy_dead))
                    if not enemy_dead:
                        logging.info('Enemy killed: {0}'.format(str(current_object)))
                        self.background.all_objects.remove(current_object)
                        self.background.draw_all()
                return False
        return True

    def move_check(self, x, y):
        """ Given the x,y that the player wants to move to make sure nothing is in the way """
        return self.bounds_check(x, y) and self.object_check(-x, -y)

    def move(self):
        """Move the background, not the player. 
        
        As the player moves around, we also want to make sure they are centered in the screen. This means
        that it is the background that needs to move while the player stays stationary. Thus all the movements
        are 'backwards' below.  The the player wants to move to the right, move the board to the left. 
        
        """
        for k in self.down_keys:
            if k == K_DOWN or k == K_s:
                tmpy = self.background.y - STEP_SIZE
                if self.move_check(self.background.x, tmpy):
                    self.background.y = tmpy
            elif k == K_UP or k == K_w:
                tmpy = self.background.y + STEP_SIZE
                if self.move_check(self.background.x, tmpy):
                    self.background.y = tmpy
            elif k == K_RIGHT or k == K_d:
                tmpx = self.background.x - STEP_SIZE
                if self.move_check(tmpx, self.background.y):
                    self.background.x = tmpx
            elif k == K_LEFT or k == K_a:
                tmpx = self.background.x + STEP_SIZE
                if self.move_check(tmpx, self.background.y):
                    self.background.x = tmpx
            self.player.x = -self.background.x  # Since we start at (0.0) this will always be true
            self.player.y = -self.background.y

    def redraw(self):
        """ Update the display """
        if self.change:
            self.background.redraw()
            self.change = False
        self.display_surf.blit(self.background.get_surface(), (self.background.x, self.background.y))
#.........这里部分代码省略.........
开发者ID:lightningmonkey,项目名称:dino,代码行数:103,代码来源:main.py


注:本文中的background.Background.add_object方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。