コード例 #1
0
def run_game():
	# Initialize pygame, settings, and screen object.
	pygame.init()
	ai_settings = Settings()
	screen = pygame.display.set_mode(
		(ai_settings.screen_width, ai_settings.screen_height))
	pygame.display.set_caption("Alien Invasion")

	# Make a ship.
	ship = Ship(ai_settings, screen)
	# Make a group to store bullets in.
	bullets = Group()
	# Make an alien.
	alien = Alien(ai_settings, screen)

	# Start the main loop for the game.
	while True:

		# Watch for keyboard and mouse events.
		gf.check_events(ai_settings, screen, ship, bullets)
		ship.update()
		gf.update_bullets(bullets)

		# Redraw the screen during each pass through the loop.
		gf.update_screen(ai_settings, screen, ship, alien, bullets)

		# Make the most recently drawn screen visible.
		pygame.display.flip()
コード例 #2
0
ファイル: main.py プロジェクト: Bitos88/MyGame
def runGame():  #funcion que iniciara el juego
    pygame.init()
    aiSettings = Config()
    screen = pygame.display.set_mode(
        (aiSettings.screenW, aiSettings.screenH
         ))  #instanciamos el objeto para definir las dimensiones de la ventana
    imagen_defondo = pygame.image.load("images/space.jpg").convert()
    rectangulo = imagen_defondo.get_rect()
    screen.blit(imagen_defondo, (0, 0))
    pygame.display.set_caption("ALIEN INVASION")  #Titulo de nuestra ventana
    pygame.mixer.init()
    pygame.display.init()

    bgCOLOR = (230, 230, 230)

    ship = Ship(aiSettings, screen)
    meteor = Meteor(aiSettings, screen)
    bullets = Group()
    meteoritos = Group()

    tiempo = 1
    puntuacion = 0

    letra = pygame.font.SysFont("Arial", 18)

    while True:

        if tiempo == 1 or tiempo % 100 == 0:
            meteor = Meteor(aiSettings, screen)
            meteoritos.add(meteor)

        gf.check_events(ship, aiSettings, screen, bullets)
        ship.update()
        bullets.update()
        meteoritos.update()
        gf.updateScreen(aiSettings, screen, ship, meteoritos, bullets)

        ship.animation()

        for bullet in bullets.copy():
            if bullet.rect.left >= 1200:
                bullets.remove(bullet)

        for x in meteoritos.copy():
            if x.rect.right <= 0:
                meteoritos.remove(x)

        if pygame.sprite.groupcollide(meteoritos, bullets, True, True):
            puntuacion += 1

            print(puntuacion)

        gf.updateScreen(aiSettings, screen, ship, meteoritos, bullets)
        screen.blit(imagen_defondo, (0, 0))

        tiempo += 1
コード例 #3
0
def run_game():
    #inizilizziamo il gioco e creiamo gli oggetti a schermo
    pygame.init()
    #instanziamo un oggetto impostazioni
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    icona = pygame.image.load("./static/images/icona.png")
    pygame.display.set_icon(icona)
    pygame.display.set_caption("Alien invasion")

    #crazione istanza delle statistiche di gioco
    stats = Game_stats(ai_settings)

    # crazione bottone per avvio del gioco
    play_button = Button(ai_settings, screen, "Touch here or type 'p'")

    # creazione scoreboard
    scoreboard = Scoreboard(ai_settings, screen, stats)

    # creazione navetta
    ship = Ship(screen, ai_settings)

    # creazione  gruppo per alieni
    aliens = Group()

    # creazione della flotta di alieni
    gf.create_fleet(ai_settings, screen, aliens, ship)

    # gruppo dove immagazzinare i proiettili
    bullets = Group()

    #inzia il loop principale per il gioco
    while True:
        # rileva eventi di input
        gf.check_events(ai_settings, screen, stats, play_button, ship, bullets,
                        aliens, scoreboard)

        if stats.game_active:
            # aggiorna posizione nave
            ship.update()
            # aggiorna posizione alieni
            gf.update_aliens(ai_settings, stats, screen, aliens, ship, bullets,
                             scoreboard)
            # aggiorna posizione proiettili
            gf.update_bullets(ai_settings, screen, ship, bullets, aliens,
                              stats, scoreboard)

    #  else:
    #      sys.exit()

    # aggiorna oggetti a schermo
        gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets,
                         play_button, scoreboard)
コード例 #4
0
ファイル: main.py プロジェクト: Kaine-R/AlienKaineR
def run_game():

    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load("images/music.mp3")
    pygame.mixer.music.play(-1, 130.0)
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Aliens")
    stats = GameStats(ai_settings)
    playButton = Button(ai_settings, screen, "Play")
    highScore = HighScores(screen, ai_settings)
    score = Score(ai_settings, screen, "Wave#: " + str(stats.waveNumber),
                  stats)
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    stars = Group()
    badBullets = Group()
    barriers = Group()
    gf.makeBarriers(barriers, screen, ai_settings)
    gf.createFleet(ai_settings, screen, ship, aliens)
    highScore.prepScores()

    screen.fill(ai_settings.bg_color)
    while True:
        screen.fill(ai_settings.bg_color)
        gf.updateBackground(ai_settings, screen, stars)
        gf.drawBarrier(barriers)
        gf.check_events(ai_settings, screen, ship, stats, playButton, aliens,
                        bullets, barriers)
        gf.updateScreen(ai_settings, screen, stats, ship, aliens, bullets,
                        playButton, score, badBullets)

        if stats.gameActive:
            ship.update()
            gf.barriersCollide(barriers, bullets)
            gf.updateBullets(ai_settings, screen, stats, ship, aliens, bullets,
                             badBullets, highScore, barriers)
            gf.updateAliens(ai_settings, stats, screen, ship, aliens, bullets,
                            highScore, badBullets)
            gf.badShotting(aliens, badBullets, ai_settings, screen)
        else:
            highScore.prepScores()
            highScore.drawScores()

        pygame.display.flip()
コード例 #5
0
ファイル: main.py プロジェクト: Will179/finalFinalProject
def run_game():
    # Initialize pygame, settings, and screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Dungeon Fighter")

    # Make the Play button.
    play_button = Button(ai_settings, screen, "Play")

    # Create an instance to store game statistics and create a scoreboard
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    #create a player
    player = Player(ai_settings, screen)
    # Make a group to store bullets in. u
    bullets = Group()
    #Make a group to store Aliens
    aliens = Group()

    # Create the fleet of aliens. v
    gf.create_fleet(ai_settings, screen, player, aliens)

    # Start the main loop for the game.
    while True:
        sb = Scoreboard(ai_settings, screen, stats)
        #gf.start(player, enemy)
        gf.check_events(ai_settings, screen, stats, play_button, player,
                        aliens, bullets)
        if stats.game_active:
            player.update()
            gf.update_bullets(ai_settings, screen, stats, sb, player, aliens,
                              bullets)
            gf.update_aliens(ai_settings, stats, screen, player, aliens,
                             bullets)

        gf.update_screen(ai_settings, screen, stats, sb, player, aliens,
                         bullets, play_button)

        # Get rid of bullets that have disappeared. u
        for bullet in bullets.copy():
            if bullet.rect.bottom <= 0:
                bullets.remove(bullet)
                print(len(bullets))
コード例 #6
0
#getting the sound running
# music from https://www.youtube.com/watch?v=D0eD2ioPOqo
pygame.mixer.init()
pygame.mixer.music.load("finalProject/assets/sounds/themesong.mp3")
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)

# the game loop
while True:
    # actually runs the setup function from above
    setup()
    # makes the fps rate for the clock equal to 30
    clock.tick(30)
    # if the user presses x then the window is exited out of - the functions in gameFunctions.py
    gf.check_events()
    # makes the screen move continuously via blitting - this was probably my biggest challenge
    # the convert function allows the background to fit the window
    background = background.convert()
    # uses the modulus operator - this divides x by the width of the background and returns the remainder
    # thus, relx can only be the values between 0 and the screen
    relx = keyvar % background.get_rect().width
    # now when we deduct the background width we get the x coordinate needed for blitting
    screen.blit(background, [relx - background.get_rect().width, 0])
    # this attaches another background image to the end of the first image for continuous blitting75
    if relx < width:
        screen.blit(background, [relx, 0])
    keyvar -= 1
    # loads the character on the screen
    all_sprites.update()
    all_sprites.draw(screen)
コード例 #7
0
ファイル: pacman.py プロジェクト: Mbraun5/Pac-Portal
def run_game():
    #   Provides consistent window positioning. These settings center Pygame window for my computer.
    os.environ['SDL_VIDEO_WINDOW_POS'] = '60, 35'
    pygame.init()

    #   Initial Set-up
    settings = s.Settings()
    pygame.display.set_caption(settings.get_game_title())
    screen = pygame.display.set_mode(
        (settings.get_screen_width(), settings.get_screen_height()))
    screen.fill(settings.get_bg_color())
    clock = pygame.time.Clock()

    # Generate Game Objects
    image_lib = gF.import_image_library()
    sound_lib = gF.import_sound_library()
    maze = m.Maze(image_lib, screen, settings)
    pacman = p.PacMan(maze.pacmanCoordinates, image_lib, screen, settings,
                      sound_lib)
    pills = maze.pills.copy()
    large_pills = maze.largePills.copy()
    blinky = g.Blinky(clock, image_lib, screen, settings, sound_lib,
                      maze.blinkyCoordinates[0], maze.blinkyCoordinates[1],
                      maze.nodes)
    clyde = g.Clyde(clock, image_lib, screen, settings, sound_lib,
                    maze.clydeCoordinates[0], maze.clydeCoordinates[1],
                    maze.nodes)
    inky = g.Inky(clock, image_lib, screen, settings, sound_lib,
                  maze.inkyCoordinates[0], maze.inkyCoordinates[1], maze.nodes)
    pinky = g.Pinky(clock, image_lib, screen, settings, sound_lib,
                    maze.pinkyCoordinates[0], maze.pinkyCoordinates[1],
                    maze.nodes)
    ghosts = [blinky, clyde, inky, pinky]
    title_sequence = tS.TitleScreen(clock, pacman.images.copy(), image_lib,
                                    screen, settings)
    title_sequence.refresh_screen()
    pacman.set_map(maze.get_map(), maze.rowIndex, maze.columnIndex)
    maze.draw_part_maze()
    scoreboard = sb.ScoreBoard(maze, screen, settings)

    timer = 1  # pacTimer
    timer2 = 0.5  # pillTimer
    timer3 = 1.5  # ghostTimer
    delta_t = 0  # Delta to subtract from time
    reset_flag = False
    print(reset_flag)
    gF.start_game(ghosts, large_pills, pacman, pills, sound_lib[0])
    pygame.mixer.Sound.play(sound_lib[1], -1)

    while True:
        gF.check_events(pacman)
        pacman.update()
        reset_flag = gF.check_collisions(ghosts, large_pills, maze, pacman,
                                         pills, scoreboard, sound_lib[3])
        delta_t, timer, timer2, timer3 = gF.check_time(clock, delta_t, ghosts,
                                                       large_pills, timer,
                                                       timer2, timer3, pacman)
        for pill in large_pills:
            pill.blit()
        for ghost in ghosts:
            #   Comment ghost.update out for game to run without ghosts updating.
            ghost.update()
            ghost.blit()
        pacman.blit()
        pygame.display.flip()
        if reset_flag:
            screen.fill(settings.get_bg_color())
            pills = maze.pills.copy()
            large_pills = maze.largePills.copy()
            maze.draw_part_maze()
            gF.start_game(ghosts, large_pills, pacman, pills, sound_lib[0])
            timer = 1
            timer2 = 0.5
            timer3 = 1.5
            delta_t = 0
            reset_flag = False
            print(reset_flag)
コード例 #8
0
def run_game():
    #   Provides consistent window positioning. These settings center Pygame window for my computer.
    os.environ['SDL_VIDEO_WINDOW_POS'] = '60, 35'
    pygame.init()
    pygame.mouse.set_visible(
        False)  # Mouse is invisible until Title Sequence finishes animation.

    settings = se.Settings()
    screen = pygame.display.set_mode(
        (settings.get_screen_width(), settings.get_screen_height()))
    pygame.display.set_caption(settings.get_game_title())

    screen.fill(settings.get_bg_color())
    title_screen = tS.TitleScreen(screen, settings)
    title_screen.title_loop()

    image_library = gF.load_image_library()
    beep_sounds, sound_library = gF.load_sounds()

    scoreboard = sb.ScoreBoard(screen, settings, image_library[0],
                               title_screen)

    alien_list = []
    ship_bullet_list = []
    alien_bullet_list = []
    bunker_list = []
    ufo_list = []

    gF.create_bunkers(bunker_list, image_library, screen, settings)
    for bunker in bunker_list:
        bunker.blit()
    gF.create_fleet(alien_list, image_library, screen, settings)
    ship = sh.Ship(image_library, screen, settings, sound_library[2])

    counter = 0
    beep_counter = 0
    time_var = settings.get_time_var()
    while True:
        #   Now attached to title_screen. If you uncomment this, comment that one out or it's redundant.
        if settings.get_game_reset():
            alien_list = []
            bunker_list = []
            alien_list, bunker_list = gF.reset_game(alien_list, bunker_list,
                                                    image_library, scoreboard,
                                                    screen, settings, ship)
            ufo_list = []
            alien_bullet_list = []
            ship_bullet_list = []
        gF.check_events(ship_bullet_list, screen, settings, ship,
                        sound_library)
        ship.update()
        ufo_list = gF.update_bullets(alien_list, scoreboard, screen, settings,
                                     ship_bullet_list, sound_library, ufo_list)
        ufo_list = gF.update_ufo(ufo_list)
        beep_counter, counter, time_var = gF.update_aliens(
            alien_list, beep_counter, beep_sounds, bunker_list, counter,
            image_library, scoreboard, screen, settings, time_var)
        if random.randint(1, settings.get_alien_fire_rate()) == 5:
            gF.create_alien_bullet(
                alien_list[random.randint(0,
                                          len(alien_list) - 1)],
                alien_bullet_list, screen, settings)
            a = random.randint(1, 25)
            if a is 5 and len(ufo_list) is 0:
                gF.create_ufo(image_library, scoreboard, screen, settings,
                              sound_library, ufo_list)
        gF.update_alien_bullets(alien_bullet_list, alien_list, screen,
                                settings)
        alien_bullet_list, ship_bullet_list = gF.check_collisions(
            alien_bullet_list, alien_list, bunker_list, screen, ship,
            ship_bullet_list, scoreboard)

        ship.draw_ship()
        scoreboard.display_scores()
        pygame.display.flip()