예제 #1
0
    def render_move(self, ignore_tail=False):
        # moving snake to the next position
        render = []
        new_head = self.next_position()
        self.body.appendleft(new_head)
        # draw head in the next position
        render.append(Draw(new_head.x, new_head.y, self.CH_HEAD, self.color))
        # draw body in the old place of head
        render.append(
            Draw(self.body[1].x, self.body[1].y, self.CH_BODY, self.color))
        # save current direction of the head
        self.current_direction = self.direction

        # if we grow this turn, the tail remains in place
        if self.grow > 0:
            self.grow -= 1
            self.grew = True
        else:
            self.grew = False
            # otherwise the tail moves
            old_tail = self.body.pop()
            if not ignore_tail:
                render.append(
                    Draw(old_tail.x, old_tail.y, self.CH_VOID, self.COLOR_0))
            new_tail = self.body[-1]
            render.append(
                Draw(new_tail.x, new_tail.y, self.CH_TAIL, self.color))

        return render
예제 #2
0
 def render_game_over(self):
     render = []
     # dead snake
     for i, pos in enumerate(self.snake):
         if i == 0:
             render.append(Draw(pos.x, pos.y, self.DEAD_HEAD_CHAR, 0))
         elif i == len(self.snake) - 1:
             render.append(Draw(pos.x, pos.y, self.DEAD_TAIL_CHAR, 0))
         else:
             render.append(Draw(pos.x, pos.y, self.DEAD_BODY_CHAR, 0))
     return render
예제 #3
0
 def render_game_over(self):
     render = []
     # dead snake
     for i in range(0, len(self.snake)):
         pos = self.snake[i]
         if i == 0:
             render.append(Draw(pos.x, pos.y, self.DEAD_HEAD_CHAR, 0))
         elif i == len(self.snake) - 1:
             render.append(Draw(pos.x, pos.y, self.DEAD_TAIL_CHAR, 0))
         else:
             render.append(Draw(pos.x, pos.y, self.DEAD_BODY_CHAR, 0))
     return render
예제 #4
0
    def create(self):
        assert not self.grow
        assert not self.body
        assert not self.direction

        # try to spawn snake at some distance from world's borders
        distance = settings.INIT_LENGTH + settings.INIT_MIN_DISTANCE_BORDER
        x = randint(distance, World.SIZE_X - distance)
        y = randint(distance, World.SIZE_Y - distance)
        self.direction = self.current_direction = self.DIRECTIONS[randint(
            0, 3)]
        # create snake from tail to head
        render = []
        pos = Position(x, y)

        for i in range(0, settings.INIT_LENGTH):
            target = self._world[pos.y][pos.x]

            if target.char != self.CH_VOID:
                raise SnakePlacementError(
                    'Cannot place snake on %r because the position '
                    'is occupied by %r', pos, target)

            if i == 0:
                char = self.CH_TAIL
            elif i == settings.INIT_LENGTH - 1:
                char = self.CH_HEAD
            else:
                char = self.CH_BODY

            self.body.appendleft(pos)
            render.append(Draw(pos.x, pos.y, char, self.color))
            pos = self.next_position()

        return render
예제 #5
0
    def render_game_over(self):
        render = []

        # dead snake
        for i, pos in enumerate(self.body):
            if i == 0:
                render.append(
                    Draw(pos.x, pos.y, self.CH_DEAD_HEAD, self.COLOR_0))
            elif i == len(self.body) - 1:
                render.append(
                    Draw(pos.x, pos.y, self.CH_DEAD_TAIL, self.COLOR_0))
            else:
                render.append(
                    Draw(pos.x, pos.y, self.CH_DEAD_BODY, self.COLOR_0))

        return render
예제 #6
0
 def render_text(self, text, color):
     # render in the center of play field
     posy = int(settings.FIELD_SIZE_Y / 2)
     posx = int(settings.FIELD_SIZE_X / 2 - len(text) / 2)
     render = []
     for i in range(0, len(text)):
         render.append(Draw(posx + i, posy, text[i], color))
     return render
예제 #7
0
 def spawn_stone(self, right_now=False):
     render = []
     if right_now or\
        randint(1, 100) <= settings.STONE_SPAWN_RATE:
         x, y = self._get_spawn_place()
         if x and y:
             render += [Draw(x, y, '#', 0)]
     return render
예제 #8
0
 def spawn_digit(self, right_now=False):
     render = []
     if right_now or\
        randint(1, 100) <= settings.DIGIT_SPAWN_RATE:
         x, y = self._get_spawn_place()
         if x and y:
             char = str(randint(1, 9))
             color = randint(1, settings.NUM_COLORS)
             render += [Draw(x, y, char, color)]
     return render
예제 #9
0
    def _render_text(text, color):
        # render in the center of play field
        pos_y = int(World.SIZE_Y / 2)
        pos_x = int(World.SIZE_X / 2 - len(text)/2)
        render = []

        for i in range(0, len(text)):
            render.append(Draw(pos_x + i, pos_y, text[i], color))

        return render
예제 #10
0
 def render_move(self):
     # moving snake to the next position
     render = []
     new_head = self.next_position()
     self.snake.appendleft(new_head)
     # draw head in the next position
     render.append(Draw(new_head.x, new_head.y, self.HEAD_CHAR, self.color))
     # draw body in the old place of head
     render.append(
         Draw(self.snake[1].x, self.snake[1].y, self.BODY_CHAR, self.color))
     # if we grow this turn, the tail remains in place
     if self.grow > 0:
         self.grow -= 1
     else:
         # otherwise the tail moves
         old_tail = self.snake.pop()
         render.append(Draw(old_tail.x, old_tail.y, " ", 0))
         new_tail = self.snake[-1]
         render.append(
             Draw(new_tail.x, new_tail.y, self.TAIL_CHAR, self.color))
     return render
예제 #11
0
    def spawn_digit(self, right_now=False):
        render = []

        if right_now or randint(1, 100) <= settings.DIGIT_SPAWN_RATE:
            x, y = self._get_spawn_place()

            if x and y:
                char = str(1)
                color = self._pick_random_color()
                render += [Draw(x, y, char, color)]

        return render
예제 #12
0
 def render_new_snake(self):
     # try to spawn snake at some distance from world's borders
     distance = settings.INIT_LENGHT + 2
     x = randint(distance, settings.FIELD_SIZE_X - distance)
     y = randint(distance, settings.FIELD_SIZE_Y - distance)
     self.direction = self.DIRECTIONS[randint(0, 3)]
     # create snake from tail to head
     render = []
     pos = Position(x, y)
     for i in range(0, settings.INIT_LENGHT):
         self.snake.appendleft(pos)
         if i == 0:
             char = self.TAIL_CHAR
         elif i == settings.INIT_LENGHT - 1:
             char = self.HEAD_CHAR
         else:
             char = self.BODY_CHAR
         render.append(Draw(pos.x, pos.y, char, self.color))
         pos = self.next_position()
     return render
예제 #13
0
 def spawn_digit(self):
     x = randint(0, settings.FIELD_SIZE_X - 1)
     char = str(randint(0,9))
     self._draw = Draw(x, char)