Beispiel #1
0
    def __init__(self):
        """Create a new game of Supercars."""

        self._screen = self._makeScreen()  #Initialize game window
        self._clock = pygame.time.Clock(
        )  #Initialising game clock(used to make the animation run smoothly)

        self._ground = self._makeGround()  #Create background
        self._obstacles = self._makeObstacles()  #Create obstacles
        self._checkpoints = self._makeCheckpoints()  #Create checkpoints
        self._font = makeFont(FONT, FONTSIZE)  #Create a standard font

        #Make a car for the player
        keys = self._makeControls()
        self._car = Supercar(Vector2D(int(RES_X / 2 + WIDTH),
                                      int((ROAD_WIDTH - WIDTH) / 2)),
                             Vector2D(0, 0),
                             SPEEDLIMIT,
                             RED,
                             WIDTH,
                             LENGTH,
                             Rectangle(RES_X, RES_Y, TRANSPARENT),
                             keys,
                             CAR_ROTATION,
                             bgcolor=WHITE)

        self.run()  #Run the game
Beispiel #2
0
    def collide(self, obstacles):
        """Changes the velocity of the supercar when hitting an obstacle.

        obstacles: The list of obstacles that the car can collide with.

        The method only handles circular obstacles at the moment.
        It is advised to avoid obstacles close to each other or to the borders of _room,
        since hitting multiple objects between two updates is not taken into consideration by the method.
        Additionally, the change in velocity is not very scientific (assumes a head-on elastic collision).
        """

        for thing in obstacles:
            if isinstance(thing, Circle):
                #Test if intersection is possible
                delta = Vector2D(self._w / 2, self._h / 2)
                dist = thing._pos - (self._pos + delta)
                if dist.magnitude() < thing._radius + delta.magnitude():
                    #Check each corner for intersection
                    ul = thing._pos - self._pos
                    ur = thing._pos - (self._pos + Vector2D(self._w, 0))
                    bl = thing._pos - (self._pos + Vector2D(0, self._h))
                    br = thing._pos - (self._pos + Vector2D(self._w, self._h))
                    corners = (ul, ur, bl, br)

                    for pos in corners:
                        if pos.magnitude() < thing._radius:
                            self._pos = self._pos - self._velocity
                            self._velocity *= -1
                            break
Beispiel #3
0
    def __init__(self, x, y):
        super().__init__()
        self.hoik_img = pygame.image.load('hoik.png').convert_alpha()
        pygame.display.get_surface().blit(self.hoik_img, (x, y))

        self.image = self.hoik_img
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.hoik_list = pygame.sprite.Group()
        self.position = Vector2D(self.rect.x, self.rect.y)
        self.velocity = Vector2D(0,0)
        self.radius = self.rect.x - self.rect.centerx
Beispiel #4
0
    def __init__(self, x, y):
        super().__init__()

        self.boid_img = pygame.image.load('boid.png').convert_alpha()
        pygame.display.get_surface().blit(self.boid_img, (x, y))

        self.image = self.boid_img
        #self.image.fill((random.randrange(10, 255), random.randrange(10, 255), random.randrange(10, 255)))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.boid_list = pygame.sprite.Group()
        self.position = Vector2D(self.rect.x, self.rect.y)
        self.velocity = Vector2D(0,0)
        self.radius = self.rect.x - self.rect.centerx
Beispiel #5
0
    def objectCollision(self, other):  #TODO: should be updated
        """Checks for collision between the rectangle and another object.

        other:  Another object. It is assumed that this is a rectangle or a circle.
                A type error will be raised if it isn't.

        Returns True if the rectangle collides with the other object. False otherwise.
        """

        if isinstance(other, Circle):  #TODO: See if this needs to be altered
            collision = precode.intersect_rectangle_circle(
                self._pos, self._w, self._h, other._pos, other._radius,
                Vector2D(1, 1))
        elif isinstance(other, Rectangle):
            collision = intersect_rectangles(self._pos, self._w, self._h,
                                             other._pos, other._w, other._h)
        else:
            raise TypeError(
                "other must be an object of class Rectangle, class Circle or a subclass of these."
            )

        if collision:
            return True
        else:
            return False
Beispiel #6
0
    def __init__(
        self, x, y, picture
    ):  #the picture argument is added so that I can load different images for each spaceship.
        """Initializing the Spaceship class"""
        super(Spaceship, self).__init__()

        self.img = pygame.image.load(
            picture).convert_alpha()  #loading the given spaceship image
        self.img = pygame.transform.scale(
            self.img,
            (70, 70
             ))  #scaling the picture so that any input image will be this size
        pygame.display.get_surface().blit(
            self.img, (x, y))  #blit the picture to the surface

        self.image = self.img
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.rect.center = (x, y)
        self.dir = 0
        self.velocity = Vector2D(0, 0)
        self.score = 0
        self.health = HEALTH_MAX
        self.fuel = FUEL_MAX
Beispiel #7
0
 def second_rule(self, liste):
     c = Vector2D(0, 0)
     for boid in liste:
         boid.radius = boid.rect.centerx - boid.rect.x
         if (boid.position - self.position).magnitude() < boid.radius * 5:
             c -= (boid.position - self.position)
     return c * 0.2
Beispiel #8
0
 def fourth_rule(self, liste, hoiks):
     c = Vector2D(0, 0)
     for boid in liste:
         boid.radius = boid.rect.centerx - boid.rect.x
         for hoik in hoiks:
             if (boid.position - hoik.position).magnitude() < 200:
                 c -= (boid.position - hoik.position)
     return c * 0.3
Beispiel #9
0
 def avoid_obstacles(self, liste, obstacles):
     c = Vector2D(0, 0)
     for boid in liste:
         boid.radius = boid.rect.centerx - boid.rect.x
         for obstacle in obstacles:
             if (boid.position - obstacle.position).magnitude() < 300:
                 c -= (boid.position - obstacle.position)
     return c
Beispiel #10
0
    def heading(self):
        """Determines the direction the car is pointing.

        Returns a Vector2D object representing a unit vector in that direction.
        """

        return (Vector2D(math.cos(self._rotation * math.pi / 180),
                         math.sin(self._rotation * math.pi / 180)))
Beispiel #11
0
def keep_centered(self, things):
    '''
    attempts to center the boid based on the list recieved
    '''
    centerall = Vector2D(0, 0)
    for i in things:
        centerall += i.pos
    centerall /= len(things)
    self.speed += (centerall - self.pos) / (CENTER * 10)
Beispiel #12
0
def gen_direction(self, things):
    '''
    attempts to modify the speed so that it
    matches the direction of the other boids in the list
    '''
    direction = Vector2D(0, 0)
    for i in things:
        direction += i.speed
    direction /= len(things)

    self.speed += direction / DIRECTION
Beispiel #13
0
    def __init__(self, x, y):
        super().__init__()
        self.obstacle_img = pygame.image.load('obstacle.png').convert_alpha()
        pygame.display.get_surface().blit(self.obstacle_img, (x, y))

        self.image = self.obstacle_img
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.obstacle_list = pygame.sprite.Group()
        self.position = Vector2D(self.rect.x, self.rect.y)
Beispiel #14
0
    def draw(self, layer, offset=Vector2D(0, 0)):
        """Draws the rectangle.

        layer:  The surface being drawn to.
        offset: An offset between the rectangle's position (absolute) and
                the position of the rectangle on a specific surface (relative).
        """

        pygame.draw.rect(layer, self._color,
                         (int(self._pos.x + offset.x),
                          int(self._pos.y + offset.y), self._w, self._h))
Beispiel #15
0
    def __init__(self, posx, posy, radius, color):
        """Create a circle.

        posx, posy: x- and y-components used for a Vector2D object
                    pointing to the center of the circle.
        radius:     Radius of the circle.
        color:      Color of the circle.
        """

        self._pos = Vector2D(posx, posy)
        self._radius = radius
        self._color = color
Beispiel #16
0
    def __init__(self, width, height, color, x=0, y=0):
        """Create a rectangle.

        x, y:           x- and y-components used for a Vector2D object
                        pointing to the upper left corner of the rectangle.
        width, height:  Width and height of the rectangle.
        color:          Color of the rectangle.
        """

        self._w = width  #width
        self._h = height  #height
        self._pos = Vector2D(x, y)  #coordinates for upper left corner
        self._color = color  #Frequently used colors should be defined in the config file
Beispiel #17
0
 def first_rule(self, liste):
     c = Vector2D(0,0)
     a = 0
     for boid in liste:
         if boid is not self:
             distance = (boid.position - self.position).magnitude()
             if distance < 500:
                 c += boid.position
                 a += 1
     if a > 0:      
         c *= (1.0/a)
         c = ((c - self.position) * 0.01)
     return c * 0.9
Beispiel #18
0
    def third_rule(self, liste):
        speedvector = Vector2D(0, 0)
        a = 0
        for boid in liste:
            if boid is not self:
                distance = (boid.position - self.position).magnitude()
                if distance < 400:
                    speedvector += boid.velocity
                    a += 1
        if (a > 0):
            speedvector = speedvector * (1 / a)

        return (speedvector - (self.velocity)) * (1/2)
Beispiel #19
0
    def __init__(self, posx, posy, length, width, angle, color):
        """Create a straight line.

        posx, posy: x- and y-components for the startpoint of the line.
        length:     Length of the line.
        width:      Width of the line.
        angle:      Clockwise angle relative to positive x.
        color:      Color of the line.
        """

        self._pos = Vector2D(posx, posy)
        self._length = length
        self._width = width
        self._angle = angle
        self._color = color
Beispiel #20
0
    def __init__(self, posx, posy, length, width, angle, span, color):
        """Create an arc.

        posx, posy: x and y-components for the center point.
        length:     Length from the centre point to the arc.
        width:      Width of the arc. Grows towards the center point.
        angle:      Clockwise angle relative to positive x for the start of the arc.
        span:       Number of radians the arc spans across.
        color:      Color of the arc.
        """

        self._pos = Vector2D(posx, posy)
        self._length = length
        self._width = width
        self._angle = angle
        self._span = span
        self._color = color
Beispiel #21
0
    def update(self):
        for hoik in self.hoik_list:
            v1 = Vector2D(4, 0)
            hoik.velocity += v1
            hoik.limit_velocity(self.hoik_list)
            hoik.rect.x += hoik.velocity.x
            hoik.rect.y += hoik.velocity.y
            hoik.position.y = hoik.rect.y
            hoik.position.x = hoik.rect.x

            if (hoik.rect.x > pygame.display.get_surface().get_width()):
                hoik.rect.x = 0
            elif (hoik.rect.x < 0):
                hoik.rect.x = pygame.display.get_surface().get_width()
            if (hoik.rect.y > pygame.display.get_surface().get_height()):
                hoik.rect.y = 0
            elif (hoik.rect.y < 0):
                hoik.rect.y = pygame.display.get_surface().get_height()
Beispiel #22
0
    def __init__(self, x, y, num):
        """Initializing the Bullet class"""
        super(Bullet, self).__init__()

        self.img = pygame.image.load(
            'pictures/boid2.png').convert_alpha()  #load image
        pygame.display.get_surface().blit(self.img, (x, y))  #blit image

        self.image = self.img
        if (num == 1):
            self.image.fill(
                RED
            )  #making sure that both players don't have bullets that look exactly the same, with color being the only difference.

        self.image = pygame.transform.rotate(
            self.img,
            Spaceship.list.sprites()[num].dir
        )  #this line of code shoots the bullets in the direction the given spaceship is pointed
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.velocity = Vector2D(
            (degrees(sin(radians(Spaceship.list.sprites()[num].dir)))) * -1,
            (degrees(cos(radians(Spaceship.list.sprites()[num].dir)))) * -1)