def set_level(self, reset): #This method sets the level to self.gamefield that user has chosen if possible #It is called via keypress event when enter is pressed and user is selecting level (self.selecting_level == True) #reset is a bool. if true this method is called from reset_gui otherwise this is called after keypress enter try: path = os.getcwd() #get the current working dir os.chdir(path) #change dir to the current working dir os.chdir('game_levels') #open the file if possible if not reset: file = open(self.level_text, 'r') else: #method is called from reset_gui file = open(self.gamefield_level, 'r') #parse a gamefield and a copy from the file gamefield = GameField() return_value = gamefield.parse_gamefield(file) file.close() if return_value: #if return value is False, the level is broken self.set_gamefield(gamefield) if not reset: #if method is called from reset_gui these shouldn't be executed self.gamefield_level = self.level_text #this is used by reset_gui method #show user a message screen that level change was successfull self.message_box.setIcon(QMessageBox.Information) self.message_box.setText('Level successfully changed') else: #create a message box that informs the user self.message_box.setIcon(QMessageBox.Warning) self.message_box.setText( 'Level is not ok, using the previous level') except FileNotFoundError: self.message_box.setIcon(QMessageBox.Warning) self.message_box.setText('Level with name {} not found'.format( self.level_text)) finally: os.chdir( path ) #This changes back to working directory where classes etc are defined if not reset: #show the message box to the user self.message_box.setWindowTitle('Level select') self.message_box.show()
def set_level(self): #This method sets the level to self.gamefield that user has chosen if possible #It is called via keypress event when enter is pressed and user is selecting level (self.selecting_level == True) try: path = os.getcwd() #get the current working dir os.chdir(path) #change dir to the current working dir os.chdir('game_levels') #open the file if possible file = open(self.level_text, 'r') #parse a gamefield and a copy from the file gamefield = GameField() game_copy = GameField() return_value = gamefield.parse_gamefield(file) file.seek(0) #This is very important otherwise program will die game_copy.parse_gamefield(file) file.close() if return_value: #if return value is False, the level is broken self.set_gamefield(gamefield, game_copy) self.gamefield_level = self.level_text #this is used by reset_gui method print('Level successfully changed') else: print('Level is not ok, using the previous level') except FileNotFoundError: print('Level with name {} not found'.format(self.level_text)) finally: os.chdir( path ) #This changes back to working directory where classes etc are defined
print('The game is partially using standard set values') path = os.getcwd() #get the current working dir os.chdir('game_levels') #file = open('erikoisempi_kentta.txt','r') #file = open('vihollis_testi_kentta.txt','r') file = open('game_testi.txt', 'r') #file = open('leijuva_testi_kentta.txt','r') #file = open ('erilainen_kentta.txt','r') #file = open('melko_tyhja_kentta.txt','r') os.chdir(path) #change dir to the current working dir gamefield = GameField() gamefield.parse_gamefield(file) file.close() main = Gui(gamefield) #print(Gui.Level_number) #print(Config.level_number) #main.set_gamefield(gamefield) main.gamefield_level = 'game_testi.txt' main.show() sys.exit(app.exec_()) else:
def reset_gui(self): #this method is called when game has ended and is #switched back to starting screen #this is test version of this method #Init win and lose screen values for a new game self.first_win_screen = True self.first_lose_screen = True self.restart = True # This adjusts scene rect when user restarts the game #initialize gamefield's key press capturing class variables GameField.Right_arrow = 0 GameField.Left_arrow = 0 GameField.Space = 0 #create same widget as in init, make scene and view not drawn self.scene.setSceneRect(0, 0, 0, 0) self.scene_view.setFixedSize(0, 0) #delete all the items in the scene self.scene.clear() #These need to be redefined because the old items were destroyed above self.end_text = QGraphicsTextItem('GAME OVER,') self.continue_text = QGraphicsTextItem( 'Continue by pressing the button') self.win_text = QGraphicsTextItem('YOU WON!') self.lose_text = QGraphicsTextItem('YOU LOST') #initialize gamefield path = os.getcwd() #get the current working dir os.chdir(path) #change dir to the current working dir os.chdir('game_levels') #open the file, it should always success because this file has been already opened and checked file = open(self.gamefield_level, 'r') #parse a gamefield and a copy from the file gamefield = GameField() return_value = gamefield.parse_gamefield(file) file.close() self.gamefield = gamefield os.chdir(path) #change dir to the current working dir #initialize all lists of scene items self.enemy_items = [] self.static_items = [] #set the correct screen style GameField.Fail = False pixmap = QPixmap( 'start_screen_test.png') #this pic will be changed later widget = QLabel() widget.setPixmap(pixmap) widget.setScaledContents(True) self.setCentralWidget(widget) self.centralWidget().setLayout(self.layout) #enable start and cancel buttons and disable continue button self.start_button.setEnabled(True) self.start_button.setFixedSize(70, 50) self.cancel_button.setEnabled(True) self.cancel_button.setFixedSize(70, 50) self.continue_button.setFixedSize(0, 0) self.continue_button.setEnabled(False) #this is for changing the background color, prior was used for testing (that's why name is not good) self.count = 0
if config.config_return_value: #If is true when return value is 1 or 2 if config.config_return_value == 2: print('NOTICE: There is something wrong in the config file') print('The game is partially using standard set values') #file = open('erikoisempi_kentta.txt','r') #file = open('vihollis_testi_kentta.txt','r') file = open('game_testi.txt', 'r') #file = open('leijuva_testi_kentta.txt','r') #file = open ('erilainen_kentta.txt','r') #file = open('melko_tyhja_kentta.txt','r') gamefield = GameField() gamefield.parse_gamefield(file) file.close() #file = open('erikoisempi_kentta.txt','r') #file = open('vihollis_testi_kentta.txt','r') #file = open('game_testi.txt','r') #file = open('leijuva_testi_kentta.txt','r') #file = open ('erilainen_kentta.txt','r') file = open('melko_tyhja_kentta.txt', 'r') gamefield_copy = GameField() gamefield_copy.parse_gamefield(file) file.close() main = Gui()
def main(): file1 = open('game_testi.txt','r') gamefield1 = GameField() print(gamefield1.player) print(GameField.Space) gamefield1.parse_gamefield(file1) print('leveli on :{}'.format(gamefield1.max_level)) print(gamefield1.player) print(gamefield1.player.position.x_min) #this should be 100 print(gamefield1.player.position.x_max) #this should be 130 print(gamefield1.player.position.y_max) #this should be 250+3*50=400 print(gamefield1.player.position.y_min) # 460 #those coordinates (in comments) were wrong and the should be ignored for i in range(0,len(gamefield1.ground_objects)): ob = gamefield1.ground_objects[i].position print('ground objekti y_min: {} y_max: {} x_min: {} x_max: {}'.format(ob.y_min,ob.y_max, ob.x_min, ob.x_max)) print() for i in range (len(gamefield1.static_objects)): #print all positions (compare to game_testi.txt) ob = gamefield1.static_objects[i].position if gamefield1.static_objects[i].type == 'finish_object': print('maali loytyy') print('y_min: {} y_max: {} x_min: {} x_max: {}'.format(ob.y_min,ob.y_max, ob.x_min, ob.x_max)) for i in range (len(gamefield1.enemies)): enemy = gamefield1.enemies[i].position print('enemies in file: y_min {}, y_max {}, x_min {}, x_max {}'.format(enemy.y_min, enemy.y_max, enemy.x_min, enemy.x_max)) enemy = gamefield1.enemies[1].position #2 enemy which is read so it's enemy at the bottom left corner print('enemy 2 in file: y_min {}, y_max {}, x_min {}, x_max {}'.format(enemy.y_min, enemy.y_max, enemy.x_min, enemy.x_max)) print(gamefield1.static_objects[27].type) #shoud be invisible file1.close() print() file2 = open('game_testi2.txt','r') gamefield2 = GameField() gamefield2.parse_gamefield(file2) #should be same as test 1 but 1 more line so y location should be shiftet 50 down for i in range (len(gamefield2.static_objects)): #print all positions (compare to game_testi.txt) ob = gamefield2.static_objects[i].position print('y_min: {} y_max: {} x_min: {} x_max: {}'.format(ob.y_min,ob.y_max, ob.x_min, ob.x_max)) file2.close() print() #let's test malformed files file3 = open ('game_testi_broken1.txt') #this file is missing one - so it's not well-formed gamefield3 = GameField() ret = gamefield3.parse_gamefield(file3) #should return false and print error message print(ret) file3.close() gamefield4 = GameField() file4 = open('game_testi_broken2.txt') #file is missing finish print() gamefield4.parse_gamefield(file4) file4.close() gamefield5 = GameField() file5 = open('game_testi_broken3.txt') #file contains too many player locations print() gamefield5.parse_gamefield(file5) file5.close() gamefield6 = GameField() file6 = open('game_testi_broken4.txt') #file contains incorrect object symbol print() gamefield6.parse_gamefield(file6) file5.close()