Esempio n. 1
0
    def __init__(self, res):
        self.res = res

        self.bords = [[-30, self.res[0] + 30], [-30, self.res[1] + 30]]

        self.player_bords = [[20, self.res[0] - 20], [20, self.res[1] - 20]]

        self.window_title = "SpaceShips Battle"
        self.is_running = True
        self.clock = pygame.time.Clock()

        self.bg = pygame.image.load("res/bg/fond.png")
        self.bg_img = pygame.transform.scale2x(self.bg)
        self.player_ship_img = pygame.image.load("res/sprites/spaceship.png")
        self.bullet_img = pygame.image.load("res/sprites/bullet.png")

        self.player = SpaceShip((self.res[0] / 2, self.res[1] / 2), 10,
                                self.player_ship_img, self.bullet_img)

        self.player_bullet_group = pygame.sprite.Group()

        self.enemy_manager = EnemyManager(self.res, self.player)

        self.score = 0
        self.score_font_size = 40
        self.score_text_pos = (self.res[0] / 2, 30)

        self.game_over_timer = 1
        self.game_over_timer_start = 0

        self.start()
Esempio n. 2
0
    def __init__(self):
        pygame.init()

        pygame.display.set_caption("SpaceInvaders")

        self.running = True

        self.screen = pygame.display.set_mode((500, 720))

        self.ship = SpaceShip(self.screen, self)

        self.aliens = []

        schedule.every(1).seconds.do(self.spawnAlien)

        self.gameLoop()
Esempio n. 3
0
 def __init__(self, game_engine):
     self.score = 0
     self.game_engine = game_engine
     self.heart_containers = []
     self.aliens = []
     self.alien_bullets = []
     self.bullets = []
     self.destroyed_aliens = []
     self.SPACESHIP_HIT = pygame.USEREVENT + 1  # event for player getting hit
     self.ALIEN_HIT = pygame.USEREVENT + 2  # event for player hitting alien
     self.REMOVE_ALIEN = pygame.USEREVENT + 3  # event for timer that removes alien from list of aliens to render
     self.spaceship = SpaceShip.SpaceShip(
         asset=self.game_engine.assets.get("spaceship"),
         ship_height=50,
         ship_width=40,
         starting_pos_x=self.game_engine.constants.get("WIDTH") / 2 - 50,
         starting_pos_y=self.game_engine.constants.get("HEIGHT") / 2 - 40,
         rotation=180)
Esempio n. 4
0
 def spawn_aliens(self, number_of_aliens):
     last_pos = 0
     flip = random.randint(0, 1)
     for x in range(number_of_aliens):
         alien_object = SpaceShip.Alien(
             asset=self.game_engine.assets.get("alien"),
             ship_height=60,
             ship_width=50,
             starting_pos_x=self.game_engine.constants.get("WIDTH") / 1.5 -
             last_pos,
             starting_pos_y=self.game_engine.constants.get("HEIGHT") - 500,
             rotation=0)
         last_pos += 80
         if flip == 1:
             alien_object.direction = 1
         else:
             alien_object.direction = -1
         self.aliens.append(alien_object)
Esempio n. 5
0
class Game:
    def __init__(self, res):
        self.res = res

        self.bords = [[-30, self.res[0] + 30], [-30, self.res[1] + 30]]

        self.player_bords = [[20, self.res[0] - 20], [20, self.res[1] - 20]]

        self.window_title = "SpaceShips Battle"
        self.is_running = True
        self.clock = pygame.time.Clock()

        self.bg = pygame.image.load("res/bg/fond.png")
        self.bg_img = pygame.transform.scale2x(self.bg)
        self.player_ship_img = pygame.image.load("res/sprites/spaceship.png")
        self.bullet_img = pygame.image.load("res/sprites/bullet.png")

        self.player = SpaceShip((self.res[0] / 2, self.res[1] / 2), 10,
                                self.player_ship_img, self.bullet_img)

        self.player_bullet_group = pygame.sprite.Group()

        self.enemy_manager = EnemyManager(self.res, self.player)

        self.score = 0
        self.score_font_size = 40
        self.score_text_pos = (self.res[0] / 2, 30)

        self.game_over_timer = 1
        self.game_over_timer_start = 0

        self.start()

    def start(self):
        pygame.init()
        self.screen = pygame.display.set_mode(self.res)
        pygame.display.set_caption(self.window_title)

        self.run()

    def run(self):
        while self.is_running:
            for evt in pygame.event.get():
                self.manage_events(evt)
            self.manage_pressed_keys()
            self.update()
        self.quit()

    def manage_events(self, evt):
        if evt.type == QUIT:
            self.is_running = False
        if evt.type == KEYDOWN:
            if evt.key == K_SPACE:
                bullet = self.player.fire()
                if bullet:
                    self.player_bullet_group.add(bullet)

    def manage_pressed_keys(self):
        pressed = pygame.key.get_pressed()

        vector = [0, 0]
        if pressed[K_q] or pressed[K_LEFT]:
            vector[0] -= 1
        if pressed[K_d] or pressed[K_RIGHT]:
            vector[0] += 1
        if pressed[K_z] or pressed[K_UP]:
            vector[1] -= 1
        if pressed[K_s] or pressed[K_DOWN]:
            vector[1] += 1

        self.player.move(vector[0], vector[1])

        # x
        if self.player.pos[0] < self.player_bords[0][0]:
            self.player.pos = (self.player_bords[0][0], self.player.pos[1])
        elif self.player.pos[0] > self.player_bords[0][1]:
            self.player.pos = (self.player_bords[0][1], self.player.pos[1])

        # y
        if self.player.pos[1] < self.player_bords[1][0]:
            self.player.pos = (self.player.pos[0], self.player_bords[1][0])
        elif self.player.pos[1] > self.player_bords[1][1]:
            self.player.pos = (self.player.pos[0], self.player_bords[1][1])

    def draw(self):
        self.screen.blit(self.player.image, self.player.rect)

        self.enemy_manager.draw(self.screen)

        self.player_bullet_group.draw(self.screen)

    def manage_collision(self):
        for enemy in self.enemy_manager.sprite_group.sprites():
            for bullet in pygame.sprite.spritecollide(enemy,
                                                      self.player_bullet_group,
                                                      False):
                enemy.kill()
                bullet.kill()
                enemy.play_explo_sound()
                del enemy
                del bullet
                score += 1

        for bullet in pygame.sprite.spritecollide(
                self.player, self.enemy_manager.bullet_group, False):
            bullet.kill()
            self.player.play_explo_sound()

            del bullet
            del self.player

            self.game_over()

    def draw_score(self):
        screen_text("Score : " + str(self.score), self.score_font_size,
                    pygame.Color(255, 255, 255, 255), self.screen,
                    self.score_text_pos)

    def clear_bullets(self, group):
        for bullet in group.sprites():
            if bullet.rect.centerx < self.bords[0][
                    0] or bullet.rect.centerx > self.bords[0][1]:
                group.remove(bullet)
            if bullet.rect.centery < self.bords[1][
                    0] or bullet.rect.centery > self.bords[1][1]:
                group.remove(bullet)
            if bullet not in group.sprites():
                del bullet

    def update(self):
        self.screen.blit(self.bg_img, (0, 0))

        self.clear_bullets(self.player_bullet_group)
        self.clear_bullets(self.enemy_manager.bullet_group)

        self.player.update()
        self.player_bullet_group.update()

        self.enemy_manager.update()

        self.draw()
        self.draw_score()

        self.manage_collision()

        self.clock.tick(50)
        pygame.display.update()

    def game_over(self):
        self.game_over_timer_start = time.time()

        while time.time() - self.game_over_timer_start < self.game_over_timer:
            self.screen.fill(pygame.Color(0, 0, 0, 255))

            screen_text("Game Over !!!", 70, pygame.Color(255, 0, 0, 255),
                        self.screen, (self.res[0] / 2, self.res[1] / 2))

            pygame.display.update()

        self.quit()

    def quit(self):
        pygame.display.quit()
        pygame.quit()
        del self
        quit()
Esempio n. 6
0
import numpy as np

import space_objects
import Weapon
import SpaceShip

name = 'LaserMkI'
w_dict = space_objects.weapons[name]
bw = Weapon.BeamWeapon(name,
                       **w_dict,
                       position=np.array([0, 0, 0]),
                       velocity_vec=np.array([1, 0, 0]))

######

hms = SpaceShip.SpaceShip('Cruiser')

a = 1
Esempio n. 7
0
import SpaceShip
import Missiles
import Aliens
from font import *

pygame.init()

DISPLAY = pygame.display.set_mode((800, 800))
displaywidth = 800
displayheight = 800
pygame.display.set_caption("Space Invaders")

ICON = pygame.image.load("./sprites/spaceship.png")
BACKGROUND = pygame.image.load("./sprites/Background.jpeg")

SHIP = SpaceShip.spaceship()

pygame.display.set_icon(ICON)

CLOCK = pygame.time.Clock()

def intro():
    '''Introduction Page'''
    intro = True
    while intro:
        DISPLAY.fill(BLACK)
        message_display("Welcome To The Game", GREEN, -100, "large")
        message_display("Press P to play and Q to Exit", RED, -50, "medium")
        message_display("Use A and D to Move The SpaceShip", RED, -25)
        message_display("Use S and Space Bar to Shoot", WHITE, 0)
        pygame.display.update()
Esempio n. 8
0
class Game:

    text = "text"

    def __init__(self):
        pygame.init()

        pygame.display.set_caption("SpaceInvaders")

        self.running = True

        self.screen = pygame.display.set_mode((500, 720))

        self.ship = SpaceShip(self.screen, self)

        self.aliens = []

        schedule.every(1).seconds.do(self.spawnAlien)

        self.gameLoop()

    def spawnAlien(self):
        alien = Alien(self.screen, self, random.randint(10, 480), 0)
        self.aliens.append(alien)
        print("Un alien spawned")

    def refreshAliens(self):

        for alien in self.aliens:
            if alien.rect.y < 720:
                alien.move()
                alien.refresh()
            else:
                self.aliens.remove(alien)

    def refreshHUD(self):
        font = pygame.font.SysFont("Arial", 25)
        healthInfo = font.render("Vie: " + str(self.ship.health), False,
                                 (255, 255, 255), (0, 0, 0))
        KillsInfo = font.render("Aliens: " + str(self.ship.kills), False,
                                (255, 255, 255), (0, 0, 0))
        BulletInfo = font.render("Munitions: " + str(self.ship.ammo), False,
                                 (255, 255, 255), (0, 0, 0))

        self.screen.blit(healthInfo, [10, 10, 100, 40])
        self.screen.blit(KillsInfo, [10, 40, 100, 40])
        self.screen.blit(BulletInfo, [10, 70, 100, 40])

    def gameLoop(self):

        #boucle de jeu

        while self.running:

            self.screen.fill((0, 0, 0))
            self.refreshHUD()
            self.ship.refresh(self.screen)
            self.refreshAliens()

            pygame.display.flip()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    running = False
                    pygame.quit()
                    print("Fermeture de la fenêtre")

                elif event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_RIGHT:
                        self.ship.left = False
                        self.ship.right = True
                    elif event.key == pygame.K_LEFT:
                        self.ship.right = False
                        self.ship.left = True

                    if event.key == pygame.K_UP:
                        self.ship.down = False
                        self.ship.up = True
                    elif event.key == pygame.K_DOWN:
                        self.ship.up = False
                        self.ship.down = True

                elif event.type == pygame.KEYUP:

                    if event.key == pygame.K_RIGHT:
                        self.ship.right = False
                    elif event.key == pygame.K_LEFT:
                        self.ship.left = False

                    if event.key == pygame.K_UP:
                        self.ship.up = False
                    elif event.key == pygame.K_DOWN:
                        self.ship.down = False

                    if event.key == pygame.K_SPACE:
                        self.ship.shootBullet()

            schedule.run_pending()
            pygame.time.delay(15)

        #end - Game Over
        self.screen.fill((0, 0, 0))
        font = pygame.font.SysFont("Arial", 40)
        gameOver = font.render("Game Over", False, (255, 255, 255), (0, 0, 0))
        self.screen.blit(gameOver, [180, 300, 100, 30])
        pygame.display.flip()
        os.system("PAUSE")
Esempio n. 9
0
def main():
    pygame.init()

    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

    pygame.display.set_caption("First game")

    clock = pygame.time.Clock()

    print("TEST")

    # SPACE
    spaceShip = SpaceShip.SpaceShip()

    spaceSprite = pygame.sprite.Group(spaceShip)

    # FIRS ENEMY
    enemies = []
    shift_x = 20
    shift_y = 15
    for i in range(10):
        enemies.append(Enemies.Enemy())
        enemies[i].set_pos(0+shift_x, shift_y)
        shift_x += 80

    shift_x = 60
    shift_y += 50
    for i in range(9):
        enemies.append(Enemies.Enemy())
        enemies[10+i].set_pos(0+shift_x, shift_y)
        shift_x += 80

    shift_x = 100
    shift_y += 50
    for i in range(8):
        enemies.append(Enemies.Enemy())
        enemies[i+19].set_pos(0+shift_x, shift_y)
        shift_x += 80

    enemiesSprite = pygame.sprite.Group(enemies)

    # GAME LOOP
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    spaceShip.go_left()
                if event.key == pygame.K_RIGHT:
                    spaceShip.go_right()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and spaceShip.change_x < 0:
                    spaceShip.stop()
                if event.key == pygame.K_RIGHT and spaceShip.change_x > 0:
                    spaceShip.stop()

        screen.fill((0, 0, 0))

        spaceSprite.update()

        spaceSprite.draw(screen)

        enemiesSprite.update()

        enemiesSprite.draw(screen)

        clock.tick(60)

        pygame.display.flip()

    pygame.quit()