Exemple #1
0
 def reset(self):
     """
     Fill the screen with black pixels
     """
     surface = Surface(self.width, self.height)
     surface.fill(BLACK)
     self.matrix = surface.matrix
Exemple #2
0
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port, self.width, self.height)
        self.clock = Clock(15)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]

        # surfaces
        self.corners = Surface(self.screen.width, self.screen.height)
        self.lines = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
        self.rects = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
        self.circle = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
        self.filled = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
Exemple #3
0
 def reset(self):
     """
     Fill the screen with black pixels
     """
     surface = Surface(self.width, self.height)
     surface.fill(BLACK)
     self.matrix = surface.matrix
Exemple #4
0
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port,
                             self.width, self.height)
        self.clock = Clock(15)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]

        # surfaces
        self.corners = Surface(self.screen.width, self.screen.height)
        self.lines = Surface(int(self.screen.width / 2) - 2,
                             int(self.screen.height / 2) - 2)
        self.rects = Surface(int(self.screen.width / 2) - 2,
                             int(self.screen.height / 2) - 2)
        self.circle = Surface(int(self.screen.width / 2) - 2,
                              int(self.screen.height / 2) - 2)
        self.filled = Surface(int(self.screen.width / 2) - 2,
                              int(self.screen.height / 2) - 2)
Exemple #5
0
    def render_naked_map(self):
        """
        Renders the naked map without destroyable objects

        :returns: Surface - The surface of the naked map
        """
        s = Surface(self.width, self.height)
        for y in range(self.height):
            for x in range(self.width):
                s.draw_dot((x, y), self._nakedmap[x, y])
        return s
Exemple #6
0
    def render_pixmap(self):
        """
        Renders the current view of of the map.

        :returns: Surface - The surface of the map
        """
        s = Surface(self.width, self.height)
        for y in range(self.height):
            for x in range(self.width):
                s.draw_dot((x, y), self._pixmap[x, y])
        return s
Exemple #7
0
    def __init__(self):
        self.normal = Surface(2, 4)
        self.normal.draw_line((0, 0), (1, 0), RED)
        self.normal.draw_dot((0, 1), (127, 0, 0))
        self.normal.draw_dot((1, 1), (255, 127, 0))
        self.normal.draw_dot((0, 2), RED)
        self.normal.draw_dot((1, 2), BLUE)
        self.normal.draw_line((0, 3), (1, 3), BLUE)

        self.current = self.normal

        self.x = 2
        self.y = 8
        self.width = self.current.width
        self.height = self.current.height

        self.direction = 1  # 0: left, 1: right

        self.jumping = False
        self.moving = False
Exemple #8
0
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port, self.width, self.height)
        self.clock = Clock(5)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]
        self.color_length = math.ceil(self.screen.height / len(self.colors))

        # surfaces
        self.game_board = None
        self.init_game_board()

        self.dots = Surface(self.screen.width, self.screen.height)
Exemple #9
0
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port,
                             self.width, self.height)
        self.clock = Clock(5)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]
        self.color_length = math.ceil(self.screen.height / len(self.colors))

        # surfaces
        self.game_board = None
        self.init_game_board()

        self.dots = Surface(self.screen.width, self.screen.height)
Exemple #10
0
class Game(object):
    """
    The main game class that holds the gameloop.
    """
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port, self.width, self.height)
        self.clock = Clock(5)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]
        self.color_length = math.ceil(self.screen.height / len(self.colors))

        # surfaces
        self.game_board = None
        self.init_game_board()

        self.dots = Surface(self.screen.width, self.screen.height)

    def init_game_board(self):
        self.game_board = [
            0 for _ in range(self.screen.height * self.screen.width)
        ]
        indices = random.sample(
            range(self.screen.height * self.screen.width),
            random.randint(self.screen.width // 2,
                           (2 *
                            (self.screen.height * self.screen.width) // 3)))

        for index in indices:
            self.game_board[index] = 1

    def offset(self, width_idx, height_idx):
        return height_idx * self.screen.width + width_idx

    def board_value(self, width_idx, height_idx):
        if width_idx == -1 or height_idx == -1:
            return 0
        return self.game_board[self.offset(width_idx, height_idx)]

    def update(self):
        """
        Update the screens contents in every loop.
        """
        # this is not really neccesary because the surface is black after initializing
        self.dots.fill(BLACK)

        intermediate_buffer = self.game_board.copy()

        for h_index in range(self.screen.height):
            for w_index in range(self.screen.width):
                id_above = h_index - 1 if h_index - 1 >= 0 else -1
                id_left = w_index - 1 if w_index - 1 >= 0 else -1
                id_right = w_index + 1 if w_index + 1 < self.screen.width else -1
                id_bottom = h_index + 1 if h_index + 1 < self.screen.height else -1

                cell_offset = self.offset(w_index, h_index)

                alive_check = [
                    self.board_value(id_left, id_above),
                    self.board_value(w_index, id_above),
                    self.board_value(id_right, id_above),
                    self.board_value(id_left, h_index),
                    self.board_value(w_index, h_index),
                    self.board_value(id_right, h_index),
                    self.board_value(id_left, id_bottom),
                    self.board_value(w_index, id_bottom),
                    self.board_value(id_right, id_bottom),
                ]

                # count neighbours that are alive
                counter = Counter(alive_check)
                num_alive = counter[1]

                if self.game_board[cell_offset] == 0:
                    if num_alive == 3:
                        intermediate_buffer[cell_offset] = 1
                        self.dots.draw_dot(
                            (w_index, h_index),
                            self.colors[h_index // self.color_length])
                elif num_alive < 2:
                    intermediate_buffer[cell_offset] = 0
                elif 2 <= num_alive <= 3:
                    self.dots.draw_dot(
                        (w_index, h_index),
                        self.colors[h_index // self.color_length])
                else:
                    intermediate_buffer[cell_offset] = 0

        self.game_board = intermediate_buffer.copy()

    def render(self):
        """
        Send the current screen content to Mate Light.
        """
        self.screen.reset()
        self.screen.blit(self.dots)

        self.screen.update()
        self.clock.tick()

    def handle_events(self):
        """
        Loop through all events.
        """
        for event in pymlgame.get_events():
            if event.type == E_NEWCTLR:
                #print(datetime.now(), '### new player connected with uid', event.uid)
                self.players[event.uid] = {
                    'name': 'alien_{}'.format(event.uid),
                    'score': 0
                }
            elif event.type == E_DISCONNECT:
                #print(datetime.now(), '### player with uid {} disconnected'.format(event.uid))
                self.players.pop(event.uid)
            elif event.type == E_KEYDOWN:
                #print(datetime.now(), '###', self.players[event.uid]['name'], 'pressed', event.button)
                if event.button == 9:
                    self.init_game_board()
                else:
                    self.colors.append(self.colors.pop(0))
            elif event.type == E_PING:
                #print(datetime.now(), '### ping from', self.players[event.uid]['name'])
                pass

    def gameloop(self):
        """
        A game loop that circles through the methods.
        """
        try:
            while True:
                self.handle_events()
                self.update()
                self.render()
        except KeyboardInterrupt:
            pass
Exemple #11
0
class Game(object):
    """
    The main game class that holds the gameloop.
    """
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port,
                             self.width, self.height)
        self.clock = Clock(5)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]
        self.color_length = math.ceil(self.screen.height / len(self.colors))

        # surfaces
        self.game_board = None
        self.init_game_board()

        self.dots = Surface(self.screen.width, self.screen.height)

    def init_game_board(self):
        self.game_board = [0 for _ in range(self.screen.height * self.screen.width)]
        indices = random.sample(
            range(self.screen.height * self.screen.width),
            random.randint(self.screen.width // 2, (2 * (self.screen.height * self.screen.width) // 3))
        )

        for index in indices:
            self.game_board[index] = 1

    def offset(self, width_idx, height_idx):
        return height_idx * self.screen.width + width_idx

    def board_value(self, width_idx, height_idx):
        if width_idx == -1 or height_idx == -1:
            return 0
        return self.game_board[self.offset(width_idx, height_idx)]

    def update(self):
        """
        Update the screens contents in every loop.
        """
        # this is not really neccesary because the surface is black after initializing
        self.dots.fill(BLACK)

        intermediate_buffer = self.game_board.copy()

        for h_index in range(self.screen.height):
            for w_index in range(self.screen.width):
                id_above = h_index - 1 if h_index - 1 >= 0 else -1
                id_left = w_index - 1 if w_index - 1 >= 0 else -1
                id_right = w_index + 1 if w_index + 1 < self.screen.width else -1
                id_bottom = h_index + 1 if h_index + 1 < self.screen.height else -1

                cell_offset = self.offset(w_index, h_index)

                alive_check = [
                    self.board_value(id_left, id_above),
                    self.board_value(w_index, id_above),
                    self.board_value(id_right, id_above),
                    self.board_value(id_left, h_index),
                    self.board_value(w_index, h_index),
                    self.board_value(id_right, h_index),
                    self.board_value(id_left, id_bottom),
                    self.board_value(w_index, id_bottom),
                    self.board_value(id_right, id_bottom),
                ]

                # count neighbours that are alive
                counter = Counter(alive_check)
                num_alive = counter[1]

                if self.game_board[cell_offset] == 0:
                    if num_alive == 3:
                        intermediate_buffer[cell_offset] = 1
                        self.dots.draw_dot((w_index, h_index), self.colors[h_index // self.color_length])
                elif num_alive < 2:
                    intermediate_buffer[cell_offset] = 0
                elif 2 <= num_alive <= 3:
                    self.dots.draw_dot((w_index, h_index), self.colors[h_index // self.color_length])
                else:
                    intermediate_buffer[cell_offset] = 0

        self.game_board = intermediate_buffer.copy()

    def render(self):
        """
        Send the current screen content to Mate Light.
        """
        self.screen.reset()
        self.screen.blit(self.dots)

        self.screen.update()
        self.clock.tick()

    def handle_events(self):
        """
        Loop through all events.
        """
        for event in pymlgame.get_events():
            if event.type == E_NEWCTLR:
                #print(datetime.now(), '### new player connected with uid', event.uid)
                self.players[event.uid] = {'name': 'alien_{}'.format(event.uid), 'score': 0}
            elif event.type == E_DISCONNECT:
                #print(datetime.now(), '### player with uid {} disconnected'.format(event.uid))
                self.players.pop(event.uid)
            elif event.type == E_KEYDOWN:
                #print(datetime.now(), '###', self.players[event.uid]['name'], 'pressed', event.button)
                if event.button == 9:
                    self.init_game_board()
                else:
                    self.colors.append(self.colors.pop(0))
            elif event.type == E_PING:
                #print(datetime.now(), '### ping from', self.players[event.uid]['name'])
                pass

    def gameloop(self):
        """
        A game loop that circles through the methods.
        """
        try:
            while True:
                self.handle_events()
                self.update()
                self.render()
        except KeyboardInterrupt:
            pass
Exemple #12
0
class Game(object):
    """
    The main game class that holds the gameloop.
    """
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port, self.width, self.height)
        self.clock = Clock(15)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]

        # surfaces
        self.corners = Surface(self.screen.width, self.screen.height)
        self.lines = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
        self.rects = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
        self.circle = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)
        self.filled = Surface(
            int(self.screen.width / 2) - 2,
            int(self.screen.height / 2) - 2)

    def update(self):
        """
        Update the screens contents in every loop.
        """
        # this is not really neccesary because the surface is black after initializing
        self.corners.fill(BLACK)
        self.corners.draw_dot((0, 0), self.colors[0])
        self.corners.draw_dot((self.screen.width - 1, 0), self.colors[0])
        self.corners.draw_dot((self.screen.width - 1, self.screen.height - 1),
                              self.colors[0])
        self.corners.draw_dot((0, self.screen.height - 1), self.colors[0])

        self.lines.fill(BLACK)
        self.lines.draw_line((1, 0), (self.lines.width - 1, 0), self.colors[1])
        self.lines.draw_line((0, 1), (0, self.lines.height - 1),
                             self.colors[3])
        self.lines.draw_line((0, 0),
                             (self.lines.width - 1, self.lines.height - 1),
                             self.colors[2])

        self.rects.fill(BLACK)
        self.rects.draw_rect(
            (0, 0), (int(self.rects.width / 2) - 1, self.rects.height),
            self.colors[2], self.colors[3])
        self.rects.draw_rect(
            (int(self.rects.width / 2) + 1, 0),
            (int(self.rects.width / 2) - 1, self.rects.height), self.colors[3],
            self.colors[2])

        self.circle.fill(BLACK)
        radius = int(min(self.circle.width, self.circle.height) / 2) - 1
        self.circle.draw_circle(
            (int(self.circle.width / 2) - 1, int(self.circle.height / 2) - 1),
            radius, self.colors[4], self.colors[5])

        self.filled.fill(self.colors[6])

    def render(self):
        """
        Send the current screen content to Mate Light.
        """
        self.screen.reset()
        self.screen.blit(self.corners)
        self.screen.blit(self.lines, (1, 1))
        self.screen.blit(self.rects, (int(self.screen.width / 2) + 1, 1))
        self.screen.blit(self.circle, (0, int(self.screen.height / 2) + 1))
        self.screen.blit(
            self.filled,
            (int(self.screen.width / 2) + 1, int(self.screen.height / 2) + 1))

        self.screen.update()
        self.clock.tick()

    def handle_events(self):
        """
        Loop through all events.
        """
        for event in pymlgame.get_events():
            if event.type == E_NEWCTLR:
                #print(datetime.now(), '### new player connected with uid', event.uid)
                self.players[event.uid] = {
                    'name': 'alien_{}'.format(event.uid),
                    'score': 0
                }
            elif event.type == E_DISCONNECT:
                #print(datetime.now(), '### player with uid {} disconnected'.format(event.uid))
                self.players.pop(event.uid)
            elif event.type == E_KEYDOWN:
                #print(datetime.now(), '###', self.players[event.uid]['name'], 'pressed', event.button)
                self.colors.append(self.colors.pop(0))
            elif event.type == E_KEYUP:
                #print(datetime.now(), '###', self.players[event.uid]['name'], 'released', event.button)
                self.colors.append(self.colors.pop(0))
            elif event.type == E_PING:
                #print(datetime.now(), '### ping from', self.players[event.uid]['name'])
                pass

    def gameloop(self):
        """
        A game loop that circles through the methods.
        """
        try:
            while True:
                self.handle_events()
                self.update()
                self.render()
        except KeyboardInterrupt:
            pass
Exemple #13
0
class Mario:
    def __init__(self):
        self.normal = Surface(2, 4)
        self.normal.draw_line((0, 0), (1, 0), RED)
        self.normal.draw_dot((0, 1), (127, 0, 0))
        self.normal.draw_dot((1, 1), (255, 127, 0))
        self.normal.draw_dot((0, 2), RED)
        self.normal.draw_dot((1, 2), BLUE)
        self.normal.draw_line((0, 3), (1, 3), BLUE)

        self.current = self.normal

        self.x = 2
        self.y = 8
        self.width = self.current.width
        self.height = self.current.height

        self.direction = 1  # 0: left, 1: right

        self.jumping = False
        self.moving = False

    def render(self):
        """
        Return current form of mario.

        :returns: Surface - Marios surface
        """
        return self.current
Exemple #14
0
class Game(object):
    """
    The main game class that holds the gameloop.
    """
    def __init__(self, host, port, width, height):
        """
        Create a screen and define some game specific things.
        """
        self.host = host
        self.port = port
        self.width = width
        self.height = height

        self.players = {}

        pymlgame.init()
        self.screen = Screen(self.host, self.port,
                             self.width, self.height)
        self.clock = Clock(15)
        self.running = True
        self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]

        # surfaces
        self.corners = Surface(self.screen.width, self.screen.height)
        self.lines = Surface(int(self.screen.width / 2) - 2,
                             int(self.screen.height / 2) - 2)
        self.rects = Surface(int(self.screen.width / 2) - 2,
                             int(self.screen.height / 2) - 2)
        self.circle = Surface(int(self.screen.width / 2) - 2,
                              int(self.screen.height / 2) - 2)
        self.filled = Surface(int(self.screen.width / 2) - 2,
                              int(self.screen.height / 2) - 2)

    def update(self):
        """
        Update the screens contents in every loop.
        """
        # this is not really neccesary because the surface is black after initializing
        self.corners.fill(BLACK)
        self.corners.draw_dot((0, 0), self.colors[0])
        self.corners.draw_dot((self.screen.width - 1, 0), self.colors[0])
        self.corners.draw_dot((self.screen.width - 1, self.screen.height - 1),
                              self.colors[0])
        self.corners.draw_dot((0, self.screen.height - 1), self.colors[0])

        self.lines.fill(BLACK)
        self.lines.draw_line((1, 0), (self.lines.width - 1, 0), self.colors[1])
        self.lines.draw_line((0, 1), (0, self.lines.height - 1), self.colors[3])
        self.lines.draw_line((0, 0), (self.lines.width - 1,
                                      self.lines.height - 1), self.colors[2])

        self.rects.fill(BLACK)
        self.rects.draw_rect((0, 0), (int(self.rects.width / 2) - 1,
                                      self.rects.height),
                             self.colors[2], self.colors[3])
        self.rects.draw_rect((int(self.rects.width / 2) + 1, 0),
                             (int(self.rects.width / 2) - 1,
                              self.rects.height),
                             self.colors[3], self.colors[2])

        self.circle.fill(BLACK)
        radius = int(min(self.circle.width, self.circle.height) / 2) - 1
        self.circle.draw_circle((int(self.circle.width / 2) - 1,
                                 int(self.circle.height / 2) - 1), radius,
                                self.colors[4], self.colors[5])

        self.filled.fill(self.colors[6])

    def render(self):
        """
        Send the current screen content to Mate Light.
        """
        self.screen.reset()
        self.screen.blit(self.corners)
        self.screen.blit(self.lines, (1, 1))
        self.screen.blit(self.rects, (int(self.screen.width / 2) + 1, 1))
        self.screen.blit(self.circle, (0, int(self.screen.height / 2) + 1))
        self.screen.blit(self.filled, (int(self.screen.width / 2) + 1,
                                       int(self.screen.height / 2) + 1))

        self.screen.update()
        self.clock.tick()

    def handle_events(self):
        """
        Loop through all events.
        """
        for event in pymlgame.get_events():
            if event.type == E_NEWCTLR:
                #print(datetime.now(), '### new player connected with uid', event.uid)
                self.players[event.uid] = {'name': 'alien_{}'.format(event.uid), 'score': 0}
            elif event.type == E_DISCONNECT:
                #print(datetime.now(), '### player with uid {} disconnected'.format(event.uid))
                self.players.pop(event.uid)
            elif event.type == E_KEYDOWN:
                #print(datetime.now(), '###', self.players[event.uid]['name'], 'pressed', event.button)
                self.colors.append(self.colors.pop(0))
            elif event.type == E_KEYUP:
                #print(datetime.now(), '###', self.players[event.uid]['name'], 'released', event.button)
                self.colors.append(self.colors.pop(0))
            elif event.type == E_PING:
                #print(datetime.now(), '### ping from', self.players[event.uid]['name'])
                pass

    def gameloop(self):
        """
        A game loop that circles through the methods.
        """
        try:
            while True:
                self.handle_events()
                self.update()
                self.render()
        except KeyboardInterrupt:
            pass