Advertisement
Guest User

game for pygame

a guest
May 8th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import button
  4. import pygame_gui
  5.  
  6. pygame.init()
  7.  
  8. #setting up the window
  9. WIDTH, HEIGHT = 1900, 1050
  10. FPS = 120
  11. WIN = pygame.display.set_mode((WIDTH, HEIGHT))
  12. pygame.display.set_caption('GameStudy')
  13.  
  14. #images
  15. BG = pygame.transform.scale(pygame.image.load("gamestudy.png"), (WIDTH, HEIGHT))
  16. login_img = pygame.image.load("loginbtn.PNG").convert_alpha()
  17. signup_img = pygame.image.load("signupbtn.PNG").convert_alpha()
  18.  
  19. #buttons
  20. login_button = button.Button(790, 630, login_img, 0.4)
  21. signup_button = button.Button(790, 800, signup_img, 0.4)
  22.  
  23. MANAGER = pygame_gui.UIManager((WIDTH, HEIGHT))
  24. clock = pygame.time.Clock()
  25.  
  26.  
  27. class Game():
  28. def __init__(self):
  29. self.WIN = WIN
  30. self.clock = pygame.time.Clock()
  31.  
  32. self.gameStateManager = GameStateManager('Openscene')
  33. self.openingscene = Openscene(self.WIN, self.gameStateManager) #tells the program which scene to run
  34. self.loginscene = Login(self.WIN, self.gameStateManager)
  35.  
  36. #create a dictionary of states so the program knows which state we have created to run
  37. self.states = {'Openscene': self.openingscene, 'loginscene': self.loginscene}
  38.  
  39.  
  40. def run(self):
  41. UI_REFRESH_RATE = self.clock.tick(FPS)/1000
  42. run = True
  43. while run:
  44. for event in pygame.event.get():
  45. if event.type == pygame.QUIT:
  46. pygame.quit()
  47. sys.exit()
  48.  
  49. if login_button.draw(self.WIN): #when button is pressed, action becomes true and is returned, this will run, and it changes the game state
  50. self.gameStateManager.set_state('loginscene')
  51.  
  52. MANAGER.process_events(event)
  53.  
  54. MANAGER.update(UI_REFRESH_RATE)
  55. MANAGER.draw_ui(self.WIN)
  56.  
  57. self.states[self.gameStateManager.get_state()].run() #gets the state from the gamestatemanager class, then checks the
  58. dictionary created and runs it
  59.  
  60.  
  61.  
  62. pygame.display.update()
  63. self.clock.tick(FPS)
  64.  
  65.  
  66. class Openscene(): #login/sign up scene, where the game starts
  67. def __init__(self, display, gameStateManager):
  68. self.display = display
  69. self.gameStateManager = gameStateManager
  70.  
  71. def run(self):
  72. self.display.blit(BG,(0,0))
  73. login_button.draw(self.display)
  74. signup_button.draw(self.display)
  75.  
  76.  
  77. class Login(): #login/sign up scene, where the game starts
  78. def __init__(self, display, gameStateManager):
  79. self.display = display
  80. self.gameStateManager = gameStateManager
  81.  
  82. def run(self):
  83. text_input = pygame_gui.elements.UITextEntryLine(relative_rect=pygame.Rect((350, 275), (900, 50)),
  84. manager=MANAGER,
  85. object_id='#main_text_entry')
  86. self.display.fill('white')
  87.  
  88.  
  89.  
  90.  
  91.  
  92. class GameStateManager:
  93. def __init__(self, currentstate):
  94. self.currentstate = currentstate
  95.  
  96. def get_state(self): #getting the state which we are currently running
  97. return self.currentstate
  98.  
  99. def set_state(self, state): #when we set the state, setting it to whatever value we put in
  100. self.currentstate = state
  101.  
  102.  
  103.  
  104.  
  105. if __name__ == '__main__':
  106. game = Game()
  107. game.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement