Exemple #1
0
class GameEntity(object):

      def __init__(self, world, name, image, text):
            """Base class used to construct a game entity.

            Returns new GameEntity object
            with the specified variables world, name,image, text, location, destination, speed, brain and id, this is a super
            class and should ideally be used via inheritance

            world: holds a world object. For game entities this should ideally be the
            world game

            name: holds the name of the particular entity being constructed e.g. zombie

            image: holds the location of the image that will be used as the main image
            for whatever GameEntity is constructed. This image holder will also be used to
            store the image as a result of rotation for display

            start_image: holds the location of the image that will be used as the main image
            for whatever GameEntity is constructed. This one however will not be altered but
            used as a reference to reset the rotational image when a new rotation is required.

            text: holds the text that will be displayed in a textual entity; for example
            the TextBox Entity

            location: holds a default Vector of (0,0) that will be altered to set the location
            of the enity that is created

            destination: holds a default Vector of (0,0) that will be altered to set the destination
            location of the entity

            heading: holds a default Vector of (0,0) that will be altered to set the heading; vector
            between two points with no size. Can be seen as the direction

            last_heading: holds a default Vector of (0,0), this heading will typically be used
            to remember the last heading that was used for rotating an entity image, so that rotation
            does not get caught in an infinite loop

            speed: holds a number variable default 0. that will be altered to set the speed
            of the entity that is created

            brain: holds a StateManager object that is used to manage the states that the
            entity object will use

            id: holds the numerial id for the entity object that is being created

            """

            self.world = world
            self.name = name
            self.image = image
            self.start_image = image
            self.text = text
            self.location = Vector(0,0)
            self.destination = Vector(0,0)
            self.heading = Vector(0,0)
            self.last_heading = Vector(0,0)
            self.speed = 0.

            self.brain = StateManager()

            self.id = 0

      def render(self, surface):
            """Function to render the entity image to the screen

            Blit in this particular function places the sepecified image
            so that it its central point coorespondes to the location specified

            w,h: holds the width and height of the image used as the
            visual representation of the game entity

            new_image_vector: A vector that holds the central point of an image

            position: holds the value that represents the center of the image
            at a specified location
            """
            if self.speed != 0:
                  self.rotate_image()
                  
            w, h = self.image.get_size()
            w = w/2
            h = h/2
            new_image_vector = Vector(w, h)
            position = (self.location - new_image_vector)
            surface.blit(self.image, (position.get_x(), position.get_y()))

      def renderText(self, surface):
            """Function to render the entity text to the screen.

            Blit in this particular function places the specified text
            so that it appears in the center of the world it is created for

            font: holds a new font object, usually using a filename and the
            size of the font required Font(filename, size)

            lines: holds the separate lines of the text box so that they can be rendered.
            A new line is represented by a return character in the StartUpText(textBox) Code

            center: holds the location of the textbox object, and renders text in relation
            to this variable

            antialias: Boolean with value of 1, to ensure that when the font is rendered on
            screen all of the characters have smooth edges

            black: holds RGB value for the colour black, used later to set the text colour
            to black

            text: draws text on to a new surface. in this case it takes the line of text to
            be displayed, whether or not the charaters need antialising and what colour the
            characters should be

            text_pos: used to store the position of where the text will be rendered

            center: used in conjunction with self.location and text_pos to calculate the
            position of where the text will be rendered and then passes the value to the
            text_pos so that the text can be rendered

            height: simply the height of the font being used. Used to space one line of
            text from another
            """
            
            font = pygame.font.Font(None, font_size)

            lines = self.text.strip().splitlines()

            center = (self.location.get_x(), self.location.get_y())
            
            antialias = 1
            red = 255,0,0

            for line in lines:
                  text = font.render(line.strip(), antialias, red)
                  
                  text_pos = text.get_rect()

                  center_image_x = text_pos.width/2
                  center_image_y = text_pos.height/2
                  w, h = center
                  center = ((w - center_image_x),(h - center_image_y))
                  
                  text_pos = center 

                  height = font.get_linesize()
                  surface.blit(text, text_pos)
                  
                  new_y = self.location.get_y() + height
                  center = (self.location.get_x(), new_y)
                  

            pygame.display.flip()

      def process(self, time_passed):
            """Function that plots a new course.

            Function that determines whether or not the entity can move,
            if the enity can move then a new course is plotted based
            on its current location, and its destination

            vec_to_destination: holds a vector object that determines how to get
            from the current location to a destination location e.g. A (1,1) B(10,10)
            vec_to_des(9,9)

            distance_to_destination: holds the distance between two points. Should
            be a numeric value

            heading: holds the heading/direction that leads to the destination, Created
            by normalising the vec_to_destination. vect_to_destination / magnitude, giving it
            a magnitude of 1, which provides a heading

            travel_speed: holds the speed the entity should travel based on its given speed and
            the amount of time that has passed

            position: holds the current position of the entity based on its heading
            and its travel speed.
            """
            
            
            self.brain.think()

            if self.speed > 0 and self.location != self.destination:

                vec_to_destination = self.destination - self.location
                distance_to_destination = vec_to_destination.get_magnitude()
                self.heading = vec_to_destination
                self.heading.normalise()
                travel_speed = min(distance_to_destination, time_passed * self.speed)
                position = self.heading * travel_speed 
                self.location += position

      def rotate_image(self):
            """This function is used to rotate the entity image depending on its heading.
            """

            if self.heading.get_x() == 0 and self.heading.get_y() == 0: #or self.heading.compare_to(self.last_heading) == True:
                  self.heading = self.last_heading

            if self.heading.get_x() >=0 and self.heading.get_y() <=0:
                  value = math.degrees(math.acos(self.heading.get_x()))
                  self.image = self.start_image
                  self.image = pygame.transform.rotate(self.image, value)
                  self.last_heading = self.heading
                  return

            elif self.heading.get_x() <=0 and self.heading.get_y() <=0:
                  value = math.degrees(math.acos(self.heading.get_x()))
                  self.image = self.start_image
                  self.image = pygame.transform.rotate(self.image, value)
                  self.last_heading = self.heading
                  return

            elif self.heading.get_x() <=0 and self.heading.get_y() >=0 :
                  value = 180 + math.degrees(math.asin(self.heading.get_y()))
                  self.image = self.start_image
                  self.image = pygame.transform.rotate(self.image, value)
                  self.last_heading = self.heading
                  return

            elif self.heading.get_x() >=0 and self.heading.get_y() >=0:
                  value = 360 - math.degrees(math.asin(self.heading.get_y()))
                  self.image = self.start_image
                  self.image = pygame.transform.rotate(self.image, value)
                  self.last_heading = self.heading
                  return

      def handle_click(self, event):
            return