class Scoreboard: """A Class to report scoring information""" def __init__(self, settings, screen): self.screen = screen self.screen_rect = screen.get_rect() self.settings = settings # Font for scoring information self.text_color = (255, 255, 255) self.font = pygame.font.Font("Font/super_mario_bros.ttf", 24) # Prepare the initial score image self.prep_score() self.prep_timer() self.prep_lives() self.prep_world() self.prep_coins() def prep_score(self): """Turn the score into a rendered image""" self.score_text = TextBox(self.settings, self.screen) self.score_text.update_text("SCORE") self.score_text.text_rect.top = self.screen_rect.top + 5 self.score_text.text_rect.left = self.screen_rect.left + 50 self.score_str = "{:,}".format(self.settings.score) self.score_image = self.font.render(self.score_str, True, self.text_color, self.settings.bg_color) # Display the score at the top right of the screen self.score_rect = self.score_image.get_rect() self.score_rect.centerx = self.score_text.text_rect.centerx self.score_rect.top = self.score_text.text_rect.bottom def show_score(self, screen, settings): self.score_text.draw(screen) score_str = "{:,}".format(self.settings.score) self.score_image = self.font.render(score_str, True, self.text_color, self.settings.bg_color) screen.blit(self.score_image, self.score_rect) self.timer_text.draw(screen) timer_str = "{:,}".format(self.settings.timer) self.timer_image = self.font.render(timer_str, True, self.text_color, self.settings.bg_color) screen.blit(self.timer_image, self.timer_rect) self.lives_text.draw(screen) lives_str = "{:,}".format(self.settings.lives) self.lives_image = self.font.render(lives_str, True, self.text_color, self.settings.bg_color) screen.blit(self.lives_image, self.lives_rect) self.world_text.draw(screen) # world_str = " {:,}".format(self.settings.world_level) self.world_image = self.font.render(self.settings.world_level, True, self.text_color, self.settings.bg_color) screen.blit(self.world_image, self.world_rect) self.coin_text.draw(screen) coin_str = " {:,}".format(settings.coin_count) self.coin_image = self.font.render(coin_str, True, self.text_color, self.settings.bg_color) screen.blit(self.coin_image, self.coin_rect) def prep_timer(self): timer_str = "{:,}".format(self.settings.timer) self.timer_image = self.font.render(timer_str, True, self.text_color, self.settings.bg_color) self.timer_text = TextBox(self.settings, self.screen) self.timer_text.update_text("TIMER") self.timer_text.text_rect.centerx = self.score_text.text_rect.centerx + 175 self.timer_text.text_rect.top = self.screen_rect.top + 5 # Display the score at the top right of the screen self.timer_rect = self.timer_image.get_rect() self.timer_rect.centerx = self.score_text.text_rect.centerx + 175 self.timer_rect.top = self.timer_text.text_rect.bottom def prep_lives(self): """Turn level into a rendered image""" self.lives_text = TextBox(self.settings, self.screen) self.lives_text.update_text("LIVES") self.lives_text.text_rect.right = self.settings.screen_width - 50 self.lives_text.text_rect.top = self.screen_rect.top + 5 lives_str = " {:,}".format(self.settings.lives) self.lives_image = self.font.render(lives_str, True, self.text_color, self.settings.bg_color) self.lives_rect = self.lives_image.get_rect() self.lives_rect.centerx = self.lives_text.text_rect.centerx self.lives_rect.top = self.lives_text.text_rect.bottom def prep_world(self): self.world_text = TextBox(self.settings, self.screen) self.world_text.update_text("WORLD") self.world_text.text_rect.centerx = self.settings.screen_width / 2 self.world_text.text_rect.top = self.screen_rect.top + 5 self.world_image = self.font.render(self.settings.world_level, True, self.text_color, self.settings.bg_color) self.world_rect = self.world_image.get_rect() self.world_rect.centerx = self.world_text.text_rect.centerx self.world_rect.top = self.world_text.text_rect.bottom def prep_coins(self): self.coin_text = TextBox(self.settings, self.screen) self.coin_text.update_text("COINS") self.coin_text.text_rect.centerx = self.lives_rect.centerx - 175 self.coin_text.text_rect.top = self.screen_rect.top + 5 coin_str = " {:,}".format(self.settings.coin_count) self.coin_image = self.font.render(coin_str, True, self.text_color, self.settings.bg_color) self.coin_rect = self.coin_image.get_rect() self.coin_rect.centerx = self.coin_text.text_rect.centerx self.coin_rect.top = self.coin_text.text_rect.bottom
class MainWindow(QtGui.QMainWindow): ''' Creates the main window for the program and creates the widgets and menus for it. Works as a path for GUILogic, sidepanel, tablegui and handgui to contact each other. ''' def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.tablegui = TableGUI(self) self.setCentralWidget(self.tablegui) # Central Widget self.gui = GUILogic(self) self.sidepanel = SidePanel(self) self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.sidepanel) self.handgui = handGUI(self) self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.handgui) self.textbox = TextBox(self) self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, self.textbox) self.handgui.setStatusTip("Your hand.") self.tablegui.setStatusTip("Table") self.sidepanel.setFixedWidth(100) self.handgui.setFixedWidth(100) menubar = QtGui.QMenuBar(self) self.setMenuBar(menubar) self.menubar = self.create_menus(menubar) self.statusbar = self.statusBar() self.resize(850,680) self.setWindowTitle("Kasino") self.setMinimumSize(700, 500) text = "Welcome to play Kasino!" self.textbox.update_text(text) # Getters ######################################## def get_sidepanel(self): return self.sidepanel def get_menubar(self): return self.menubar def get_guilogic(self): return self.gui def get_tablegui(self): return self.tablegui def get_handgui(self): return self.handgui def get_textbox(self): return self.textbox ######################################## def create_menus(self, menubar): # File menu fileMenu = menubar.addMenu("&File") # File actions newAction = fileMenu.addAction("&New Game") saveAction = fileMenu.addAction("&Save") loadAction = fileMenu.addAction("&Load") fileMenu.addSeparator() exitAction = fileMenu.addAction("E&xit") newAction.setShortcut('Ctrl+N') newAction.setStatusTip('Start a new game.') newAction.triggered.connect(self.newAct) saveAction.setShortcut('Ctrl+S') saveAction.setStatusTip('Save your current game.') saveAction.triggered.connect(self.saveAct) loadAction.setShortcut('Ctrl+O') loadAction.setStatusTip('Load game.') loadAction.triggered.connect(self.loadAct) exitAction.setStatusTip('Exit application.') exitAction.triggered.connect(self.close) # built-in close thingy # Help menu helpMenu = menubar.addMenu("&Help") # Help actions rulesAction = helpMenu.addAction("&Rules and hints") aboutAction = helpMenu.addAction("&About") rulesAction.setStatusTip('Show rules of the game.') rulesAction.triggered.connect(self.rulesAct) aboutAction.setStatusTip('Show information about the game.') aboutAction.triggered.connect(self.aboutAct) return menubar def newAct(self): self.sidepanel.enable_buttons() self.gui.new_game() def saveAct(self): if (self.gui.get_game() != None): fileName = QtGui.QFileDialog.getSaveFileName(None, 'Save Game', '', '.txt') self.gui.save_game(fileName) else: # In case of trying to save a game with no game on box = QtGui.QMessageBox() box.setWindowTitle("Error") box.setIcon(QtGui.QMessageBox.Critical) box.setText("You must create a game first!") box.addButton(QtGui.QMessageBox.Ok) box.exec_() def loadAct(self): fileName = QtGui.QFileDialog.getOpenFileName(None, 'Load Game', '', '.txt') if (fileName): self.gui.load_game(fileName) def rulesAct(self): try: file = open("rules.txt", 'r') text = '' box = QtGui.QMessageBox() box.setWindowTitle("Rules and hints") for line in file: line = line.rstrip() text += line + '\n' box.setText(text) box.setFixedSize(400,300) box.exec_() except IOError: error = QtGui.QMessageBox() error.setWindowTitle("Error") error.setText("An error occured while trying to open the file.") error.exec_() def aboutAct(self): about = QtGui.QMessageBox() about.setWindowTitle("About") about.setText("This game was made for the Python course CSE-A1121 in Spring 2014.\n" + \ "Project done by Aleksi Simell.") about.exec_()
def run_game(): settings = Settings() pygame.init() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height)) pygame.display.set_caption("Mario") clock = pygame.time.Clock() current_level = Level1_1(screen, settings) # current_level = Level1_2(screen, settinggit pus) # current_level = Level1_3(screen, settings) # background = Background(screen, settings) mario = Character(screen, settings) blocks = current_level.blocks blocks.add(current_level.floor_blocks) pipes = current_level.pipes items = current_level.items # enemies = current_level.enemies sb = Scoreboard(settings, screen) start_text = [] start_image = pygame.image.load("Images/Misc/Starting-Screen.png") start_image_rect = start_image.get_rect() start_image_rect.top = screen.get_rect().top + 75 start_image_rect.left = screen.get_rect().left + 75 start_image_2 = pygame.image.load("Images/Misc/Starting-Screen-2.png") start_image_2_rect = start_image_2.get_rect() start_image_2_rect.top = start_image_rect.bottom start_image_2_rect.centerx = start_image_rect.centerx start_image_3 = pygame.image.load("Images/Misc/Starting-Screen-3.png") start_image_3_rect = start_image_3.get_rect() start_image_3_rect.top = start_image_2_rect.bottom start_image_3_rect.centerx = start_image_2_rect.centerx start_text_4 = TextBox(settings, screen) start_text_4.update_font("Font/super_mario_bros.ttf", 18) start_text_4.update_text("Press space to start!") start_text_4.text_rect.left = settings.screen_width / 2 start_text_4.text_rect.centery = settings.screen_height / 2 start_text.append([start_image, start_image_rect]) start_text.append([start_image_2, start_image_2_rect]) start_text.append([start_image_3, start_image_3_rect]) start_text.append(start_text_4) while True: clock.tick(60) mario.update(settings, screen, current_level, mario, blocks, pipes, items, current_level.enemies) gf.update_screen(screen, settings, mario, current_level, sb, start_text) gf.check_events(screen, settings, mario, current_level.background)