def MainScreen(mainGame, screen, clock): mainGame.prop_thumbs = pygame.transform.smoothscale( CreateThumbs(mainGame.board, mainGame.cur_player), [385, 170]) roll_dice_button = pygame.Rect( 180, 610, 150, 70) #Create rectangle for roll dice/end turn button buy_prop_button = pygame.Rect( 675, 690, 250, 70 ) #Create rectangle for property buying button (also used for mortgaging and unmortgaging buy_upgrade_button = pygame.Rect(350, 700, 150, 50) sell_upgrade_button = pygame.Rect(520, 700, 150, 50) in_jail_button = pygame.Rect(350, 610, 150, 70) TB_img = pygame.transform.smoothscale( pygame.image.load("img/Tower Block.png"), [75, 75]) CH_img = pygame.transform.smoothscale( pygame.image.load("img/Council House.png"), [75, 75]) font_40 = pygame.font.SysFont('Arial', 40) #Font object for button captions font_28 = pygame.font.SysFont( 'Arial', 28) #font object for displaying whose turn it is (among other things) font_20 = pygame.font.SysFont('Arial', 20) #Font for the upgrade buttons main_buts = [ Button(10, 690, 150, 70, "Leaderboards", font_28), Button(10, 610, 150, 70, "Pause", font_40), Button(900, 160, 100, 50, "Details", font_28) ] dice_but_click = False #Booleans tracking whether the roll dice, end turn and but property buttons have been clicked yet this turn turn_but_click = False buy_but_click = False mort_but_click = False buy_upgrade_but_click = False sell_upgrade_but_click = False leave_bogside_but_click = False use_card_but_click = False msgBox = None exitOnBoxClose = False advanceOnBoxClose = False fps = 10 #Used to determine the waiting between updating the game display main_screen_running = True while main_screen_running: for event in pygame.event.get(): for but in main_buts: but.handle_input_event(event) if msgBox != None: msgBox.handle_input_event(event) if exitOnBoxClose and msgBox.should_exit: main_screen_running = False gotoScreen = -1 if advanceOnBoxClose and msgBox.should_exit: mainGame.advancePlayer() mainGame.prop_thumbs = pygame.transform.smoothscale( CreateThumbs(mainGame.board, mainGame.cur_player), [385, 170] ) #Generate thumbnails for new player (here so it is only done when the player changes, not every frame change) advanceOnBoxClose = False if msgBox.should_exit == False: break if event.type == pygame.QUIT: main_screen_running = False gotoScreen = -1 if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: #Escape key exits the game main_screen_running = False gotoScreen = -1 if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: #Left mouse button mouse_pos = event.pos #Position of the cursor when nouse was clicked if buy_prop_button.collidepoint(mouse_pos): if mainGame.getCurProp( ).prop_owner == -1: #Property is unowned buy_but_click = True elif mainGame.getCurProp( ).prop_owner == mainGame.cur_player: #If owned by current player, it may be mortgaged mort_but_click = True if roll_dice_button.collidepoint(mouse_pos): if mainGame.controller.card_used == False: use_card_but_click = True #Roll dice button was clicked elif mainGame.controller.player_rolled == False: dice_but_click = True #End turn button was clicked else: turn_but_click = True #Button to apply card effects was clicked if in_jail_button.collidepoint( mouse_pos ): #Button to pay £50 to get out of bogside was clicked leave_bogside_but_click = True if buy_upgrade_button.collidepoint(mouse_pos): buy_upgrade_but_click = True if sell_upgrade_button.collidepoint(mouse_pos): sell_upgrade_but_click = True #Clear screen and display main board displayScreenAndBoard(screen, mainGame.board.board_img) if dice_but_click: #If Roll Dice button was clicked #Roll dice, move the piece accordingly, and display the dice rolls mainGame.getDie(0).roll() mainGame.getDie(1).roll() dice_total = mainGame.getDiceTotal() if mainGame.getCurPlayer().player_inJail == False: mainGame.getCurPlayer().movePlayer(dice_total, mainGame.board) elif mainGame.getDie(0).cur_score == mainGame.getDie( 1 ).cur_score: #Doubles rolled, so player gets out of bogside mainGame.getCurPlayer().leaveJail() mainGame.getCurPlayer().movePlayer(dice_total, mainGame.board) #Player does not move otherwise, as they must be lost in bogside #Generate the dice images mainGame.controller.roll_img1 = pygame.transform.smoothscale( mainGame.getDie(0).getImg(), [70, 70]) mainGame.controller.roll_img2 = pygame.transform.smoothscale( mainGame.getDie(1).getImg(), [70, 70]) if mainGame.getDie(0).cur_score != mainGame.getDie( 1 ).cur_score: #If a double has not been rolled (rolling a double gives the player another turn) mainGame.controller.player_rolled = True #So player only gets another turn if they rolled doubles mainGame.controller.may_buy = True if mainGame.getDie(0).cur_score == mainGame.getDie(1).cur_score: mainGame.controller.cur_doubles += 1 if mainGame.controller.cur_doubles >= 3: #If player rolls 3 consecutive doubles, they go to Bogside mainGame.sendCurPlayerToBog() mainGame.controller.player_rolled = True #Will not get to roll again #Determine rent if applicable mainGame.controller.turn_rent = mainGame.determineRent() if mainGame.controller.turn_rent != 0: mainGame.getCurPlayer().spendMoney( mainGame.controller.turn_rent ) #Decrease the player's money and credit the owner of the property that amount if mainGame.getCurProp().prop_type != Prop_Type.PAYMENT: mainGame.getPlayer( mainGame.getCurProp().prop_owner).addMoney( mainGame.controller.turn_rent) #If the current space returns a card if mainGame.getCurProp().prop_type == Prop_Type.POT_LUCK: mainGame.controller.cur_card = mainGame.board.PL_Deck.getNextCard( ) elif mainGame.getCurProp().prop_type == Prop_Type.COUNCIL_CHEST: mainGame.controller.cur_card = mainGame.board.CC_Deck.getNextCard( ) #If card will have just been returned, render the text that will show its effects if mainGame.getCurProp( ).prop_type == Prop_Type.POT_LUCK or mainGame.getCurProp( ).prop_type == Prop_Type.COUNCIL_CHEST: #Card will have been returned mainGame.controller.card_effs, mainGame.controller.card_texts = renderCardTexts( font_28, mainGame.controller.cur_card) mainGame.controller.card_used = False #If the player lands on the 'Go To Bogside' space if mainGame.getCurProp().prop_type == Prop_Type.GO_TO_BOGSIDE: mainGame.sendCurPlayerToBog() #Display whose turn it is, how much money this player has, and show their property overview displayWhoseTurn(screen, font_28, mainGame.getCurPlayer()) displayPlayerMoney(screen, font_28, mainGame.getCurPlayer().player_money) displayPlayerToken(screen, mainGame.getCurPlayer()) displayPropThumbs(screen, mainGame.prop_thumbs, 610, 50) displayDiceScore(screen, mainGame.controller.roll_img1, mainGame.controller.roll_img2) #Show each of the player's pieces at its requisite position on the board displayPieces(screen, mainGame) #Show the Roll Dice/End Turn button, and the appropriate caption if mainGame.controller.card_used == False: displayButtonRect(screen, roll_dice_button, (100, 100, 100), font_40, 'Use Card', (0, 0, 0)) elif mainGame.controller.player_rolled == False: displayButtonRect(screen, roll_dice_button, (100, 100, 100), font_40, 'Roll Dice', (0, 0, 0)) else: displayButtonRect(screen, roll_dice_button, (100, 100, 100), font_40, 'End Turn', (0, 0, 0)) if mainGame.getCurPlayer().player_inJail and mainGame.getCurPlayer( ).player_hasBogMap: #If player is lost in bogside, but they have the equivelant of a "Get out of Jail Free" card displayButtonRect(screen, in_jail_button, (100, 100, 100), font_28, 'Use Map', (0, 0, 0)) elif mainGame.getCurPlayer().player_inJail: #Don't have card displayButtonRect(screen, in_jail_button, (100, 100, 100), font_28, 'Buy Map (£50)', (0, 0, 0)) #Display title deed for property currently on if mainGame.getCurProp( ).prop_type == Prop_Type.NORMAL or mainGame.getCurProp( ).prop_type == Prop_Type.SCHOOL or mainGame.getCurProp( ).prop_type == Prop_Type.STATION: #If property actually will have a title deed to display title_deed = pygame.transform.smoothscale( mainGame.getCurProp().getTitleDeed(), [270, 400]) screen.blit(title_deed, [665, 230]) if mainGame.getCurProp().prop_type == Prop_Type.NORMAL: #Normal properties are the only ones that can have Council Houses and Tower Blocks on them displayUpgrades(screen, CH_img, TB_img, mainGame.getCurProp(), font_40) if mainGame.getCurProp().prop_owner == mainGame.cur_player: if mainGame.board.wholeGroupOwned( mainGame.cur_player, mainGame.getCurPlayer().player_pos ): #May only be bought if the property is owned by the current player and the entire colour group is owned if mainGame.getCurProp().C_Houses < 4: displayButtonRect(screen, buy_upgrade_button, (100, 100, 100), font_20, 'Buy Council House', (0, 0, 0)) elif mainGame.getCurProp().T_Blocks == 0: displayButtonRect(screen, buy_upgrade_button, (100, 100, 100), font_20, 'Buy Tower Block', (0, 0, 0)) if mainGame.getCurProp().T_Blocks > 0: displayButtonRect(screen, sell_upgrade_button, (100, 100, 100), font_20, 'Sell Tower Block', (0, 0, 0)) elif mainGame.getCurProp().C_Houses > 0: displayButtonRect(screen, sell_upgrade_button, (100, 100, 100), font_20, 'Sell Council House', (0, 0, 0)) if mainGame.getCurProp( ).prop_type == Prop_Type.NORMAL or mainGame.getCurProp( ).prop_type == Prop_Type.SCHOOL or mainGame.getCurProp( ).prop_type == Prop_Type.STATION: if mainGame.getCurProp().prop_owner == mainGame.cur_player: #Display relevant button for mortgaging or unmortgaging a property if mainGame.getCurProp( ).mortgage_status: #Property is mortgaged displayButtonRect(screen, buy_prop_button, (100, 100, 100), font_28, 'Unmortgage Property', (0, 0, 0)) else: displayButtonRect(screen, buy_prop_button, (100, 100, 100), font_28, 'Mortgage Property', (0, 0, 0)) if mainGame.getCurProp( ).prop_owner == -1 and mainGame.controller.may_buy: #Give player the opportunity to buy property (since it is available and they have began their turn by rolling the dice) displayButtonRect(screen, buy_prop_button, (100, 100, 100), font_40, 'Buy Property', (0, 0, 0)) elif mainGame.getCurProp().prop_owner != -1: #Property is owned by a player so display information pertaining to the owning of said property by this aforementioned player displayOwner( screen, font_28, mainGame.getPlayer(mainGame.getCurProp().prop_owner)) else: if mainGame.getCurProp( ).prop_type != Prop_Type.LOST_IN_BOGSIDE: #Will work perfectly normally for all properties but the Lost In Bogside square tit_str = mainGame.getCurProp().prop_title elif mainGame.getCurPlayer( ).player_inJail: #If Player is actually 'in jail' tit_str = "Lost In Bogside" else: tit_str = "On The Paths" #In the same space but can move freely (i.e. 'not in jail') tit_text = font_40.render( tit_str, True, (0, 0, 0) ) #Render the property name as it does not have a title deed that can do so t_width, t_height = font_40.size(tit_str) screen.blit(tit_text, [(400 - t_width) / 2 + 600, 220]) if mainGame.getCurProp( ).prop_type == Prop_Type.NORMAL or mainGame.getCurProp( ).prop_type == Prop_Type.SCHOOL or mainGame.getCurProp( ).prop_type == Prop_Type.STATION or mainGame.getCurProp( ).prop_type == Prop_Type.PAYMENT: #If incurs a charge try: if mainGame.controller.turn_rent != 0: #If rent has actually been charged then the player is told they themselves have paid whatever amount displayPaidRent(screen, font_28, mainGame.controller.turn_rent) elif mainGame.getCurProp( ).prop_owner == mainGame.cur_player and mainGame.getCurProp( ).prop_type == Prop_Type.NORMAL: #If property is owned by the current player and NORMAL (since other properties depend on those owned and dice rolls if mainGame.board.wholeGroupOwned( mainGame.getCurProp().prop_owner, mainGame.getCurPlayer().player_pos ) and mainGame.getCurProp().C_Houses == 0: displayRent(screen, font_28, mainGame.getCurProp().getRent() * 2) else: displayRent(screen, font_28, mainGame.getCurProp().getRent()) except AttributeError: #Prevents errors as PAYMENT property has no owner but changes variable turn_rent pass if mainGame.getCurProp( ).prop_type == Prop_Type.POT_LUCK or mainGame.getCurProp( ).prop_type == Prop_Type.COUNCIL_CHEST: if mainGame.controller.cur_card != None: #If player was already on one of these places when their turn begins, cur_card and card_texts will be None object; this condition prevents an error when the following code thinks that it is displayCard(screen, mainGame.controller.cur_card) t_count = 0 for cur_text in mainGame.controller.card_texts: w, h = cur_text.get_size() screen.blit(cur_text, [(400 - w) / 2 + 600, 480 + t_count * 25]) t_count += 1 if turn_but_click: #End Turn button #If player could sell some things to avoid going bankrupt cont = True if mainGame.getCurPlayer().player_money < 0 and ( getObtainMon(mainGame.board, mainGame.cur_player) + mainGame.getCurPlayer().player_money) >= 0: msgBox = MessageBox( screen, 'You need to ensure your money is 0 or above before you can finish your turn. Please sell or mortgage some assets to continue.', 'Not Enough Money') cont = False elif ( getObtainMon(mainGame.board, mainGame.cur_player) + mainGame.getCurPlayer().player_money ) < 0: #If it is impossible for a player to not end up in debt, they go bankrupt mainGame.getCurPlayer().deactivate( ) #Remove player from the game cont = False for counter in range(mainGame.board.max_pos): if mainGame.board.getProp( counter).prop_type == Prop_Type.NORMAL: if mainGame.board.getProp( counter).prop_owner == mainGame.cur_player: mainGame.board.getProp(counter).p_owner = -1 mainGame.board.getProp( counter).mortgage_status = False mainGame.board.getProp(counter).C_Houses = 0 mainGame.board.getProp(counter).T_Blocks = 0 if mainGame.board.getProp( counter ).prop_type == Prop_Type.SCHOOL or mainGame.board.getProp( counter).prop_type == Prop_Type.STATION: if mainGame.board.getProp( counter).prop_owner == mainGame.cur_player: mainGame.board.getProp(counter).p_owner = -1 mainGame.board.getProp( counter).mortgage_status = False msgBox = MessageBox( screen, 'Unfortunately, this utopian capitalist world has ceased to be utopian for you: you have gone bankrupt and are no longer in the game.', 'Game Over') advanceOnBoxClose = True #Next player's turn now (if the previous player has no more to do if cont: mainGame.advancePlayer() mainGame.prop_thumbs = pygame.transform.smoothscale( CreateThumbs(mainGame.board, mainGame.cur_player), [385, 170] ) #Generate thumbnails for new player (here so it is only done when the player changes, not every frame change) if mainGame.countActivePlayers() < 2: mainGame.advancePlayer() msgBox = MessageBox( screen, mainGame.getCurPlayer().player_name + ' has won the game.', 'Game Over') exitOnBoxClose = True #Button for buying a property has been clicked if buy_but_click and ( mainGame.getCurProp().prop_type == Prop_Type.NORMAL or mainGame.getCurProp().prop_type == Prop_Type.SCHOOL or mainGame.getCurProp().prop_type == Prop_Type.STATION ): #Final check that the property can actually be owned #Player wished to buy property if mainGame.getCurProp( ).prop_owner == -1: #Property is unowned, hence can actually be bought if mainGame.getCurPlayer().player_money >= mainGame.getCurProp( ).cost: #Player has enough money mainGame.getCurPlayer().spendMoney(mainGame.getCurProp( ).cost) #Decrease the player's bank balance accordingly mainGame.getCurProp().buyProperty( mainGame.cur_player ) #Change the property's status to track the new ownership mainGame.prop_thumbs = pygame.transform.smoothscale( CreateThumbs(mainGame.board, mainGame.cur_player), [385, 170] ) #Update title deed thumbnails to reflect newly purchased properties #Button to apply the effects of a Pot Luck or Council Chest card if use_card_but_click and mainGame.controller.cur_card != None: #Check there is a card to work with mainGame.controller.card_used = True mainGame.applyCardEffects() #Apply card effects #All of the following may only be done if the current player owns the property #Button for mortgaging or unmortgaging a property if mort_but_click and ( mainGame.getCurProp().prop_type == Prop_Type.NORMAL or mainGame.getCurProp().prop_type == Prop_Type.SCHOOL or mainGame.getCurProp().prop_type == Prop_Type.STATION ): #Final check that the property is one that may be mortgaged if mainGame.getCurProp( ).prop_owner == mainGame.cur_player and mainGame.getCurProp( ).mortgage_status == False: #Property must be owned by the current player and not already mortgaged mainGame.getCurProp( ).mortgage_status = True #Property is now mortgaged mainGame.getCurPlayer().addMoney( int(mainGame.getCurProp().mortgage_val)) elif mainGame.getCurProp( ).prop_owner == mainGame.cur_player and mainGame.getCurProp( ).mortgage_status: #Property must be owned by the current player and is mortgaged if mainGame.getCurPlayer().player_money >= mainGame.getCurProp( ).mortgage_val * 1.2: #Player has sufficient money to unmortgage the property (twice the money gotten by mortgaging it) mainGame.getCurProp( ).mortgage_status = False #Property is no longer in a state of being mortgaged mainGame.getCurPlayer().spendMoney( int(mainGame.getCurProp().mortgage_val * 1.2) ) #Decrease player's money by the cost of unmortgaging the property #Button for buying a Council House or Tower Block if buy_upgrade_but_click and mainGame.getCurProp( ).prop_type == Prop_Type.NORMAL and mainGame.board.wholeGroupOwned( mainGame.cur_player, mainGame.getCurPlayer().player_pos ): #Player wishes to upgrade the property and said upgrade can actually be purchaed if mainGame.getCurProp( ).prop_owner == mainGame.cur_player: #May only be bought if the property is owned by the current player if mainGame.getCurProp( ).C_Houses < 4: #Fewer than 4 Council Houses, so these are the next upgrade to be bought if mainGame.getCurPlayer().player_money >= ( mainGame.getCurProp().CH_cost * mainGame.board.countGroupSize( mainGame.cur_player, mainGame.getCurPlayer().player_pos) ): #Player actually has enough money to buy the Council House upgrade mainGame.board.buyCHGroup( mainGame.cur_player, mainGame.getCurPlayer().player_pos ) #Buy the Council Houses for the whole group mainGame.getCurPlayer().spendMoney( mainGame.getCurProp().CH_cost * mainGame.board.countGroupSize( mainGame.cur_player, mainGame.getCurPlayer().player_pos) ) #Decrease the player's money by the cost of a Council House for however many properties are in the group elif mainGame.getCurProp( ).C_Houses == 4 and mainGame.getCurProp( ).T_Blocks == 0: #4 Council Houses and no Tower Blocks, so Tower Block can be bought if mainGame.getCurPlayer().player_money >= ( mainGame.getCurProp().TB_cost * mainGame.board.countGroupSize( mainGame.cur_player, mainGame.getCurPlayer().player_pos) ): #Player actually has enough money to buy the Tower Block upgrade mainGame.board.buyTBGroup( mainGame.cur_player, mainGame.getCurPlayer().player_pos ) #Buy the Council Houses for the whole group mainGame.getCurPlayer().spendMoney( mainGame.getCurProp().TB_cost * mainGame.board.countGroupSize( mainGame.cur_player, mainGame.getCurPlayer().player_pos) ) #Decrease the player's money by the cost of a Tower Block for however many properties are in the group #Button for selling a Council House or Tower Block if sell_upgrade_but_click and mainGame.getCurProp( ).prop_type == Prop_Type.NORMAL and mainGame.board.wholeGroupOwned( mainGame.cur_player, mainGame.getCurPlayer().player_pos ): #Player wishes to upgrade the property and said upgrade can actually be purchaed if mainGame.getCurProp( ).prop_owner == mainGame.cur_player: #May only be bought if the property is owned by the current player if mainGame.getCurProp( ).T_Blocks > 0: #Property has a Tower Block that can be sold mainGame.board.sellTBGroup( mainGame.cur_player, mainGame.getCurPlayer().player_pos ) #Sell the Tower Blocks for the whole group mainGame.getCurPlayer().addMoney( int(mainGame.getCurProp().TB_cost / 2 * mainGame.board.countGroupSize( mainGame.cur_player, mainGame.getCurPlayer().player_pos)) ) #Increase the player's money by half of what the upgrades were bought for elif mainGame.getCurProp( ).C_Houses > 0: #No Tower Blocks, buy some Council Houses which can instead be sold mainGame.board.sellCHGroup( mainGame.cur_player, mainGame.getCurPlayer().player_pos ) #Sell the Council Houses for the whole group mainGame.getCurPlayer().addMoney( int(mainGame.getCurProp().CH_cost / 2 * mainGame.board.countGroupSize( mainGame.cur_player, mainGame.getCurPlayer().player_pos)) ) #Increase the player's money by half of what the upgrades were bought for #Button to buy a map out of Bogside for £50 if leave_bogside_but_click and ( mainGame.getCurPlayer().player_money >= 50 or mainGame.getCurPlayer().player_hasBogMap ) and mainGame.getCurPlayer().player_inJail: mainGame.getCurPlayer().leaveJail() if mainGame.getCurPlayer().player_hasBogMap == False: mainGame.getCurPlayer().spendMoney(50) else: mainGame.getCurPlayer().useBogMap() if msgBox != None: msgBox.update() if msgBox.should_exit == False: msgBox.draw(screen) if main_buts[2].clicked(): #Details main_screen_running = False gotoScreen = 2 if main_buts[0].clicked(): #Leaderboards main_screen_running = False gotoScreen = 3 if main_buts[1].clicked(): #Pause main_screen_running = False gotoScreen = 4 for but in main_buts: but.render(screen) #Reset button booleans so that effects of clicking buttons do not happen more than once dice_but_click = False turn_but_click = False buy_but_click = False mort_but_click = False buy_upgrade_but_click = False sell_upgrade_but_click = False leave_bogside_but_click = False use_card_but_click = False clock.tick( fps ) #10 fps currently, but could easily be changed to update more or less often pygame.display.flip( ) #Refresh display from a pygame perspective, to reflect the screen.blit()s return mainGame, gotoScreen #Pass the Game object and the integer storing where the game will go to next back out to the main game loop
def OfflinePropDetails(mainGame, screen, clock): font_40 = pygame.font.SysFont('Arial', 40) #Font for title, money and exit button font_20 = pygame.font.SysFont('Arial', 20) #Font for actual property details font_20b = pygame.font.SysFont('Arial', 20, True) #Font for column headings font_16 = pygame.font.SysFont('Arial', 16) #Font for button captions props_owned = countPropsOwned(mainGame.board, mainGame.cur_player) board_poses = setupBoardPoses( mainGame.board, mainGame.cur_player, props_owned ) #Array containing the board positions of all of the current player's owned properties tit_text = font_40.render('Viewing Property Details:', True, (0, 0, 0)) #Render title at top left of screen headers = [ font_20b.render('Property', True, (0, 0, 0)), font_20b.render('Group', True, (0, 0, 0)), font_20b.render('Rent (£)', True, (0, 0, 0)), font_20b.render('Mortgage(£)', True, (0, 0, 0)), font_20b.render('CH/TB', True, (0, 0, 0)), font_20b.render('Options', True, (0, 0, 0)) ] head_x = [30, 200, 260, 330, 440, 640] #Initialise button arrays buy_buts = np.array([None] * props_owned) sell_buts = np.array([None] * props_owned) mort_buts = np.array([None] * props_owned) deed_buts = np.array([None] * props_owned) exit_but = pygame.Rect(880, 10, 120, 50) y_top = 90 #First y co-ordinate for a row of details y_space = 30 #Co-ordinate spacing between rows #Setup buttons (one element of each array for each property) for counter in range(props_owned): buy_buts[counter] = pygame.Rect(500, y_top + y_space * counter, 60, 25) sell_buts[counter] = pygame.Rect(565, y_top + y_space * counter, 60, 25) mort_buts[counter] = pygame.Rect(630, y_top + y_space * counter, 60, 25) deed_buts[counter] = pygame.Rect(695, y_top + y_space * counter, 75, 25) exit_but = Button(880, 10, 120, 50, "Exit", font_40) fps = 10 cur_deed = None #Image for a title deed that is being displayed at any one moment deed_prop = -1 #Board position of the property whose title deed is currently being shown buy_but_click = -1 #Variables storing when buttons are clicked sell_but_click = -1 #-1 indicates not clicked for this iteration mort_but_click = -1 #Not -1 indicates the integer contents of the variable is the row of whatever of the four types of button was clicked (zero-indexed) deed_but_click = -1 prop_details_running = True while prop_details_running: #Main loop for this part of the program for event in pygame.event.get(): exit_but.handle_input_event(event) if event.type == pygame.QUIT: prop_details_running = False gotoScreen = -1 if event.type == pygame.KEYDOWN: #If any key pressed if event.key == pygame.K_ESCAPE: #Escape key exits the game prop_details_running = False gotoScreen = -1 if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: #Left mouse button mouse_pos = event.pos #Position of the cursor when nouse was clicked for counter in range( props_owned ): #Cycle through all the arrays of buttons ot see if any have been clicked if buy_buts[counter].collidepoint(mouse_pos): buy_but_click = counter if sell_buts[counter].collidepoint(mouse_pos): sell_but_click = counter if mort_buts[counter].collidepoint(mouse_pos): mort_but_click = counter if deed_buts[counter].collidepoint(mouse_pos): deed_but_click = counter screen.fill((255, 255, 255)) screen.blit(tit_text, [10, 0]) pygame.draw.rect( screen, (0, 0, 0), pygame.Rect(10, 50, 770, 700), 10) #Draw black rectangle surrounding the property data mon_text = font_40.render( '£' + str(mainGame.getCurPlayer().player_money), True, (0, 0, 0)) #Render player money on screen f_width, f_height = font_40.size( '£' + str(mainGame.getCurPlayer().player_money)) screen.blit(mon_text, [(770 - f_width), 0]) #Display each of the column headings for counter in range(6): screen.blit(headers[counter], [head_x[counter], 60]) if cur_deed != None: #Can only display a chosen title deed if one has already been chosen screen.blit(cur_deed, [790, 200]) y_pos = y_top #Y co-ordinate of the first row of data for counter in range(props_owned): text_1 = font_20.render( mainGame.board.getProp(board_poses[counter]).prop_title, True, (0, 0, 0)) #Property name/title screen.blit(text_1, [30, y_pos]) if mainGame.board.getProp( board_poses[counter] ).prop_type == Prop_Type.NORMAL: #SCHOOL and STATION properties have no 'Group Colour', Council Houses or Tower Blocks pygame.draw.rect( screen, mainGame.board.getProp(board_poses[counter]).group_col, pygame.Rect(200, y_pos, 30, 20)) show_rent = mainGame.board.getProp( board_poses[counter]).getRent() if mainGame.board.wholeGroupOwned( mainGame.cur_player, board_poses[counter]) and mainGame.board.getProp( board_poses[counter]).C_Houses == 0: show_rent = show_rent * 2 text_2 = font_20.render(str(show_rent), True, (0, 0, 0)) screen.blit(text_2, [260, y_pos]) text_4 = font_20.render( str(mainGame.board.getProp( board_poses[counter]).C_Houses) + '/' + str(mainGame.board.getProp(board_poses[counter]).T_Blocks), True, (0, 0, 0)) screen.blit(text_4, [440, y_pos]) text_3 = font_20.render( str(mainGame.board.getProp(board_poses[counter]).mortgage_val), True, (0, 0, 0)) #Mortgage value of the property screen.blit(text_3, [330, y_pos]) y_pos += y_space #Increment y co-ordinate variable by the difference in co-ordinates between each row, as already defined if mainGame.board.wholeGroupOwned(mainGame.cur_player, board_poses[counter]): if mainGame.board.getProp( board_poses[counter] ).C_Houses < 4: #Council Houses are still available to buy displayButtonRect(screen, buy_buts[counter], (100, 100, 100), font_16, 'Buy CH', (0, 0, 0)) elif mainGame.board.getProp( board_poses[counter] ).T_Blocks == 0: #Player may still buy a Tower Block displayButtonRect(screen, buy_buts[counter], (100, 100, 100), font_16, 'Buy TB', (0, 0, 0)) if mainGame.board.getProp( board_poses[counter] ).T_Blocks > 0: #Player has Tower Blocks available to sell displayButtonRect(screen, sell_buts[counter], (100, 100, 100), font_16, 'Sell TB', (0, 0, 0)) elif mainGame.board.getProp( board_poses[counter] ).C_Houses > 0: #Player has no Tower Blocks, but still has Council Houses which may be sold displayButtonRect(screen, sell_buts[counter], (100, 100, 100), font_16, 'Sell CH', (0, 0, 0)) if mainGame.board.getProp( board_poses[counter] ).mortgage_status: #Properrty is mortgaged, thus it can only be bought back displayButtonRect(screen, mort_buts[counter], (100, 100, 100), font_16, 'Buy-Back', (0, 0, 0)) else: #Property may be mortgaged as it is not currently mortgaged displayButtonRect(screen, mort_buts[counter], (100, 100, 100), font_16, 'Mortgage', (0, 0, 0)) displayButtonRect(screen, deed_buts[counter], (100, 100, 100), font_16, 'View Deed', (0, 0, 0)) if mort_but_click != -1: #One of the mortgaging buttons has been clicked if mainGame.board.getProp(board_poses[mort_but_click] ).mortgage_status == False: #Unmortgaged mainGame.board.getProp( board_poses[mort_but_click] ).mortgage_status = True #Mortgage property mainGame.getCurPlayer().addMoney( mainGame.board.getProp( board_poses[mort_but_click]).mortgage_val ) #Increase the player's money by the mortgage value of the property else: #Mortgaged already if mainGame.getCurPlayer( ).player_money >= mainGame.board.getProp( board_poses[mort_but_click] ).mortgage_val * 1.2: #If the player has 120% of the mortgage value of the property (this is the buy-back cost) mainGame.board.getProp( board_poses[mort_but_click] ).mortgage_status = False #Unmortgage the property mainGame.getCurPlayer().spendMoney( int( mainGame.board.getProp( board_poses[mort_but_click]).mortgage_val * 1.2) ) #Debit the player's money by 120% of the mortgage value if deed_prop == board_poses[ mort_but_click]: #If title deed has changed cur_deed = pygame.transform.smoothscale( mainGame.board.getProp( board_poses[mort_but_click]).getTitleDeed(), [225, 400]) if deed_but_click != -1: #One of the buttons for viewing a title deed has been clicked cur_deed = pygame.transform.smoothscale( mainGame.board.getProp( board_poses[deed_but_click]).getTitleDeed(), [225, 400]) #Scale title deed so it fits in the narrow sidebar deed_prop = board_poses[deed_but_click] if buy_but_click != -1: #One of the buttons for buying CH or TB has been clicked if mainGame.board.getProp( board_poses[buy_but_click] ).C_Houses < 4: #Fewer than 4 Council Houses, so these are the next upgrade to be bought if mainGame.getCurPlayer().player_money >= ( mainGame.board.getProp( board_poses[buy_but_click]).CH_cost * mainGame.board.countGroupSize( mainGame.cur_player, board_poses[buy_but_click]) ): #Player actually has enough money to buy the Council House upgrade mainGame.board.buyCHGroup( mainGame.cur_player, board_poses[buy_but_click] ) #Buy the Council Houses for the whole group mainGame.getCurPlayer().spendMoney( mainGame.board.getProp( board_poses[buy_but_click]).CH_cost * mainGame.board.countGroupSize( mainGame.cur_player, board_poses[buy_but_click]) ) #Decrease the player's money by the cost of a Council House for however many properties are in the group elif mainGame.board.getProp( board_poses[buy_but_click] ).C_Houses == 4 and mainGame.board.getProp( board_poses[buy_but_click] ).T_Blocks == 0: #4 Council Houses and no Tower Blocks, so Tower Block can be bought if mainGame.getCurPlayer().player_money >= ( mainGame.board.getProp( board_poses[buy_but_click]).TB_cost * mainGame.board.countGroupSize( mainGame.cur_player, board_poses[buy_but_click]) ): #Player actually has enough money to buy the Tower Block upgrade mainGame.board.buyTBGroup( mainGame.cur_player, board_poses[buy_but_click] ) #Buy the Council Houses for the whole group mainGame.getCurPlayer().spendMoney( mainGame.board.getProp( board_poses[buy_but_click]).TB_cost * mainGame.board.countGroupSize( mainGame.cur_player, board_poses[buy_but_click]) ) #Decrease the player's money by the cost of a Tower Block for however many properties are in the group if sell_but_click != -1: #One of the buttons for selling CH or TB has been clicked if mainGame.board.getProp( board_poses[sell_but_click] ).T_Blocks > 0: #Property has a Tower Block that can be sold mainGame.board.sellTBGroup( mainGame.cur_player, board_poses[sell_but_click] ) #Sell the Tower Blocks for the whole group mainGame.getCurPlayer().addMoney( int( mainGame.board.getProp( board_poses[sell_but_click]).TB_cost / 2 * mainGame.board.countGroupSize( mainGame.cur_player, board_poses[sell_but_click])) ) #Increase the player's money by half of what the upgrades were bought for elif mainGame.board.getProp( board_poses[sell_but_click] ).C_Houses > 0: #No Tower Blocks, buy some Council Houses which can instead be sold mainGame.board.sellCHGroup( mainGame.cur_player, board_poses[sell_but_click] ) #Sell the Council Houses for the whole group mainGame.getCurPlayer().addMoney( int( mainGame.board.getProp( board_poses[sell_but_click]).CH_cost / 2 * mainGame.board.countGroupSize( mainGame.cur_player, board_poses[sell_but_click])) ) #Increase the player's money by half of what the upgrades were bought for if exit_but.clicked(): prop_details_running = False gotoScreen = 1 exit_but.render(screen) #Reset all button variables so the actions of buttons only happen once buy_but_click = -1 sell_but_click = -1 mort_but_click = -1 deed_but_click = -1 clock.tick( fps ) #10 fps currently, but could easily be changed to update more or less often pygame.display.flip( ) #Refresh display from a pygame perspective, to reflect the screen.blit()s return mainGame, gotoScreen #Pass the Game object and the integer storing where the game will go to next back out to the main game loop
def Leaderboards(mainGame, screen, clock): font_48 = pygame.font.SysFont('Arial', 48) #Font for title and name font_40 = pygame.font.SysFont('Arial', 40) #Font for the "?" and Exit buttons font_28 = pygame.font.SysFont('Arial', 28) #Font for actual leaderboards and attributes font_32b = pygame.font.SysFont('Arial', 32, True) #Font for column headings lead_arr = setup2DArray(mainGame) #Arrow images to be displayed on the buttons that are used by the player for choosing which column to sort on and whether to sort ascending or descending arrow_both = pygame.image.load("img/Arrows/both.png") arrow_up = pygame.image.load("img/Arrows/up.png") arrow_down = pygame.image.load("img/Arrows/down.png") #Initialise button array sort_buts = [pygame.Rect(360,80,40,40), pygame.Rect(610,80,40,40), pygame.Rect(940,80,40,40)] leader_buts = [Button(880, 10, 120, 50, 'Exit', font_40), Button(819, 10, 50, 50, '?', font_40)] msgBox = MessageBox(screen, 'Total Money measures simply how much money each player has in the Bank. \n Total Assets counts the values of all owned properties, upgrades, etc. based on how much was paid for them initially. \n Obtainable Money is how much money each player could get if they were to sell off all of their properties and the like.', 'Leaderboards: Explained') msgBox.should_exit = True tit_text = font_48.render('Viewing Leaderboards:', True, (0,0,0)) #Render title at top left of screen head_1 = font_32b.render('Player', True, (0,0,0)) head_2 = font_32b.render('Total Money', True, (0,0,0)) head_3 = font_32b.render('Total Assets', True, (0,0,0)) head_4 = font_32b.render('Obtainable Money', True, (0,0,0)) mon_text = font_48.render(mainGame.getCurPlayer().player_name, True, (0,0,0)) #Render player money on screen f_width, f_height = font_48.size(mainGame.getCurPlayer().player_name) y_top = 120 #First y co-ordinate for a row of details y_space = 40 #Co-ordinate spacing between rows fps = 10 sort_column = 1 sort_asc = False sort_but_click = -1 lead_arr = quickSort(lead_arr, 0, lead_arr.shape[0]-1, sort_column, sort_asc) leaderboards_running = True while leaderboards_running: #Main loop for this part of the program for event in pygame.event.get(): for but in leader_buts: but.handle_input_event(event) msgBox.handle_input_event(event) if msgBox.should_exit == False: break if event.type == pygame.QUIT: leaderboards_running = False gotoScreen = -1 if event.type == pygame.KEYDOWN: #If any key pressed if event.key == pygame.K_ESCAPE: #Escape key exits the game leaderboards_running = False gotoScreen = -1 if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: #Left mouse button mouse_pos = event.pos #Position of the cursor when nouse was clicked for counter in range(3): #Cycle through all the arrays of buttons to see if any have been clicked if sort_buts[counter].collidepoint(mouse_pos): sort_but_click = counter screen.fill((255,255,255)) screen.blit(tit_text, [10, 10]) screen.blit(mon_text, [(770-f_width), 10]) pygame.draw.rect(screen, (0,0,0), pygame.Rect(10,70,1000,700), 10) #Draw black rectangle surrounding the property data #Display each of the column headings (bold text) screen.blit(head_1, [30, 80]) screen.blit(head_2, [200, 80]) screen.blit(head_3, [450, 80]) screen.blit(head_4, [700, 80]) for counter in range(3): displayButtonRect(screen, sort_buts[counter], (100, 100, 100), font_28, '', (0, 0, 0)) if counter == sort_column-1: if sort_asc: screen.blit(arrow_down, [sort_buts[counter].x, sort_buts[counter].y]) else: screen.blit(arrow_up, [sort_buts[counter].x, sort_buts[counter].y]) else: screen.blit(arrow_both, [sort_buts[counter].x, sort_buts[counter].y]) y_pos = y_top #Y co-ordinate of the first row of data for counter in range(lead_arr.shape[0]): text_1 = font_28.render(mainGame.getPlayer(lead_arr[counter][0]).player_name, True, (0,0,0)) #Property name/title screen.blit(text_1, [30, y_pos]) text_2 = font_28.render(str(lead_arr[counter][1]), True, (0,0,0)) screen.blit(text_2, [200, y_pos]) text_3 = font_28.render(str(lead_arr[counter][2]), True, (0,0,0)) screen.blit(text_3, [450, y_pos]) text_4 = font_28.render(str(lead_arr[counter][3]), True, (0,0,0)) screen.blit(text_4, [700, y_pos]) y_pos += y_space #Increment y co-ordinate variable by the difference in co-ordinates between each row, as already defined if sort_but_click != -1: if sort_column == sort_but_click+1: sort_asc = not sort_asc else: sort_column = sort_but_click + 1 sort_asc = False lead_arr = quickSort(lead_arr, 0, lead_arr.shape[0]-1, sort_column, sort_asc) if leader_buts[0].clicked(): leaderboards_running = False gotoScreen = 1 if leader_buts[1].clicked(): msgBox.should_exit = False msgBox.update() if msgBox.should_exit == False: msgBox.draw(screen) for but in leader_buts: but.render(screen) sort_but_click = -1 clock.tick(fps) #10 fps currently, but could easily be changed to update more or less often pygame.display.flip() #Refresh display from a pygame perspective, to reflect the screen.blit()s return mainGame, gotoScreen #Pass the Game object and the integer storing where the game will go to next back out to the main game loop