StarCraft: Little Overlord and Little Bee (14)--The Tears of the Capitalist

Table of Contents of Series Articles

StarCraft: Little Overlord and Little Bee (13)--playing music and dancing

StarCraft: Little Overlord and Little Bee (12)--Cats have nine lives

 StarCraft: Little Overlord and Little Bee (11)--Kill, kill, kill

 StarCraft: Little Overlord and Little Bee (10)--Rat Road

StarCraft: Overlord and Bee (9) - Junkrat's Scourge

 StarCraft: Little Overlord and Little Bee (8)--Blue Rat and Big-faced Cat

 StarCraft: Little Overlord: Little Bee (7)--The disappearing bullet

StarCraft: Little Overlord and Little Bee (6)--Let the Bullets Fly

 StarCraft's Little Overlord's Little Bee (5)--Slow down the speed of the Little Bee

 StarCraft: Little Overlord and Little Bee (4)--Event Monitoring-Let the Little Bee Move


Table of contents

Table of Contents of Series Articles

Article directory

Preface

1. p increases the difficulty of the game

2. Scoring system

3. Display score

 4. Update score

Summarize



Preface

As of now, this game is already playable, but in order to let more players indulge in it, we learn from the penguin spirit and increase consumption points and addiction points, such as adding scoring mechanisms and difficulty upgrade mechanisms.


1. p increases the difficulty of the game

The book adds a lot, such as speeding up, and then keeping score, the highest score, level, the number of remaining mice, etc. I don’t think we need to go to so much trouble. I just choose a few functions to implement, just speed up, score, level, and the highest score. , we won’t do any other bells and whistles.

 The principle of increasing the difficulty of the game is that after each group of mice is eliminated, the initial speed of the mice is increased and new mice are refreshed. In this way, we need to increase the speed before each new mouse is refreshed, and then restore the speed to the speed after the game fails. Raw speed.

class Settings():

    def __init__(self):

        self.screen_width = 800
        self.screen_height = 600
        self.bg_color = (255,255,255)
        self.ship_limit = 2

        
        self.bullet_width = 2
        self.bullet_hight = 5
        self.bullet_color = 60,60,60
        self.bullets_allowed = 20
        
        self.fleet_drop_speed = 50
       
        
        self.speedup_scale = 1.2
        
        self.initialize_dynamic_settings()
        
    def initialize_dynamic_settings(self):
        self.ship_speed_factor = 0.1
        self.bullet_speed_factor = 0.5
        self.alien_speed_factor = 2
        self.fleet_direction = 1

 We found that a self.speedup_scale attribute has been added. It can be expected that every time the speed is increased, multiplying the original speed by it will prompt a speed of 20%. We can debug this value in actual use. At the same time, we will need The homing attributes are placed separately in the initialize_dynamic_settings function, so that when initialization is required, the function can be called directly. It can be seen that the initialize_dynamic_settings function mainly describes the attributes of the speed, and there is also an attribute indicating the movement direction of the mouse.

 Therefore, we create another function and call it when we need to upgrade the speed

def increase_speed(self):
        self.ship_speed_factor *= self.speedup_scale
        self.bullet_speed_factor *= self.speedup_scale
        self.alien_speed_factor *= self.speedup_scale

 According to the previous analysis, we need to call when re-creating a new mouse

def check_bullet_alien_collisions(new_setting,screen,bullets,aliens):
    collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
    if len(aliens) == 0:
        bullets.empty()
        new_setting.increase_speed()
        create_fleet(new_setting, screen, aliens)

 At the same time, after the game is over, that is, after clicking start, we call and set the initial properties.

def check_play_button(stats,play_button,mouse_x,mouse_y,new_setting,aliens,bullets,ship,screen):
    if play_button.rect.collidepoint(mouse_x,mouse_y) and not stats.game_active:
        pygame.mouse.set_visible(False)
        stats.reset_stats()
        stats.game_active = True

        aliens.empty()
        bullets.empty()
        new_setting.initialize_dynamic_settings()

        create_fleet(new_setting,screen,aliens)
        ship.center_ship()

2. Scoring system

The increasing difficulty function has been implemented. Now we will implement the scoring function. The book scores according to the different scores of mice killed in each difficulty. In fact, there is no need. It is very scientific to score according to the number of mice killed. It is very scientific to pass the level. The more points, the higher the points.

 The idea of ​​​​this is also very simple, similar to creating a start button. We first set an attribute. Every time a mouse is eliminated, the attribute value increases by 1. When the game is restarted, the attribute returns to 0. Then according to the display settings of the start button, the numbers are converted into images and displayed on the upper right side of the screen. The principle of level is the same as that of scores, so there is no need to write it anymore. The highest score is nothing more than creating another variable. After the game is over, add a judgment and assign the maximum score.

 According to the above idea, we first establish the variable of the score

class GameStats():
    def __init__(self,new_setting):
        self.new_setting = new_setting
        self.reset_stats()
        self.game_active = False


    def reset_stats(self):
        self.ships_left = self.new_setting.ship_limit
        self.score = 0

 Then we refer to the button class to create the score class.

import pygame.font

class Scoreboard():
    
    def __init__(self,new_setting,screen,stats):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.new_setting  = new_setting
        self.stats = stats
        
        self.text_color = (30,30,30)
        self.font = pygame.font.SysFont(None,48)
        
        self.prep_score()
        
    def prep_score(self):
        score_str = str(self.stats.score)
        self.score_image =  self.font.render(score_str,True,self.text_color,self.new_setting.bg_color)
        
        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right-20
        self.score_rect.top = 20
        
    def show_score(self):
        self.screen.blit(self.score_image,self.score_rect)

 The above code will not be explained one by one. It is similar to the button class created before.

3. Display score

After the scoring class is written, just like the button, we first need to create an instance of the score. We create an instance in the main function

 sb = Scoreboard(new_setting,screen,stats)

Then we pass the instance as a parameter and call the show_score function of sb when refreshing the screen.

def update_screen(new_setting,screen,stats,ship,bullets,aliens,play_button,sb):
    screen.fill(new_setting.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    aliens.draw(screen)
    sb.show_score()
    if not stats.game_active:
        play_button.draw_button()
pygame.display.flip()

 At this point, we can already display the score

 

 4. Update score

 We can now display the score, but the score is always 0, we don't update it, every time a mouse disappears, we add 1 to the score

def update_bullets(new_setting,screen,bullets,aliens,stats,sb):
    check_bullet_alien_collisions(new_setting,screen,bullets,aliens,stats,sb)
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

def check_bullet_alien_collisions(new_setting,screen,bullets,aliens,stats,sb):
    collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
    if collisions:
        for aliens in collisions.values():
            stats.score += len(aliens)
            sb.prep_score()
    if len(aliens) == 0:
        bullets.empty()
        new_setting.increase_speed()
        create_fleet(new_setting, screen, aliens)

 How many mice we collided with each time we traversed, and we added points based on how many mice we had. Len (aliens) is the number of mice, so the score is the number of mice killed, which is simple and clear.

 

 We found a problem where the score is not recalculated when the game is restarted. We need to reset the score after clicking the start button. I won't paste the code here.


Summarize

As of now, the first version of this game has been completed. We have not implemented some of the messy functions in the book. The codes are actually similar. I will make a summary tomorrow.

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/133199776