Exemple #1
0
    def __init__(self, text):

        super().__init__()
        self.set_velocity(Location(1, 0))
        self.set_text(text)

        x = 0
        y = randint(1, constants.MAX_Y)
        self.set_position(Location(x, y))
Exemple #2
0
    def __init__(self):
        """The class constructor.

        Args:
            self (Actor): an instance of Actor.
        """
        self._text = []
        #self._lines = [Point(0, 0), Point(0, 0), Point(0, 0), Point(0, 0), Point(0, 0)]
        # TODO find better way to space out lines
        self._velocity = Location(0, 0)
        self._position = Location(0, 0)
Exemple #3
0
    def _reset(self):

        for n in range(len(self._new_words)):
            x = random.randint(1, 10)
            y = random.randint(2, constants.MAX_Y - 2)
            w_reverse = ""
            for char in self._new_words[n]:
                w_reverse = char + w_reverse
            self._new_words[n] = w_reverse
            for i in range(len(self._new_words[n])):
                text = self._new_words[n][i:i + 1]
                position = Location(x - i, y)
                velocity = Location(1, 0)
                self._add_segment(text, position, velocity)
Exemple #4
0
class Piece(object):
    location = Location()
    size = 0
    colour = 0
    edge = 0

    def get_location(self):
        return self.location

    def get_size(self):
        return self.size

    def get_colour(self):
        return self.colour

    def get_width(self):
        return self.edge

    def get_rect(self):
        # Adjust the drawing location so rect is drawn in the center of a square unit
        newLoc = Location().create(self.location).transform(
            (UNIT - self.size) // 2)
        return pygame.Rect(newLoc.get_location(), [self.size, self.size])

    def update_location(self, loc):
        self.location = loc

    def draw(self, surface):
        return pygame.draw.rect(surface, self.colour, self.get_rect(),
                                self.edge)

    def __init__(self, loc):
        self.location = loc
 def __init__(self, screen):
     """The class constructor.
     
     Args:
         self (InputService): An instance of InputService.
     """
     self._direction = Location(1, 0)
     self._screen = screen
Exemple #6
0
 def neighbors(self, location: Location) -> List[Location]:
     assert self.is_free(location)
     neighbors = []
     for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
         loc = location.add(dx, dy)
         if self.contains(loc) and self.is_free(loc):
             neighbors.append(loc)
     return neighbors
Exemple #7
0
    def __init__(self):
        """The class constructor. Invokes the superclass constructor, initializes points to zero, sets the position and updates the text.

        Args:
            self (Score): an instance of Score.
        """
        super().__init__()
        self._points = 0
        position = Location(1, 0)
        self.set_position(position)
        self.set_text(f"Score: {self._points}")
Exemple #8
0
    def move_next(self):
        """Moves the actor to its next position according to its velocity. Will
        wrap the position from one side of the screen to the other when it
        reaches the boundary in either direction.

        Args:
            self (Actor): an instance of Actor.
        """
        x1 = self._position.get_x()
        y1 = self._position.get_y()
        x2 = self._velocity.get_x()
        y2 = self._velocity.get_y()
        x = 1 + (x1 + x2 - 1)
        if x == (constants.MAX_X):
            x = -10
        y = 1 + (y1 + y2 - 1)
        position = Location(x, y)
        self._position = position
Exemple #9
0
 def get_rect(self):
     # Adjust the drawing location so rect is drawn in the center of a square unit
     newLoc = Location().create(self.location).transform((UNIT - self.size) // 2)
     return pygame.Rect(newLoc.get_location(), [self.size, self.size])
Exemple #10
0
    def __init__(self):

        super().__init__()
        position = Location(1, 20)
        self.set_position(position)
        self.set_text(f"Buffer: ")
Exemple #11
0
class Actor:
    """A visible, moveable thing that participates in the game. The responsibility of Actor is to keep track of its appearance, position
    and velocity in 2d space.

    Stereotype:
        Information Holder

    Attributes:
        _text (string): The textual representation of the actor.
        _position (Point): The actor's position in 2d space.
        _velocity (Point): The actor's speed and direction.
    """
    def __init__(self):
        """The class constructor.

        Args:
            self (Actor): an instance of Actor.
        """
        self._text = []
        #self._lines = [Point(0, 0), Point(0, 0), Point(0, 0), Point(0, 0), Point(0, 0)]
        # TODO find better way to space out lines
        self._velocity = Location(0, 0)
        self._position = Location(0, 0)

    def get_position(self):
        """Gets the actor's position in 2d space.
        
        Args:
            self (Actor): an instance of Actor.

        Returns:
            Point: The actor's position in 2d space.
        """
        return self._position

    def get_text(self):
        """Gets the actor's textual representation.

        Args:
            self (Actor): an instance of Actor.

        Returns:
            string: The actor's textual representation.
        """
        return self._text

    def get_velocity(self):
        """Gets the actor's speed and direction.

        Args:
            self (Actor): an instance of Actor.

        Returns:
            Point: The actor's speed and direction.
        """
        return self._velocity

    def move_next(self):
        """Moves the actor to its next position according to its velocity. Will
        wrap the position from one side of the screen to the other when it
        reaches the boundary in either direction.

        Args:
            self (Actor): an instance of Actor.
        """
        x1 = self._position.get_x()
        y1 = self._position.get_y()
        x2 = self._velocity.get_x()
        y2 = self._velocity.get_y()
        x = 1 + (x1 + x2 - 1)
        if x == (constants.MAX_X):
            x = -10
        y = 1 + (y1 + y2 - 1)
        position = Location(x, y)
        self._position = position

    def set_position(self, position):
        """Updates the actor's position to the given one.

        Args:
            self (Actor): An instance of Actor.
            position (Point): The given position.
        """
        self._position = position

    def set_text(self, text):
        """Updates the actor's text to the given value.

        Args:
            self (Actor): An instance of Actor.
            text (string): The given value.
        """
        self._text = text

    def set_velocity(self, velocity):
        """Updates the actor's velocity to the given one.

        Args:
            self (Actor): An instance of Actor.
            velocity (Point): The given velocity.
        """
        self._velocity = velocity
Exemple #12
0
 def create(self, typ, loc=Location()):
     if typ == "snake_head": return SnakeHead(loc)
     if typ == "snake_body": return SnakeBody(loc)
     if typ == "food": return Food(loc)
     assert 0, "Bad shape creation: " + typ
Exemple #13
0
 def get_rect(self):
     # Adjust the drawing location so rect is drawn in the center of a square unit
     newLoc = Location().create(self.location).transform(
         (UNIT - self.size) // 2)
     return pygame.Rect(newLoc.get_location(), [self.size, self.size])
Exemple #14
0
def move_head(old_head, direc):
    head = Location(old_head.get_x() + direc[0], old_head.get_y() + direc[1])

    if head.get_x() < 0:
        head.set_x(G_WIDTH - UNIT)
    elif head.get_x() >= G_WIDTH:
        head.set_x(0)
    elif head.get_y() < 0:
        head.set_y(G_HEIGHT - UNIT)
    elif head.get_y() >= G_HEIGHT:
        head.set_y(0)

    return head
Exemple #15
0
def generate_snake():
    return Location(((G_WIDTH//UNIT)//2)*UNIT,((G_HEIGHT//UNIT)//2)*UNIT)
Exemple #16
0
def generate_food():
    return Location(random.randint(0,G_WIDTH//UNIT-1)*UNIT, random.randint(0,G_HEIGHT//UNIT-1)*UNIT)