Advertisement
Guest User

Testversion

a guest
Dec 29th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3. pygame.font.init()
  4. import pycountry
  5. import time
  6. import os
  7. import random
  8. from functools import wraps
  9. import pycountry_convert
  10.  
  11. SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 700 # Hardcoded window size / feel free to switch
  12. flag_path = r"C:\Users\ktown\Dokumente\Maximilian\Programmieren\Phyton\Geogame\Flagchoose+Backend\flags" # the folder with the flag-images
  13.  
  14.  
  15. WIN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  16. pygame.display.set_caption("Geo Game") # it sets all about the window
  17.  
  18. FPS = 30 # Hardcoded fps
  19.  
  20. BLACK = (0, 0, 0) # A summary of used colors
  21. WHITE = (255, 255, 255)
  22. RED = (255, 0, 0)
  23. GREEN = (0, 255, 0)
  24. BLUE = (0, 0, 255)
  25.  
  26. SELECTION_FONT = pygame.font.SysFont('comicsans', 40)
  27. COUNTRY_FONT = pygame.font.SysFont('comicsans', 60)
  28.  
  29. countries = pycountry.countries
  30.  
  31. # decorator who makes a function only run once in a loop
  32. def run_once(f):
  33. @wraps(f)
  34. def wrapper(*args, **kwargs):
  35. if not wrapper.has_run:
  36. result = f(*args, **kwargs)
  37. wrapper.has_run = True
  38. return result
  39. wrapper.has_run = False
  40. return wrapper
  41.  
  42. class Game:
  43. class Easy_name2flag_game:
  44. def __init__(self):
  45. self.displayed_flags = 1 # Displayed flags
  46. self.TOTAL_FLAGS = 255 # Number of the flags in the directory
  47. self.run_permission = True
  48. self.picked_flag = ""
  49. self.FLAG_WIDTH, self.FLAG_HEIGHT = SCREEN_WIDTH / 3.5, SCREEN_HEIGHT / 3.5
  50. self.country_name = None
  51. self.countrys = {}
  52. self.needed_countrynames = 3
  53. self.list_of_countries = []
  54. self.list_of_choosen_countries = []
  55.  
  56. @run_once
  57. def getvalues(self):
  58. global picked_flag
  59. global list_of_choosen_countries
  60. list_of_choosen_countries = []
  61. random_number = random.randint(1, self.TOTAL_FLAGS) # picks a random image
  62. # Get a list of all the items in the folder
  63. items = os.listdir(flag_path) # gets the directory information
  64. # Get any item in the list
  65. picked_flag = items[random_number] # it pickes a item from the directory
  66. print(picked_flag)
  67. flag_code = picked_flag.replace(".png", "")
  68. flag_code = flag_code.upper() # transforms the imagename to an alpha_2 code
  69. print(flag_code)
  70. self.country_name = pycountry_convert.country_alpha2_to_country_name(flag_code) # converts the alpha_2 code into the country name
  71. print(self.country_name)
  72. for country in countries:
  73. self.list_of_countries.append(f"{country.name}")
  74. for i in range(0, self.needed_countrynames):
  75. random_item = random.choice(self.list_of_countries)
  76. list_of_choosen_countries.append(random_item)
  77. print(list_of_choosen_countries)
  78. Game.Easy_name2flag_game.rendering(self=Game.Easy_name2flag_game())
  79.  
  80. def rendering(self): # renders the text, etc.
  81. global text_render
  82. var = Game.Easy_name2flag_game()
  83. print(var.list_of_choosen_countries)
  84. text_render = COUNTRY_FONT.render(self.country_name, 1, BLACK)
  85. print(self.country_name)
  86. print("rendering done")
  87.  
  88. def handel(self):
  89. global flag
  90.  
  91. for i in range(0, self.displayed_flags):
  92. Game.Easy_name2flag_game.getvalues(self=Game.Easy_name2flag_game())
  93.  
  94. flag_img = pygame.image.load(
  95. os.path.join('flags', picked_flag)) # Search the choosen flag
  96. flag = pygame.transform.scale(flag_img, (self.FLAG_WIDTH, self.FLAG_HEIGHT))
  97.  
  98.  
  99.  
  100. class frontend:
  101. def __init__(self):
  102.  
  103. self.gameselection_1 = pygame.Rect(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 10,
  104. SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 5,
  105. 200,
  106. 100
  107. )
  108. self.flag_easy_flaggame = pygame.Rect(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 7,
  109. SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 2.5,
  110. 200,
  111. 100
  112. )
  113. self.country_choice_1 = pygame.Rect(SCREEN_WIDTH / 10 - SCREEN_WIDTH / 15,
  114. SCREEN_HEIGHT / 2,
  115. 200,
  116. 100
  117. )
  118. self.country_choice_2 = pygame.Rect(500,
  119. 500,
  120. 200,
  121. 100
  122. )
  123.  
  124.  
  125. def draw_gameselection(self):
  126. WIN.fill(WHITE)
  127. gameselection = SELECTION_FONT.render("Easy-Flag-Game", 1, BLACK)
  128. WIN.blit(gameselection, (self.gameselection_1.x, self.gameselection_1.y))
  129.  
  130. def draw_easy_flag_game(self):
  131. WIN.fill(WHITE)
  132. WIN.blit(flag, (self.flag_easy_flaggame.x, self.flag_easy_flaggame.y))
  133. WIN.blit(text_render, (self.country_choice_1.x, self.country_choice_1.y))
  134.  
  135.  
  136.  
  137.  
  138.  
  139. class Maingameloop:
  140. def __init__(self):
  141. self.clock = pygame.time.Clock()
  142. self.run = True
  143. self.frontend_vars = Game.frontend() # imports the vars from frontend
  144. self.modus = "Start" # Modus that handels the frontend
  145.  
  146. def Gameloop(self):
  147. while self.run:
  148. self.clock.tick(FPS) # Sets the hardcoded fps
  149. for event in pygame.event.get():
  150. if event.type == pygame.QUIT:
  151. self.run = False
  152. pygame.quit() # It checks im the window is getting closed
  153.  
  154. if self.modus == "Start":
  155. Game.frontend.draw_gameselection(self=Game.frontend()) # handels the frontend
  156. # The actions are in the mode defined that they can be individuall in every mode and can be used multiple times
  157. if event.type == pygame.MOUSEBUTTONDOWN:
  158. self.pos = pygame.mouse.get_pos()
  159. if self.frontend_vars.gameselection_1.collidepoint(self.pos):
  160. self.modus = "Easy-Flag-Game" # changes the mode
  161. print("hit")
  162.  
  163. if self.modus == "Easy-Flag-Game":
  164. Game.Easy_name2flag_game.handel(self=Game.Easy_name2flag_game())
  165. Game.frontend.draw_easy_flag_game(self=Game.frontend())
  166.  
  167.  
  168. pygame.display.update()
  169.  
  170.  
  171. if __name__ == "__main__":
  172. game = Game.Maingameloop()
  173. game.Gameloop()
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement