Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- pygame.font.init()
- import pycountry
- import time
- import os
- import random
- from functools import wraps
- import pycountry_convert
- SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 700 # Hardcoded window size / feel free to switch
- flag_path = r"C:\Users\ktown\Dokumente\Maximilian\Programmieren\Phyton\Geogame\Flagchoose+Backend\flags" # the folder with the flag-images
- WIN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Geo Game") # it sets all about the window
- FPS = 30 # Hardcoded fps
- BLACK = (0, 0, 0) # A summary of used colors
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- SELECTION_FONT = pygame.font.SysFont('comicsans', 40)
- COUNTRY_FONT = pygame.font.SysFont('comicsans', 60)
- countries = pycountry.countries
- # decorator who makes a function only run once in a loop
- def run_once(f):
- @wraps(f)
- def wrapper(*args, **kwargs):
- if not wrapper.has_run:
- result = f(*args, **kwargs)
- wrapper.has_run = True
- return result
- wrapper.has_run = False
- return wrapper
- class Game:
- class Easy_name2flag_game:
- def __init__(self):
- self.displayed_flags = 1 # Displayed flags
- self.TOTAL_FLAGS = 255 # Number of the flags in the directory
- self.run_permission = True
- self.picked_flag = ""
- self.FLAG_WIDTH, self.FLAG_HEIGHT = SCREEN_WIDTH / 3.5, SCREEN_HEIGHT / 3.5
- self.country_name = None
- self.countrys = {}
- self.needed_countrynames = 3
- self.list_of_countries = []
- self.list_of_choosen_countries = []
- @run_once
- def getvalues(self):
- global picked_flag
- global list_of_choosen_countries
- list_of_choosen_countries = []
- random_number = random.randint(1, self.TOTAL_FLAGS) # picks a random image
- # Get a list of all the items in the folder
- items = os.listdir(flag_path) # gets the directory information
- # Get any item in the list
- picked_flag = items[random_number] # it pickes a item from the directory
- print(picked_flag)
- flag_code = picked_flag.replace(".png", "")
- flag_code = flag_code.upper() # transforms the imagename to an alpha_2 code
- print(flag_code)
- self.country_name = pycountry_convert.country_alpha2_to_country_name(flag_code) # converts the alpha_2 code into the country name
- print(self.country_name)
- for country in countries:
- self.list_of_countries.append(f"{country.name}")
- for i in range(0, self.needed_countrynames):
- random_item = random.choice(self.list_of_countries)
- list_of_choosen_countries.append(random_item)
- print(list_of_choosen_countries)
- Game.Easy_name2flag_game.rendering(self=Game.Easy_name2flag_game())
- def rendering(self): # renders the text, etc.
- global text_render
- var = Game.Easy_name2flag_game()
- print(var.list_of_choosen_countries)
- text_render = COUNTRY_FONT.render(self.country_name, 1, BLACK)
- print(self.country_name)
- print("rendering done")
- def handel(self):
- global flag
- for i in range(0, self.displayed_flags):
- Game.Easy_name2flag_game.getvalues(self=Game.Easy_name2flag_game())
- flag_img = pygame.image.load(
- os.path.join('flags', picked_flag)) # Search the choosen flag
- flag = pygame.transform.scale(flag_img, (self.FLAG_WIDTH, self.FLAG_HEIGHT))
- class frontend:
- def __init__(self):
- self.gameselection_1 = pygame.Rect(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 10,
- SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 5,
- 200,
- 100
- )
- self.flag_easy_flaggame = pygame.Rect(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 7,
- SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 2.5,
- 200,
- 100
- )
- self.country_choice_1 = pygame.Rect(SCREEN_WIDTH / 10 - SCREEN_WIDTH / 15,
- SCREEN_HEIGHT / 2,
- 200,
- 100
- )
- self.country_choice_2 = pygame.Rect(500,
- 500,
- 200,
- 100
- )
- def draw_gameselection(self):
- WIN.fill(WHITE)
- gameselection = SELECTION_FONT.render("Easy-Flag-Game", 1, BLACK)
- WIN.blit(gameselection, (self.gameselection_1.x, self.gameselection_1.y))
- def draw_easy_flag_game(self):
- WIN.fill(WHITE)
- WIN.blit(flag, (self.flag_easy_flaggame.x, self.flag_easy_flaggame.y))
- WIN.blit(text_render, (self.country_choice_1.x, self.country_choice_1.y))
- class Maingameloop:
- def __init__(self):
- self.clock = pygame.time.Clock()
- self.run = True
- self.frontend_vars = Game.frontend() # imports the vars from frontend
- self.modus = "Start" # Modus that handels the frontend
- def Gameloop(self):
- while self.run:
- self.clock.tick(FPS) # Sets the hardcoded fps
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.run = False
- pygame.quit() # It checks im the window is getting closed
- if self.modus == "Start":
- Game.frontend.draw_gameselection(self=Game.frontend()) # handels the frontend
- # The actions are in the mode defined that they can be individuall in every mode and can be used multiple times
- if event.type == pygame.MOUSEBUTTONDOWN:
- self.pos = pygame.mouse.get_pos()
- if self.frontend_vars.gameselection_1.collidepoint(self.pos):
- self.modus = "Easy-Flag-Game" # changes the mode
- print("hit")
- if self.modus == "Easy-Flag-Game":
- Game.Easy_name2flag_game.handel(self=Game.Easy_name2flag_game())
- Game.frontend.draw_easy_flag_game(self=Game.frontend())
- pygame.display.update()
- if __name__ == "__main__":
- game = Game.Maingameloop()
- game.Gameloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement