本文整理匯總了Python中pygame.sprite.Group.update方法的典型用法代碼示例。如果您正苦於以下問題:Python Group.update方法的具體用法?Python Group.update怎麽用?Python Group.update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pygame.sprite.Group
的用法示例。
在下文中一共展示了Group.update方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: update
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
def update(self, dt):
Group.update(self, dt)
self.spawn_timer += dt
if self.spawn_timer >= self.spawn_rate:
self.spawn()
self.spawn_timer = 0
示例2: run_game
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# make a ship
ship = Ship(ai_settings, screen)
bullets = Group()
aliens = Group()
alien = Alien(ai_settings, screen)
gf.create_fleet(ai_settings, screen, aliens)
while True:
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings, screen, ship, aliens, bullets)
bullets.update()
# Get rid of bullets that have disappeared.
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
print(len(bullets))
示例3: ExplorableScreen
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [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: update
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
def update(self):
"""Updates cells' positions only if target is followable"""
if self.target.is_followable():
Group.update(self)
if self.delay > 0:
self.delay -= 1
示例5: CollectCoinMicrogame
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class CollectCoinMicrogame(Microgame):
def __init__(self):
# TODO: Initialization code here
Microgame.__init__(self)
self.coins = coin(0) #coin(locals.HEIGHT + 70)]
self.ironman = ironman()
self.sprites = Group(self.ironman, self.coins)
self.time = pygame.time.get_ticks()
def start(self):
# TODO: Startup code here
music.load(os.path.join("games", "catching", "super_mario_levels.wav"))
music.play()
def stop(self):
# TODO: Clean-up code here
music.stop()
self.lose()
def update(self, events):
# TODO: Update code here
self.sprites.update()
ctrls = pygame.key.get_pressed()
if ctrls[K_q]:
self.win()
elif ctrls[K_a] or ctrls[K_LEFT]:
self.ironman.rect.x = max(self.ironman.rect.x - 30, 0)
elif ctrls[K_d] or ctrls[K_RIGHT]:
self.ironman.rect.x = min(locals.WIDTH - 68, self.ironman.rect.x + 30)
if self.coins.rect.colliderect(self.ironman):
# self.time = pygame.time.get_ticks()
# self.sprites.remove(self.coins)
# print str(self.time) + " " + str(pygame.time.get_ticks())
# if self.time + 3000 <= pygame.time.get_ticks():
# self.coins = coin(0)
# self.sprites.add(self.coins)
self.coins.rect.y = 0
self.coins.velocity = 0
self.coins.rect.left = randint(0, locals.WIDTH - COIN_WIDTH)
# self.sprites.update()
elif self.coins.rect.top > locals.HEIGHT:
self.lose()
def render(self, surface):
# TODO: Rendering code here
surface.fill(Color(0, 0, 0))
imgpath = os.path.join("games", "catching", "8bitsky.jpg")
test_image = pygame.image.load(imgpath)
surface.blit(test_image,(0,0))
self.sprites.draw(surface)
def get_timelimit(self ):
# TODO: Return the time limit of this game (in seconds)
return 15
示例6: __init__
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class Panel:
def __init__(self, world_map):
self.world_map = world_map
self.group = Group()
self.energy_bar = EnergyBar(world_map.main_character, 15, 10, self.group)
self.energy_bar.put(75, 13)
self.energy_bar = LifeBar(world_map.main_character, 15, 10, self.group)
self.energy_bar.put(75, 30)
self.rect = Rect(0, 0, SCREEN_W, 40)
self.background = data.load_image('panel.png')
font = Font(data.filepath('fonts', 'vera.ttf'), 12)
#font.set_bold(True)
self.twister_text = font.render("Twister:", True, (0,0,0))
self.life_text = font.render("Life:", True, (0,0,0))
self.world_text = font.render("World: %s" % world_map.name, True, (0,0,0))
class TimeText(Sprite):
def __init__(self, world_map, *groups):
Sprite.__init__(self, *groups)
self.world_map = world_map
def update(self, *args):
time = self.world_map.time
time_str = self.world_map.get_time()
if time < 60:
color = (170, 0, 0)
elif time < 120:
color = (255, 100, 0)
else:
color = (0, 0, 0)
self.image = font.render("Time: %s"% time_str , True, color)
self.rect = self.image.get_rect()
self.rect.move_ip(500, 0)
TimeText(world_map, self.group)
self.key = KeyItem(world_map)
self.key.put(620, 20)
self.key.remove(self.key.groups())
def draw(self, screen):
if self.world_map.got_key:
self.key.add(self.group)
screen.set_clip(self.rect)
self.group.clear(screen, self.background)
self.group.update()
self.group.draw(screen)
def draw_background(self, screen):
screen.blit(self.background, (0,0))
screen.blit(self.twister_text, (10, 0))
screen.blit(self.life_text, (10, 17))
screen.blit(self.world_text, (500, 17))
示例7: __init__
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class Logo:
"Escena que muestra el logotipo del grupo losersjuegos."
def __init__(self, world):
self.world = world
self.sprites = Group()
self._create_sprites()
self._reset_timer()
def _reset_timer(self):
self.time_out = 50
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_ESCAPE]:
self.world.state = Logo(self.world)
self._reset_timer()
return
else:
self.sprites.update()
if self.time_out < 1 or key[pygame.K_RETURN]:
self.world.change_state(scenes.mainmenu.MainMenu(self.world))
self.time_out -= 1
def draw(self, screen):
screen.fill((92, 123, 94))
self.sprites.draw(screen)
pygame.display.flip()
def _create_sprites(self):
steps = [0, 0, 0, 1, 2, 3, 4]
losers = SimpleAnimation('logo', 'losers_5.png', steps, 2)
sprite_losers = LogoSpriteAnimated(losers, LEFT, 190, 190)
steps2 = [0, 0, 0, 1, 2, 3, 4]
juegos = SimpleAnimation('logo', 'juegos_5.png', steps2, 2)
sprite_juegos = LogoSpriteAnimated(juegos, RIGHT, 390, 190)
steps3 = [0, 0, 0, 1, 1, 2, 3]
ceferino = SimpleAnimation('logo', 'ceferino_4.png', steps3, 2)
sprite_ceferino = LogoSpriteAnimated(ceferino, RIGHT, 40, 160)
self.sprites.add([sprite_juegos, sprite_losers, sprite_ceferino])
def handle_event(self, event):
pass
示例8: Game
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class Game(object):
background = 80,80,80
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((640,480))
self.players = Group()
p1 = Player((255,0,0), self.players)
p2 = Player((0,0,255), self.players)
def draw(self):
self.screen.fill(self.background)
def update(self):
self.players.update()
示例9: run_game
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
def run_game():
# Initialize game and create a screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Zombie Tower Ultimate Sonic The Next Generation Super EX")
play_button = Button(ai_settings, screen, "Play")
# Set the background color
ship = Ship(screen, ai_settings)
bullets = Group()
bomb = Group()
lasercheat = Group()
nuke = Nuke(ai_settings, screen, ship)
block = Block(ai_settings, screen, nuke)
wall = Wall(ai_settings, screen, ship)
lift = Lift(ai_settings, screen, nuke)
aliens = Group()
alien = Alien(ai_settings, screen)
# Start the main loop for the game.
stats = GameStats(ai_settings, bomb, aliens, bullets)
sb = Scoreboard(ai_settings, screen, stats)
gf.create_fleet(ai_settings, screen, ship, aliens)
while True:
# Watch for keyboard and mouse events.
gf.check_events(ai_settings, screen, stats, ship, bullets, lasercheat, aliens, nuke, play_button, bomb, wall, lift)
if stats.game_active:
ship.update()
gf.update_aliens(ai_settings, stats, screen, ship, aliens, bomb, wall)
ai_settings.counterpnts -= 1
if ai_settings.counterpnts <=0:
ai_settings.counterpnts = 60
stats.score += 1
sb.prep_score()
bullets.update()
bomb.update(aliens, ai_settings, screen)
gf.update_screen(ai_settings, screen, stats, sb, ship, bullets, lasercheat, aliens, nuke, play_button, wall, bomb, lift, block)
# Get rid of bullets that have disappeared.
for bullet in bullets.copy():
if bullet.rect.right >= 1200:
bullets.remove(bullet)
print(len(bullets))
示例10: WvmSpritesList
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class WvmSpritesList():
"""A class listing all the Sprites of the game."""
def __init__(self, config, screen):
"""Initialize the sprite list."""
self.config = config
self.screen = screen
#initialize the sprites
self.wiz = Wizard(config, self)
self.monsters = Group()
self.missiles = Group()
def update_all(self):
"""Update the positions of all sprites."""
self.update_missiles()
self.wiz.update()
self.monsters.update()
def update_missiles(self):
"""update magic missiles positions"""
self.missiles.update()
# remove the missiles that have left the screen
for mi in self.missiles.copy():
if mi.rect.left >= self.screen.get_rect().right:
self.missiles.remove(mi)
def draw(self):
self.screen.fill(self.config.bg_color)
for mi in self.missiles:
mi.draw_missile()
self.wiz.blitme()
for mo in self.monsters:
mo.blitme()
def fire_missile(self):
"""Fire a missile if limit not reached yet."""
if len(self.missiles) < self.wiz.magic_missile_allowed:
self.missiles.add(MagicMissile(self.config, self))
def create_monster(self):
"""Create a new monster and place it randomly at the right."""
monster=Monster(self.config, self)
#TODO move the monster
self.monsters.add(monster)
示例11: game
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
def game():
# init
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
clock = pygame.time.Clock()
dude = PlayerShip(260, 500, screen.get_rect())
dude_grp = GroupSingle(dude)
enemies = Group()
enemies.add(Burger(200, 200, screen.get_rect()))
enemies.add(Hotdog(100, 100, screen.get_rect()))
#loop
while True:
# input
for evt in pygame.event.get():
if evt.type == QUIT:
return
elif evt.type == KEYDOWN and evt.key == K_ESCAPE:
return
elif evt.type == KEYDOWN and evt.key == K_a:
dude.dx = -10
elif evt.type == KEYDOWN and evt.key == K_d:
dude.dx = 10
elif evt.type == KEYUP and evt.key == K_a and dude.dx == -10:
dude.dx = 0
elif evt.type == KEYUP and evt.key == K_d and dude.dx == 10:
dude.dx = 0
elif evt.type == KEYDOWN and evt.key == K_SPACE and dude.alive():
dude.shoot()
# update
clock.tick(FPS)
dude.update()
enemies.update()
dude.bullets.update()
pygame.sprite.groupcollide(enemies, dude.bullets, 1, 1)
pygame.sprite.groupcollide(dude_grp, enemies, 1, 0)
# draw
screen.fill(BLACK)
dude_grp.draw(screen)
enemies.draw(screen)
dude.bullets.draw(screen)
pygame.display.flip()
示例12: KoalaKross
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class KoalaKross(Microgame):
def __init__(self):
Microgame.__init__(self)
self.rect= Rect(0,0, locals.WIDTH, locals.HEIGHT)
## surface=pygame.display.set_mode((1024,768))
## surface.fill(Color(0,0,250))
koala=Koala(self)
self.panda1=Panda(200,100,0,2)
self.panda2=Panda(500,100,0,2)
self.bread=Bread(self)
self.sprites=Group((koala,self.panda1,self.panda2,self.panda3))
self.clock=pygame.time.Clock()
def start():
pass
def stop():
pass
def update(self):
time=pygame.time.get_ticks()
self.sprites.update()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key==K_DOWN:
koala.velocity=(0,-3)
elif event.key==K_UP:
koala.velocity=(3,0)
elif event.key == K_Right:
koala.velocity=(3,0)
elif event.key == K_Left:
koala.velocity=(-3,0)
elif event.key == K_q:
self.lose()
elif event.type == KEYUP:
koala.velocity=(0,0)
def render (self, surface):
## Surface=display.set_mode((1024,768))
surface.fill.Color(0,0,0)
self.sprites.draw(surface)
示例13: evade
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class evade(Microgame):
def __init__(self):
Microgame.__init__(self)
self.e_icicles = [e_icicle(0), e_icicle(locals.HEIGHT + 70),e_icicle(locals.HEIGHT+100),e_icicle(locals.HEIGHT+10),e_icicle(100),
e_icicle(100),e_icicle(700),e_icicle(300),e_icicle(500)]
self.e_eskimo = eskimo()
self.sprites = Group(self.e_eskimo, *self.e_icicles)
def start(self):
music.load(os.path.join("games", "evadeFull", "alt_song.wav"))
music.play()
def stop(self):
music.stop()
self.lose()
def update(self, events):
self.sprites.update()
keys = pygame.key.get_pressed()
if keys[K_q]:
self.win()
elif (keys[K_RIGHT] or keys[K_d]) and (keys[K_LEFT] or keys[K_a]):
pass
elif keys[K_LEFT] or keys[K_a]:
self.e_eskimo.rect.x = max(self.e_eskimo.rect.x - 15, 0)
elif keys[K_RIGHT] or keys[K_d]:
self.e_eskimo.rect.x = min(locals.WIDTH -57, self.e_eskimo.rect.x + 15)
for icicle in self.e_icicles:
if self.e_eskimo.rect.colliderect(icicle.rect):
music.stop()
self.lose()
def render(self, surface):
surface.fill((0, 0, 0))
imgpathh = os.path.join("games", "evadeFull", "tile.png")
test_image = pygame.image.load(imgpathh)
surface.blit(test_image,(0,0))
self.sprites.draw(surface)
def get_timelimit(self):
return 15
示例14: run_game
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
def run_game():
# Initialize game, settings and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# Make a ship
ship = Ship(ai_settings, screen)
# Make a group to store bullets in.
bullets = Group()
# Start the main loop for the game.
while True:
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()
bullets.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings, screen, ship, bullets)
示例15: __init__
# 需要導入模塊: from pygame.sprite import Group [as 別名]
# 或者: from pygame.sprite.Group import update [as 別名]
class Board:
def __init__(self, width: int, height: int):
super().__init__()
self.width = width
self.height = height
self.players = Group()
self.walls = Group()
for w in range(width >> 1):
for h in range(height >> 1):
wall = Wall()
wall.rect.x = PPM * (2 * w + 1)
wall.rect.y = PPM * (2 * h + 1)
self.walls.add(wall)
def draw(self, canvas):
self.walls.draw(canvas)
self.players.draw(canvas)
def update(self, dt):
self.players.update(dt)