Beispiel #1
0
class GameScene(BaseScene):
    max_collisions = 15
    balls_count = 3

    def __init__(self, game):
        super().__init__(game)
        self.balls = [BallObject(game) for _ in range(GameScene.balls_count)]
        self.platform = Platform(game, 'images/brick.png',
                                 game.width // 2 + 50, game.height - 50, 4)
        self.collision_count = 0
        self.score = 0
        self.hs_arr = []
        self.update_highs_scores()
        self.status_text = TextObject(self.game, 0, 0,
                                      self.get_collisions_text(),
                                      (255, 255, 255))
        self.score_text = TextObject(self.game, 0, 0, self.get_score_text(),
                                     (255, 255, 255))
        self.status_text.move(10, 10)
        self.objects += self.balls
        self.objects.append(self.status_text)
        self.objects.append(self.score_text)
        self.objects.append(self.platform)

        self.reset_balls_position()

    def process_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.game.set_scene(self.game.SCENE_MENU)
        self.platform.process_event(event)

    def get_random_position(self, radius):
        return randint(10, self.game.width - radius * 2 - 10), randint(
            10, self.game.height - radius * 2 - 10)

    def set_random_position(self, ball):
        pos = self.get_random_position(ball.radius)
        ball.move(*pos)

    def reset_balls_position(self):
        for ball in self.balls:
            ball.move(self.game.width, self.game.height)

    def set_random_unique_position(self):
        for index in range(len(self.balls)):
            other_rects = [
                self.balls[i].rect for i in range(len(self.balls))
                if i != index
            ]
            self.set_random_position(self.balls[index])
            while self.balls[index].rect.collidelist(other_rects) != -1:
                self.set_random_position(self.balls[index])

    def on_activate(self):
        self.collision_count = 0
        self.score = 0
        self.reset_balls_position()
        self.set_random_unique_position()
        self.status_text.update_text(self.get_collisions_text())
        self.status_text.move(10, 10)
        self.score_text.update_text(self.get_score_text())
        self.score_text.move(10, 40)

    def check_ball_intercollisions(self):
        for i in range(len(self.balls) - 1):
            for j in range(i + 1, len(self.balls)):
                if self.balls[i].collides_with(self.balls[j]):
                    self.balls[i].bounce(self.balls[j])

    def get_collisions_text(self):
        return 'Wall collisions: {}/{}'.format(self.collision_count,
                                               GameScene.max_collisions)

    def get_score_text(self):
        return 'Score: ' + str(int(self.score))

    def check_ball_edge_collision(self):
        for ball in self.balls:
            if ball.edge_collision():
                self.collision_count += 1
                self.status_text.update_text(self.get_collisions_text())
                self.status_text.move(10, 10)

    def increase_score(self):
        self.score += 0.01
        self.score_text.update_text(self.get_score_text())
        self.score_text.move(10, 40)

    def add_new_highscore(self):
        with open('highscores.txt', 'a') as hs_file:
            hs_file.writelines('\n' + str(round(self.score, 2)))
        print('Highscore has been written')

    def update_highs_scores(self):
        with open('highscores.txt', 'r') as hs_file:
            self.hs_arr = [round(float(i), 2) for i in hs_file.readlines()]

    def check_game_over(self):
        if self.collision_count >= GameScene.max_collisions:
            self.add_new_highscore()
            self.update_highs_scores()
            self.game.set_scene(self.game.SCENE_GAMEOVER)

    def check_ball_platform_collision(self):
        for ball_ndx in range(len(self.balls)):
            self.balls[ball_ndx].check_platform_collision(self.platform)

    def process_logic(self):
        super().process_logic()
        self.increase_score()
        self.check_ball_edge_collision()
        self.check_ball_intercollisions()
        self.check_ball_platform_collision()
        self.platform.move()
        self.check_game_over()
Beispiel #2
0
class GameScene(BaseScene):
    max_collisions = 15
    balls_count = 1
    collision_tolerance = 6
    standart_speed = randrange(2, 3)
    accelerate = 1.15

    def __init__(self, game):
        super().__init__(game)
        self.start_time = time.time()
        self.platform = PlatformObject(game, speed=GameScene.standart_speed)
        self.balls = [BallObject(game, speed=[GameScene.standart_speed, GameScene.standart_speed])
                      for _ in range(GameScene.balls_count)]
        self.collision_count = 0
        self.score_text = TextObject(self.game, 0, 0, self.get_score_text(), ORANGE)
        self.score_text.move(10, 10)
        self.objects += self.balls
        self.objects.append(self.score_text)
        self.objects.append(self.platform)
        self.reset_balls_position()

    def process_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.game.set_scene(self.game.SCENE_MENU)
            elif event.key == pygame.K_a or event.key == pygame.K_d:
                self.platform.process_event(event)

    def update_score(self):
        self.score_text.update_text(self.get_score_text())
        self.score_text.move(10, 10)

    def get_score(self):
        return int(time.time() - self.start_time)

    def get_score_text(self):
        return f'Score: {self.get_score()} seconds'

    def get_random_position(self, radius):
        return randint(10, self.game.width - radius * 2 - 10), randint(10, self.game.height - radius * 2 - 10)

    def set_random_position(self, ball):
        pos = self.get_random_position(ball.radius)
        ball.move(*pos)

    def reset_balls_position(self):
        for ball in self.balls:
            ball.move(self.game.width, self.game.height)

    def set_random_unique_position(self):
        for index in range(len(self.balls)):
            other_rects = [self.balls[i].rect for i in range(len(self.balls)) if i != index]
            self.set_random_position(self.balls[index])
            while self.balls[index].rect.collidelist(other_rects) != -1:
                self.set_random_position(self.balls[index])

    def on_activate(self):
        self.start_time = time.time()
        self.collision_count = 0
        self.reset_balls_position()
        self.set_random_unique_position()
        self.score_text.update_text(self.get_score_text())
        self.score_text.move(10, 10)

    def collide_platform_with_ball(self):
        if self.platform.rect.colliderect(self.balls[0].rect):
            if abs(self.balls[0].rect.bottom - self.platform.rect.top) < GameScene.collision_tolerance and \
                    self.balls[0].speed[1] > 0:
                self.balls[0].speed[1] *= -GameScene.accelerate
            elif abs(self.balls[0].rect.left - self.platform.rect.right) < GameScene.collision_tolerance and \
                    self.balls[0].speed[0] < 0:
                self.balls[0].speed[0] *= -GameScene.accelerate
            elif abs(self.balls[0].rect.right - self.platform.rect.left) < GameScene.collision_tolerance and\
                    self.balls[0].speed[0] > 0:
                self.balls[0].speed[0] *= -GameScene.accelerate

    def check_game_over(self):
        if self.balls[0].rect.bottom >= self.game.height:
            self.game.fileul.set_score(self.get_score())
            self.game.fileul.write_file_data()
            self.game.set_scene(self.game.SCENE_GAMEOVER)
            self.start_time = 0

    def process_logic(self):
        super().process_logic()
        self.collide_platform_with_ball()
        self.update_score()
        self.check_game_over()