Exemple #1
0
class MainGameScene(Scene):
    def __init__(self):
        self.db = Database()
        self.db['points'] = 0
        self.db['points_per_click'] = 1
        self.db['speed'] = 10
        super(MainGameScene, self).__init__()
        self.background = Background((0, 123, 228))
        self.rails = MovingRail()
        self.label = TextLabel("Points: {}".format(self.db['points']),
                               os.path.join("Resources",
                                            Database()['font']), 16, (50, 18))
        self.train = Vehicle()
        self.train_rect = self.train.get_rect()
        self.button = Button(60, 200, 32, "Shop", (100, 100))
        self.button_rect = self.button.get_rect()
        print(self.train_rect)

    def render(self, screen):
        self.background.render(screen)
        self.rails.render(screen)
        self.label.render(screen)
        self.train.render(screen)
        self.button.render(screen)

    def update(self, delta_time: float):
        self.rails.update(delta_time)
        self.label.update_text("Points: {}".format(self.db['points']))

    def handle_event(self, event: pygame.event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.train_rect.collidepoint(event.pos):
                self.db['points'] += self.db['points_per_click']
            elif self.button_rect.collidepoint(event.pos):
                Director().set_scene(ShopScene())
Exemple #2
0
class Menu:
    def __init__(self, num_snows_scroll=50, background=BACKGROUNG_IMG, wind_power=50):
        pygame.init()
        self.num_snows_scroll = num_snows_scroll
        self.screen = pygame.display.set_mode((500, 500))
        self.btn_start = Button(pos=(150, 150), image_names=('button_on.png', 'button_hover.png', 'button_click.png'),
                                path=BUTTON_IMAGE_PATH, function=self.on_btn_start, text='Start! ', w=200)
        self.btn_settings = Button(pos=(150, 205),
                                   image_names=('button_on.png', 'button_hover.png', 'button_click.png'),
                                   path=BUTTON_IMAGE_PATH, function=self.on_btn_settings, text='Settings ', w=200)
        self.btn_exit = Button(pos=(150, 260), image_names=('button_on.png', 'button_hover.png', 'button_click.png'),
                               path=BUTTON_IMAGE_PATH, function=self.on_btn_exit, text='Exit ', w=200)
        self.work = True
        self.background = background
        self.background_image = pygame.transform.scale(load_image(self.background, 0, IMAGE_PATH), PLATFORM)
        self.wind_power = wind_power

    def on_btn_exit(self):
        sys.exit()

    def on_btn_start(self):
        self.work = False
        win_snow = Program(self.num_snows_scroll, background=self.background if self.background else None,
                           wind_power=self.wind_power)
        win_snow.run()
        # print('start')

    def on_btn_settings(self):
        self.work = False
        win_settings = Settings(background=self.background)
        win_settings.run()

    def render(self):
        self.btn_start.render(self.screen)
        self.btn_settings.render(self.screen)
        self.btn_exit.render(self.screen)

    def event(self, event):
        self.btn_start.event(event)
        self.btn_settings.event(event)
        self.btn_exit.event(event)

    def run(self):
        while self.work:
            self.screen.blit(self.background_image, (0, 0))

            for event in pygame.event.get():
                self.event(event)
                if event.type == pygame.QUIT:
                    sys.exit()

                    # self.update(0)
            self.render()

            pygame.display.flip()
Exemple #3
0
class Menu:
    def __init__(self):
        pygame.display.set_mode((WIN_WIDHT, WIN_HEIGH))  # создание окна
        self.start_b = Button(('button_on.png', 'button_hover.png', 'button_click.png'),
                              path=BUTTONS_PATH, pos=(410, 224), text='Start', function=self.start_game)
        self.top_b = Button(('button_on.png', 'button_hover.png', 'button_click.png'),
                              path=BUTTONS_PATH, pos=(410, 287), text='Top Records', function=self.start_top)
        # self.setting_b = Button(('button_on.png', 'button_hover.png', 'button_click.png'),
        #                       path=BUTTONS_PATH, pos=(410, 350), text='Setting', function=None)
        self.exit_b = Button(('button_on.png', 'button_hover.png', 'button_click.png'),
                              path=BUTTONS_PATH, pos=(410, 350), text='Exit', function=self.close)
        self.done = True

    def close(self):
        sys.exit()

    def update(self):
        pass

    def events(self, event):
        self.start_b.event(event)
        # self.setting_b.event(event)
        self.top_b.event(event)
        self.exit_b.event(event)

    def render(self, screen):
        self.start_b.render(screen)
        # self.setting_b.render(screen)
        self.top_b.render(screen)
        self.exit_b.render(screen)

    def start_game(self):
        pygame.font.init()
        snake = Snake((4, 4), (0, 0, 0), (255, 255, 255),
                  (pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s),
                  4, 4, menu=Menu())
        snake.run_snake()

    def start_top(self):
        top = Top()
        top.run()

    def run(self):
        pygame.init()
        pygame.font.init()
        pygame.display.set_caption('Menu')
        game_screen = pygame.Surface((WIN_WIDHT, WIN_HEIGH))
        screen = pygame.display.get_surface()
        clock = pygame.time.Clock()
        while self.done:  # главный цикл программы
            for event in pygame.event.get():  # цикл обработки очереди событий окна
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        sys.exit()
                self.events(event)
            pygame.display.update()
            clock.tick(FPS)
            screen.fill((10, 20, 30))
            game_screen.fill((10, 20, 30))
            self.render(screen)
Exemple #4
0
class ShopScene(Scene):
    def __init__(self):
        self.db = Database()
        super().__init__()
        self.background = Background((0, 123, 228))
        self.button = Button(60,
                             200,
                             32,
                             "Shop", (100, 100),
                             color=(144, 191, 51))
        self.button_rect = self.button.get_rect()
        self.label = TextLabel("Points: {}".format(self.db['points']),
                               "Resources/F77-Minecraft.ttf", 16, (50, 18))
        self.ten_km_up = Button(60,
                                350,
                                24,
                                "+10km/h (50 pts)", (200, 370),
                                color=(144, 191, 51))
        self.hundred_km_up = Button(60,
                                    350,
                                    24,
                                    "+100km/h (300 pts)", (200, 280),
                                    color=(144, 191, 51))
        self.five_hundred_km_up = Button(60,
                                         350,
                                         24,
                                         "+500km/h (1000 pts)", (200, 190),
                                         color=(144, 191, 51))
        self.double_click_points = Button(60,
                                          350,
                                          24,
                                          "2X pts. (200 pts)", (600, 370),
                                          color=(144, 191, 51))

    def render(self, screen: pygame.Surface):
        self.background.render(screen)
        self.button.render(screen)
        self.label.render(screen)
        self.ten_km_up.render(screen)
        self.hundred_km_up.render(screen)
        self.five_hundred_km_up.render(screen)
        self.double_click_points.render(screen)

    def update(self, delta_time: float):
        self.label.update_text("Points: {}".format(self.db['points']))

    def handle_event(self, event: pygame.event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.button_rect.collidepoint(event.pos):
                Director().pop_scene()
            elif self.ten_km_up.get_rect().collidepoint(event.pos):
                if self.db['points'] >= 50:
                    self.db['speed'] += 10
                    self.db['points'] -= 50
            elif self.hundred_km_up.get_rect().collidepoint(event.pos):
                if self.db['points'] >= 300:
                    self.db['speed'] += 100
                    self.db['points'] -= 300
            elif self.five_hundred_km_up.get_rect().collidepoint(event.pos):
                if self.db['points'] >= 1000:
                    self.db['speed'] += 500
                    self.db['points'] -= 1000
            elif self.double_click_points.get_rect().collidepoint(event.pos):
                if self.db['points'] >= 200:
                    self.db['points_per_click'] *= 2
                    self.db['points'] -= 200
Exemple #5
0
class Settings:
    def __init__(self, background=BACKGROUNG_IMG, pos=(0, 0), size=(20, 20)):
        pygame.init()
        self.screen = pygame.display.set_mode((500, 500))
        self.btn_select_background = Button(pos=(150, 275),
                                        image_names=('button_on.png', 'button_hover.png', 'button_click.png'),
                                        path=BUTTON_IMAGE_PATH,
                                        function=self.on_btn_select_background, text=' Background ', w=200)
        self.btn_back_to_menu = Button(pos=(150, 330), image_names=('button_on.png', 'button_hover.png',
                                                                    'button_click.png'),
                                       path=BUTTON_IMAGE_PATH,
                                       function=self.on_btn_back_to_menu, text='Back to menu', w=200)

        self.scrollbar = ScrollBar(150, 150, min_num=50, max_num=5000, text='Number snow') # изменение кол-ва снежинок
        self.scrollbar_wind = ScrollBar(150, 220, min_num=5, max_num=500, text='Wind power') # изменение силы ветра
        self.work = True
        self.num_snows_scroll = None
        self.background = background
        self.background_image = pygame.transform.scale(load_image(self.background, 0, IMAGE_PATH), PLATFORM)
        self.rect = pygame.Rect(pos, size)

    def on_btn_back_to_menu(self):
        from Classes.Menu import Menu

        self.work = False
        num_snows_scroll = self.scrollbar.get_num()  # кол-во снежинок = значение снежинок на скроллинге
        wind_power = self.scrollbar_wind.get_num()
        print("from sc = ", num_snows_scroll)
        win_snow = Menu(num_snows_scroll, wind_power=wind_power)
        win_snow.run()

    def on_btn_select_background(self):
        from Classes.Background import SelectBackground

        win_snow = SelectBackground(background=self.background)
        win_snow.run()
        print('Background')

    def draw(self):
        pygame.draw.rect(self.screen, (10, 100, 100), Rect((145, 127), (212, 140)))

    def render(self):
        self.screen.blit(self.screen, self.rect)
        self.btn_back_to_menu.render(self.screen)
        self.btn_select_background.render(self.screen)
        self.scrollbar.render(self.screen)
        self.scrollbar_wind.render(self.screen)

    def event(self, event):
        self.btn_back_to_menu.event(event)
        self.btn_select_background.event(event)
        self.scrollbar.event(event)
        self.scrollbar_wind.event(event)

    def run(self):
        while self.work:
            self.screen.blit(self.background_image, (0, 0))
            self.draw()

            for event in pygame.event.get():
                self.event(event)
                if event.type == pygame.QUIT:
                    sys.exit()

                    # self.update(0)
            self.render()

            pygame.display.flip()