current position:Home>Teach you how to use Python to transform an alien invasion game

Teach you how to use Python to transform an alien invasion game

2021-08-22 05:15:09 Python advanced


Many friends are from python Programming : From introduction to practice python Study , This book is more suitable for beginners .

There is a in this book pygame Small game project , Alien invasion .

For beginners , Learn from small games python It's a good choice , However, I think the alien invasion game in this book is too monotonous and rough , So I decided to upgrade it to super magic revision .

This paper first gives the original idea of the game according to this book 1.0 Version and original 1.0 Version complete code , Then gradually analyze and change it to a new version .

Compared with the monotonous and boring original version , In addition to the basic playing method, the magic revision is also a shooting game , It hardly seems to have anything to do with the original version .

This article is mainly divided into three parts :

 One 、 The original version reproduces 
  1. original 1.0 Version of the source code, get the address for free :
 Two 、 Magic change ideas 
  1. Eye catching UI Interface 
  2. Implement maps , The monster , Blood volume diversification 
  3. Realize the game mall and enrich the financial system 
  3、 ... and 、 To be continued 

One 、 The original version reproduces

original 1.0 edition :

original 1.0 The version has the following features :

1. Players can control the muzzle at the bottom of the screen ( The book says it's a spaceship ) Move left and right and shoot zombies 
2. The zombie swarm is fixed 3 row , Fixed quantity , Fixed pitch generation , It will not change with the level upgrade 
3. Players have 3 Life , The upper left corner represents the remaining life , Being touched by a zombie group or reaching the bottom of the screen is life -1
4. Players have to press the shooting key every time they shoot ( Here is the space ), Can't shoot continuously 
5. The corpses move left and right as a whole and move further before touching the edge of the screen , If you can't kill the most marginal zombies in time , Then the corpses will move very fast 
6. There is only one monster , And the monster doesn't have a blood amount , Level upgrade is just that the monster moves from left to right faster 
7. The play is monotonous , There is no specific , No skills , No props , There is no map 
8. Score storage cannot be realized , The record of the highest score each time the program is run is not saved 

It can only be said that the original 1.0 Version implements the basic framework of a small game , Here's the picture :

At this time, there is... In the upper left corner 3 A cannon icon , I still have 3 Life .

The top center of the screen is the highest score , The top right corner of the screen is the current score , Because this is the first game after running the program , The current score is the highest score .

The number below the current score in the top right corner of the screen is the current level , Destroy all zombies and enter the next level , With the upgrade of the level, the movement speed of the corpse group will be faster and faster .

Being hit by a zombie group or reaching the bottom of the screen will reduce life by one , In the picture above, only 2 I'm dead .

original 1.0 The files to be used in the version are as follows :

There are categories , Each document has its own clear division of labor ,alien It's about aliens ,alien_invasion Is the main program ,bullet It controls bullets and related parameters ,button Is to control the start game button ,game_stats Are some initialization settings of the game ,scoreboard It's the score control panel ,settings Used for game parameter setting ,ship Special control of the ship .

original 1.0 The images used in the version are also very simple , There is images Inside , Only one alien and one spaceship , Here I use a zombie and a cannon . You can also choose your favorite image to replace .

original 1.0 Version of the source code, get the address for free : 

Two 、 Magic change ideas

This article will initially launch the modified version , It might as well be remembered as magic change 2.0 edition .

1. Eye catching UI Interface

Through the well-designed login cover , The game is called star front , And achieve the following results :

This login interface supports clicking the start game button to start the game , Click the mouse or press the keyboard s Key to enter / Exit the points Mall , Click the mouse to contact, we can see the author information .

Part of the code :

        self.color=(230,230,230) = pygame.image.load('images/fm.png').convert_alpha()
        self.title = pygame.image.load('images/title.png').convert_alpha()
        self.titlerect = pygame.Rect(-190, -60, 1920, 1000)  #  rectangular (x,y,width,height) = pygame.image.load('images/play.png').convert_alpha()
        self.playrect = pygame.Rect(645,300, 280, 280)# rectangular (x,y,width,height) = pygame.image.load('images/shop.png').convert_alpha()
        self.shoprect = pygame.Rect(0, 700, 330, 158)  #  rectangular (x,y,width,height)
        self.lianxi1 = pygame.image.load('images/lianxi1.png').convert_alpha()
        self.lianxi1rect = pygame.Rect(1330, 780, 187, 80)  #  rectangular (x,y,width,height)
        self.shop2 = pygame.image.load('images/shop2.png').convert_alpha()
        self.shop2rect = pygame.Rect(1395, 735, 140, 126)  #  rectangular (x,y,width,height)

2. Implement maps , The monster , Blood volume diversification

The number of all monsters generated is related to the level , The generated quantity is random within a certain limit , But the higher the level , The number of generation tends to be more .

The monster generation range is also randomly generated within a certain limit .

The monster's movement speed is related to the level , The higher the level is , The faster forward speed , But there is a speed limit .

The monster blinks and moves within a body position with a certain probability , Can dodge the ship's bullets .

The first level is the lovely little robot ,1 Point blood volume , In the third level, a robot with a shield appeared ,3 Point blood volume .

In the 5 Guan appeared BOSS, Above is its red blood strip , For a long .

BOSS Well , If you don't resist beating, how can you be the boss ?

I set up rich maps in the game , Every time 10 Switch off a scene .

BOSS The arrangement follows every 5 Turn it off a little boss, Every time 10 Turn it up BOSS Laws , Let players experience the fierce battle .

Part of the code :

    def _create_fleet(self):
        " Create a mob 1"
        if self.stats.level>=5:
            self.alien_bloodmax =2
            self.alien_blood = 2
            if self.stats.level >=10:
                self.alien_bloodmax = 3
                self.alien_blood = 3
                if self.stats.level >=15:
                    self.settings.guainum = 2
                    self.alien_bloodmax =5
                    self.alien_blood =5
                    if self.stats.level >=25:
                        self.alien_bloodmax =10
                        self.alien_blood =10
                        if self.stats.level >= 30:
                            self.alien_bloodmax = 20
                            self.alien_blood = 20
                            if self.stats.level >= 40:
                                self.settings.guainum = 3
                                self.alien_bloodmax = 50
                                self.alien_blood = 50
                                if self.stats.level >= 45:
                                    self.alien_bloodmax = 100
                                    self.alien_blood = 100
                                    if self.stats.level >= 50:
                                        self.alien_bloodmax = 150
                                        self.alien_blood = 150
        for row_number in range(number_alien):
            alien = Alien(self)

3. Realize the game mall and enrich the financial system

What about? , The game mall made by Xiaobai himself can still be seen ?

In the upper left corner are gold coins , Linked to the points obtained by killing monsters in the game .

In the upper right corner is a diamond , seeing the name of a thing one thinks of its function , It is a scarce resource belonging to krypton king and a few elite players .

Moving the mouse over the corresponding product will display a translucent Introduction , name , Price , Categories and functions .

Load picture and location :

self.gaosi1 = pygame.image.load('images/gaosi1.png').convert_alpha()
self.gaosi1rect = pygame.Rect(230, 480, 751, 240)  #  rectangular (x,y,width,height)

Whether to display the product introduction is determined by the mouse position :

button3_clicked = self.gaosirect.collidepoint(mouse_pos)
if button3_clicked:
    self.screen.blit(self.gaosi1, self.gaosi1rect)

As for more explanation and source code of magic revision, we'll leave it for later explanation .

3、 ... and 、 summary

This issue's fantastic super magic modified alien invasion games -01 That's it , Interested friends can pay attention to me , In the later stage, some fantastic ideas will be launched accordingly -02-03 Etc .

Friends who have better ideas about this game can also put forward your magic change ideas , Make the game more interesting and fun , Let everyone's sparks of innovative thinking collide . Want your magic change plan to be implemented in the game , You can leave a message in the comment area below or write a private letter to the author .

This article is from WeChat official account. - Python Crawler and data mining (crawler_python)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2021-08-13

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[Python advanced],Please bring the original link to reprint, thank you.

Random recommended