本文整理匯總了Python中pygame.sprite.Group.sprites方法的典型用法代碼示例。如果您正苦於以下問題:Python Group.sprites方法的具體用法?Python Group.sprites怎麽用?Python Group.sprites使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pygame.sprite.Group
的用法示例。
在下文中一共展示了Group.sprites方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _create_image
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
def _create_image(self):
grp = Group()
for word in self.words:
if len(grp.sprites()) == 0:
word.scale(self.ratio+0.2)
pm_w = word.rect.width
pm_h = word.rect.height
pm_x, pm_y = place_primary(pm_w, pm_h)
word.rect.x, word.rect.y = pm_x, pm_y
arch_x = pm_x + pm_w/2
arch_y = pm_y + pm_h/2
else:
word.scale(self.ratio)
for x, y in archimedean_spiral(False):
if self.debug:
rs = Surface((5, 5))
rs.fill((0, 0, 255))
rs.set_alpha(100)
self.sf.blit(rs, (x + arch_x, y + arch_y))
word.set_position(x + arch_x, y + arch_y)
x_out = CANVAS_WIDTH - CANVAS_PADDING < word.rect.x + word.rect.width or word.rect.x < CANVAS_PADDING
y_out = CANVAS_HEIGHT - CANVAS_PADDING < word.rect.y + word.rect.height or word.rect.y < CANVAS_PADDING
out_of_bound = x_out or y_out
if spritecollideany(word, grp, collide_mask) is None and not out_of_bound:
if self.debug:
rs = Surface((5, 5))
rs.fill((0, 255, 0))
rs.set_alpha(100)
self.sf.blit(rs, (x + arch_x, y + arch_y))
break
self.sf.blit(word.image, word.rect)
grp.add(word)
示例2: shoot
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
def shoot(self):
"""Chooses a random bottom enemy to shoot
the bullet is added to the self.bullets group
"""
# pick a random living enemy, then make the bottom living enemy of that column shoot
r = random.randint(0, len(self) - 1)
##print ("Random = ", r)
##print ("Enemies = ", len(self))
col = Group.sprites(self)[r].col
shooter = self.bottom_sprite(col)
new_bullet = Bullet(window_size, bullet_filename, bullet_speed, shooter.rect.center, SOUTH)
new_bullet.add(self.bullets)
示例3: update
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
def update(self):
"""Instead of updating each sprite independently,
this method is changed to move the enemies in unison
movement happens once every <move_delay> frames
New: a random bottom row enemy will shoot once every <shoot_delay> frames
"""
# movement
self.move_delay_step += 1
if self.move_delay_step == self.move_delay:
self.move_delay_step = 0
movement_vector = self.check_vector()
for current_sprite in Group.sprites(self):
current_sprite.move(movement_vector)
self.shoot_delay_step += 1
if self.shoot_delay_step == self.shoot_delay:
self.shoot_delay_step = 0
self.shoot()
示例4: Main
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
#.........這裏部分代碼省略.........
def setUpViewGroceryList(self):
x,y = self.SCREEN_SIZE
x-=100
viewAllWindow = ScrollableWindow(self.IMG_DIR, (x,y),self.SCREEN_SIZE, "groceryListViewer")
self.windowGroup.add(viewAllWindow)
backButton = Button(self.IMG_DIR, "backButton", (705,30), "menu", -1)
self.buttonGroup.add(backButton)
viewAllWindow.addGroceryList(self.groceryList.getAllIngredients())
writeDesktopButton = Button(self.IMG_DIR, "writeToDesktop", (705,80), "writeToDesktop", -1)
self.buttonGroup.add(writeDesktopButton)
def setUpRecipeAdder(self):
ingParser = IngredientParser()#self.dbm.loadParserDataFields())
recipeAdderWindow = RecipeReciever("addRecipePage",self.IMG_DIR, ingParser)
self.windowGroup.add(recipeAdderWindow)
backButton = Button(self.IMG_DIR, "backButton", (705,30), "menu", -1)
self.buttonGroup.add(backButton)
"""def setUpSearch(self):
self.windowGroup.add(TextBox(
#def setUpSearchTextBox(self):"""
def doPaste(self):
if self.state == "addRecipe":
(self.windowGroup.sprites())[0].recieveClipboardPaste(pyperclip.paste())
def attemptScroll(self, dy):
for sprite in self.windowGroup.sprites():
sprite.scroll(dy)
def handleLeftMouseClick(self, xy):
for button in self.buttonGroup.sprites():
if button.getRect().collidepoint(xy):
## stupid organization leads to hacky solutions...
# in groceryList viewer, the writeToDesktop button must be stored in the main
# class. So thats whats going on here, we call writeToDesktop
# in the scrollable window and grab the grocery list there and send that to
# dbm to write it to desktop
if button.getIdentity() == "writeToDesktop":
theString = self.windowGroup.sprites()[0].writeToDesktop()
#print
#print
#print theString
self.dbm.writeStringToDesktop(self.windowGroup.sprites()[0].writeToDesktop(), "Grocery List")
self.state = "menu"
self.stateChanged = True
#print "got here!"
return
## we want to store the state of the stuff people are looking at
# whether that is a grocery list they have increased or recipes they have added to cart
# we call the "grabState()" method of the scrollableWindow to get the information
#### keep in mind, this is the previous state
# so if self.state == "browseAll" it means we are coming from browseAll state
# to the menu
示例5: SpaceGame
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
class SpaceGame(Microgame):
def __init__(self):
Microgame.__init__(self)
# TODO: Initialization code here
self.count = 0
self.spaceship = Spaceship()
self.bg = Background()
self.planet = Planet()
self.monster = Group(Monster())
self.earth = Group(EarthExp())
self.background = Group(self.bg)
self.sprites = Group(self.planet, self.spaceship)
self.is_win = False
self.is_lose = False
def generate_asteroid(self):
if self.count == 10:
if randint(0,1) == 0:
self.sprites.add(Asteroid(-10, randint(0, locals.HEIGHT - 400), 1))
else:
self.sprites.add(Asteroid(locals.WIDTH + 10, randint(0, locals.HEIGHT - 400), -1))
self.count = 0
else:
self.count += 1
def start(self):
# TODO: Startup code here
music.load(join("games","space","music","space_song.ogg"))
music.play()
def stop(self):
# TODO: Clean-up code here
music.stop()
def update(self, events):
# TODO: Update code here
self.background.update()
self.sprites.update()
#Make asteroids
self.generate_asteroid()
#Check if spaceship hits sides of screen
x_ship_left, _ = self.spaceship.rect.bottomleft
x_ship_right, _ = self.spaceship.rect.bottomright
if x_ship_left <= 0:
self.spaceship.x_velocity = 0
elif x_ship_right >= locals.WIDTH - 14:
self.spaceship.x_velocity = 0
#Check if spaceship hits top/bottom of screen sectioned to movement
_, y_ship_top = self.spaceship.rect.topleft
_, y_ship_bottom = self.spaceship.rect.bottomleft
if y_ship_top <= locals.HEIGHT - 300:
self.spaceship.y_velocity = 0
elif y_ship_bottom >= locals.HEIGHT - 20:
self.spaceship.y_velocity = 0
#Process user input
for event in events:
if event.type == KEYDOWN and event.key == K_LEFT:
if x_ship_left > 0:
self.spaceship.x_velocity -= VELOCITY_INC
elif event.type == KEYUP and event.key == K_LEFT:
self.spaceship.x_velocity = 0
elif event.type == KEYDOWN and event.key == K_RIGHT:
if x_ship_right < locals.WIDTH - 14:
self.spaceship.x_velocity += VELOCITY_INC
elif event.type == KEYUP and event.key == K_RIGHT:
self.spaceship.x_velocity = 0
elif event.type == KEYDOWN and event.key == K_UP:
if y_ship_top > locals.HEIGHT - 300:
self.spaceship.y_velocity -= VELOCITY_INC
elif event.type == KEYUP and event.key == K_UP:
self.spaceship.y_velocity = 0
elif event.type == KEYDOWN and event.key == K_DOWN:
if y_ship_bottom < locals.HEIGHT - 20:
self.spaceship.y_velocity += VELOCITY_INC
elif event.type == KEYUP and event.key == K_DOWN:
self.spaceship.y_velocity = 0
#music.load(join("games","space","music","Powerup7.wav"))
#music.play()
#Make win when spaceship hits planet
if self.planet.rect.colliderect(self.spaceship.rect):
sound_planet = pygame.mixer.Sound(join("games","space","music","Powerup.wav"))
sound_planet.play()
self.is_win = True
for each in self.sprites.sprites():
if isinstance(each, Asteroid):
if each.rect.colliderect(self.spaceship.rect):
sound_asteroid = pygame.mixer.Sound(join("games","space","music","Explosion.wav"))
sound_asteroid.play()
self.is_lose = True
#Creates win scree
if self.is_win:
self.sprites.empty()
self.background.empty()
#.........這裏部分代碼省略.........
示例6: Controller
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
class Controller(object):
def __init__(self, size, scene=None, **kw):
pygame.init()
self.width, self.height = self.size = size
self.screen = pygame.display.set_mode(size, **kw)
try:
self.hard_reset()
except Reset:
pass
self.load_scene(scene)
def hard_reset(self):
# This dict is where the game should keep
# character and story data that persist across phaes,
# like lifes, energy, inventory, encounters that happened, etc:
self.inside_cut = False
self.diary = {}
return self.soft_reset()
def soft_reset(self, raise_=True):
self.actor_positions = {}
self.old_top = -20
self.old_left = -20
self.old_tiles = {}
self.dirty_tiles = {}
self.force_redraw = False
self.post_cut_action = None
if raise_:
raise SoftReset
def load_scene(self, scene, skip_post_cut=False, skip_pre_cut=False):
if getattr(self, "scene", None) and self.scene.post_cut and not skip_post_cut:
post_cut_action = partial(self.load_scene, scene, skip_post_cut=True, skip_pre_cut=skip_pre_cut)
return self.enter_cut(self.scene.post_cut, post_cut_action)
self.scene = scene
scene.set_controller(self)
self.all_actors = Group()
self.actors = {}
self.load_initial_actors()
self.messages = Group()
if self.scene.pre_cut and not skip_pre_cut:
return self.enter_cut(self.scene.pre_cut)
def enter_cut(self, cut, post_action=None):
self.post_cut_action = post_action
self.inside_cut = True
self.current_cut = cut(self)
return self.update()
def leave_cut(self):
self.inside_cut = False
if self.post_cut_action:
action = self.post_cut_action
self.post_cut_action = None
action()
def draw_cut(self):
self.current_cut.update()
def load_initial_actors(self):
for x in range(self.scene.width):
for y in range(self.scene.height):
cls = self.scene.get_actor_at((x, y))
if not cls:
continue
name = cls.__name__.lower()
actor = cls(self, pos=(x, y))
self.all_actors.add(actor)
self.actors.setdefault(name, Group())
self.actors[name].add(actor)
if getattr(actor, "main_character", False):
self.set_main_character(actor)
def set_main_character(self, actor):
self.main_character = Group()
self.main_character.add(actor)
@property
def protagonist(self):
sprites = self.main_character.sprites()
return sprites[0] if sprites else None
@staticmethod
def _touch(actor1, actor2):
if actor1 is actor2:
return False
return actor1.rect.colliderect(actor2.rect)
def update(self):
if self.inside_cut:
try:
return self.draw_cut()
except CutExit:
self.leave_cut()
try:
self.scene.update()
#.........這裏部分代碼省略.........
示例7: LumberjackGame
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import sprites [as 別名]
class LumberjackGame(Microgame):
def __init__(self):
# TODO: Initialization code here
Microgame.__init__(self)
self.jack = Man()
self.count = 0
self.mycount = pygame.font.SysFont("arial", 50, bold = True)
self.left, self.left_rect = _load_image(join("games","Lumberjack","images","timberman_normalleft.png"), 105, 400)
self.left_chop, self.leftc_rect = _load_image(join("games","Lumberjack","images","timberman_chopleft.png"), 130, 450)
self.right, self.right_rect = _load_image(join("games","Lumberjack","images","timberman_normalright.png"), 500, 400)
self.right_chop, self.rightc_rect = _load_image(join("games","Lumberjack","images","timberman_chopright.png"), 375, 450)
self.stump = load(join("games","Lumberjack","images","stump.png"))
self.sprites = Group(self.jack)
self.background = load(join("games","Lumberjack","images","forest.png"))
self.tree = Group(treeBlock(LEFT_POSITION, 550, 1))
def start(self):
# TODO: Startup code here
music.load(join("games","Lumberjack","music","tree_song.ogg"))
music.play()
self.generateTree()
def stop(self):
# TODO: Clean-up code here
music.stop()
def generateTree(self):
_ , min_y = self.tree.sprites()[len(self.tree.sprites()) - 1].rect.topleft
cur_tree = 0
for n in range(0, (len(self.tree.sprites()) - 1)):
_ , y = self.tree.sprites()[n].rect.topleft
if y < min_y:
min_y = y
cur_tree = n
if min_y > 0 and min_y <= 550:
tree_type = self.tree.sprites()[cur_tree].type
if tree_type == 2:
self.tree.add(treeBlock(LEFT_POSITION, (min_y - 140), randint(1,2)))
elif tree_type == 0:
self.tree.add(treeBlock(LEFT_POSITION, (min_y - 140), randint(0,1)))
else:
self.tree.add(treeBlock(LEFT_POSITION, (min_y - 140), randint(0,2)))
def updateTree(self, side):
max_y = locals.HEIGHT
cur_tree = 0
for n in range(0, (len(self.tree.sprites()))):
_ , y = self.tree.sprites()[n].rect.topleft
if 550 == y:
max_y = y
cur_tree = n
print max_y, self.tree.sprites()[cur_tree].type
if self.tree.sprites()[cur_tree].type == side:
self.lose()
else:
self.tree.remove(self.tree.sprites()[cur_tree])
#self.tree.update()
for each in self.tree:
each.update()
def update(self, events):
# TODO: Update code here
self.sprites.update()
self.generateTree()
#Process user input
sound_chop = pygame.mixer.Sound(join("games","Lumberjack","music","axe_chop.wav"))
if self.jack.image == self.left_chop:
self.jack.image = self.left
self.jack.rect = self.left_rect
elif self.jack.image == self.right_chop:
self.jack.image = self.right
self.jack.rect = self.right_rect
for event in events:
if event.type == KEYDOWN and event.key == K_LEFT:
self.count += 1
sound_chop.play()
self.jack.image = self.left_chop
self.jack.rect = self.leftc_rect
self.updateTree(2)
elif event.type == KEYDOWN and event.key == K_RIGHT:
self.count += 1
sound_chop.play()
self.jack.image = self.right_chop
self.jack.rect = self.rightc_rect
self.updateTree(0)
def render(self, surface):
# TODO: Rendering code here
surface.fill(Color(255, 255, 255))
surface.blit(self.background, (0, 0), area = None, special_flags = 0)
self.tree.draw(surface)
surface.blit(self.stump, (318, 690), area = None, special_flags = 0)
self.sprites.draw(surface)
label = self.mycount.render(str(self.count), 1, (255, 255, 255))
if self.count <= 9:
surface.blit(label, (398, 200))
else:
surface.blit(label, (385, 200))
#.........這裏部分代碼省略.........