예제 #1
0
class Game:
    def __init__(self, ws):
        pygame.init()
        self.screen = pygame.display.set_mode((RESX, RESY), 0, 32)
        pygame.display.set_caption("BlackJack v0.1.0a")
        self.run = True
        self.deck_image = load_image(path=os.path.join("images", "cards"), name="back.png")
        self.deck_pos = (600, 50)
        self.ws = ws
        # self.ws.connect()

        # Кнопки gui:
        self.app = app = gui.App()
        self.rect = pygame.Rect((300, 550, 175, 25))
        connect_button = gui.Button("Connect")
        connect_button.connect(gui.CLICK, self.but_connect)
        table = gui.Table()
        table.td(connect_button)
        app.init(widget=table, screen=self.screen, area=self.rect)

        # login form
        self.login_form = LoginForm((200, 200), self.screen, self.ws)


        # Колода
        self.deck = Deck()

        # Диллер
        self.dealer = Dealer((250, 110), self.deck)

        # Список игроков

        # self.players = [Player((25, 400), self.deck), Player((275, 400), self.deck), Player((525, 400), self.deck)]
        self.players_position = ((25, 400), (275, 400), (525, 400))
        self.players = []
        self.add_player(1)
        self.add_player(2)
        self.add_player(3)
        self.server = Server()
        # TEMP
        # for player in self.players:
        #     player.add_cards()
        #     player.add_cards()
        self.dealer.add_cards()
        self.dealer.add_cards()

    def but_connect(self):
        print("Connect")
        self.ws.connect()

    def add_player(self, id):
        player = Player((self.players_position[id-1]), self.deck)
        self.players.append(player)

    def render(self, screen):
        screen.blit(self.deck_image, self.deck_pos)
        self.app.paint()
        self.login_form.render(screen)

    def event(self, event):
        self.app.event(event)
        self.login_form.event(event)

    def mainloop(self):
        while self.run:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or event.type == pygame.QUIT:
                    sys.exit()
                self.event(event)
                # window.event(event)
            # dt = clock.tick(FPS)
            self.screen.fill((0, 100, 0))
            self.render(self.screen)
            self.dealer.render(self.screen)
            for player in self.players:
                player.render(self.screen)
            pygame.display.flip()
예제 #2
0
파일: Game.py 프로젝트: LightChris/BJ_new
class Game:
    def __init__(self, server):
        pygame.init()
        self.screen = pygame.display.set_mode((RESX, RESY), 0, 32)
        pygame.display.set_caption("BlackJack beta")
        self.myFont = pygame.font.SysFont("None", 16, bold=False, italic=False)
        self.run = True
        self.deck_image = load_image(path=os.path.join("images", "cards"), name="back.png")
        self.deck_pos = (600, 50)
        self.server = server

        # buttons form
        self.buttons_form = ButtonsForm((300, 550), self.screen, self.server)

        # connect form
        self.connect_form = ConnectForm((0, 0), self.screen, self.server)

        # login form
        self.login_form = LoginForm((0, 0), self.screen, self.server)

        # Игроки, Диллер
        self.players_position = [(275, 400), (25, 400), (525, 400)]
        self.dealer_position = (250, 75)
        self.current_player = None
        self.dealer = Dealer(self.dealer_position)
        self.other_players = []
        self.server = Server()
        self.current_id = None

    def add_player(self, pos, id):
        player = Player(pos, id)
        self.other_players.append(player)
        return player

    def render(self, screen):
        self.connect_form.render(screen)
        self.buttons_form.render(screen)
        self.login_form.render(screen)
        self.dealer.render(screen)
        if self.current_player:
            self.current_player.render(screen)
            # current_player_font = self.myFont.render(str(self.current_player.name) + '  ' +
            #                                          str(self.current_player.points) + '/21', 0, (0, 0, 0))
            # screen.blit(current_player_font, (290, 380))
        for player in self.other_players:
            player.render(screen)
            # player_font = self.myFont.render(str(player.name) + '  ' + str(player.points) + '/21', 0, (0, 0, 0))
            # screen.blit(player_font, (player.pos[0] + 15, player.pos[1] - 20))
        screen.blit(self.deck_image, self.deck_pos)

    def event(self, event):
        self.buttons_form.event(event)
        self.connect_form.event(event)
        self.login_form.event(event)
        if event.type == WS_YOU_ID:
            self.current_id = event.id
            self.current_player = Player(self.players_position.pop(0), self.current_id)
            # self.current_player.name = event.name
        if event.type == WS_MESSAGE:
            if event.data.get('type') == 'hit' and event.data.get('id') == self.current_id:
                card = event.data.get('card')
                print('Вы получили карту:', card)
                self.current_player.add_cards(card[0], card[1])
            if event.data.get('type') == 'other_players':
                player = self.add_player(self.players_position.pop(0), event.data.get('id'))
                for card in event.data.get('hand'):
                    player.add_cards(card[1], card[0])
            if event.data.get('type') == 'new_client':
                self.add_player(self.players_position.pop(0), event.data.get('message'))
            if event.data.get('type') == 'hit' and event.data.get('id') == 'Dealer':
                card = event.data.get('card')
                self.dealer.add_cards(card[0], card[1])
            if event.data.get('id') != self.current_id:
                for player in self.other_players:
                    if player.id == event.data.get('id'):
                        player.points = str(event.data.get('points'))
            if self.current_player:
                if event.data.get('id') == self.current_id:
                    self.current_player.points = str(event.data.get('points'))
                    print("У вас " + self.current_player.points + " очков")
            if event.data.get('type') == 'bust':
                self.buttons_form.visible = False
                print("У вас перебор, ход закончен.")
            if event.data.get('type') == 'winner' and event.data.get('id') == self.current_id:
                print('Вы выиграли.')
            if event.data.get('type') == 'winner' and event.data.get('id') != self.current_id:
                print('Вы проиграли.')
            if event.data.get('type') == 'restart':
                self.current_player.restart()
                self.dealer.restart()
                for player in self.other_players:
                    player.restart()
                self.buttons_form.visible = True
            # if self.current_player:
            #     if event.data.get('id') == self.current_id:
            #         name = event.data.get('username')
            #         self.current_player.name = name
            if event.data.get('id') != self.current_id:
                for player in self.other_players:
                    if player.id == event.data.get('id'):
                        name = event.data.get('username')
                        player.name = name
            for player in self.other_players:
                if event.data.get('type') == 'hit' and player.id == event.data.get('id'):
                    card = event.data.get('card')
                    player.add_cards(card[0], card[1])

    def mainloop(self):
        while self.run:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or event.type == pygame.QUIT:
                    sys.exit()
                self.event(event)
            self.screen.fill((0, 100, 0))
            self.render(self.screen)
            pygame.display.flip()