Example #1
0
class Asteroids( Game ):
    """
    Asteroids extends the base class Game to provide logic for the specifics of the game
    """
    def __init__(self, name, width, height):
        super().__init__( name, width, height )
        self.ship = Ship()  # Creates a ship
        self.asteroids=[]   # A list of all asteroids
        for i in range(4):                  # Change for different amount of Asteroids
            self.asteroids.append(Asteroid(random.randrange(0, 1280, 5),random.randrange(0, 720, 5)))
        self.stars=[]       # A list of all background stars
        for i in range(50):                # Change for different amount of background Stars
            self.stars.append(Star())
        self.bullets = []   # A list of all bullets

    def handle_input(self):
        super().handle_input()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[K_LEFT] and self.ship:
            self.ship.rotate(-1)
        if keys_pressed[K_RIGHT] and self.ship:
            self.ship.rotate(1)
        if keys_pressed[K_UP] and self.ship:
            self.ship.accelerate(0.05)
        if keys_pressed[K_DOWN] and self.ship:
            self.ship.accelerate(0) #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE.
        if keys_pressed[K_SPACE] and self.ship:
            if len(self.bullets) == 0:
                self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))
            else:
                if self.bullets[len(self.bullets)-1].ttl > 50:
                    self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))

            # Försöker få delay i bullet interval
            if len(self.bullets) >= 10:
                del self.bullets[0]
                self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))



    def update_simulation(self):
        """
        update_simulation() causes all objects in the game to update themselves
        """
        super().update_simulation()

        if self.ship:
            self.ship.update( self.width, self.height )
        for asteroid in self.asteroids:
            asteroid.update( self.width, self.height )
        for star in self.stars:
            star.update( self.width, self.height )
        for bullet in self.bullets:
            bullet.update( self.width, self.height)
            if bullet.ttl + 100 < self.frame:
                self.bullets.pop(self.bullets.index(bullet))

        self.handle_collisions()

    def render_objects(self):
        """
        render_objects() causes all objects in the game to draw themselves onto the screen
        """
        super().render_objects()
        # Render the ship:
        if self.ship:
            self.ship.draw( self.screen )
        # Render all the stars, if any:
        for star in self.stars:
            star.draw( self.screen )
        # Render all the asteroids, if any:
        for asteroid in self.asteroids:
            asteroid.draw( self.screen )
        # Render all the bullet, if any:
        for bullet in self.bullets:
            bullet.draw( self.screen )


    def handle_collisions(self):
        if self.ship:
            for asteroid in self.asteroids:
                if asteroid.collide(self.ship):
                    self.running = False
                for bullet in self.bullets:
                    if asteroid.contains(bullet.position):
                        self.bullets.pop(self.bullets.index(bullet))
                        self.asteroids.pop(self.asteroids.index(asteroid))
                        if asteroid.health >= 2:
                            self.asteroids.append(Debris(asteroid.position))
                            self.asteroids.append(Debris(asteroid.position))
                            self.asteroids.append(Debris(asteroid.position))
                            self.asteroids.append(Debris(asteroid.position))
                        break
Example #2
0
class Asteroids(Game):

    def __init__(self, name, width, height):
        super().__init__( name, width, height )
        self.ship = Ship()  # Creates a ship
        self.asteroids=[]   # A list of all asteroids
        for i in range(5):                  # Change for different amount of Asteroids
            self.asteroids.append(Asteroid(random.randrange(0, width, 5),random.randrange(0, height, 5)))
        self.stars=[]       # A list of all background stars
        for i in range(25):                # Change for different amount of background Stars
            self.stars.append(Star())
        self.bullets = []   # A list of all bullets
        self.score = 0 # Possible score variable

    def handle_input(self):
        super().handle_input()
        pygame.key.set_repeat()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[K_LEFT] and self.ship:
            self.ship.rotate(-1)
        if keys_pressed[K_RIGHT] and self.ship:
            self.ship.rotate(1)
        if keys_pressed[K_UP] and self.ship:
            self.ship.accelerate(0.05)
        if keys_pressed[K_DOWN] and self.ship:
            self.ship.accelerate(0) #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE.
        if keys_pressed[K_SPACE] and self.ship:
            if len(self.bullets) >= 15:
                del self.bullets[0]
                self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))
            else:
                self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))
    """         if len(self.bullets) == 0:
                self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))
            else:
                if self.bullets[len(self.bullets)-1].ttl > 50:
                    self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))

            # Försöker få delay i bullet interval
             """



    def update_simulation(self):
        """
        update_simulation() causes all objects in the game to update themselves
        """
        super().update_simulation()

        if self.ship:
            self.ship.update( self.width, self.height )
        for asteroid in self.asteroids:
            asteroid.update( self.width, self.height )
        for star in self.stars:
            star.update( self.width, self.height )
        for bullet in self.bullets:
            bullet.update( self.width, self.height )
            if bullet.ttl + 30 < self.frame:
                self.bullets.pop(self.bullets.index(bullet))
        self.handle_collisions()

    def render_objects(self):
        """
        render_objects() causes all objects in the game to draw themselves onto the screen
        """
        super().render_objects()
        # Render the ship:
        if self.ship:
            self.ship.draw( self.screen )
        # Render all the stars, if any:
        for star in self.stars:
            star.draw( self.screen )
        # Render all the asteroids, if any:
        for asteroid in self.asteroids:
            asteroid.draw( self.screen )
        # Render all the bullet, if any:
        for bullet in self.bullets:
            bullet.draw( self.screen )


    def handle_collisions(self):
        if self.ship:
            for asteroid in self.asteroids:
                if asteroid.collide(self.ship):
                    self.death_screen()
                for bullet in self.bullets:
                    if asteroid.contains(bullet.position):
                        self.score += 10
                        self.bullets.pop(self.bullets.index(bullet))
                        self.asteroids.pop(self.asteroids.index(asteroid))
                        if asteroid.health >= 2:
                            self.asteroids.append(Debris(asteroid.position))
                            self.asteroids.append(Debris(asteroid.position))
                            self.asteroids.append(Debris(asteroid.position))
                            self.asteroids.append(Debris(asteroid.position))

    def death_screen(self):
        game = Asteroids("Asteroids", 640, 480)
        label = self.myfont.render("Bitch Please!", 1, (8, 8, 8))
        label2 = self.myfont.render("You Died!", 1, (255, 255, 255))
        score = self.smallfont.render("Score:" + str(self.score), 1, (255, 255, 255))
        self.screen.blit(label, (self.width * 0.30, self.height * 0.35))
        self.screen.blit(label2, (self.width * 0.35, self.height * 0.40))
        self.screen.blit(score, (self.width * 0.4, self.height * 0.5))
        pygame.display.update()
        pygame.time.wait(2000)
        pygame.time.wait(500)
        game.runGame()
Example #3
0
class Asteroids(Game):
    def __init__(self, name, width, height):
        super().__init__(name, width, height)
        self.width = width
        self.height = height
        self.ship = Ship()  # Creates a ship
        self.asteroids = []  # A list of all asteroids
        for i in range(5):  # Change for different amount of Asteroids
            self.asteroids.append(
                Asteroid(random.randrange(0, width, 5),
                         random.randrange(0, height, 5)))
        self.stars = []  # A list of all background stars
        for i in range(25):  # Change for different amount of background Stars
            self.stars.append(Star())
        self.bullets = []  # A list of all bullets
        self.score = 0  # Possible score variable

    def handle_input(self):
        super().handle_input()
        pygame.key.set_repeat(0, 100)
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[K_LEFT] and self.ship:
            self.ship.rotate(-3)
        if keys_pressed[K_RIGHT] and self.ship:
            self.ship.rotate(3)
        if keys_pressed[K_UP] and self.ship:
            self.ship.accelerate(0.05)
        if keys_pressed[K_DOWN] and self.ship:
            self.ship.accelerate(
                0
            )  #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE.
        if keys_pressed[K_SPACE] and self.ship:
            if time.time(
            ) - self.ship.shot_timer > self.ship.shot_delay:  #Limits the rate of fire. Cannot fire more often than shot_delay value
                self.ship.shot_timer = time.time(
                )  #if it shoots, saves last fired timestamp
                self.ship.spawnProtection = False  #removes Spawn protection if bullet is fired
                if len(
                        self.bullets
                ) >= 15:  #Does not allow more than 15 bullets in total. deletes the oldest if more than 15.
                    del self.bullets[0]
                    self.bullets.append(
                        Bullet(self.ship.position.copy(), self.ship.rotation,
                               self.ship.shot_timer)
                    )  #Spawns a bullet with ships location. rotation and timestamp when fired.
                else:
                    self.bullets.append(
                        Bullet(self.ship.position.copy(), self.ship.rotation,
                               self.ship.shot_timer))
        if keys_pressed[K_f] and self.ship:
            self.asteroids.append(
                Asteroid(
                    random.randrange(0, self.width, 5),
                    random.randrange(0, self.height,
                                     5)))  #Command for spawning more asteroids
        if keys_pressed[K_t] and self.ship:
            if time.time(
            ) - self.ship.jump_timer > self.ship.jump_delay:  #Checks if jumpdrive is on cooldown
                self.ship.jump_timer = time.time()  #Saves timestamp for jump
                self.ship.jumpDrive()  #Jumps the ship

    def update_simulation(self):
        """
        update_simulation() causes all objects in the game to update themselves
        """
        super().update_simulation()
        currentTime = time.time()  #Saves current timestamp for the update

        if self.ship:
            self.ship.update(self.width, self.height, self.dt)
        for asteroid in self.asteroids:
            asteroid.update(self.width, self.height, self.dt)
        for star in self.stars:
            star.update(self.width, self.height, self.dt)
        for bullet in self.bullets:
            bullet.update(self.width, self.height, self.dt)
            if bullet.time + 2 < currentTime:  #Deletes bullets that is older than 2 seconds
                self.bullets.pop(self.bullets.index(bullet))
        if self.ship.jump_timer + self.ship.jumpProtectionDuration < currentTime:  #Checks if jump protection is still active. if not, remove it
            self.ship.jumpProtection = False
        if self.ship.spawnProtectionTime + self.ship.spawnProtectionDuration < currentTime:
            self.ship.spawnProtection = False  #Checks if spawn protection is still active. if not, remove it
        self.handle_collisions()

    def render_objects(self):
        """
        render_objects() causes all objects in the game to draw themselves onto the screen
        """
        super().render_objects()
        # Render the ship:
        if self.ship:
            self.ship.draw(self.screen)
        # Render all the stars, if any:
        for star in self.stars:
            star.draw(self.screen)
        # Render all the asteroids, if any:
        for asteroid in self.asteroids:
            asteroid.draw(self.screen)
        # Render all the bullet, if any:
        for bullet in self.bullets:
            bullet.draw(self.screen)

    def handle_collisions(self):
        if self.ship:
            for asteroid in self.asteroids:
                if asteroid.collide(
                        self.ship
                ) and self.ship.spawnProtection == False and self.ship.jumpProtection == False:  #Checks if player is protected when colliding
                    self.ship.lives = -1  #NOT WORKING YET, Player has 3 lives. Plan is to do remove a life and gain 2-3 seconds immunity when losing a life.
                    if self.ship.lives < 0:  #If no more lives yet, > player dead
                        self.death_screen()
                    elif self.ship.lives > 1:
                        self.ship.spawnProtection = True
                        self.ship.spawnProtectionTime = time.time()

                for bullet in self.bullets:
                    if asteroid.contains(bullet.position):
                        self.score += 10
                        self.bullets.pop(self.bullets.index(bullet))
                        self.asteroids.pop(self.asteroids.index(asteroid))
                        if asteroid.health >= 2:
                            self.asteroids.append(
                                Debris(asteroid.position.copy()))
                            self.asteroids.append(
                                Debris(asteroid.position.copy()))
                            self.asteroids.append(
                                Debris(asteroid.position.copy()))
                            self.asteroids.append(
                                Debris(asteroid.position.copy()))
            #if asteroid.collide(self.asteroids):

    def death_screen(self):
        game = Asteroids("Asteroids", 1280, 720)
        label = self.myfont.render("Lives:" + str(self.ship.lives), 1,
                                   (255, 255, 255))
        label2 = self.myfont.render("You Died!", 1, (255, 255, 255))
        score = self.smallfont.render("Score:" + str(self.score), 1,
                                      (255, 255, 255))
        self.screen.blit(label, (self.width * 0.30, self.height * 0.35))
        self.screen.blit(label2, (self.width * 0.35, self.height * 0.40))
        self.screen.blit(score, (self.width * 0.4, self.height * 0.5))
        pygame.display.update()
        pygame.time.wait(2000)
        pygame.time.wait(500)
        for asteroids in self.asteroids:
            self.asteroids.pop(self.asteroids.index(asteroids))
        for bullet in self.bullets:
            self.bullets.pop(self.bullets.index(bullet))
        game.runGame()
Example #4
0
class Asteroids(Game):
    """
    Asteroids extends the base class Game to provide logic for the specifics of the game
    """
    def __init__(self, name, width, height):
        super().__init__(name, width, height)
        # TODO: should create a Ship object here
        self.ship = Ship()  # None
        # TODO: should create asteroids
        self.asteroids = []
        for i in range(8):  # Change for different amount of Asteroids
            self.asteroids.append(Asteroid())
        self.stars = []
        for i in range(250):  # Change for different amount of background Stars
            self.stars.append(Star())
        # TODO: should create bullets
        self.bullets = []

    def handle_input(self):
        super().handle_input()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[K_LEFT] and self.ship:
            self.ship.rotate(-0.5)
        if keys_pressed[K_RIGHT] and self.ship:
            self.ship.rotate(0.5)
        if keys_pressed[K_UP] and self.ship:
            self.ship.accelerate(0.005)
        if keys_pressed[K_DOWN] and self.ship:
            self.ship.accelerate(
                0
            )  #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE.
            pass
        if keys_pressed[K_SPACE] and self.ship:
            if len(self.bullets) < 10:
                self.bullets.append(
                    Bullet(self.ship.position, self.ship.rotation))
            elif len(self.bullets) >= 10:
                del self.bullets[0]
                self.bullets.append(
                    Bullet(self.ship.position, self.ship.rotation))
                # TODO: should create a bullet when the user fires

    def update_simulation(self):
        """
        update_simulation() causes all objects in the game to update themselves
        """
        super().update_simulation()

        if self.ship:
            self.ship.update(self.width, self.height)
        for asteroid in self.asteroids:
            asteroid.update(self.width, self.height)
        for star in self.stars:
            star.update(self.width, self.height)
        for bullets in self.bullets:
            bullets.update(self.width, self.height)
        # TODO: should probably work out how to remove a bullet when it gets old
        self.handle_collisions()

    def render_objects(self):
        """
        render_objects() causes all objects in the game to draw themselves onto the screen
        """
        super().render_objects()
        # Render the ship:
        if self.ship:
            self.ship.draw(self.screen)
        # Render all the stars, if any:
        for star in self.stars:
            star.draw(self.screen)
        # Render all the asteroids, if any:
        for asteroid in self.asteroids:
            asteroid.draw(self.screen)
        # Render all the bullet, if any:
        for bullet in self.bullets:
            bullet.draw(self.screen)

    def handle_collisions(self):
        if self.ship:
            for asteroid in self.asteroids:
                if asteroid.collide(self.ship):
                    self.running = False
                for bullet in self.bullets:
                    if asteroid.contains(bullet.position):
                        self.bullets.pop(self.bullets.index(bullet))
                        self.asteroids.pop(self.asteroids.index(asteroid))
Example #5
0
class Asteroids(Game):
    """
    Asteroids extends the base class Game to provide logic for the specifics of the game
    """
    def __init__(self, name, width, height):
        super().__init__(name, width, height)

        self.ship = Ship()  # None  # TODO: should create a Ship object here
        # TODO: should create asteroids
        self.asteroids = []
        # TODO: should create stars
        self.stars = []
        self.bullets = []

    def handle_input(self):
        super().handle_input()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[K_LEFT] and self.ship:
            self.ship.rotate(-0.1)
        if keys_pressed[K_RIGHT] and self.ship:
            self.ship.rotate(0.1)
        if keys_pressed[K_UP] and self.ship:
            self.ship.accelerate(0.0001)
        if keys_pressed[K_DOWN] and self.ship:
            self.ship.accelerate(0)
        if keys_pressed[K_SPACE] and self.ship:
            #self.bullet TODO: should create a bullet when the user fires
            pass

    def update_simulation(self):
        """
        update_simulation() causes all objects in the game to update themselves
        """
        super().update_simulation()

        if self.ship:
            self.ship.update(self.width, self.height)
        for asteroid in self.asteroids:
            asteroid.update(self.width, self.height)
        for star in self.stars:
            star.update(self.width, self.height)
        # TODO: should probably call update on our bullet/bullets here
        # TODO: should probably work out how to remove a bullet when it gets old
        self.handle_collisions()

    def render_objects(self):
        """
        render_objects() causes all objects in the game to draw themselves onto the screen
        """
        super().render_objects()
        # Render the ship:
        if self.ship:
            self.ship.draw(self.screen)
        # Render all the stars, if any:
        for star in self.stars:
            star.draw(self.screen)
        # Render all the asteroids, if any:
        for asteroid in self.asteroids:
            asteroid.draw(self.screen)
        # Render all the bullet, if any:
        for bullet in self.bullets:
            bullet.draw(self.screen)

    def handle_collisions(self):
        """
        handle_collisions() should check:
            - if our ship has crashed into an asteroid (the ship gets destroyed - game over!)
            - if a bullet has hit an asteroid (the asteroid gets destroyed)
        :return: 
        """
        # TODO: implement collission detection,
        #       using the collission detection methods in all of the shapes
        pass