2017-04-24 6 views
1

一定の時間が経過するか、x個のmobが生成された後、どのようにタイマーを表示することができますか?スクリーン。時間制限の後にスプライトを生成する方法とタイマーのパイゲームを表示する方法

Images

Current Code

ボス

class Boss(pygame.sprite.Sprite): 
def __init__(self): 
    pygame.sprite.Sprite.__init__(self) 
    self.image = pygame.transform.scale(boss_img, (150, 200)) 
    self.image.set_colorkey(Black) 
    self.rect = self.image.get_rect() 
    self.rect.center = ((Width/2, -70)) 
    self.speedy = 1 
    self.shoot_delay = 250 
    self.last_shot = pygame.time.get_ticks() 
    self.hp = 150 
    self.dead = False 

def update(self): 
    if self.hp <= 25: 
     self.speedy = 3 
    if self.rect.top > Height + 10: 
     player.lives = 0 
+0

イメージ/サウンドが失われてコードをエラーなく実行できるように、イメージ/サウンドもアップロードしますか?これは、それを見て簡単になります... – Claudio

+0

編集された追加imgurアルバム、まだ音を追加していない。 – Fraser

答えて

1

のクラスpygameの中にタイマーを実装する方法はいくつかあります。 pygame.Clock.tickが返す時刻を使用してタイマ変数を増減するか、pygame.time.get_ticksで時差を計算するか、pygame.time.set_timerと組み合わせてカスタムイベントを使用します。

例1 - デルタ時間:あなたは正確に1つのスプライトを起動したい場合は、タイマーをboss_spawned = Falseのような別の変数を追加して変更することができ

import sys 
import random 
import pygame as pg 


class Block(pg.sprite.Sprite): 

    def __init__(self, pos): 
     super().__init__() 
     self.image = pg.Surface((40, 40)) 
     self.image.fill(pg.Color('sienna1')) 
     self.rect = self.image.get_rect(topleft=pos) 


def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 
    font = pg.font.Font(None, 30) 
    all_sprites = pg.sprite.Group() 
    # Delta time is the time that has passed since clock.tick 
    # was called the last time. 
    dt = 0 
    # We'll subtract dt (delta time) from this timer variable. 
    timer = 1 # 1 means one second. 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 

     # Decrease timer to get a countdown. 
     timer -= dt 
     # When the timer is below or equal to 0, we spawn 
     # a new block. 
     if timer <= 0: 
      all_sprites.add(Block((random.randrange(600), 
            random.randrange(440)))) 
      # Reset the countdown timer to one second. 
      timer = 1 
     all_sprites.update() 
     screen.fill(pg.Color('gray15')) 
     all_sprites.draw(screen) 
     timer_surface = font.render(
      str(round(timer, 3)), True, pg.Color('yellow')) 
     screen.blit(timer_surface, (20, 20)) 

     pg.display.flip() 
     # dt = time in seconds that passed since last tick. 
     # Divide by 1000 to convert milliseconds to seconds. 
     dt = clock.tick(30)/1000 


if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit() 
    sys.exit() 

上司が生み出されていない場合にのみ:

if not boss_spawned: 
    timer -= dt 
    if timer <= 0: 
     all_sprites.add(Block((random.randrange(600), 
           random.randrange(440)))) 
     boss_spawned = True 

または、スポーン後にタイマーを正確に0に設定し、!= 0の場合はタイマーを減らしてください。

if timer != 0: 
    timer -= dt 
    if timer <= 0: 
     all_sprites.add(Block((random.randrange(600), 
           random.randrange(440)))) 
     timer = 0 

例2 - pygame.time.get_ticks(上記の主な機能を置き換える):

def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 
    font = pg.font.Font(None, 30) 
    all_sprites = pg.sprite.Group() 
    # Start time. 
    now = pg.time.get_ticks() 
    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 

     # If the time difference is greater than 1000 
     # milliseconds, spawn a block. 
     time_difference = pg.time.get_ticks() - now 
     if time_difference >= 1000: 
      all_sprites.add(Block((random.randrange(600), 
            random.randrange(440)))) 
      # Reset the start time. 
      now = pg.time.get_ticks() 
     all_sprites.update() 
     screen.fill(pg.Color('gray15')) 
     all_sprites.draw(screen) 
     timer_surface = font.render(
      str(time_difference/1000), True, pg.Color('yellow')) 
     screen.blit(timer_surface, (20, 20)) 

     pg.display.flip() 
     clock.tick(30) 

あなただけのキルまたは生み出されたモブをカウントしたい場合は、インクリメントすることができますカウンター変数を使用し、敵のボスが一定の限界を超えたときにそれを発動させます。次の例では、マウスのクリック数をカウントし、3回のクリック後にブロックを生成します。

def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 
    font = pg.font.Font(None, 30) 
    all_sprites = pg.sprite.Group() 
    # We'll just count mouse clicks in this example. 
    # You can replace it with the kill count in your game. 
    clicks = 0 
    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      if event.type == pg.MOUSEBUTTONDOWN: 
       clicks += 1 

     if clicks >= 3: 
      all_sprites.add(Block((random.randrange(600), 
            random.randrange(440)))) 
      clicks = 0 
     all_sprites.update() 
     screen.fill(pg.Color('gray15')) 
     all_sprites.draw(screen) 
     clicks_surface = font.render(str(clicks), True, pg.Color('yellow')) 
     screen.blit(clicks_surface, (20, 20)) 

     pg.display.flip() 
     clock.tick(30) 
関連する問題