Skip to content
Snippets Groups Projects
Interface.py 4.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • 's avatar
    committed
    ## @file interface.py
    #  @author Vaibhav Chadha
    #  @brief implements the main interface of this game
    
    Chadha Vaibhav's avatar
    Chadha Vaibhav committed
    #  @date 11/09/2018
    
    Hameed Andy's avatar
    Hameed Andy committed
    #importing necessary libraries
    
    x = 10
    y = 40
    import os
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
    
    
    Hameed Andy's avatar
    Hameed Andy committed
    import pygame, sys
    
    's avatar
    committed
    import help_, highscore, theme
    
    's avatar
    committed
    ## @brief A Class that will contain useful functions in order for the creation of main interface
    
    Hameed Andy's avatar
    Hameed Andy committed
    class GUI():
    
    
    's avatar
    committed
      ## @brief A method to create a button
      #  @details This method will make a box on the interface
      #  @param surface The background (surface) the box should be made on
      #  @param color The color of the button to be made
      #  @param Rect The coordinate of the button with the length and width
      #  @param width The width of the sides of button
    
    Hameed Andy's avatar
    Hameed Andy committed
      def button(Surface, color,Rect,width):
        pygame.draw.rect(Surface, color,Rect,width)
    
    
    's avatar
    committed
      ## @brief A method to display text
      #  @details This function will print the text on the interface
      #  @param text The text to be printed
      #  @param fontStyle The font Style of the text to be displayed
      #  @param fontSize The size of the text written
      #  @param color The color of the text
      #  @param coord The coordinate at which the text should start displaying
      #  @param surface The background (surface) the text should be printed on
    
    Hameed Andy's avatar
    Hameed Andy committed
      def text(text,fontStyle,fontSize,color,coord,surface):
        font = pygame.font.SysFont(fontStyle,fontSize)
        text = font.render(text,True,color)
        surface.blit(text,coord)
    
    
      def custom_text(text,fontName,fontSize,color,coord,surface):
        font = pygame.font.Font(fontName,fontSize)
        text = font.render(text,True,color)
        surface.blit(text,coord)
    
    's avatar
    committed
    ## @brief Makes the main interface of this game
    #  @details This will output the main page of this game by using the class above
    
    Hameed Andy's avatar
    Hameed Andy committed
    def main():
      pygame.init()
    
    Hameed Andy's avatar
    Hameed Andy committed
      darkgray = [100,100,100]
    
    's avatar
    committed
      image1 = pygame.image.load("Snake_Game_Logo_background.png")
    
    's avatar
    committed
      image2 = pygame.image.load("Snake_image.png")
    
    Hameed Andy's avatar
    Hameed Andy committed
    
      #while loop required to always refresh the page
    
    's avatar
    committed
      run = True
      while run:
    
    Hameed Andy's avatar
    Hameed Andy committed
          game = pygame.display.set_mode((800, 610))
    
    's avatar
    committed
          game.blit(image1,(0,0))
    
    's avatar
    committed
          game.blit(image2,(550,0))
    
    Hameed Andy's avatar
    Hameed Andy committed
          mousepos = pygame.mouse.get_pos() #checking mouse position
          mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
          pygame.display.set_caption("Lets Play")
    
    's avatar
    committed
    
    
    Hameed Andy's avatar
    Hameed Andy committed
          #Adding the play game button
    
    's avatar
    committed
          if (400 <= mousepos[0] <= 400+170 and 250 <= mousepos[1] <= 250+50 ):
    
    Hameed Andy's avatar
    Hameed Andy committed
            #checks if the mouse is hovering over the button
    
    's avatar
    committed
              GUI.button(game,darkgray, [400,250,170,50], 0)
    
    Hameed Andy's avatar
    Hameed Andy committed
              #checking if the button is clicked
              if mouseclick[0] == 1:
    
    's avatar
    committed
                theme.Themes.themes(100)        
    
    Hameed Andy's avatar
    Hameed Andy committed
          else:
    
    's avatar
    committed
              GUI.button(game,lightBlue, [400,250,170,50], 0)
    
          GUI.custom_text('Beginner',"Roboto-Light.ttf", 25,[239, 245, 224],(438,260),game)
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    's avatar
    committed
          if (430 <= mousepos[0] <= 430+220 and 350 <= mousepos[1] <= 350+50 ):
    
    's avatar
    committed
            #checks if the mouse is hovering over the button
    
    's avatar
    committed
              GUI.button(game,darkgray, [430,350,220,50], 0)
    
    's avatar
    committed
              #checking if the button is clicked
              if mouseclick[0] == 1:
    
    's avatar
    committed
                theme.Themes.themes(70)          
    
    's avatar
    committed
          else:
    
    's avatar
    committed
              GUI.button(game,lightBlue, [430,350,220,50], 0)
    
          GUI.custom_text('Intermediate',"Roboto-Light.ttf", 25,[239, 245, 224],(470,357),game)
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    's avatar
    committed
          if (400 <= mousepos[0] <= 400+180 and 450 <= mousepos[1] <= 450+50 ):
    
    's avatar
    committed
            #checks if the mouse is hovering over the button
    
    's avatar
    committed
              GUI.button(game,darkgray, [400,450,180,50], 0)
    
    's avatar
    committed
              #checking if the button is clicked
              if mouseclick[0] == 1:
    
    's avatar
    committed
                theme.Themes.themes(50)         
    
    's avatar
    committed
          else:
    
    's avatar
    committed
              GUI.button(game,lightBlue, [400,450,180,50], 0)
    
          GUI.custom_text('Advanced',"Roboto-Light.ttf", 25,[239, 245, 224],(435,457),game)
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    's avatar
    committed
          if (365 <= mousepos[0] <= 365+55 and 565 <= mousepos[1] <= 565+35 ):
              if mouseclick[0] == 1:
                help_.main()
          GUI.text('Help',"comicsansms", 25,[0,0,0],(365,565),game)
          
    
    's avatar
    committed
          if (15 <= mousepos[0] <= 15+115 and 565 <= mousepos[1] <= 565+35 ):
    
    Hameed Andy's avatar
    Hameed Andy committed
            if mouseclick[0] == 1:
    
    's avatar
    committed
              highscore.main()
    
          if (725 <= mousepos[0] <= 725+50 and 565 <= mousepos[1] <= 565+35 ):
    
    Hameed Andy's avatar
    Hameed Andy committed
            if mouseclick[0] == 1:
    
    's avatar
    committed
              pygame.quit()
              sys.exit()
    
    Hameed Andy's avatar
    Hameed Andy committed
          
          pygame.display.update()
    
    if __name__ == "__main__":
      main()