本文整理匯總了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))
#.........這裏部分代碼省略.........