def test_generate_values(): from Controller import Controller controller = Controller() logic = GameLogic(controller) logic.generate_mines(10, 10) assert len(logic.field_values) == 10 assert len(logic.field_values[0]) == 10
def preference_button(window, pre_dictionary, choice_dic, name): return_dictionary = {} for preference in list(choice_dic.keys()): return_dictionary[preference] = [] i = 0 for choice in choice_dic[preference]: if choice.get() == 1: return_dictionary[preference].append(pre_dictionary[preference][i]) i += 1 gL.add_preferences_to_user(name.get(), return_dictionary) start_menu.start_menu_window(window, name)
def __init__(self, parent=None): """ Initializes Controller and connects the logic with the UI :param parent: Parent window for UI """ super().__init__(parent) self.myForm = Ui_MainWindow() self.myForm.setupUi(self) self.myForm.pushButton.clicked.connect(self.start_game) self.myForm.b1.clicked.connect(self.restart_game) self.logic = GameLogic(self)
def play_game(self): """ Initialize the game logic and handle whatever the game logic returns from a run of the game. """ self.__logic = GameLogic(self.__graphics, self.__resources, self.__options) self.__logic.add_player() levels = level_convert() status = CLEARED # # Play the levels # for i in range(len(levels)): lev = levels[i] # Background music # TODO: Different music for different levels if self.__options.music: pygame.mixer.music.load(join(MP3_PATH,"cruising.mp3")) pygame.mixer.music.set_volume(0.8) pygame.mixer.music.play() # Prepare the level self.__logic.clear() self.__graphics.reset_distance() self.__graphics.set_scroll(5) self.__logic.set_level(lev) status = self.__logic.game_loop() if status != CLEARED: break # Last level cleared = game cleared! if i != len(levels)-1: self.level_cleared() # Turn off the music, since game is obviously over pygame.mixer.music.stop() # # Depending on how the game went, show some information, or not. # if status == CLEARED: self.game_cleared() self.main_menu() elif status == DIED: self.game_over() elif status == ABORTED: self.main_menu() else: self.quit()
def validateRegister(window, frame, name, pword, fileBackground, fileBackground2): validateR = gL.register(name.get(), pword.get()) # 0 - user exist, -1- password not ok, -2-user name not ok, else- everything ok go to preference menu if validateR == 0: label = Label(frame, text='User Exists!', fg='red', bg="light blue", font=("Comic Sans MS", 12)) label.grid(row=5, columnspan=2, pady=(5, 5)) return elif validateR == -1: label = Label(frame, text='Please enter only 6-12 chars in password!', fg='red', bg="light blue", font=("Comic Sans MS", 12)) label.grid(row=5, columnspan=2, pady=(5, 5)) elif validateR == -2: label = Label(frame, text='Please enter at least 2 chars in username!', fg='red', bg="light blue", font=("Comic Sans MS", 12)) label.grid(row=5, columnspan=2, pady=(5, 5)) else: preferences_page.preference_window(window, name, fileBackground, fileBackground2)
def showQuestion(Gamer_name, window, frame, GameInfoDict, numberOfQ, answers, get_anwser): answers.append(get_anwser) if numberOfQ < len(GameInfoDict['questions'].keys()): list = frame.grid_slaves() for l in list: l.destroy() frame = Frame(window) frame.grid(row=1, column=1) list_of_q = [] for question_name in GameInfoDict['questions'].keys(): list_of_q.append(question_name) q = list_of_q[numberOfQ] question = Label(frame, text=q, fg='black', font='Ariel 16 bold') question.grid(row=0, column=0, pady=(10, 10)) a1_text = GameInfoDict['questions'][q]['answers'][0] a1 = Button(frame, text=a1_text, bg="blue", fg="white", width=20, font='Ariel 12 bold', command=lambda: showQuestion(Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a1_text)) a1.grid(row=1, column=0, pady=(5, 5)) a2_text = GameInfoDict['questions'][q]['answers'][1] a2 = Button(frame, text=a2_text, bg="blue", fg="white", width=20, font='Ariel 12 bold', command=lambda: showQuestion(Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a2_text)) a2.grid(row=2, column=0, pady=(5, 5)) a3_text = GameInfoDict['questions'][q]['answers'][2] a3 = Button(frame, text=a3_text, bg="blue", fg="white", width=20, font='Ariel 12 bold', command=lambda: showQuestion(Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a3_text)) a3.grid(row=3, column=0, pady=(5, 5)) a4_text = GameInfoDict['questions'][q]['answers'][3] a4 = Button(frame, text=a4_text, bg="blue", fg="white", width=20, font='Ariel 12 bold', command=lambda: showQuestion(Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a4_text)) a4.grid(row=4, column=0, pady=(5, 5)) right_answer = Label(frame, text=GameInfoDict['questions'][q]['true'], fg='black', font='Ariel 8') right_answer.grid(row=5, column=0, pady=(10, 10)) else: del answers[0] grade = gL.end(Gamer_name.get(), answers, GameInfoDict, 1) list = frame.grid_slaves() for l in list: l.destroy() frame = Frame(window) frame.grid(row=1, column=1) frame.grid_columnconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1) message1 = Label(frame, text='you grade: ' + str(grade), fg='black', font='Ariel 16 bold') message1.grid(row=0, column=0, pady=(5, 5)) bottonEasy = Button(frame, text='Back to menu', bg="green", fg="white", font='Ariel 12 bold', command=lambda: start(window, Gamer_name)) bottonEasy.grid(row=1, column=0, pady=(5, 5))
def validateRegister(window, frame, name, pword): print(name.get(), pword.get()) validateR = gL.register(name.get(), pword.get()) if validateR == 0: label = Label(frame, text='User Exists!', fg='red', font='Ariel 8 bold') label.grid(row=5, columnspan=2, pady=(10, 10)) else: preferences_page.preference_window(window, name)
def validateLogin(window, frame, name, pword): print(name.get(), pword.get()) status = gL.login(name.get(), pword.get()) if status == -1: Need = Label(frame, text='User does not exist', fg='red', font='Ariel 8 bold') Need.grid(row=5, columnspan=2, pady=(10, 10)) elif status == 0: preferences_page.preference_window(window, name) else: start_menu.start_menu_window(window, name)
def preference_button(window, frame, pre_dictionary, choice_dic, name, rowindex, fileBackground2): return_dictionary = {} for preference in list(choice_dic.keys()): return_dictionary[preference] = [] i = 0 for choice in choice_dic[preference]: if choice.get() == 1: return_dictionary[preference].append( pre_dictionary[preference][i]) i += 1 # check if the list is empty if len(return_dictionary['Genre']) == 0: Need = Label(frame, text='must choose Genre', fg='red', bg="white", font=("Comic Sans MS", 12)) Need.grid(row=rowindex + 2, columnspan=7, pady=(5, 5)) else: # everything ok gL.add_preferences_to_user(name.get(), return_dictionary) # go to stare menu start_menu.start_menu_window(window, name, fileBackground2)
def validateLogin(window, frame, name, pword, fileBackground, fileBackground2): status = gL.login(name.get(), pword.get()) # -1 - user does not exist, 0 - user ok but does not choose preferences, else- everything ok if status == -1: Need = Label(frame, text='User does not exist', fg='red', bg="light blue", font=("Comic Sans MS", 12)) Need.grid(row=5, columnspan=2, pady=(5, 5)) elif status == 0: preferences_page.preference_window(window, name, fileBackground, fileBackground2) else: start_menu.start_menu_window(window, name, fileBackground2)
def test_logic_init(): from Controller import Controller controller = Controller() logic = GameLogic(controller)
class Controller: # # def __init__(self): """ pygame.init() does initialize parts of the sound, but according to the documentation for the mixer module, safest way is to do mixer.pre_init, then pygame.init and then mixer.init. The controller also makes sure any resources needed in the game are preloaded. """ self.__options = Options.load() self.__resources = Resources() pygame.mixer.pre_init(frequency = 44100, buffer = 2048) pygame.init() pygame.mixer.init(frequency = 44100, buffer = 2048) self.__graphics = Graphics(self.__resources, self.__options) # Load all graphics and sound effects before the game is started self.__resources.preload_all() self.__logic = None def main(self): self.main_menu() def play_game(self): """ Initialize the game logic and handle whatever the game logic returns from a run of the game. """ self.__logic = GameLogic(self.__graphics, self.__resources, self.__options) self.__logic.add_player() levels = level_convert() status = CLEARED # # Play the levels # for i in range(len(levels)): lev = levels[i] # Background music # TODO: Different music for different levels if self.__options.music: pygame.mixer.music.load(join(MP3_PATH,"cruising.mp3")) pygame.mixer.music.set_volume(0.8) pygame.mixer.music.play() # Prepare the level self.__logic.clear() self.__graphics.reset_distance() self.__graphics.set_scroll(5) self.__logic.set_level(lev) status = self.__logic.game_loop() if status != CLEARED: break # Last level cleared = game cleared! if i != len(levels)-1: self.level_cleared() # Turn off the music, since game is obviously over pygame.mixer.music.stop() # # Depending on how the game went, show some information, or not. # if status == CLEARED: self.game_cleared() self.main_menu() elif status == DIED: self.game_over() elif status == ABORTED: self.main_menu() else: self.quit() # # Menus below # def main_menu(self): o = self.__options entries = ["Start game", "Instructions", "Options", "Quit"] funs = [lambda _: self.play_game(), lambda _: self.instructions() ,lambda _: self.options(), lambda _: self.quit()] active = 0 self.__graphics.main_menu(entries, active) keys = [o.up, o.down, o.confirm] kpress = wait_for_key(keys) while kpress in [o.up, o.down]: if kpress == o.up and active > 0: active -= 1 elif kpress == o.down and active < len(entries) - 1: active += 1 self.__graphics.main_menu(entries, active) kpress = wait_for_key(keys) if kpress == o.confirm: f = funs[active] f(0) else: self.quit() # TODO: Collect and print stats from the level, too def level_cleared(self): self.__graphics.paint_level_cleared() anykey() # TODO: Highscores and stuff def game_cleared(self): self.__graphics.paint_game_cleared() anykey() # Shows an options menu and allows setting various game parameters def options(self, active = 0): o = self.__options key_opts = [ ("Left:", o.left), ("Down:", o.down), ("Up:", o.up) , ("Right:", o.right), ("Fire:", o.fire) , ("Abort:", o.abort), ("Confirm:", o.confirm) ] bool_opts = [ ("Fullscreen: ", o.fullscreen), ("Music: ", o.music) , ("Sound effects: ", o.sfx) ] self.__graphics.paint_options(key_opts, bool_opts, active, False) keys = [o.up, o.down, o.confirm, o.abort] kpress = wait_for_key(keys) while kpress in [o.up, o.down]: if kpress == o.up and active > 0: active -= 1 elif kpress == o.down and active < 9: active += 1 self.__graphics.paint_options(key_opts, bool_opts, active, False) kpress = wait_for_key(keys) if kpress == o.confirm: # Key options if active < len(key_opts): # Paint selection and wait for input self.__graphics.paint_options(key_opts, bool_opts, active, True) key = anykey() # Only keys that are not already assigned may be chosen if key not in self.__options.get_all_keys(): self.__options.set_index(active, key) self.__options.save() # Boolean options, just reverse the value else: fs = self.__options.fullscreen val = self.__options.get_index(active) self.__options.set_index(active, not val) # If the fullscreen option was changed, reinitialize the # graphics if fs != self.__options.fullscreen: pygame.display.quit() self.__graphics = Graphics(self.__resources, self.__options) self.options(active) elif kpress == o.abort: self.main_menu() else: # event == QUIT self.quit() # TODO: A describing text about the gameplay def instructions(self): opts = self.__options enemies = all_ships() funs = [lambda _: self.__graphics.paint_instructions_keys(), lambda _: self.__graphics.paint_instructions_ship(), lambda _: self.__graphics.paint_instructions_enemies(enemies) ] active = 0 kpress = None while active >= 0 and active <= 2 and kpress != QUIT: screen = funs[active] screen(0) kpress = wait_for_key([opts.left, opts.right]) if kpress == opts.left: active -= 1 elif kpress == opts.right: active += 1 if kpress == QUIT: self.quit() else: self.main_menu() def game_over(self): self.__graphics.game_over() # There is an option of restarting without going through the main menu if yn_key(): self.play_game() else: self.main_menu() def quit(self): self.__options.save() pygame.mixer.quit() pygame.quit()
def getpre(): global preferences_dict preferences_dict = gL.get_all_preferences()
class Controller(QMainWindow): """ Controller for the program, adding events to the ui """ def __init__(self, parent=None): """ Initializes Controller and connects the logic with the UI :param parent: Parent window for UI """ super().__init__(parent) self.myForm = Ui_MainWindow() self.myForm.setupUi(self) self.myForm.pushButton.clicked.connect(self.start_game) self.myForm.b1.clicked.connect(self.restart_game) self.logic = GameLogic(self) def start_game(self): """ Initializes the game window content and generates mines on the logic component :return: none """ x, y = self.myForm.get_x_y() self.myForm.teardown_menu() self.myForm.setupMines(x, y) for row in self.myForm.mines: for item in row: item.leftclick_handler = self.logic.leftclick_button item.rightclick_handler = self.logic.rightclick_button self.logic.generate_mines(x, y) self.logic.generate_mine_numbers(x, y) def restart_game(self): """ Restarts the game by tearing down the UI, show restart game window and resets the logic :return: none """ self.myForm.teardown_game() self.myForm.showStart() self.logic.restart_game() self.myForm.pushButton.clicked.connect(self.start_game) self.myForm.d.close() def has_won(self): """ Handles the has_won event, when triggered locks all fields and shows win window :return: none """ x, y = self.myForm.get_x_y() for y_pos in range(y): for x_pos in range(x): self.set_button_text(x_pos, y_pos) self.myForm.disableButton(x_pos, y_pos) self.myForm.showWinRestart() def end_game(self, button): """ Triggered when a mine is hit. Locks all fields and sets mine to red :param button: button from which the event is triggered :return: none """ x, y = self.myForm.get_x_y() for y_pos in range(y): for x_pos in range(x): self.set_button_text(x_pos, y_pos) self.myForm.disableButton(x_pos, y_pos) button.setStyleSheet(""" QPushButton { background-color: red } """) self.myForm.showRestart() def set_button_text(self, x, y): """ Merges data from logic component into the UI component on coordinate :param x: coordinate :param y: coordinate :return: none """ self.myForm.mines[y][x].setText(str(self.logic.get_field_text(x, y)))
def showQuestion(Gamer_name, window, frame, GameInfoDict, numberOfQ, answers, get_anwser, fileBackground2): answers.append(get_anwser) if numberOfQ < len(GameInfoDict['questions'].keys()): list = frame.grid_slaves() for l in list: l.destroy() frame = Frame(window, bg="white") frame.grid(row=1, column=1) list_of_q = [] for question_name in GameInfoDict['questions'].keys(): list_of_q.append(question_name) q = list_of_q[numberOfQ] question = Label(frame, text=q, fg='black', bg="white", font=("Comic Sans MS", 14)) question.grid(row=0, column=0, pady=(10, 10)) a1_text = GameInfoDict['questions'][q]['answers'][0] a1 = Button(frame, text=a1_text, bg="#3abdef", fg="white", width=30, font=("Comic Sans MS", 14), command=lambda: showQuestion( Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a1_text, fileBackground2)) a1.grid(row=1, column=0, pady=(5, 5)) a2_text = GameInfoDict['questions'][q]['answers'][1] a2 = Button(frame, text=a2_text, bg="#3abdef", fg="white", width=30, font=("Comic Sans MS", 14), command=lambda: showQuestion( Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a2_text, fileBackground2)) a2.grid(row=2, column=0, pady=(5, 5)) a3_text = GameInfoDict['questions'][q]['answers'][2] a3 = Button(frame, text=a3_text, bg="#3abdef", fg="white", width=30, font=("Comic Sans MS", 14), command=lambda: showQuestion( Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a3_text, fileBackground2)) a3.grid(row=3, column=0, pady=(5, 5)) a4_text = GameInfoDict['questions'][q]['answers'][3] a4 = Button(frame, text=a4_text, bg="#3abdef", fg="white", width=30, font=("Comic Sans MS", 14), command=lambda: showQuestion( Gamer_name, window, frame, GameInfoDict, numberOfQ + 1, answers, a4_text, fileBackground2)) a4.grid(row=4, column=0, pady=(5, 5)) right_answer = Label(frame, text=GameInfoDict['questions'][q]['true'], bg="white", fg='black', font=("Comic Sans MS", 10)) right_answer.grid(row=5, column=0, pady=(10, 10)) else: # the last question del answers[0] # get grade grade = gL.end(Gamer_name.get(), answers, GameInfoDict, 1) list = frame.grid_slaves() for l in list: l.destroy() background_label = Label(window, image=fileBackground2) background_label.place(x=0, y=0, relwidth=1, relheight=1) frame = Frame(window, bg="white") frame.grid(row=1, column=1) frame.grid_columnconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1) message1 = Label(frame, text='you grade: ' + str(grade), fg='black', bg="white", font=("Comic Sans MS", 16)) message1.grid(row=0, column=0, pady=(5, 5)) bottonEasy = Button( frame, text='Back to menu', bg="#10c716", fg="white", font=("Comic Sans MS", 16), command=lambda: start(window, Gamer_name, fileBackground2)) bottonEasy.grid(row=1, column=0, pady=(5, 5))
import tkinter as tk from tkinter import * import requests from PIL import ImageTk, Image import Objects as O import Player as P from tkinter.ttk import Progressbar from tkinter import Button, Tk, HORIZONTAL from Logic import GameLogic import Logic as L Logic = GameLogic() HEIGHT = 500 WIDTH = 1000 class Game(tk.Tk): def __init__(self): tk.Tk.__init__(self) self._frame = None self.switch_frame(startMenu) def switch_frame(self, frame_class): new_frame = frame_class(self) if self._frame is not None: self._frame.destroy() self._frame = new_frame self._frame.pack()
def getleaderboard(): global leaderboard leaderboard = gL.get_leader_board()
def getGameInfoDict(Gamer_name): global GameInfoDict GameInfoDict = gL.start(Gamer_name.get(), Conventions.EASY_GAME_CODE)
def getGameInfoDict(Gamer_name): global GameInfoDict GameInfoDict = gL.start(Gamer_name.get(), Conventions.CHALLENGING_GAME_CODE)