예제 #1
0
class Level(object):
    initial_coins = 20

    def __init__(self, size):
        self.bounds = Rect((0, 0), size)

    def draw(self, surf):
        self.draw_background(surf)
        self.coins.draw(surf)
        surf.blit(self.player.image, self.player.rect)

    def draw_background(self, surf):
        surf.fill((80, 80, 80))

    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()

    def update(self, dt):
        self.player.update(dt)
        self.coins.update(dt)

        # lock player in bounds
        self.player.rect.clamp_ip(self.bounds)

        spritecollide(self.player, self.coins, True)
예제 #2
0
class Game(object):
    def __init__(self, screen):
        self.screen = screen

        #        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
        #        self.level = Level("test_level", tilesheet)
        #        self.player = GroupSingle(Player(self.level.bounds))
        self.coins = CoinGroup(self.level.bounds)

        self.font = pygame.font.Font(None, 36)

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()

    def quit(self):
        self.done = True

    def update(self):
        dt = self.clock.tick(FPS)

        # basic update
        self.player.update(dt)
        self.coins.update(dt)

        # check if the player is on the path
        onpath = spritecollide(self.player, self.level.path, False)
        self.player.on_path(onpath)

        # collide coins
        for coin in spritecollide(self.player, self.coins, True):
            self.score += 1

        self.cam.update(self.player.rect)

    def draw(self):
        # draw level
        self.cam.draw_background(self.game_area, self.level.background)
        self.cam.draw_sprite(self.game_area, self.player)

        for coin in self.coins:
            self.cam.draw_sprite(self.game_area, coin)

    def run(self):
        self.done = False
        self.clock = pygame.time.Clock()

        while not self.done:
            # input
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.quit()
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    self.quit()

            self.update()
            self.draw()
            pygame.display.flip()
예제 #3
0
    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()
예제 #4
0
    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()

        # start the background music
        play_song(self.song)  
예제 #5
0
    def __init__(self, screen):
        self.screen = screen

        #        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
        #        self.level = Level("test_level", tilesheet)
        #        self.player = GroupSingle(Player(self.level.bounds))
        self.coins = CoinGroup(self.level.bounds)

        self.font = pygame.font.Font(None, 36)

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()
예제 #6
0
    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()
예제 #7
0
파일: level.py 프로젝트: alecgoebel/div2
class Level(object):
    def __init__(self, size):
        self.bounds = Rect((0,0), size)

    def restart(self):
        self.player = Player(self)
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

    def update(self, dt):
        self.player.update(dt)
        self.coins.update(dt)

        # lock player in bounds
        self.player.rect.clamp_ip(self.bounds)

        # collide player with coins
        for coin in spritecollide(self.player, self.coins, False):
            coin.collect(self.player)
예제 #8
0
class Level(object):
    def __init__(self, size):
        self.bounds = Rect((0, 0), size)

    def restart(self):
        self.player = Player(self)
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

    def update(self, dt):
        self.player.update(dt)
        self.coins.update(dt)

        # lock player in bounds
        self.player.rect.clamp_ip(self.bounds)

        # collide player with coins
        for coin in spritecollide(self.player, self.coins, False):
            coin.collect(self.player)
예제 #9
0
    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()

        # start the background music
        play_song(self.song)  
예제 #10
0
class Level(object):
    initial_coins = 20
    song = "maintheme"

    def __init__(self, size):
        self.bounds = Rect((0,0), size)
        self.bg_tile = load_image("grass")

    def draw(self, surf):
        self.draw_background(surf)
        self.coins.draw(surf)
        surf.blit(self.player.image, self.player.rect)

    def draw_background(self, surf):
        tw, th = self.bg_tile.get_size()
        sw, sh = surface.get_size()
        for y in range(0, sh, th):
            for x in range(0, sw, tw):
                surf.blit(self.bg_tile, (x,y))
        
    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()

        play_song(self.song)
    
    def update(self, dt):
        self.player.update(dt)
        self.coins.update(dt)

        # lock player in bounds
        self.player.rect.clamp_ip(self.bounds)

        spritecollide(self.player, self.coins, True)
예제 #11
0
    def __init__(self, screen):
        self.screen = screen

#        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
#        self.level = Level("test_level", tilesheet)
#        self.player = GroupSingle(Player(self.level.bounds))
        self.coins = CoinGroup(self.level.bounds)

        self.font = pygame.font.Font(None, 36)

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()
예제 #12
0
    def __init__(self, screen):
        self.screen = screen

        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
        self.level = Level("test_level", tilesheet)
        self.player = Player(self.level.bounds)
        self.coins = CoinGroup(self.level.bounds)

        self.hud = screen.subsurface((0, 0, screen.get_width(), 40))
        self.game_area = screen.subsurface(
            (0, 40, screen.get_width(), screen.get_height() - 40))

        self.cam = Camera(self.player, self.level.bounds,
                          self.game_area.get_size())

        self.font = pygame.font.Font(None, 36)

        self.score = 0

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()
예제 #13
0
class Level(object):
    initial_coins = 5
    song = "maintheme"

    def __init__(self, size):
        self.bounds = Rect((0, 0), size)
        self.bg_tile = load_image("grass")
        self.score_font = pygame.font.Font(None, 48)

    def draw_background(self, surf):
        tw, th = self.bg_tile.get_size()
        sw, sh = surf.get_size()

        for y in range(0, sh, th):
            for x in range(0, sw, tw):
                surf.blit(self.bg_tile, (x, y))

    def restart(self):
        self.score = 0
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()

        # start the background music
        play_song(self.song)

    def update(self, dt):
        self.player.update(dt)
        self.coins.update(dt)

        # lock player in bounds
        self.player.rect.clamp_ip(self.bounds)

        # collide player with coins
        if spritecollide(self.player, self.coins, True):
            self.score += 1

    def draw(self, surf):
        self.draw_background(surf)
        self.coins.draw(surf)
        surf.blit(self.player.image, self.player.rect)

        score = self.score_font.render(str(self.score), True, (0, 0, 0))
        rect = score.get_rect()
        rect.topright = self.bounds.inflate(-5, -5).topright
        surf.blit(score, rect)
예제 #14
0
    def __init__(self, screen):
        self.screen = screen

        self.level = Level()
        self.player = Player(self.level.bounds) 
        self.coins = CoinGroup(self.level.bounds)

        self.hud = screen.subsurface((0, 0, screen.get_width(), 40))
        self.game_area = screen.subsurface((0, 40, screen.get_width(), screen.get_height() - 40))

        self.cam = Camera(self.player, self.level.bounds, self.game_area.get_size())

        self.font = pygame.font.Font(None, 36)

        self.score = 0

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()
예제 #15
0
    def __init__(self, screen):
        self.screen = screen

        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
        self.level = Level("test_level", tilesheet)
        self.player = Player(self.level.bounds) 
        self.coins = CoinGroup(self.level.bounds)

        self.hud = screen.subsurface((0, 0, screen.get_width(), 40))
        self.game_area = screen.subsurface((0, 40, screen.get_width(), screen.get_height() - 40))

        self.cam = Camera(self.player, self.level.bounds, self.game_area.get_size())

        self.font = pygame.font.Font(None, 36)

        self.score = 0

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()
예제 #16
0
class Level(object):
    initial_coins = 20
    song = "maintheme"

    def __init__(self, size):
        self.bounds = Rect((0,0), size)
        self.bg_tile = load_image("grass")
        self.coin_sfx = load_sfx("coin")

    def draw_background(self, surf):
        tw, th = self.bg_tile.get_size()
        sw, sh = surf.get_size()

        for y in range(0, sh, th):
            for x in range(0, sw, tw):
                surf.blit(self.bg_tile, (x,y))

    def restart(self):
        self.player = Player()
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)

        # create initial coins
        for i in range(self.initial_coins):
            self.coins.spawn()

        # start the background music
        play_song(self.song)  
    
    def update(self, dt):
        self.player.update(dt)
        self.coins.update(dt)

        # lock player in bounds
        self.player.rect.clamp_ip(self.bounds)

        # collide player with coins
        if spritecollide(self.player, self.coins, True):
            self.coin_sfx.stop()
            self.coin_sfx.play()

    def draw(self, surf):
        self.draw_background(surf)
        self.coins.draw(surf)
        surf.blit(self.player.image, self.player.rect)
예제 #17
0
    def restart(self):
        self.player = Player(self)
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)
예제 #18
0
파일: level.py 프로젝트: alecgoebel/div2
    def restart(self):
        self.player = Player(self)
        self.player.rect.center = self.bounds.center

        self.coins = CoinGroup(self.bounds)
예제 #19
0
class Game(object):
    def __init__(self, screen):
        self.screen = screen

        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
        self.level = Level("test_level", tilesheet)
        self.player = Player(self.level.bounds)
        self.coins = CoinGroup(self.level.bounds)

        self.hud = screen.subsurface((0, 0, screen.get_width(), 40))
        self.game_area = screen.subsurface(
            (0, 40, screen.get_width(), screen.get_height() - 40))

        self.cam = Camera(self.player, self.level.bounds,
                          self.game_area.get_size())

        self.font = pygame.font.Font(None, 36)

        self.score = 0

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()

    def quit(self):
        self.done = True

    def update(self):
        dt = self.clock.tick(FPS)

        # basic update
        self.player.update(dt)
        self.coins.update(dt)

        # check if the player is on the path
        onpath = spritecollide(self.player, self.level.path, False)
        self.player.on_path(onpath)

        # collide coins
        for coin in spritecollide(self.player, self.coins, True):
            self.score += 1

        self.cam.update(self.player.rect)

    def draw(self):
        # draw level
        self.cam.draw_background(self.game_area, self.level.background)
        self.cam.draw_sprite(self.game_area, self.player)

        for coin in self.coins:
            self.cam.draw_sprite(self.game_area, coin)

        # draw hud
        self.hud.fill((20, 20, 20))
        text = self.font.render("Score: %04d" % self.score, True,
                                (255, 255, 255), (20, 20, 20))
        self.hud.blit(text, (15, 10))

    def run(self):
        self.done = False
        self.clock = pygame.time.Clock()

        while not self.done:
            # input
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.quit()
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    self.quit()

            self.update()
            self.draw()
            pygame.display.flip()
예제 #20
0
class Game(object):

    def __init__(self, screen):
        self.screen = screen

#        tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
#        self.level = Level("test_level", tilesheet)
#        self.player = GroupSingle(Player(self.level.bounds))
        self.coins = CoinGroup(self.level.bounds)

        self.font = pygame.font.Font(None, 36)

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()


    def quit(self):
        self.done = True


    def update(self):
        dt = self.clock.tick(FPS)

        # basic update
        self.player.update(dt)
        self.coins.update(dt)

        # check if the player is on the path
        onpath = spritecollide(self.player, self.level.path, False)
        self.player.on_path(onpath)

        # collide coins
        for coin in spritecollide(self.player, self.coins, True):
            self.score += 1

        self.cam.update(self.player.rect)


    def draw(self):
        # draw level
        self.cam.draw_background(self.game_area, self.level.background)
        self.cam.draw_sprite(self.game_area, self.player)

        for coin in self.coins:
            self.cam.draw_sprite(self.game_area, coin)


    def run(self):
        self.done = False
        self.clock = pygame.time.Clock()

        while not self.done:
            # input
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.quit()
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    self.quit()

            self.update()
            self.draw()
            pygame.display.flip()
예제 #21
0
class Game(object):

    def __init__(self, screen):
        self.screen = screen

        self.level = Level()
        self.player = Player(self.level.bounds) 
        self.coins = CoinGroup(self.level.bounds)

        self.hud = screen.subsurface((0, 0, screen.get_width(), 40))
        self.game_area = screen.subsurface((0, 40, screen.get_width(), screen.get_height() - 40))

        self.cam = Camera(self.player, self.level.bounds, self.game_area.get_size())

        self.font = pygame.font.Font(None, 36)

        self.score = 0

        # create initial coins
        for i in range(COIN_N_STARTING):
            self.coins.spawn()


    def quit(self):
        self.done = True


    def update(self):
        dt = self.clock.tick(FPS)

        # basic update
        self.player.update(dt)
        self.coins.update(dt)

        # collide coins
        for coin in spritecollide(self.player, self.coins, True):
            self.score += 1

        self.cam.update(self.player.rect)


    def draw(self):
        # draw level
        self.cam.draw_background(self.game_area, self.level.background)
        self.cam.draw_sprite(self.game_area, self.player)

        for coin in self.coins:
            self.cam.draw_sprite(self.game_area, coin)

        # draw hud
        self.hud.fill((20,20,20))
        text = self.font.render("Score: %04d" % self.score, True, (255,255,255), (20, 20, 20))
        self.hud.blit(text, (15, 10))

    def run(self):
        self.done = False
        self.clock = pygame.time.Clock()

        while not self.done:
            # input
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.quit()
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    self.quit()

            self.update()
            self.draw()
            pygame.display.flip()