Ejemplo n.º 1
0
 def process_events(self):
     """Process the event queue, take in player input."""
     #lots of other stuff will be needed, of course
     run = True
     if self.miniGame:
         if not self.miniGame.process_events():
             self.lastScore = self.miniGame.get_score()
             self.scoreScreen = ScoreScreen(screenSize, self.miniGame.get_game(), self.lastScore)
             self.miniGame = False
             pygame.display.set_caption("Giga-Bright presents:")
             #self.createGUI()
     elif self.scoreScreen:
         if not self.scoreScreen.process_events():
             self.highscoresList = HighscoresList(screenSize, self.scoreScreen.get_game())
             self.scoreScreen = False
             pygame.display.set_caption("Giga-Bright presents:")
     elif self.highscoresList:
         if not self.highscoresList.process_events():
             self.highscoresList = False
             self.createGUI()
             pygame.display.set_caption("Giga-Bright presents:")
     elif self.guideMenu:
         if not self.guideMenu.process_events():
             self.guideData = self.guideMenu.retrieveData()
             #print self.guideData
             self.guideMenu = False
             self.createGUI()
             pygame.display.set_caption("Giga-Bright presents:")
     elif self.loadMenu:
         if not self.loadMenu.process_events():
             self.loadMenu = False
             self.createGUI()
             pygame.display.set_caption("Giga-Bright presents:")
     else:
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 run = False
             if event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_ESCAPE:
                     run = False
                 if self.avatar:
                     if event.key == pygame.K_RIGHT:
                         self.avatar.state['right'] = True
                     if event.key == pygame.K_LEFT:
                         self.avatar.state['left'] = True
                     if event.key == pygame.K_UP:
                         self.avatar.state['up'] = True
                     if event.key == pygame.K_DOWN:
                         self.avatar.state['down'] = True
                         
             if event.type == pygame.KEYUP:
                 if self.avatar:
                     if event.key == pygame.K_RIGHT:
                         self.avatar.state['right'] = False
                     if event.key == pygame.K_LEFT:
                         self.avatar.state['left'] = False
                     if event.key == pygame.K_UP:
                         self.avatar.state['up'] = False
                     if event.key == pygame.K_DOWN:
                         self.avatar.state['down'] = False
     return run
Ejemplo n.º 2
0
class Game(object):

    __fps = 60 
     
    def __init__(self):
        """Initialize the game."""
        pygame.init()
        self.screen = pygame.display.set_mode(screenSize)
        pygame.display.set_caption("Giga-Bright presents:")
        self.clock = pygame.time.Clock()
        self.miniGame = False
        
        self.loaderBoxes = []
        self.curLoaderId = 0
        self.avatar = False
        self.createGUI()
        self.guideMenu = False
        self.guideData = []
        
        self.loadMenu = False
        self.scoreScreen = False
        self.highscoresList = False
        
        self.lastScore = 0
        
        #lots of other stuff will be needed, of course
        #debug stuff below
        #turning off this for GUI stuff
        #self.miniGame = LineGame([])
    
    def createGUI(self):
    
        self.avatar = Avatar(((self.screen.get_width() / 2), self.screen.get_height() / 2))
        self.avatar.set_pos((self.avatar.get_pos()[0] - self.avatar.get_res()[0] / 2, self.avatar.get_pos()[1] - self.avatar.get_res()[1] / 2))
        
        self.curLoaderId = 0
        #top loaders
        for i in range(3):
            tempBox = LoaderBox(((self.screen.get_width() / 3) * i, 0), self.curLoaderId, loaderText[self.curLoaderId])
            self.curLoaderId += 1
            tempBox.set_pos((tempBox.get_pos()[0] + tempBox.get_res()[0] / 3, tempBox.get_pos()[1]))
            self.loaderBoxes.append(tempBox)
            
        #bottom loaders
        for i in range(3):
            tempBox = LoaderBox(((self.screen.get_width() / 3) * i, self.screen.get_height()), self.curLoaderId, loaderText[self.curLoaderId])
            self.curLoaderId += 1
            tempBox.set_pos((tempBox.get_pos()[0] + tempBox.get_res()[0] / 3, tempBox.get_pos()[1] - tempBox.get_res()[1]))
            self.loaderBoxes.append(tempBox)    
        
        #side loaders
        #left
        #tempBox = LoaderBox((0, self.screen.get_height() / 2), self.curLoaderId, 90)
        #self.curLoaderId += 1
        #tempBox.set_pos((tempBox.get_pos()[0], tempBox.get_pos()[1] - tempBox.get_res()[1] / 2))
        #self.loaderBoxes.append(tempBox)  
        
        #right
        #tempBox = LoaderBox((self.screen.get_width(), self.screen.get_height() / 2), self.curLoaderId, 90)
        #self.curLoaderId += 1
        #tempBox.set_pos((tempBox.get_pos()[0] - tempBox.get_res()[0], tempBox.get_pos()[1] - tempBox.get_res()[1] / 2))
        #self.loaderBoxes.append(tempBox)
        
        #print len(self.loaderBoxes)
        
    def process_events(self):
        """Process the event queue, take in player input."""
        #lots of other stuff will be needed, of course
        run = True
        if self.miniGame:
            if not self.miniGame.process_events():
                self.lastScore = self.miniGame.get_score()
                self.scoreScreen = ScoreScreen(screenSize, self.miniGame.get_game(), self.lastScore)
                self.miniGame = False
                pygame.display.set_caption("Giga-Bright presents:")
                #self.createGUI()
        elif self.scoreScreen:
            if not self.scoreScreen.process_events():
                self.highscoresList = HighscoresList(screenSize, self.scoreScreen.get_game())
                self.scoreScreen = False
                pygame.display.set_caption("Giga-Bright presents:")
        elif self.highscoresList:
            if not self.highscoresList.process_events():
                self.highscoresList = False
                self.createGUI()
                pygame.display.set_caption("Giga-Bright presents:")
        elif self.guideMenu:
            if not self.guideMenu.process_events():
                self.guideData = self.guideMenu.retrieveData()
                #print self.guideData
                self.guideMenu = False
                self.createGUI()
                pygame.display.set_caption("Giga-Bright presents:")
        elif self.loadMenu:
            if not self.loadMenu.process_events():
                self.loadMenu = False
                self.createGUI()
                pygame.display.set_caption("Giga-Bright presents:")
        else:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        run = False
                    if self.avatar:
                        if event.key == pygame.K_RIGHT:
                            self.avatar.state['right'] = True
                        if event.key == pygame.K_LEFT:
                            self.avatar.state['left'] = True
                        if event.key == pygame.K_UP:
                            self.avatar.state['up'] = True
                        if event.key == pygame.K_DOWN:
                            self.avatar.state['down'] = True
                            
                if event.type == pygame.KEYUP:
                    if self.avatar:
                        if event.key == pygame.K_RIGHT:
                            self.avatar.state['right'] = False
                        if event.key == pygame.K_LEFT:
                            self.avatar.state['left'] = False
                        if event.key == pygame.K_UP:
                            self.avatar.state['up'] = False
                        if event.key == pygame.K_DOWN:
                            self.avatar.state['down'] = False
        return run
        
    def update(self):
        """Update the game objects."""
        #call update functions for all objects
        delta_time = float(self.clock.tick(self.__fps)) / 1000
        
        if self.miniGame:
            self.miniGame.update()
        elif self.scoreScreen:
            self.scoreScreen.update()
        elif self.highscoresList:
            self.highscoresList.update()
        elif self.guideMenu:
            self.guideMenu.update()
        elif self.loadMenu:
            self.loadMenu.update()
        else:
            self.avatar.update(delta_time)
            for l in self.loaderBoxes:
                if l.rect.colliderect(self.avatar.rect):
                    self.loadItem(l.get_id())
                    pass
                    
    def draw(self):
        """Draw the game objects."""
        #call draw functions for all objects
        pygame.draw.rect(self.screen, (0,0,0), (0,0,screenSize[0],screenSize[1]))
        if self.miniGame:
            self.miniGame.draw(self.screen)
        elif self.scoreScreen:
            self.scoreScreen.draw(self.screen)
        elif self.highscoresList:
            self.highscoresList.draw(self.screen)
        elif self.guideMenu:
            self.guideMenu.draw(self.screen)
        elif self.loadMenu:
            self.loadMenu.draw(self.screen)
        else:
            self.avatar.draw(self.screen)
            for l in self.loaderBoxes:
                l.draw(self.screen)
                
    def loadItem(self, id):
        #only loading LineGame for now
        del self.loaderBoxes[:]
        del self.avatar
        #self.avatar.set_pos(((self.screen.get_width() / 2), self.screen.get_height() / 2))
        #self.avatar.set_pos((self.avatar.get_pos()[0] - self.avatar.get_res()[0] / 2, self.avatar.get_pos()[1] - self.avatar.get_res()[1] / 2))
        if id == 0:
            #debug code
            if not self.guideData:
                m = [("Question Number 1",("Answer Number 1","Ans")),\
                ("Question Number 2",("Answer Number 2","Ans")),\
                ("Question Number 3",("Answer Number 3","Ans")),\
                ("Question Number 4",("Answer Number 4","Ans")),\
                ("Question Number 5",("Answer Number 5","Ans")),\
                ("Question Number 6",("Answer Number 6","Ans")),\
                ("Question Number 7",("Answer Number 7","Ans")),\
                ("Question Number 8",("Answer Number 8","Ans"))]
                self.miniGame = LineGame(screenSize,m)
            else:
                self.miniGame = LineGame(screenSize, self.guideData)
        elif id == 1:
            self.miniGame = FlipGame(screenSize, self.guideData)
        elif id == 2:
            self.miniGame = InvadersGame(screenSize, self.guideData)
        elif id == 3:
            self.guideMenu = GuideLoader(screenSize)
        elif id == 4:
            self.loadMenu = SGDownloader(screenSize)
        elif id == 5:
            self.loadMenu = SGUploader(screenSize)
        else:
            self.createGUI()