示例#1
0
def checkEvents1(setting, screen, stats, sb, playBtn, quitBtn, menuBtn, sel, ship, aliens, bullets, eBullets):
    """Respond to keypresses and mouse events."""
    global SmBtn

    for event in pg.event.get():
        # Check for quit event
        if event.type == pg.QUIT:
            sys.exit()
            # Check for key down has been pressed
        elif event.type == pg.KEYDOWN:
            # Check if down, up, enter, esc is pressed
            if event.key == pg.K_DOWN:
                if SmBtn < 4:
                    SmBtn += 1
                    sel.rect.y += 50
            if event.key == pg.K_UP:
                if SmBtn > 1:
                    SmBtn -= 1
                    sel.rect.y -= 50
            if event.key == pg.K_RETURN:
                if SmBtn == 1:
                    stats.mainMenu = True
                    stats.mainGame = False
                    stats.twoPlayer = False
                    stats.mainAbout = False
                    stats.settingsMenu = False
                    SmBtn = 1
                    sel.rect.centery = playBtn.rect.centery
                elif SmBtn == 2:
                    sys.exit()
                elif SmBtn == 3:
                    Button.reverseCol()
                    Settings.reverseCol()
                    Scoreboard.reverseCol()
                    mainMenu.reverseCol()
                    Explosions.reverseCol()
                    stats.mainMenu = True
                    stats.mainGame = False
                    stats.twoPlayer = False
                    stats.mainAbout = False
                    stats.settingsMenu = False
                    SmBtn = 1
                    sel.rect.centery = playBtn.rect.centery
                elif SmBtn == 4:
                    setting.checkBtnPressed += 1
                    if setting.checkBtnPressed % 2 != 0:
                        setting.interception = True
                    else:
                        setting.interception = False
                    stats.mainMenu = True
                    stats.mainGame = False
                    stats.twoPlayer = False
                    stats.mainAbout = False
                    stats.settingsMenu = False
                    SmBtn = 1
                    sel.rect.centery = playBtn.rect.centery
            if event.key == pg.K_ESCAPE:
                sys.exit()
    prepSm(setting, screen)
示例#2
0
def runGame():
    #Initialize game and create a window
    pg.init()
    #create a new object using the settings class
    setting = Settings()
    #creaete a new object from pygame display
    screen = pg.display.set_mode((setting.screenWidth, setting.screenHeight))
    #set window caption using settings obj
    pg.display.set_caption(setting.windowCaption)

    playBtn = Button(setting, screen, "PLAY", 200)
    menuBtn = Button(setting, screen, "MENU", 250)
    twoPlayBtn = Button(setting, screen, "2PVS", 250)
    #setBtnbtn = Button(setting, screen, "SETTING", 300)
    aboutBtn = Button(setting, screen, "ABOUT", 300)
    quitBtn = Button(setting, screen, "QUIT", 400)
    greyBtn = Button(setting, screen, "GREY", 200)
    redBtn = Button(setting, screen, "RED", 250)
    blueBtn = Button(setting, screen, "BLUE", 300)
    #make slector for buttons
    sel = Selector(setting, screen)
    sel.rect.x = playBtn.rect.x + playBtn.width + 10
    sel.rect.centery = playBtn.rect.centery

    #Create an instance to stor game stats
    stats = GameStats(setting)
    sb = Scoreboard(setting, screen, stats)

    #Make a ship
    ship = Ship(setting, screen)
    #Ships for two player
    ship1 = Ship(setting, screen)
    ship2 = Ship(setting, screen)

    #make a group of bullets to store
    bullets = Group()
    eBullets = Group()
    setting.explosions = Explosions()

    #Make an alien
    aliens = Group()
    gf.createFleet(setting, screen, ship, aliens)
    pg.display.set_icon(pg.transform.scale(ship.image, (32, 32)))

    #plays bgm
    pg.mixer.music.load("sounds/galtron.mp3")
    pg.mixer.music.set_volume(0.25)
    pg.mixer.music.play(-1)

    runGame = True

    #Set the two while loops to start mainMenu first
    while runGame:
        #Set to true to run main game loop
        while stats.mainMenu:
            mm.checkEvents(setting, screen, stats, sb, playBtn, twoPlayBtn,
                           aboutBtn, quitBtn, menuBtn, sel, ship, aliens,
                           bullets, eBullets)
            mm.drawMenu(setting, screen, sb, playBtn, menuBtn, twoPlayBtn,
                        aboutBtn, quitBtn, sel)

        while stats.playMenu:
            pm.checkEvents(setting, screen, stats, sb, playBtn, greyBtn,
                           redBtn, blueBtn, quitBtn, menuBtn, sel, ship,
                           aliens, bullets, eBullets)
            pm.drawMenu(setting, screen, sb, greyBtn, redBtn, blueBtn, menuBtn,
                        quitBtn, sel)

        while stats.mainGame:
            #Game functions
            gf.checkEvents(setting, screen, stats, sb, playBtn, quitBtn, sel,
                           ship, aliens, bullets, eBullets)  #Check for events

            if stats.gameActive:
                gf.updateAliens(setting, stats, sb, screen, ship, aliens,
                                bullets, eBullets)  #Update aliens
                gf.updateBullets(setting, screen, stats, sb, ship, aliens,
                                 bullets, eBullets)  #Update collisions
                ship.update(bullets)  #update the ship
            #Update the screen
            gf.updateScreen(setting, screen, stats, sb, ship, aliens, bullets,
                            eBullets, playBtn, menuBtn, quitBtn, sel)
        while stats.mainAbout:
            About.checkEvents(setting, screen, stats, sb, playBtn, quitBtn,
                              menuBtn, sel, ship, aliens, bullets, eBullets)
            About.drawMenu(setting, screen, sb, menuBtn, quitBtn, sel)

        while stats.twoPlayer:
            tp.checkEvents(setting, screen, stats, playBtn, quitBtn, sel,
                           bullets, eBullets, ship1, ship2)
            if stats.gameActive:
                ship1.update(bullets)
                ship2.update(bullets)
                tp.updateBullets(setting, screen, stats, ship1, ship2, bullets,
                                 eBullets)
            tp.updateScreen(setting, screen, stats, bullets, eBullets, playBtn,
                            menuBtn, quitBtn, sel, ship1, ship2)
        while stats.mainGame:
            if runGame == True:
                print("test")
示例#3
0
def runGame():
    # Initialize game and create a window
    pg.init()
    # create a new object using the settings class
    setting = Settings()
    # creaete a new object from pygame display
    screen = pg.display.set_mode((setting.screenWidth, setting.screenHeight))

    # intro
    intro.introimages()

    # set window caption using settings obj
    pg.display.set_caption(setting.windowCaption)

    bMenu = ButtonMenu(screen)
    bMenu.addButton("play", "PLAY")
    bMenu.addButton("menu", "MENU")
    bMenu.addButton("twoPlay", "2PVS")
    bMenu.addButton("settings", "SETTINGS")
    bMenu.addButton("invert", "INVERT")
    bMenu.addButton("about", "ABOUT")
    bMenu.addButton("quit", "QUIT")
    bMenu.addButton("grey", "GREY")
    bMenu.addButton("red", "RED")
    bMenu.addButton("blue", "BLUE")
    bMenu.addButton("retry", "RETRY")
    bMenu.addButton("hard", "HARD")
    bMenu.addButton("normal", "NORMAL")

    mainMenuButtons = ["play", "about", "settings", "quit"]  # delete "twoPlay"
    playMenuButtons = ["grey", "red", "blue", "menu", "quit"]
    levelMenuButtons = ["hard", "normal", "quit"]
    mainGameButtons = ["play", "menu", "quit"]
    aboutButtons = ["menu", "quit"]
    settingsMenuButtons = ["menu", "invert", "quit"]

    bgManager = BackgroundManager(screen)
    bgManager.setFillColor((0, 0, 0))
    bgManager.addBackground(
        "universe_1", "gfx/backgrounds/stars_back.png", 0, 1)
    bgManager.addBackground(
        "universe_1", "gfx/backgrounds/stars_front.png", 0, 1.5)
    bgManager.selectBackground("universe_1")

    # Create an instance to stor game stats
    stats = GameStats(setting)
    sb = Scoreboard(setting, screen, stats)

    # Make a ship
    ship = Ship(setting, screen)
    # Ships for two player
    ship1 = Ship(setting, screen)
    ship2 = Ship(setting, screen)

    # make a group of items to store
    items = Group()

    # make a group of bullets to store
    bullets = Group()
    charged_bullets = Group()
    eBullets = Group()
    setting.explosions = Explosions()

    # Make an alien
    aliens = Group()
    gf.createFleet(setting, stats, screen, ship, aliens)
    pg.display.set_icon(pg.transform.scale(ship.image, (32, 32)))

    bgImage = pg.image.load('gfx/title_c.png')
    bgImage = pg.transform.scale(
        bgImage, (setting.screenWidth, setting.screenHeight))
    bgImageRect = bgImage.get_rect()

    aboutImage = pg.image.load('gfx/About_modify2.png')
    aboutImage = pg.transform.scale(
        aboutImage, (setting.screenWidth, setting.screenHeight))
    aboutImageRect = aboutImage.get_rect()

    # plays bgm
    pg.mixer.music.load('sound_bgms/galtron.mp3')
    pg.mixer.music.set_volume(0.25)
    pg.mixer.music.play(-1)

    rungame = True

    sounds.stage_clear.play()
    # Set the two while loops to start mainMenu first
    while rungame:
        # Set to true to run main game loop
        bMenu.setMenuButtons(mainMenuButtons)
        while stats.mainMenu:
            if not stats.gameActive and stats.paused:
                setting.initDynamicSettings()
                stats.resetStats()
                ##stats.gameActive = True

                # Reset the alien and the bullets
                aliens.empty()
                bullets.empty()
                eBullets.empty()

                # Create a new fleet and center the ship
                gf.createFleet(setting, stats, screen, ship, aliens)
                ship.centerShip()

            mm.checkEvents(setting, screen, stats, sb, bMenu,
                           ship, aliens, bullets, eBullets)
            mm.drawMenu(setting, screen, sb, bMenu, bgImage, bgImageRect)

        bMenu.setMenuButtons(levelMenuButtons)
        while stats.levelMenu:
            lm.checkEvents(setting, screen, stats, sb, bMenu,
                           ship, aliens, bullets, eBullets)
            lm.drawMenu(setting, screen, sb, bMenu, bgImage, bgImageRect)

        bMenu.setMenuButtons(playMenuButtons)
        while stats.playMenu:
            pm.checkEvents(setting, screen, stats, sb, bMenu,
                           ship, aliens, bullets, eBullets)
            pm.drawMenu(setting, screen, sb, bMenu)

        bMenu.setMenuButtons(mainGameButtons)

        while stats.mainGame:
            # Game functions
            gf.checkEvents(setting, screen, stats, sb, bMenu, ship, aliens,
                           bullets, eBullets, charged_bullets)  # Check for events
            # Reset Game
            if gf.reset == 1:
                gf.reset = 0
                pg.register_quit(runGame())
            if stats.gameActive:
                gf.updateAliens(setting, stats, sb, screen, ship,
                                aliens, bullets, eBullets)  # Update aliens
                gf.updateBullets(setting, screen, stats, sb, ship, aliens, bullets,
                                 eBullets, charged_bullets, items)  # Update collisions
                gf.updateItems(setting, screen, stats, sb, ship,
                               aliens, bullets, eBullets, items)
                ship.update(bullets, aliens)  # update the ship
                # Update the screen
            gf.updateScreen(setting, screen, stats, sb, ship, aliens,
                            bullets, eBullets, charged_bullets, bMenu, bgManager, items)

        bMenu.setMenuButtons(aboutButtons)
        bMenu.setPos(None, 500)

        while stats.mainAbout:
            About.checkEvents(setting, screen, stats, sb,
                              bMenu, ship, aliens, bullets, eBullets)
            About.drawMenu(setting, screen, sb, bMenu,
                           aboutImage, aboutImageRect)

        while stats.twoPlayer:
            tp.checkEvents(setting, screen, stats, sb, bMenu,
                           bullets, aliens, eBullets, ship1, ship2)
            if stats.gameActive:
                ship1.update(bullets, aliens)
                ship2.update(bullets, aliens)
                tp.updateBullets(setting, screen, stats, sb,
                                 ship1, ship2, aliens, bullets, eBullets, items)
            tp.updateScreen(setting, screen, stats, sb, ship1,
                            ship2, aliens, bullets, eBullets, bMenu, items)

        bMenu.setMenuButtons(settingsMenuButtons)

        while stats.settingsMenu:
            sm.checkEvents1(setting, screen, stats, sb, bMenu,
                            ship, aliens, bullets, eBullets)
            sm.drawMenu(setting, screen, sb, bMenu)

        while stats.mainGame:
            if rungame == True:
                print("test")
示例#4
0
    sb = Scoreboard(setting, screen, stats)

    # Make a ship
    ship = Ship(setting, screen)
    # Ships for two player
    ship1 = Ship(setting, screen)
    ship2 = Ship(setting, screen)

    #make a group of items to store
    items = Group()

    # make a group of bullets to store
    bullets = Group()
    charged_bullets = Group()
    eBullets = Group()
    setting.explosions = Explosions()

    # Make an alien
    aliens = Group()
    gf.createFleet(setting, stats, screen, ship, aliens)
    pg.display.set_icon(pg.transform.scale(ship.image, (32, 32)))

    bgImage = pg.image.load('gfx/title_c.png')
    bgImage = pg.transform.scale(bgImage, (setting.screenWidth, setting.screenHeight))
    bgImageRect = bgImage.get_rect()

    aboutImage = pg.image.load('gfx/About_modify2.png')
    aboutImage = pg.transform.scale(aboutImage, (setting.screenWidth, setting.screenHeight))
    aboutImageRect = aboutImage.get_rect()

    # plays bgm