Exemplo n.º 1
0
class GameWindow(pyglet.window.Window):
    def __init__(self, bgcolor, tilecolor):
        super(GameWindow, self).__init__()
        self.width = 700
        self.height = 600
        self.itemslist = []
        self.bgcolor = bgcolor
        self.active = None
        matrice, magic_list = create_shapes(self.itemslist ,self.width, self.height, tilecolor)
        self.ml = magic_list
        self.state = GameState(matrice, self)
        self.now = time.time()

    def on_draw(self):
        self.clear()
        background = shapes.Rectangle(x=0, y=0, width=self.width, height=self.height, color=self.bgcolor)
        background.draw()
        for item in self.ml: item.draw()
        for item in self.itemslist: item.draw()
        for item in self.state.actives: item.draw()
        if self.state.solving and time.time()-self.now>1:
            self.state.board.solve()
    
    def on_mouse_motion(self, x, y, dx, dy):
        highest_y = 0
        highest_x = 0
        possible = []
        correct = None
        for element in self.itemslist:
            if y>element.y and element.y >= highest_y:
                if element.y>highest_y:
                    possible.clear()
                possible.append(element)
                highest_y = element.y
        
        for element in possible:
            if x>element.x and element.x > highest_x:
                self.correct = element
                highest_x = element.x

    def on_key_press(self, symbol, modifiers):
        coords = tuple(self.state.convert(self.correct))
        if symbol == 65293:
            self.state.actives = []
            self.state.solve()
            return
        if 0 < symbol-48 < 10:
            self.state.add(coords, symbol-48)
            label = pyglet.text.Label("{0}".format(symbol-48),
                            font_name='Times New Roman',
                            font_size=36,
                            x=self.correct.x+self.correct.width/4, y=self.correct.y+self.correct.height/5,
                            )
            self.state.actives.append(label)
Exemplo n.º 2
0
 def __init__(self, bgcolor, tilecolor):
     super(GameWindow, self).__init__()
     self.width = 700
     self.height = 600
     self.itemslist = []
     self.bgcolor = bgcolor
     self.active = None
     matrice, magic_list = create_shapes(self.itemslist ,self.width, self.height, tilecolor)
     self.ml = magic_list
     self.state = GameState(matrice, self)
     self.now = time.time()
Exemplo n.º 3
0
    def __init__(self, request_handler_class):
        self.PORT = 10000
        self.BUFFERSIZE = 4096
        self.clients = set()
        super().__init__(('localhost', self.PORT), request_handler_class)

        # Player connections:
        self.player_count = 0
        self.players = {}

        # Game State:
        self.GS = GameState(MAX_PLAYERS)
Exemplo n.º 4
0
class Server(socketserver.ThreadingTCPServer):
    """ Handle TCP connections and all Player Events to update and broadcast the Game state """
    def __init__(self, request_handler_class):
        self.PORT = 10000
        self.BUFFERSIZE = 4096
        self.clients = set()
        super().__init__(('localhost', self.PORT), request_handler_class)

        # Player connections:
        self.player_count = 0
        self.players = {}

        # Game State:
        self.GS = GameState(MAX_PLAYERS)

    def add_client(self, client):
        self.clients.add(client)

    def broadcast_game_state_update(self):
        """ Send the GS game state to all the connected clients."""

        pprint.pprint(self.GS.__dict__)

        for client in tuple(self.clients):
            client.send_game_state(self.GS.to_bytes(self.players))

    def remove_client(self, client):
        self.clients.remove(client)

    def start_game(self):
        print('Starting game...')
        self.GS.started = True
        self.broadcast_game_state_update()

    def update_game_state(self, event):
        self.GS.update(event=event)
Exemplo n.º 5
0
    def _handle_events(self, gamestate: game_logic.GameState) -> None:
        'handles all user input and converting into related functions'

        if gamestate.running == False:
            if _DEBUG_MODE:
                print('#####################GAME OVER SCREEN')
            self.gameover_screen(gamestate)
        elif gamestate.if_match_exist():
            gamestate.handle_match()
        elif not gamestate.check_faller_exist():
            gamestate.auto_gen_faller()
        elif gamestate.touchdown_check() == True:
            gamestate.check_faller_match()
        else:
            if self.cycle == 20:
                gamestate.move_faller_down_by_1()
                self.cycle = 0
            else:
                self.cycle += 1
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self._end_game()
            elif event.type == pygame.VIDEORESIZE:
                self._resize_surface(event.size,gamestate)
            
            elif gamestate.touchdown_check() == False \
                 and gamestate.check_faller_exist\
                 and event.type == pygame.KEYDOWN:

                keys = pygame.key.get_pressed()

                if keys[pygame.K_DOWN]:
                    gamestate.move_faller_down_by_1()
                elif keys[pygame.K_RIGHT]:
                    gamestate.move_faller_right()
                elif keys[pygame.K_LEFT]:
                    gamestate.move_faller_left()
                elif keys[pygame.K_SPACE]:
                    gamestate.rotate_faller()
                elif keys[pygame.K_RETURN]:
                    gamestate.running = False
Exemplo n.º 6
0
def run_turns(g: game_logic.GameState):
    """Runs the player's turn and ends if there are no more valid moves"""
    valid_moves = g.list_of_valid_moves()
    if len(valid_moves) == 0:
        g.count_discs_in_cell()
        print("B:", g.num_of_black_discs, " W:", g.num_of_white_discs)
        g.new_board(g.row, g.column)
        g.new_turn()
        valid_moves = g.list_of_valid_moves()
        if len(valid_moves) == 0:
            g.new_board(g.row, g.column)
            print_board(g)
            g.declare_winner()
            return
    else:
        g.count_discs_in_cell()
        print("B:", g.num_of_black_discs, " W:", g.num_of_white_discs)

    print_board(g)
    print("TURN:", g.turn)
    g.make_a_move()
    print("VALID")
    g.new_board(g.row, g.column)
    g.new_turn()