class Game: BG = (255, 255, 255) def __init__(self): self.WIDTH = 1300 self.HEIGHT = 900 self.win = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) self.leaderboard = Leaderboard(100, 120) self.board = Board(310, 120) self.top_bar = TopBar(10, 10, 1280, 100) self.top_bar.change_round(1) def draw(self): self.win.fill(self.BG) self.leaderboard.draw(self.win) self.top_bar.draw(self.win) self.board.draw(self.win) pygame.display.update() def run(self): run = True clock = pygame.time.Clock() while run: clock.tick(30) self.draw() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False break pygame.quit()
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()
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 Game: def __init__(self, win, connection): """ :param win: Window :param connection: Connection object """ self.win = win self.connection = connection self.is_drawing = False self.players = [] self.draw_color = COLORS[1] self.game_ended = False self.drawer = "No one" self.top_bar = TopBar(20, 20, 1040, 80, self) self.leaderboard = Leaderboard(20, 120, 270, 60) self.board = Board(310, 120, 6) self.chat = Chat(810, 120, 250, 580, 30) self.bottom_bar = BottomBar(20, 620, 770, 80, self) self.person_is_drawing = PersonIsDrawing(310, 620, 480, 80) def draw(self): """ Draws Game on window :return: None """ self.win.fill(COLORS[0]) self.top_bar.draw(self.win) self.leaderboard.draw(self.win) self.board.draw(self.win) self.chat.draw(self.win) if self.is_drawing: self.bottom_bar.draw(self.win) else: self.person_is_drawing.draw(self.win, self.drawer) pygame.display.update() def handle_click(self): """ Handles clicks on buttons on screen :return: None """ mouse = pygame.mouse.get_pos() clicked_board = self.board.click(*mouse) if clicked_board and self.is_drawing: self.connection.send({8: [*clicked_board, COLORS.index(self.draw_color)]}) if self.is_drawing: self.bottom_bar.handle_click(mouse) def get_data(self): """ Gets and sets data from server :return: None """ self.leaderboard.players = self.connection.send({-1: []}) self.chat.content = self.connection.send({2: []}) self.board.compressed_board = self.connection.send({3: []}) self.top_bar.change_round(self.connection.send({5: []})) self.top_bar.word = self.connection.send({6: []}) self.top_bar.time = self.connection.send({9: []}) self.is_drawing = self.connection.send({10: []}) self.drawer = self.connection.send({13: []}) def run(self): """ Runs the game by getting data from the server :return: None """ run = True clock = pygame.time.Clock() while True: try: clock.tick(120) self.get_data() self.board.translate_board() self.draw() for event in pygame.event.get(): if event.type == pygame.QUIT: raise Exception("Player Quit") if pygame.mouse.get_pressed()[0]: self.handle_click() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN and len(self.chat.typing): self.connection.send({0: [self.chat.typing]}) self.chat.update_chat() else: key_name = pygame.key.name(event.key).upper() self.chat.type(key_name, self.is_drawing) except Exception as e: print(f"Closed because {e}") break pygame.quit()
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()
class Game(): bg_clr = (0, 0, 0) 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): pygame.font.init() self.WIDTH = 1150 self.HEIGHT = 800 self.win = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) self.background = pygame.Surface((self.WIDTH, self.HEIGHT)) self.manager = pygame_gui.UIManager((self.WIDTH, self.HEIGHT)) self.lvl = 1 self.player = Player("Joseph") self.enemy = Enemy() self.stats = Stats() # self.mod = Enemy_mod() TODO self.top_bar = TopBar(400, 0) self.left_bar = LeftBar(0, 0, self.player) self.bottom_bar = BottomBar(400, 650, self.player) self.board = Board(400, 50) self.max_click_ps = 10 def next_lvl(self): self.player.add_money(self.enemy.get_money()) if self.board: del self.board if self.enemy: del self.enemy self.lvl += 1 self.board = Board(400, 50) self.enemy = Enemy(self.lvl, self.lvl) pygame.display.update() def previous_lvl(self): self.player.add_money(self.enemy.get_money()) if self.enemy: del self.enemy if self.lvl <= 1: self.lvl = 1 else: self.lvl -= 1 self.enemy = Enemy(self.lvl, self.lvl) pygame.display.update() def draw_lvl(self): self.win.fill(self.bg_clr) self.board.draw(self.win, self.lvl, self.enemy.get_name(), self.enemy.get_img()) self.top_bar.draw(self.win, self.enemy.get_hp(), self.enemy.get_max_hp(), self.enemy.get_ttk(), self.enemy.get_max_ttk()) self.left_bar.draw(self.win) self.bottom_bar.draw(self.win) pygame.display.update() def check_clicks(self): mouse = pygame.mouse.get_pos() clicked_board = self.board.click(*mouse) if clicked_board: self.enemy.deal_dmg_click(self.player.get_click()) return True clicked_left_bar = self.left_bar.click(*mouse) if clicked_left_bar: self.left_bar.buy_hero(mouse) return True def run(self): self.clock = pygame.time.Clock() clock_loop = 0 click_clock = 0 is_running = True while is_running: fps = self.clock.tick(30) button_pressed = False if self.enemy.get_hp() <= 0: self.next_lvl() if self.enemy.get_ttk() <= 0: self.previous_lvl() self.draw_lvl() if clock_loop > 30: # timer self.enemy.decrese_ttk() self.enemy.deal_dmg_dps(self.player.get_dps()) clock_loop = 0 else: clock_loop += 1 if click_clock > 1: if pygame.mouse.get_pressed()[0]: self.check_clicks() click_clock = 0 else: click_clock += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: is_running = False