Exemple #1
0
    def __init__(self):
        self.window_size = (1000, 700)
        self.window = pygame.display.set_mode(self.window_size, pygame.RESIZABLE)
        self.todo_menu = TodoMenu(self)

        self.font = pygame.font.SysFont('consolas', 40)
        
        self.characters = { # Add player buttons
            'dave': Character(50, 10, 950, 1, exp_color = (0, 0, 255), sprites = [
                pygame.transform.scale(load_sprite('characters\\dave\\1.png', (255, 255, 255)), (500, 500)),
                pygame.transform.scale(load_sprite('characters\\dave\\2.png', (255, 255, 255)), (500, 500)),
                pygame.transform.scale(load_sprite('characters\\dave\\3.png', (255, 255, 255)), (500, 500)),
                pygame.transform.scale(load_sprite('characters\\dave\\4.png', (255, 255, 255)), (500, 500))
            ])
        }
        
        self.current_character = self.characters['dave']

        self.buttons = ButtonContainer([
                Button(10, 10, text = 'Todo menu', command = self.todo_menu.main, font = pygame.font.SysFont('consolas', 20), fg = (255, 255, 255),
                       show_box = False),
        ])

        self.particles = ParticleContainer()

        self.last_time = pygame.time.get_ticks()
Exemple #2
0
 def __init__(self):
     self._init_pygame()
     self.screen = pygame.display.set_mode((800, 600))
     self.background = load_sprite("elon.jpg", False)
     self.spaceship = GameObject((400, 300), load_sprite("spaceship.png"),
                                 (0, 0))
     self.asteroid = GameObject((400, 300), load_sprite("asteroid.png"),
                                (-1, 0))
Exemple #3
0
    def _process_game_logic(self):
        for bullet in self.bullets:
            bullet.move()

        if self.alien_cluster:
            free_aliens = []
            last_strikes = []
            for alien in self.alien_cluster:
                last_strikes.append(alien.frames_from_last_strike)
                if alien.frames_from_last_strike > alien.STRIKE_TIME:
                    alien.sprite = load_sprite("alien")
                    free_aliens.append(alien)
                else:
                    alien.sprite = load_sprite("alien_dropped")
                alien.frames_from_last_strike += 1

            if free_aliens:
                if min(last_strikes) > self.ALIEN_DROP_SPEED:
                    free_aliens[randrange(len(free_aliens))].drop()

        for alien_bullet in self.alien_bullets:
            alien_bullet.move(up=False)

        for bullet in self.bullets[:]:
            if not self.screen.get_rect().collidepoint((bullet.x, bullet.y)):
                self.bullets.remove(bullet)

        for alien_bullet in self.alien_bullets[:]:
            if not self.screen.get_rect().collidepoint(
                (alien_bullet.x, alien_bullet.y)):
                self.alien_bullets.remove(alien_bullet)

        if self.alien_cluster:
            for bullet in self.bullets[:]:
                for alien in self.alien_cluster[:]:
                    if bullet.collides_with(alien):
                        self.bullets.remove(bullet)
                        self.alien_cluster.remove(alien)

        if self.cannon:
            for alien_bullet in self.alien_bullets[:]:
                if alien_bullet.collides_with(self.cannon):
                    self.alien_bullets.remove(alien_bullet)
                    self.cannon = None
                    self.message = "You lost"
                    self.color = "tomato"
                    break

        if not self.alien_cluster and self.cannon:
            self.message = "You won"
            self.color = "green"
Exemple #4
0
    def __init__(self):
        self._init_pygame()
        self.screen = screenSize(1000, 700)
        self.background = load_sprite("space", False)
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 64)
        self.message = ""
        self.destroyed_small = 0
        self.score = 0
        self.scoretext = self.font.render("Score = " + str(self.score), 1,
                                          (255, 0, 0))

        file = 'C://Users//ActionICT//PycharmProjects//Asteroids//assets//music//jlbrock44_-_Stars_Below_Us.mp3'
        pygame.init()
        pygame.mixer.init()
        pygame.mixer.music.load(file)
        pygame.mixer.music.play(
            -1)  # If the loops is -1 then the music will repeat indefinitely.

        self.asteroids = []
        self.bullets = []
        self.spaceship = Spaceship((400, 300), self.bullets.append)

        for _ in range(6):
            while True:
                position = get_random_position(self.screen)
                if (position.distance_to(self.spaceship.position) >
                        self.MIN_ASTEROID_DISTANCE):
                    break

            self.asteroids.append(Asteroid(position, self.asteroids.append))
Exemple #5
0
    def __init__(self):
        self._init_pygame()
        # Adjust Screen Size
        self.screen = pygame.display.set_mode((800, 600))
        # Customize Game Background
        self.background = load_sprite("industrial-background", False)
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 64)
        self.message = ""

        self.asteroids = []
        self.bullets = []
        # Adjust player starting position: Spaceship((x, y))
        # Center of screen: (400, 300)
        # Far left center of screen: (20, 300)
        self.spaceship = Spaceship((100, 490), self.bullets.append)

        # Adjust number fo initial spawned asteroids: for _ in range(x)
        for _ in range(6):
            while True:
                position = get_random_position(self.screen)
                if (position.distance_to(self.spaceship.position) >
                        self.MIN_ASTEROID_DISTANCE):
                    break

            self.asteroids.append(Asteroid(position, self.asteroids.append))
Exemple #6
0
    def __init__(self, position, create_bullet_callback):
        self.create_bullet_callback = create_bullet_callback
        self.laser_sound = load_sound("laser")
        # Make a copy of the original UP vector
        self.direction = Vector2(UP)

        super().__init__(position, load_sprite("spaceship"), Vector2(0))
    def __init__(self, position, create_bullet_callback):
        self.create_bullet_callback = create_bullet_callback
        self.blaster_sound = load_sound("blaster")
        # Make a copy of the original UP vector
        self.direction = Vector2(LEFT_TO_RIGHT)

        super().__init__(position, load_sprite("disinfector"), Vector2(0))
    def __init__(self, position, create_asteroid_callback, size=3):
        self.create_asteroid_callback = create_asteroid_callback
        self.size = size

        size_to_scale = {3: 1.0, 2: 0.5, 1: 0.25}
        scale = size_to_scale[size]
        sprite = rotozoom(load_sprite("asteroid"), 0, scale)

        super().__init__(position, sprite, get_random_velocity(1, 3))
Exemple #9
0
 def __init__(self, position, create_bullet_callback):
     # Create a callback for bullets
     self.create_bullet_callback = create_bullet_callback
     # Load the sound file for the lasers
     self.laser_sound = load_sound("laser")
     # Add a direction to the spaceship, make a copy of the constant UP
     self.direction = Vector2(UP)
     # Call to parent class constructor
     super().__init__(position, load_sprite("spaceship"), Vector2(0))
Exemple #10
0
    def __init__(self):
        self._init_pygame()
        self.screen = pygame.display.set_mode((800, 600))

        self.bullets = []
        self.bullet_shot = False

        self.alien_bullets = []

        self.alien_cluster = [Alien((50 + (50+10)*k, 100), load_sprite("alien"), self.alien_bullets.append) for k in range(12)] +\
                            [Alien((50 + (50+10)*k, 150), load_sprite("alien"), self.alien_bullets.append) for k in range(12)] +\
                            [Alien((50 + (50+10)*k, 200), load_sprite("alien"), self.alien_bullets.append) for k in range(12)]

        canon_sprite = load_sprite("cannon")
        self.cannon = Cannon(
            (self.screen.get_rect().width / 2 -
             canon_sprite.get_rect().width / 2, self.screen.get_rect().height -
             canon_sprite.get_rect().height - 10), canon_sprite,
            self.bullets.append)

        self.clock = pygame.time.Clock()

        self.font = pygame.font.Font(None, 64)
        self.message = ""
Exemple #11
0
 def _handle_input(self):
     for event in pygame.event.get():
         if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN
                                          and event.key == pygame.K_ESCAPE):
             pygame.quit()
             self.camera.release()
             cv2.destroyAllWindows()
             sys.exit()
         if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
             self.ball = Ball(
                 Vector2(SCREEN_RES) / 2,
                 load_sprite("ball"),
                 random_velocity(15, 30),
             )
     return
Exemple #12
0
    def __init__(self):
        self._init_pygame()
        self.screen = pygame.display.set_mode(SCREEN_RES)
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 64)
        self.message = ""

        self.ball = Ball(
            Vector2(SCREEN_RES) / 2, load_sprite("ball"),
            random_velocity(15, 30))
        self.camera = cv2.VideoCapture(0)
        self.camera.set(3, SCREEN_RES[0])
        self.camera.set(4, SCREEN_RES[1])
        self.frame = None
        self.finger_coords = []
    def __init__(self, position, create_asteroid_callback, size=3):
        self.create_asteroid_callback = create_asteroid_callback
        self.size = size

        size_to_scale = {
            3: 1,
            2: 0.5,
            1: 0.25,
        }
        scale = size_to_scale[size]
        sprite = rotozoom(load_sprite("happy-covid"), 0, scale)

        super().__init__(
            # Adjust Possible Asteroid Velocity Range: get_random_velocity(x, y)
            position, sprite, get_random_velocity(1, 3) # (1, 3)
        )
Exemple #14
0
    def __init__(self):
        self._init_pygame()
        self.screen = pygame.display.set_mode((800, 600))
        self.background = load_sprite("space", False)
        self.clock = pygame.time.Clock()

        self.asteroids = []
        self.spaceship = Spaceship((400, 300))

        for _ in range(6):
            while True:
                position = get_random_position(self.screen)
                if (position.distance_to(self.spaceship.position) >
                        self.MIN_ASTEROID_DISTANCE):
                    break

            self.asteroids.append(Asteroid(position))
Exemple #15
0
    def __init__(self) -> None:
        self._init_pygame()
        self.screen = pygame.display.set_mode((800, 600))
        self.background = load_sprite(name='space', with_alpha=False)
        self.bullets = []
        self.asteroids = []
        self.spaceship = Spaceship((400, 300), self.bullets.append)
        for _ in range(6):
            while True:
                position = get_random_position(self.screen)
                if (position.distance_to(self.spaceship.position) >
                        self.MIN_ASTEROID_DISTANCE):
                    break
            self.asteroids.append(Asteroid(position, self.asteroids.append))

        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 64)
        self.message = ''
Exemple #16
0
    def __init__(self):
        # Call a private initialization method
        self._init_pygame()
        self.screen = pygame.display.set_mode((800, 600))
        self.background = load_sprite("space", False)
        self.clock = pygame.time.Clock()  # Ensure stable FPS
        self.font = pygame.font.Font(None, 64)
        self.message = ""

        self.asteroids = []
        self.bullets = []
        self.spaceship = Spaceship((400, 300), self.bullets.append)

        for _ in range(6):
            while True:
                position = get_random_position(self.screen)
                if (position.distance_to(self.spaceship.position) >
                        self.MIN_ASTEROID_DISTANCE):
                    break

            self.asteroids.append(Asteroid(position, self.asteroids.append))
Exemple #17
0
    def __init__(self):
        self._init_pygame()
        self.screen = pygame.display.set_mode((800, 600))
        self.background = load_sprite("space", False)
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 64)
        self.message = ""

        self.asteroids = []
        self.bullets = []
        self.spaceship = Spaceship((400, 300), self.bullets.append)

        for _ in range(6):
            while True:
                position = get_random_position(self.screen)
                if (
                    position.distance_to(self.spaceship.position)
                    > self.min_distance
                ):
                    break

            self.asteroids.append(Asteroid(position, self.asteroids.append))
Exemple #18
0
 def __init__(self):
     self._init_pygame()
     self.screen = pygame.display.set_mode((800, 600))
     self.background = load_sprite("nebula.jpg", False)
 def __init__(self, position, create_bullet_callback):
     self.create_bullet_callback = create_bullet_callback
     self.laser_sound = load_sound("laser")
     UP = Vector2(0, -1)
     self.direction = Vector2(UP)
     super().__init__(position, load_sprite("spaceship"), Vector2(0))
Exemple #20
0
    def __init__(self, position):
        # Make a copy of the original UP vector
        self.direction = Vector2(UP)

        super().__init__(position, load_sprite("spaceship"), Vector2(0))
Exemple #21
0
 def __init__(self, position):
     super().__init__(
         position, load_sprite("asteroid"), get_random_velocity(1, 3)
     )
Exemple #22
0
 def __init__(self, position, velocity):
     super().__init__(position, load_sprite("bullet"), velocity)
Exemple #23
0
 def __init__(self, position, create_bullet_callback):
     self.create_bullet_callback = create_bullet_callback
     self.laser_sound = load_sound("laser")
     self.direction = Vector2(UP)
     sprite = rotozoom(load_sprite("puppy"), 0, .3)
     super().__init__(position, sprite, Vector2(0))
Exemple #24
0
    def __init__(self, position):
        self.direction = Vector2(UP)

        super().__init__(position, load_sprite("ship"), Vector2(0))
Exemple #25
0
 def shoot(self):
     bullet_sprite = load_sprite("bullet")
     bullet = Bullet((self.x + self.sprite.get_rect().width/2-bullet_sprite.get_rect().width/2, self.y), bullet_sprite)
     self.create_bullet_callback(bullet)
Exemple #26
0
	def __init__(self):
		self._init_pygame()
		self.screen = pygame.display.set_mode((800, 600))
		self.background = load_sprite("space", False)
		self.clock = pygame.time.Clock()
		self.spaceship = SpaceShip((400, 300))
Exemple #27
0
 def drop(self):
     self.frames_from_last_strike = 0
     bullet_sprite = load_sprite("alien_bullet")
     bullet = Bullet((self.x + self.sprite.get_rect().width/2-bullet_sprite.get_rect().width/2, self.y), bullet_sprite)
     #self.create_bullet_callback(bullet)