示例#1
0
    def __init__(self, channel):

        self.server = SnakePost(channel, "server")

        self.players = {}
        self.apples = []

        self.clock = pygame.time.Clock()
        self.current_time = 0
        self.new_apple_timer = Timer(Constants.NEW_APPLE_PERIOD * 1000,
                                     self.current_time,
                                     periodic=True)
        self.update_snakes_timer = Timer(0.1 * 1000,
                                         self.current_time,
                                         periodic=True)
示例#2
0
    def run(self):

        self.address = (self.preferences.get("server"),
                        self.preferences.get("port"))

        #initialisation du snakePost
        self.client = SnakeChan()

        self.client.connect(self.address, self.preferences.get("color"),
                            self.preferences.get("nickname"))
        self.com = SnakePost(self.client, self.preferences.get("nickname"))

        whole_second = 0
        self.running = True
        while self.running:
            #time tracking
            self.current_time += self.clock.tick(Constants.FPS)

            #check if the snake is still alive
            if not self.me.alive:
                self.me.alive = True
                self.me.restart()

            #check if game need more food
            # if self.new_apple_timer.expired(self.current_time):
            #     self.f.make()

            #check if we need to move our own snake's state
            #if we do, send an update of our position to
            #the server
            if self.move_snake_timer.expired(self.current_time):
                self.me.move()
                pos = self.me.netinfo()
                self.com.send(pos, self.address)

            #check if we need to blink the unready snakes (unready state)
            if self.blink_snake_timer.expired(self.current_time):
                for snake in self.others:
                    self.others[snake].blink()

            #cleanup background
            self.gamescreen.fill(Constants.COLOR_BG)

            #draw scores
            self.scores.draw(self.screen)

            #draw all snakes positions as last seen by the server
            #we do not compute their positions ourselves!

            for snake in self.others:
                self.others[snake].draw(self.gamescreen)

            #draw food
            self.f.draw(self.gamescreen)

            #process external events (keyboard,...)
            self.process_events()

            #then update display
            #update game area on screen container
            self.screen.blit(self.gamescreen, (self.score_width, 0))

            pygame.display.update()

            # Sur message du serveur...
            data, addr = self.com.listen()

            if data is not None:

                dat = json.loads(data)

                for key in dat:

                    if key == 'players_info':
                        #On met a jour les scores et les etats des joueurs
                        for player in dat[key]:

                            if not self.others.get(
                                    player[0]
                            ):  # Si on a pas de joueur de ce nom, on l'ajoute
                                self.others[player[0]] = Snake(
                                    color=pygame.color.THECOLORS[player[1]],
                                    nickname=player[0])
                                self.scores.new_score(
                                    player[0],
                                    self.others.get(player[0]).color)

                            else:  # On a deja le joueur, on update son etat
                                if player[3]:
                                    self.others[player[0]].set_ready()
                                else:
                                    self.others[player[0]].set_unready()
                            # on update les scores
                            self.scores.set_score(player[0], player[2])

                    elif key == "snakes":  # message de position des joueurs

                        # on regarde si il y a des serpents a enlever
                        if len(dat[key]) != len(self.others):
                            for nickname in self.others.keys():
                                connected = False
                                for val in dat[key]:
                                    if val[0] == nickname:
                                        connected = True
                                if not connected:
                                    del self.others[nickname]
                                    self.scores.del_score(nickname)

                        for val in dat[key]:
                            # si on a ce joeur et que ses positions ne sont pas vides (premier message)
                            if len(val[1]) > 0 and self.others[val[0]]:
                                self.others[val[0]].setBody(val[1])

                    elif key == "foods":  # les pommes envoyees par le serveur
                        self.f.set_positions(dat[key])

                    elif key == "grow":  # Un serpent a mange une pomme !
                        if dat[key] == self.preferences.get("nickname"):
                            self.me.grow(Constants.GROW)

                    elif key == "game_over":  # Desole, c'est perdu
                        if dat[key] == self.preferences.get("nickname"):
                            self.me.restart()