示例#1
0
    def update(self):
        #Game Loop - Update
        #update all_sprite group
        self.all_sprites.update()
        #update the obstacles
        for obs in self.obstacles:
            obs.update()

        #check if player hits a ground - only if falling
        if self.player.velocity.y > 0:
            hits = pygame.sprite.collide_rect(self.player, self.ground)
            #if player hits a Ground, put him on the ground
            if hits:
                self.player.pos.y = self.ground.rect.y
                self.player.velocity.y = 0

        #ckeck if player hits a obstacle
        for obs in self.obstacles:
            if obs.rect.left < PLAYER_X + 30:
                hits = pygame.sprite.collide_rect(self.player, obs)
                if hits:
                    self.playing = False

        #kill old obstacles and count up score
        for i, obs in enumerate(self.obstacles):
            if obs.rect.left < -60:
                self.score += 10
                self.obstacles.pop(i)

        #only spawn new obstacles if the last one is far enough away
        if not self.soon_boss_Obstacle and self.obstacles[-1].rect.x < 830:
            self.obstacles.append(Obstacle())

        #boss is comming
        if (self.score + 10) % 90 == 0:
            self.soon_boss_Obstacle = True
            self.t0 = time.perf_counter()
            self.once = False

        #timer for boss spawntime
        self.t1 = time.perf_counter()

        #check if t0 existist
        try:
            self.t0
        except AttributeError:
            var_exists = False
        else:
            var_exists = True
        #spawns the boss
        if var_exists:
            if self.t1 - self.t0 > 3 and not self.once:
                self.soon_boss_Obstacle = False
                self.once = True
                bossObs = Obstacle()
                bossObs.speed = 30
                bossObs.setcolor(RED)
                self.obstacles.append(bossObs)