本文整理匯總了Python中pygame.sprite.Group.add方法的典型用法代碼示例。如果您正苦於以下問題:Python Group.add方法的具體用法?Python Group.add怎麽用?Python Group.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pygame.sprite.Group
的用法示例。
在下文中一共展示了Group.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Level
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
class Level(object):
_map = {
"%": "flower",
"~": "grass",
".": "path"
}
def __init__(self, name, tilesheet):
# load level data
path = os.path.join(DATA_DIR, "levels", name) + ".lvl"
with open(path, "r") as f:
data = f.read().strip().split("\n")
# parse level data
data = [ [ self._map.get(c) for c in row ] for row in data ]
# build the level
self.tiles, size = tilesheet.build(data)
self.bounds = Rect((0,0), size)
# find path
self.path = Group()
for tile in self.tiles:
if tile.type == "path":
self.path.add(tile)
# render
self.render_background()
def render_background(self):
self.background = Surface(self.bounds.size)
self.tiles.draw(self.background)
示例2: _draw_cloud
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def _draw_cloud(tag_list, surface, vertical=True, fontname='fonts/Arial.ttf', fontzoom=5, rectangular=False):
#Sort the tags by size and wordlength
tag_list.sort(key=lambda tag: len(tag['tag']))
tag_list.sort(key=lambda tag: tag['size'])
tag_list.reverse()
if rectangular:
spiral = _rectangular_spiral
else:
spiral = _archimedean_spiral
sizeRect = surface.get_rect()
tag_store = Group()
for tag in tag_list:
rot = 0
if vertical and randint(0, 2) == 0:
rot = 90
currentTag = Tag(tag, (0, 0), rot, fontname=fontname, fontzoom=fontzoom)
xpos = sizeRect.width - currentTag.rect.width
if xpos < 0: xpos = 0
xpos = randint(int(xpos * LOWER_START) , int(xpos * UPPER_START))
currentTag.rect.x = xpos
ypos = sizeRect.height - currentTag.rect.height
if ypos < 0: ypos = 0
ypos = randint(int(ypos * LOWER_START), int(ypos * UPPER_START))
currentTag.rect.y = ypos
_search_place(currentTag, tag_store, sizeRect, spiral, vertical)
tag_store.add(currentTag)
surface.blit(currentTag.image, currentTag.rect)
return tag_store
示例3: ExplorableScreen
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
class ExplorableScreen(GameScreen):
def __init__(self, config, model):
super(ExplorableScreen, self).__init__(config, model)
def setup(self):
super(ExplorableScreen, self).setup()
pastel_flowers = Image(os.path.join("sample_sprites", "tiles", "png", "pastel_flowers.png"))
grass = Image(os.path.join("sample_sprites", "tiles", "png", "grass.png"))
images = (pastel_flowers, grass)
# XXX Just a demo, do not try this at home: this (rightly!) assumes that
# pastel_flowers and grass have the same dimensions
visible_width = int(self.screen_dimensions[GameConfig.WIDTH_INDEX] / pastel_flowers.width)
visible_height = int(self.screen_dimensions[GameConfig.HEIGHT_INDEX] / pastel_flowers.height)
area_tile_size = (visible_width + 2) * (visible_height + 2)
self.tiles = Group()
for i in range(area_tile_size):
img = random.choice(images)
img_xpos = img.width * (i % visible_width)
img_ypos = img.height * (i % visible_height)
self.tiles.add(PyRoSprite(img.clone((img_xpos, img_ypos))))
def draw_screen(self, window):
self.tiles.draw(window)
self.tiles.update()
示例4: Player
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
class Player(Sprite):
color = 255,25,69
size = 20,20
def __init__(self, loc, bounds):
Sprite.__init__(self) #makes player a sprite object
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = loc
self.bounds = bounds
self.image.fill(self.color)
self.bullets = Group()
def update(self):
self.rect.center = pygame.mouse.get_pos() #player moves/stays with mouse
self.rect.clamp_ip(self.bounds) #cant leave screen bounds
def shoot(self):
if not self.alive():
return #stop doing anything in this function
bullet = Bullet(self.bounds)
bullet.rect.midbottom = self.rect.midtop
self.bullets.add(bullet)
示例5: Player
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
class Player (Sprite):
size = 20,20
color = 255,255,0
shottimer = 0
def __init__(self, loc, bounds):
Sprite.__init__(self) # required for sprites
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = loc
self.bounds = bounds # passes the bounds to the object so it can be saved.
self.image.fill(self.color)
self.bullets = Group()
def update(self):
if self.shottimer > 0:
self.shottimer -= 1
self.rect.center = pygame.mouse.get_pos()
self.rect.clamp_ip(self.bounds) #makes sure it doesn't go outside the provided rectangle
def shoot(self):
if not self.alive(): #return just stops everything in hte function
return
if self.shottimer == 0:
bullet = Bullet(self.bounds)
self.bullets.add(bullet) #adds the new bullet to the Bullets list
bullet.rect.midbottom = self.rect.midtop
self.shottimer = 10
示例6: touches
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def touches(self, group):
touching = Group()
coll = self.rect.inflate(2,1) # grow 1px to allow for edges
for sprite in group:
if coll.colliderect(sprite.rect):
touching.add(sprite)
return touching
示例7: build_wave
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def build_wave(self):
"""Creates the rectangular formation of enemies
Enemy sprites are stored in the inherited Group and the instance formation
"""
# build the formation and store in a 2d array indexed col, row
self.formation = []
for current_col in range(0, self.cols):
col_list = []
for current_row in range(0, self.rows):
new_enemy = Enemy(self.window_size, self.sprite_filename, self.speed, current_col, current_row)
left_pos = current_col * (self.gap + new_enemy.rect.width) + self.gap
top_pos = current_row * (self.gap + new_enemy.rect.height) + self.gap
new_enemy.rect = new_enemy.image.get_rect(left=left_pos, top=top_pos)
col_list.append(new_enemy)
Group.add(self, new_enemy)
self.formation.append(col_list)
self.leftmost_col = 0 # the left- and rightmost cols with living enemies
self.rightmost_col = self.cols - 1
self.move_delay_step = 0 # counts the frames between moves
self.shoot_delay_step = 0 # counts the frames between shots
self.current_vector = EAST # the wave always starts by moving right
示例8: Player
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
class Player(Sprite):
color = 255, 255, 0
size = 20, 20
def __init__(self, loc, bounds):
Sprite.__init__(self)
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = loc
self.bounds = bounds
self.image.fill(self.color)
self.bullets = Group()
def update(self):
self.rect.center = pygame.mouse.get_pos()
self.rect.clamp_ip(self.bounds)
def shoot(self):
if not self.alive():
return
bullet = Bullet(self.bounds)
bullet.rect.midbottom = self.rect.midtop
self.bullets.add(bullet)
示例9: _create_image
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [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)
示例10: _draw_cloud
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def _draw_cloud(
tag_list,
surface,
layout=LAYOUT_MIX,
fontname=DEFAULT_FONT,
palette=DEFAULT_PALETTE,
fontzoom=5,
rectangular=False,
order=True):
#Sort the tags by size and word length
tag_list.sort(key=lambda tag: len(tag['tag']))
tag_list.sort(key=lambda tag: tag['size'])
tag_list.reverse()
if rectangular:
spiral = _rectangular_spiral
else:
spiral = _archimedean_spiral
sizeRect = surface.get_rect()
tag_store = Group()
if not order:
random.shuffle(tag_list)
for tag in tag_list:
rot = 0
flip = False
if layout == LAYOUT_MIX and randint(0, 2) == 0:
rot = 90
elif layout == LAYOUT_VERTICAL:
rot = 90
elif layout == LAYOUT_MOST_VERTICAL:
rot = 90
flip = True
elif layout == LAYOUT_MOST_HORIZONTAL:
flip = True
#set color if tag has no color
if not hasattr(tag, 'color'):
pass
currentTag = Tag(tag, (0, 0), rot, fontname=fontname, fontzoom=fontzoom)
xpos = sizeRect.width - currentTag.rect.width
if xpos < 0: xpos = 0
xpos = randint(int(xpos * LOWER_START) , int(xpos * UPPER_START))
currentTag.rect.x = xpos
ypos = sizeRect.height - currentTag.rect.height
if ypos < 0: ypos = 0
ypos = randint(int(ypos * LOWER_START), int(ypos * UPPER_START))
currentTag.rect.y = ypos
_search_place(currentTag, tag_store, sizeRect, spiral, flip)
tag_store.add(currentTag)
surface.blit(currentTag.image, currentTag.rect)
return tag_store
示例11: Scoreboard
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
class Scoreboard():
def __init__(self, ai_setting, screen, stats):
self.screen = screen
self.screen_rect = screen.get_rect()
self.ai_setting = ai_setting
self.stats = stats
self.text_color = (30, 30, 30)
self.font = pygame.font.SysFont(None, 48)
self.prep_score()
self.prep_high_score()
self.prep_level()
self.prep_ships()
def prep_score(self):
# -1 means multi 10
rounded_score = int(round(self.stats.score, -1))
score_str = "{:,}".format(rounded_score)
self.score_image = self.font.render(score_str, True, self.text_color, self.ai_setting.bg_color)
self.score_rect = self.score_image.get_rect()
self.score_rect.right = self.screen_rect.right - 20
self.score_rect.top = 20
def prep_high_score(self):
high_score = int(round(self.stats.high_score, -1))
high_score_str = "{:,}".format(high_score)
self.high_score_image = self.font.render(high_score_str, True, self.text_color, self.ai_setting.bg_color)
self.high_score_rect = self.high_score_image.get_rect()
self.high_score_rect.centerx = self.screen_rect.centerx
self.high_score_rect.top = self.score_rect.top
def prep_level(self):
self.level_image = self.font.render(str(self.stats.level), True, self.text_color, self.ai_setting.bg_color)
self.level_rect = self.level_image.get_rect()
self.level_rect.right = self.score_rect.right
self.level_rect.top = self.score_rect.bottom + 10
def prep_ships(self):
self.ships = Group()
for ship_number in range(self.stats.ships_left):
ship = Ship(self.ai_setting, self.screen)
ship.rect.x = 10 + ship_number * ship.rect.width
ship.rect.y = 10
self.ships.add(ship)
def show_score(self):
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.high_score_image, self.high_score_rect)
self.screen.blit(self.level_image, self.level_rect)
# draw ships
self.ships.draw(self.screen)
示例12: create_ground
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def create_ground(settings, screen):
"""Create a ground line under player."""
ground_blocks = Group()
# Calculate how many blocks fit on the screen and create them.
for column in range(int((settings.screen_width / settings.block_size) -
int((settings.ground_offsetx * 2) / settings.block_size))):
block = Block(settings, screen, settings.green, settings.ground_offsetx + settings.block_size * column,
settings.ground_y)
ground_blocks.add(block)
return ground_blocks
示例13: create_shield
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def create_shield(settings, screen, number):
"""Create and return a group of blocks that make up a single shield."""
shield_blocks = Group()
for row in range(settings.shield_rows):
for column in range(settings.shield_columns):
if settings.shield_array[row][column] == 'b':
new_block = Block(settings, screen, settings.green,
settings.shield_x + (132 * number + number * 3) + (column * settings.block_size),
settings.shield_y + (row * settings.block_size))
shield_blocks.add(new_block)
return shield_blocks
示例14: build
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def build(self, data):
rows = len(data)
cols = len(data[0])
tiles = Group()
for y, row in enumerate(data):
for x, cell in enumerate(row):
data = self.tilemap.get(cell)
if data:
tile = Tile(cell, data["image"], (x*self.w, y*self.h), data["collides"])
tiles.add(tile)
return tiles, (cols * self.w, rows * self.h)
示例15: create_invader_shot_explosion
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import add [as 別名]
def create_invader_shot_explosion(self, settings, screen, x, y):
"""Create and return explosion "sprite" as a group of Blocks. Top-left point of group is set to x, y."""
explode_blocks = Group()
for row in range(settings.invader_shot_explode_rows):
for column in range(settings.invader_shot_explode_columns):
if settings.invader_shot_explode_array[row][column] == 'b':
new_block = Block(settings, screen, settings.white,
x + (column * settings.block_size),
y + (row * settings.block_size))
explode_blocks.add(new_block)
return explode_blocks