StarCraft: Little Overlord and Little Bee (16)--The Running Cat

Table of Contents of Series Articles

StarCraft: Little Overlord and Little Bee (15)--The play is coming to an end

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

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. Liberate the little cat

 2. Modify the bullet style

 3. Modify the movement trajectory of the mouse

Summarize


Preface

The first generation of the game has been completed, which is version 1.0. Now we are upgrading the game and will first release version 1.1.


1. Liberate the little cat

The little cat can only move left and right at the bottom of the screen. We hope it can move to any position on the screen. Referring to the code for writing left and right movements, we only need to add monitoring for pressing the up and down keys on the keyboard, and then add and subtract the y-axis coordinate of the little cat after responding to the event.

 First we write the mobile code of the little cat

class Ship():
    def __init__(self,screen,new_settings):
        self.screen = screen
        self.image = pygame.image.load('cat.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom
        self.new_settings = new_settings

        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.center = float(self.rect.centerx)
        self.centery = float(self.rect.centery)

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.new_settings.ship_speed_factor

        if self.moving_left and self.rect.left > 0:
            self.center -= self.new_settings.ship_speed_factor

        if self.moving_up :
            self.centery -= self.new_settings.ship_speed_factor

        if self.moving_down :
            self.centery += self.new_settings.ship_speed_factor

        self.rect.centerx = self.center
        self.rect.centery = self.centery

 It can be seen that we have added the self.moving_up and self.moving_down attributes that represent the up and down movement of the little cat. We use the floating point type self.centery to record the Y-axis coordinate of the little cat. Finally, we control the little cat by judging whether the attribute is FALSE or TRUE. The cat moves up and down.

 Next, we will add the monitoring code

def check_keydown_events(event,new_setting,screen,ship,bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True

    elif event.key == pygame.K_LEFT:
        ship.moving_left = True

    elif event.key == pygame.K_SPACE:
        fire_bullet(new_setting, screen, ship, bullets)

    elif event.key == pygame.K_q:
        sys.exit()

    elif event.key == pygame.K_UP:
        print('111')
        ship.moving_up = True

    elif event.key == pygame.K_DOWN:
        ship.moving_down = True


def check_keyup_events(event,ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False

    if event.key == pygame.K_LEFT:
        ship.moving_left = False

    if event.key == pygame.K_UP:
        ship.moving_up = False

    if event.key == pygame.K_DOWN:
        ship.moving_down = False

We add judgments to the pressing and lifting of the direction keys respectively to control moving_up and moving_down.

So far, we have been able to realize the up and down movement of the little cat.

 

 We found that the little cat can move normally, but we still need to add a restriction to prevent it from running outside the screen. The y coordinate is different from the x coordinate. The top is 0, and then it gets larger downwards. In other words, as long as it is greater than 0, I can go all the way up, and as long as it's less than the bottom coordinate of the screen, I can go all the way down.

if self.moving_up and self.rect.y > 0:
            self.centery -= self.new_settings.ship_speed_factor

        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.centery += self.new_settings.ship_speed_factor

 2. Modify the bullet style

 I have long been dissatisfied with the appearance of bullets. I originally wanted to change it to paw prints, but the picture is really difficult to find, and it is almost impossible to see what it is after zooming out, so I changed it to a bow and arrow, at least compared to the pixels of the painting. The strips are a little better.

 We only need to modify the bullet code according to the code for writing the little cat and the little mouse. It is relatively simple. I commented out the unnecessary parts of the original code to facilitate comparison.

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):

    def __init__(self,new_setting,screen,ship):
        super(Bullet,self).__init__()
        self.screen = screen
        self.image = pygame.image.load('zidan2.png')
        # self.rect = pygame.Rect(0,0,new_setting.bullet_width,new_setting.bullet_hight)
        self.rect = self.image.get_rect()
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        self.y = float(self.rect.y)
        # self.color =  new_setting.bullet_color
        self.speed_factor = new_setting.bullet_speed_factor

    def update(self):
        self.y -=self.speed_factor
        self.rect.y =self.y

    def draw_bullet(self):
        # pygame.draw.rect(self.screen,self.color,self.rect)
        self.screen.blit(self.image, self.rect)

 Let's take a look at the effect.

 

 Isn’t it very powerful?

 3. Modify the movement trajectory of the mouse

 I feel that this is a relatively difficult part to modify. What I hope is that the mice will be generated randomly and continuously, and then go downwards. This will cause relatively large changes to our existing code, but the playability will be greatly increased.

Random generation is easy to do. We set up to randomly generate one to three mice every second. The X-axis coordinate of the mouse is random, the Y-axis coordinate is equal to 0, and then the movement is always downward. In this way, we don't have the problem of clearing the mouse and regenerating it every time.

First we modify the code that generates mice:

def create_fleet(new_setting,screen,aliens,stats):
    # for row_number in range(3):
    #     for alien_number in range(6):
    #         alien = Alien(new_setting,screen)
    #         alien.x = alien.rect.width+2*alien.rect.width*alien_number
    #         alien.rect.x = alien.x
    #         alien.rect.y = alien.rect.height+2*alien.rect.height*row_number
    #         aliens.add(alien)
    # while stats.game_active:
    nums = set()
    for row_number in range(random.randint(1,3)):
        num = random.randint(20, 780)
        if num not in nums and all(abs(num - x) > 20 for x in nums):
            nums.add(num)
    for row_number in nums:
        alien = Alien(new_setting,screen)
        alien.x = row_number
        alien.rect.x = alien.x
        alien.rect.y = 0
        aliens.add(alien)

 In this way, we can generate 1 to 3 mice each time, and the two mice will not overlap.

 We also need to rewrite the moving part of the mouse. The mouse no longer needs to move left and right, so we can remove the left and right parts and write only the downward part.

 def update(self):
        self.y += self.new_setting.fleet_drop_speed
        self.rect.y = self.y

 Let’s take a look at the effect after the modification

 

 

 


Summarize

Gameplay and graphics have been greatly improved

Guess you like

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