class BottomBar:
    COLORS = {
        0: (255, 255, 255),  # white
        1: (0, 0, 0),  # black
        2: (255, 0, 0),  # red
        3: (0, 255, 0),  # green
        4: (0, 0, 255),  # blue
        5: (255, 255, 0),  # yello
        6: (255, 140, 0),  # orange
        7: (165, 42, 42),  # brown
        8: (128, 0, 128),  # purple
    }

    def __init__(self, x, y, game):
        self.x = x
        self.y = y
        self.WIDTH = 720
        self.HEIGHT = 100
        self.BORDER_THICKNESS = 5
        self.game = game
        self.clear_button = TextButton(
            self.x + self.WIDTH - 150, self.y + 25, 100, 50, (128, 128, 128), "Clear"
        )
        self.erase_button = TextButton(
            self.x + self.WIDTH - 300, self.y + 25, 100, 50, (128, 128, 128), "Eraser"
        )
        self.color_buttons = [
            Button(self.x + 20, self.y + 5, 30, 30, self.COLORS[0]),
            Button(self.x + 50, self.y + 5, 30, 30, self.COLORS[1]),
            Button(self.x + 80, self.y + 5, 30, 30, self.COLORS[2]),
            Button(self.x + 20, self.y + 35, 30, 30, self.COLORS[3]),
            Button(self.x + 50, self.y + 35, 30, 30, self.COLORS[4]),
            Button(self.x + 80, self.y + 35, 30, 30, self.COLORS[5]),
            Button(self.x + 20, self.y + 65, 30, 30, self.COLORS[6]),
            Button(self.x + 50, self.y + 65, 30, 30, self.COLORS[7]),
            Button(self.x + 80, self.y + 65, 30, 30, self.COLORS[8]),
        ]

    def draw(self, win):
        pygame.draw.rect(win, (0, 0, 0), (self.x, self.y, self.WIDTH, self.HEIGHT), self.BORDER_THICKNESS)
        self.clear_button.draw(win)
        self.erase_button.draw(win)
        for color_button in self.color_buttons:
            color_button.draw(win)

    def button_events(self, x: float, y: float):
        """handles all button press events here"""
        if self.clear_button.click(x, y):
            print("Clear button pressed")
            self.game.board.clear()
            self.game.conn.send({10: []})

        if self.erase_button.click(x, y):
            print("Erase button pressed")
            self.game.draw_color = (255, 255, 255)

        for i, button in enumerate(self.color_buttons):
            if button.click(x, y):
                self.game.draw_color = self.COLORS[i]
Exemple #2
0
class BottomBar:
    COLORS = {
        0: (255, 255, 255),
        1: (0, 0, 0),
        2: (255, 0, 0),
        3: (0, 255, 0),
        4: (0, 0, 255),
        5: (255, 255, 0),
        6: (255, 165, 0),
        7: (165, 42, 42),
        8: (128, 0, 128)
    }

    def __init__(self, x, y, game):
        self.x = x
        self.y = y
        self.WIDTH = 608
        self.HEIGHT = 80
        self.BORDER_THICKNESS = 3
        self.game = game
        self.clear_button = TextButton(self.x + self.WIDTH - 150, self.y + 15, 100, 50, (128, 128, 128), "Clear")
        self.eraser_button = TextButton(self.x + self.WIDTH - 300, self.y + 15, 100, 50, (128, 128, 128), "Eraser")
        self.color_buttons = [Button(self.x + 20, self.y + 5, 20, 20, self.COLORS[0]),
                              Button(self.x + 40, self.y + 5, 20, 20, self.COLORS[1]),
                              Button(self.x + 60, self.y + 5, 20, 20, self.COLORS[2]),
                              Button(self.x + 20, self.y + 25, 20, 20, self.COLORS[3]),
                              Button(self.x + 40, self.y + 25, 20, 20, self.COLORS[4]),
                              Button(self.x + 60, self.y + 25, 20, 20, self.COLORS[5]),
                              Button(self.x + 20, self.y + 45, 20, 20, self.COLORS[6]),
                              Button(self.x + 40, self.y + 45, 20, 20, self.COLORS[7]),
                              Button(self.x + 60, self.y + 45, 20, 20, self.COLORS[8])]

    def draw(self, win):
        pygame.draw.rect(win, (0, 0, 0), (self.x, self.y, self.WIDTH, self.HEIGHT), self.BORDER_THICKNESS)
        self.clear_button.draw(win)
        self.eraser_button.draw(win)

        for btn in self.color_buttons:
            btn.draw(win)

    def button_events(self):
        mouse = pygame.mouse.get_pos()

        if self.clear_button.click(*mouse):
            self.game.board.clear()
            self.game.connection.send({10: []})

        if self.eraser_button.click(*mouse):
            self.game.draw_color = (255, 255, 255)

        for btn in self.color_buttons:
            if btn.click(*mouse):
                self.game.draw_color = btn.color
class BottomBar:
    def __init__(self, x, y, width, height, game):
        """
        :param x: int
        :param y: int
        :param width: int
        :param height: int
        :param game: Game object
        """
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.game = game

        self.clear_button = TextButton(self.x + self.width - 125, self.y+self.height/2-25, 100, 50, "Clear")
        self.eraser_button = TextButton(self.x + self.width - 250, self.y+self.height/2-25, 100, 50, "Erase")
        self.color_buttons = [Button(self.x+25+50*i, self.y+self.height/2-25, 50, 50, c) for i, c in enumerate(COLORS)]

    def draw(self, win):
        """
        Draws the bottom bar
        :param win: Window object
        :return: None
        """
        self.clear_button.draw(win)
        self.eraser_button.draw(win)

        for color_button in self.color_buttons:
            color_button.draw(win)

        pygame.draw.rect(win, (0, 0, 0), (self.x, self.y, self.width, self.height), BORDER_THICKNESS)

    def handle_click(self, mouse):
        """
        Handle button clicks
        :return: None
        """
        if self.clear_button.click(*mouse):
            self.game.connection.send({11: []})

        if self.eraser_button.click(*mouse):
            self.game.draw_color = COLORS[0]

        for i, color_button in enumerate(self.color_buttons):
            if color_button.click(*mouse):
                self.game.draw_color = COLORS[i]
Exemple #4
0
class Game:
    BG = (255, 255, 255)
    COLORS = {
        (255, 255, 255): 0,
        (0, 0, 0): 1,
        (255, 0, 0): 2,
        (0, 255, 0): 3,
        (0, 0, 255): 4,
        (255, 255, 0): 5,
        (255, 140, 0): 6,
        (165, 42, 42): 7,
        (128, 0, 128): 8
    }

    def __init__(self, win, connection=None):
        pygame.font.init()
        self.connection = connection
        self.win = win
        self.leaderboard = Leaderboard(50, 125)
        self.board = Board(305, 125)
        self.top_bar = TopBar(10, 10, 1280, 100)
        self.top_bar.change_round(1)
        self.players = []
        self.skip_button = TextButton(85, 830, 125, 60, (255, 255, 0), "Skip")
        self.bottom_bar = BottomBar(305, 880, self)
        self.chat = Chat(1050, 125)
        self.draw_color = (0, 0, 0)
        self.drawing = False

    def add_player(self, player):
        self.players.append(player)
        self.leaderboard.add_player(player)

    def draw(self):
        self.win.fill(self.BG)
        self.leaderboard.draw(self.win)
        self.top_bar.draw(self.win)
        self.board.draw(self.win)
        self.skip_button.draw(self.win)
        if self.drawing:
            self.bottom_bar.draw(self.win)
        self.chat.draw(self.win)
        pygame.display.update()

    def check_clicks(self):
        """
        handles clicks on buttons and screen
        :return: None
        """
        mouse = pygame.mouse.get_pos()

        # Check click on skip button
        if self.skip_button.click(*mouse) and not self.drawing:
            skips = self.connection.send({1: []})

        clicked_board = self.board.click(*mouse)

        if clicked_board:
            self.board.update(*clicked_board, self.draw_color)
            self.connection.send(
                {8: [*clicked_board, self.COLORS[tuple(self.draw_color)]]})

    def run(self):
        run = True
        clock = pygame.time.Clock()
        while run:
            clock.tick(60)

            try:
                # get board
                response = self.connection.send({3: []})
                if response:
                    self.board.compressed_board = response
                    self.board.translate_board()

                # get time
                response = self.connection.send({9: []})
                self.top_bar.time = response

                # get chat
                response = self.connection.send({2: []})
                self.chat.update_chat(response)

                # get round info
                self.top_bar.word = self.connection.send({6: []})
                self.top_bar.round = self.connection.send({5: []})
                self.drawing = self.connection.send({11: []})
                self.top_bar.drawing = self.drawing
                self.top_bar.max_round = len(self.players)

                # get player updates
                '''response = self.connection.send({0:[]})
                self.players = []
                for player in response:
                    p = Player(player)
                    self.add_player(p)'''
            except:
                run = False
                break

            self.draw()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                    break

                if pygame.mouse.get_pressed()[0]:
                    self.check_clicks()
                    self.bottom_bar.button_events()

                if event.type == pygame.KEYDOWN:
                    if not self.drawing:
                        if event.key == pygame.K_RETURN:
                            self.connection.send({0: [self.chat.typing]})
                            self.chat.typing = ""
                        else:
                            # gets the key name
                            key_name = pygame.key.name(event.key)

                            # converts to uppercase the key name
                            key_name = key_name.lower()
                            self.chat.type(key_name)

        pygame.quit()
Exemple #5
0
class Game:
    BG = (255, 255, 255)

    def __init__(self):
        self.WIDTH = 1300
        self.HEIGHT = 1000
        self.win = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
        self.leaderboard = Leaderboard(50, 125)
        self.board = Board(305, 125)
        self.top_bar = TopBar(10, 10, 1280, 100)
        self.top_bar.change_round(1)
        self.players = [
            Player("Tim"),
            Player("Joe"),
            Player("Bill"),
            Player("Jeff"),
            Player("TBob")
        ]
        self.skip_button = TextButton(85, 830, 125, 60, (255, 255, 0), "Skip")
        self.bottom_bar = BottomBar(305, 880, self)
        self.chat = Chat(1050, 125)
        self.draw_color = (0, 0, 0)
        for player in self.players:
            self.leaderboard.add_player(player)

    def draw(self):
        self.win.fill(self.BG)
        self.leaderboard.draw(self.win)
        self.top_bar.draw(self.win)
        self.board.draw(self.win)
        self.skip_button.draw(self.win)
        self.bottom_bar.draw(self.win)
        self.chat.draw(self.win)
        pygame.display.update()

    def check_clicks(self):
        """
        handles clicks on buttons and screen
        :return: None
        """
        mouse = pygame.mouse.get_pos()

        # Check click on skip button
        if self.skip_button.click(*mouse):
            print("Clicked skip button")

        clicked_board = self.board.click(*mouse)

        if clicked_board:
            self.board.update(*clicked_board, self.draw_color)

    def run(self):
        run = True
        clock = pygame.time.Clock()
        while run:
            clock.tick(60)
            self.draw()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                    break

                if pygame.mouse.get_pressed()[0]:
                    self.check_clicks()
                    self.bottom_bar.button_events()

                if event.type == pygame.KEYDOWN:
                    # gets the key name
                    key_name = pygame.key.name(event.key)

                    # converts to uppercase the key name
                    key_name = key_name.lower()
                    self.chat.type(key_name)

        pygame.quit()
class SortingVisualizer:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((600, 560))
        pygame.display.set_caption("sortingVisualizer")
        self.clock = pygame.time.Clock()
        self.bars = [Bar() for i in range(48)]

        self.bubbleSortButton = TextButton(self.screen, 'BubbleSort', 12, 520)
        self.insertionSortButton = TextButton(self.screen, 'InsertionSort',
                                              195, 520)
        self.startButton = ImageButton(self.screen, 'assets/start.png', 495,
                                       514)
        self.resetButton = ImageButton(self.screen, 'assets/reset.png', 540,
                                       514)

        self.started = False
        self.algorithm = self.bubbleSort
        self.bubbleSortButton.text_color = WHITE

        while True:
            self.check_events()
            self.update_screen()

    def update_screen(self):
        self.screen.fill((100, 200, 0))
        self.bubbleSortButton.draw()
        self.insertionSortButton.draw()
        self.startButton.draw()
        self.resetButton.draw()

        for i, bar in enumerate(self.bars):
            bar.draw(self.screen, i)
        pygame.display.update()

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

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if not self.started:
                        self.start()

            elif pygame.mouse.get_pressed()[0]:
                pos = pygame.mouse.get_pos()

                if pos[1] > 520 and pos[1] < 565:
                    if pos[0] > 12 and pos[0] < 170:
                        self.algorithm = self.bubbleSort
                        self.bubbleSortButton.text_color = WHITE
                        self.insertionSortButton.text_color = BLACK

                    elif pos[0] > 195 and pos[0] < 385:
                        self.algorithm = self.insertionSort
                        self.insertionSortButton.text_color = WHITE
                        self.bubbleSortButton.text_color = BLACK

                    elif pos[0] > self.resetButton.x and pos[
                            0] < self.resetButton.x + 45 and not self.started:
                        self.reset()

                    elif pos[0] > self.startButton.x and pos[
                            0] < self.startButton.x + 45 and not self.started:
                        self.start()

    def reset(self):
        self.bars = [Bar() for i in range(48)]

    def start(self):
        self.started = True
        self.algorithm()
        self.started = False

    def bubbleSort(self):
        for j in range(len(self.bars)):
            swapped = False
            i = 0
            while i < len(self.bars) - 1:
                if self.bars[i].val > self.bars[i + 1].val:
                    self.bars[i].color = (0, 0, 0)
                    self.bars[i], self.bars[i + 1] = self.bars[i +
                                                               1], self.bars[i]
                    swapped = True
                    self.update_screen()
                    self.check_events()
                    self.clock.tick(40)
                    self.bars[i + 1].color = (200, 200, 200)
                i = i + 1
            if swapped == False:
                break

    def insertionSort(self):
        for i in range(1, len(self.bars)):
            j = i
            while j > 0 and self.bars[j].val < self.bars[j - 1].val:
                self.bars[j].color = (0, 0, 0)
                self.bars[j], self.bars[j - 1] = self.bars[j - 1], self.bars[j]
                j -= 1
                self.update_screen()
                self.check_events()
                self.clock.tick(40)
                self.bars[j].color = (200, 200, 200)
class BottomBar:
    COLORS = {
        0: (255, 255, 255),
        1: (0, 0, 0),
        2: (255, 0, 0),
        3: (0, 255, 0),
        4: (0, 0, 255),
        5: (255, 255, 0),
        6: (255, 140, 0),
        7: (165, 42, 42),
        8: (128, 0, 128)
    }

    def __init__(self, x, y, game):
        self.x = x
        self.y = y
        self.WIDTH = 720
        self.HEIGHT = 100
        self.BORDER_THICKNESS = 5
        self.game = game
        self.clear_button = TextButton(self.x + self.WIDTH - 150, self.y + 25,
                                       100, 50, (128, 128, 128), "Clear")
        self.eraser_button = TextButton(self.x + self.WIDTH - 300, self.y + 25,
                                        100, 50, (128, 128, 128), "Eraser")
        self.color_buttons = [
            Button(self.x + 20, self.y + 5, 30, 30, self.COLORS[0]),
            Button(self.x + 50, self.y + 5, 30, 30, self.COLORS[1]),
            Button(self.x + 80, self.y + 5, 30, 30, self.COLORS[2]),
            Button(self.x + 20, self.y + 35, 30, 30, self.COLORS[3]),
            Button(self.x + 50, self.y + 35, 30, 30, self.COLORS[4]),
            Button(self.x + 80, self.y + 35, 30, 30, self.COLORS[5]),
            Button(self.x + 20, self.y + 65, 30, 30, self.COLORS[6]),
            Button(self.x + 50, self.y + 65, 30, 30, self.COLORS[7]),
            Button(self.x + 80, self.y + 65, 30, 30, self.COLORS[8])
        ]

    def draw(self, win):
        pygame.draw.rect(win, (0, 0, 0),
                         (self.x, self.y, self.WIDTH, self.HEIGHT),
                         self.BORDER_THICKNESS)
        self.clear_button.draw(win)
        self.eraser_button.draw(win)

        for btn in self.color_buttons:
            btn.draw(win)

    def button_events(self):
        """
        handle all button press events here
        :return: None
        """
        mouse = pygame.mouse.get_pos()

        if self.clear_button.click(*mouse):
            self.game.board.clear()

        if self.eraser_button.click(*mouse):
            self.game.draw_color = (255, 255, 255)

        for btn in self.color_buttons:
            if btn.click(*mouse):
                self.game.draw_color = btn.color
Exemple #8
0
class Game(object):
    BG = (255, 255, 255)
    COLORS = {
        (255,255,255): 0,
        (0,0,0): 1,
        (255,0,0): 2,
        (0,255,0): 3,
        (0,0,255): 4,
        (255,255,0): 5,
        (255,140,0): 6,
        (165,42,42): 7,
        (128,0,128): 8
    }


    def __init__(self, win, conn: Network=None):
        pygame.font.init()
        self.win = win
        self.conn = conn
        self.leader_board = LeaderBoard(50, 125)
        self.board = Board(310, 125)
        self.top_bar = TopBar(10, 10, 1280, 100)
        self.top_bar.change_round(1)
        self.players = []
        self.skip_button = TextButton(90, 800, 125, 50, (255, 255, 0), "Skip")
        self.bottom_bar = BottomBar(310, 880, self)
        self.chat = Chat(1050, 120)
        self.draw_color = (0, 0, 0)

    def add_player(self, player):
        self.players.append(player)
        self.leader_board.add_player(player)

    def draw(self):
        self.win.fill(self.BG)
        self.leader_board.draw(self.win)
        self.board.draw(self.win)
        self.top_bar.draw(self.win)
        self.bottom_bar.draw(self.win)
        self.skip_button.draw(self.win)
        self.chat.draw(self.win)
        pygame.display.update()

    def check_clicks(self):
        """handles clicks on buttons and screen"""
        mouse = pygame.mouse.get_pos()

        if self.skip_button.click(*mouse):
            print("Skip button clicked")
            skips = self.conn.send({1: []})
            print(skips)

        clicked_board = self.board.click(*mouse)
        if clicked_board:
            row, col = clicked_board
            self.conn.send({8: [row, col, self.COLORS[self.draw_color]]})
            self.board.update(row, col, self.draw_color)

        self.bottom_bar.button_events(*mouse)

    def run(self):
        run = True
        clock = pygame.time.Clock()
        while run:
            clock.tick(60)
            self.draw()

            try:
                # get board
                response = self.conn.send({3: []})
                if response:
                    self.board.compressed_board = response
                    self.board.translate_board()

                # get time
                response = self.conn.send({9: []})
                self.top_bar.time = response

                # get chat
                response = self.conn.send({2: []})
                self.chat.update_chat(response)

                # get round info
                self.top_bar.word = self.conn.send({6: []})
                self.top_bar.round = self.conn.send({5: []})
                self.drawing = self.conn.send({11: []})
                self.top_bar.drawing = self.drawing
                self.top_bar.max_round = len(self.players)

                # get player updates
                """response = self.conn.send({0: []})
                self.players = []
                for player in response:
                    p = Player(player)
                    self.add_player(p)"""

            except Exception:
                run = False

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                    break
                    
                if pygame.mouse.get_pressed()[0]:
                    self.check_clicks()

                if event.type == pygame.KEYDOWN:
                    if not self.drawing:
                        if event.key == pygame.K_RETURN:
                            self.conn.send({0: [self.chat.typing]})
                            self.chat.typing = ""
                        else:
                        key_name = pygame.key.name(event.key)

                        key_name = key_name.lower()
                        self.chat.type(key_name)

    pygame.quit()


if __name__ == "__main__":
    g = Game()
    g.run()