Esempio n. 1
0
def death(win):
    global done, icebergs, moves, steps
    pygame.time.wait(200)
    screen = pygame.display.set_mode((xres, yres))
    if win:
        victory(screen, xres, yres)
    else:
        lose(screen, xres, yres)
    write_name(get_name(), len(path))
    if time == 6 and steps == 66:
        snake()
    bg = pygame.image.load('menu_bg.png')
    bg = pygame.transform.scale(bg, (xres, yres))
    screen.blit(bg, (0, 0))
    moves_string = ''
    for i in range(len(moves)):
        moves_string += ','.join(map(str,
                                     ceil(array(moves[i])).astype(int))) + '->'
    moves_string += ','.join(map(str, ceil(array(moves[-1])).astype(int)))
    myfont = pygame.font.SysFont('Comic Sans MS', 25)
    for i in range(len(moves_string) // 50 + 1):
        current = moves_string[i * 50:min((i + 1) * 50, len(moves_string))]
        if i > 10:
            current += '...'
        screen.blit(myfont.render(current, False, (255, 255, 255)),
                    (0, i * 25))
        end = i * 25
        if i > 10:
            break
    myfont = pygame.font.SysFont('Comic Sans MS', 30)
    end += 50
    screen.blit(
        myfont.render('Всего шагов: ' + str(steps), False, (255, 255, 255)),
        (xres / 2 - 150, end))
    end += 50
    screen.blit(
        myfont.render('Нажмите любую клавишу чтобы продолжить', False,
                      (255, 255, 255)), (20, end))
    done = False
    while not done:
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                done = True
    pygame.display.flip()
    mode = create_menu(screen, xres, yres, current_settings)
    if mode == -1:
        done = True
    else:
        done = False
        icebergs = []
        moves = [[0, 0]]
        steps = 0
        start(mode)
Esempio n. 2
0
    def client_keyboard(self, client, client_addr):
        # if someone joins on the server, prepare game configuration
        if len(self.clients) == 0:
            self.game_env = Snake.snake_map(width, height)
            self.game_env.init_map()
            Snake.get_hex(self.hex, self.game_env)
            spawn_p1, spawn_p2, spawn_f = False, False, False
            while spawn_p1 is not True and spawn_p2 is not True and self.spawn_fruit is not True:

                if not spawn_p1:
                    x = np.random.randint(0, len(self.hex))
                    spawn_p1, _ = Snake.check_space(self.game_env.map, self.hex[x], 1, [0, 0, "#", "+"], 0)
                    if self.hex[x][1] < 4 or self.hex[x][1] > 70:
                        spawn_p1 = False

                if not spawn_p2:
                    y = np.random.randint(0, len(self.hex))
                    spawn_p2, _ = Snake.check_space(self.game_env.map, self.hex[x], 1, [0, 0, "#", "+"], 0)
                    if self.hex[x][1] < 4 or self.hex[x][1] > 70:
                        spawn_p2 = False

                if not self.spawn_fruit:
                    z = np.random.randint(0, len(self.hex))
                    self.spawn_fruit, _ = Snake.check_space(self.game_env.map, self.hex[z], 1, [0, 0, "#", "+"], 0)

            animal1.insert(0, snake(self.hex[x], self.game_env.map, "#"))
            animal2.insert(0, snake(self.hex[y], self.game_env.map, "+"))
            _ = fruit(self.hex[z], self.game_env.map, "@")

        # append client to the list
        self.clients.append([client, client_addr, self.no_clients])
        self.no_clients = self.no_clients + 1

        if len(self.clients) == 2:
            for i in self.clients:
                i[0].send(bytes("Start " + str(i[2]), "utf-8"))
            time.sleep(2)
            Thread(target=self.game_loop).start()

        # receive pressed keys from clients
        while True:
            if self.end_game:
                break
            for i in self.clients:
                msg = i[0].recv(1024).decode("utf-8")
                if i[-1] == 0:
                    keys_1.append(msg[0])
                elif i[-1] == 1:
                    keys_2.append(msg[0])
Esempio n. 3
0
 def __init__(self):
     self.exit = False
     self.snake = snake(self)
     self.user = user(self)
     self.fruitGenerator = fruitGenerator(self)
     self.fruitBasket = []
     self.bodyCollision = False
Esempio n. 4
0
def run():
    global s, snack, win
    win = pygame.display.set_mode((width, height))
    s = snake((255, 0, 0), (10, 10))
    s.addCube()
    snack = cube(randomSnack(rows, s), color=(0, 20, 255))
    flag = True
    clock = pygame.time.Clock()

    while flag:
        pygame.time.delay(50)
        clock.tick(10)
        s.move()
        headPos = s.head.pos
        if headPos[0] >= 20 or headPos[0] < 0 or headPos[1] >= 20 or headPos[
                1] < 0:
            print("Score:", len(s.body))
            s.reset((10, 10))

        if s.body[0].pos == snack.pos:
            s.addCube()
            snack = cube(randomSnack(rows, s), color=(0, 20, 255))

        redrawWindow()
Esempio n. 5
0
    def start_game(self):
        self.config_info.setText("")
        self.game_loop, spawn_fruit = False, False

        if self.game_loop is not True:
            p1_keys.clear(), p2_keys.clear()

        root = ET.Element("game")
        spawn_animal1, spawn_animal2, self.game_loop = False, False, True
        p1_can_play, p2_can_play = True, True
        animal1, animal2 = [], []
        points1, points2 = 0, 0

        # find free space on the map and spawn snake (or snakes)
        if (self.hot_seat and self.against_ai is 0) or (self.hot_seat is 0
                                                        and self.against_ai):
            self.scoreboard.setText("1.P1 points - " + str(points1) +
                                    " | 2.P2 points - " + str(points2))
            while (spawn_animal1 is False) and (spawn_animal2 is False):
                if not spawn_animal1:
                    x = np.random.randint(0, len(self.hex))
                    spawn_animal1, _ = Snake.check_space(
                        self.game.map, self.hex[x], 1, [0, 0, "#", "+"], 0)
                if not spawn_animal2:
                    y = np.random.randint(0, len(self.hex))
                    spawn_animal2, _ = Snake.check_space(
                        self.game.map, self.hex[y], 1, [0, 0, "#", "+"], 0)
            animal1.insert(0, snake(self.hex[x], self.game.map, "#"))
            animal2.insert(0, snake(self.hex[y], self.game.map, "+"))
        elif self.hot_seat is 0 and self.against_ai is 0:
            self.scoreboard.setText("Points - " + str(points1))
            while not spawn_animal1:
                x = np.random.randint(0, len(self.hex))
                spawn_animal1, _ = Snake.check_space(self.game.map,
                                                     self.hex[x], 1,
                                                     [0, 0, "#"], 0)
            animal1.insert(0, snake(self.hex[x], self.game.map, "#"))

        #main game loop
        index = 0
        while self.game_loop:

            # save actual map to xml file
            doc = ET.SubElement(root, "turn", name=str(index))
            for i in range(height):
                row = ET.SubElement(doc, "cell", indexes=str(i))
                for j in range(width):
                    ET.SubElement(row, "row",
                                  indexes=str(j)).text = self.game.map[i][j]

            # spawn fruit on the map
            if not spawn_fruit:
                while not spawn_fruit:
                    x = np.random.randint(0, len(self.hex))
                    spawn_fruit, _ = Snake.check_space(self.game.map,
                                                       self.hex[x], 1,
                                                       [0, 0, "@"], 0)

            fruit_on_map = fruit(self.hex[x], self.game.map, "@")

            spawn_fruit, points1, points2, p1_can_play, p2_can_play, self.game_loop = \
                Snake.move_anim(p1_can_play, p2_can_play, spawn_fruit, animal1, animal2, points1, points2, fruit_on_map,
                                p1_keys, p2_keys, self.hot_seat, self.against_ai, self.game, self.hex, self.scoreboard,
                                self.game_loop)

            self.update_map()
            index = index + 1
            myApp.processEvents()
            time.sleep(0.5)

        # prepare configuration and map history
        if self.hot_seat is 0 and self.against_ai is 0:
            self.config = {"mode-": "Singleplayer", " points-": str(points1)}
        elif self.hot_seat and self.against_ai is 0:
            self.config = {
                "mode-": "Hotseat",
                " P1 points-": str(points1),
                " P2 points-": str(points2)
            }
        elif self.hot_seat is 0 and self.against_ai:
            self.config = {
                "mode-": "Against Bot",
                " P1 points-": str(points1),
                " P2 points-": str(points2)
            }
        with open("config.json", "w") as f:
            json.dump(self.config, f)

        tree = ET.ElementTree(root)
        tree.write("replay.xml")

        if (self.hot_seat and self.against_ai is 0) or (self.hot_seat is 0
                                                        and self.against_ai):
            if points1 == points2:
                self.scoreboard.setText("It's draw! 1.P1 score - " +
                                        str(points1) + " | 2.P2 score - " +
                                        str(points2))
            elif points1 > points2:
                self.scoreboard.setText("P1 won! 1.P1 score - " +
                                        str(points1) + " | 2.P2 score - " +
                                        str(points2))
            else:
                self.scoreboard.setText("P2 won! 1.P1 score - " +
                                        str(points1) + " | 2.P2 score - " +
                                        str(points2))
        else:
            self.scoreboard.setText("You lost! Your score - " + str(points1))