[Pygame actual combat] Aircraft shooting masterpiece the cosmic battle is about to break out… This ultra-classic shooting game should also be taken out and restarted~

Hits: 0

Introduction

hello hello! Good afternoon~ I’m Mumuzi.

I have introduced dozens of games such as football games, skiing, Contra, and Super Mario made in Python, and the effects are not bad.

The response was overwhelming. Sure enough, the game is more attractive to everyone’s attention acridine!

The complete material + source code of all articles are in 👇👇

**[Fans who prostitute source code benefits, please move to the CSDN community or the public at the end of the article for free.]**

So today, everyone will continue to learn to use Python to make a very fun and interesting game with a sense of technology: Cosmic Battle

space game . It is fun and can learn programming, mainly because children will definitely like it~

Gamers will drive an interstellar spaceship through the gorgeous and mottled universe. Facing various tasks and challenges, it can

Complete this mission? Let’s take a guess or can you try it yourself? ! 👇Let’s go to today’s topic!

💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮

text

1. Introduction to the principle

First of all, the background of the game, as you can see, there are several places that need to be implemented:

The first is the background, because the background of the game is always changing, so it must be a dynamic background;

The second is music, there are bullet hit music, missile hit and impact explosion music;

The third is the spaceship, including our main ship, enemy spaceships, etc. In addition, there are missile attacks from the big boss, our main ship can launch

The bullets attack the enemy ship (destroy an enemy aircraft by three points), or hit the enemy ship, once our main ship is hit or rammed

Hit, it will deduct one life value (only one can be debugged here), and the life value is reduced to 0, then Game Over!

2. Materials (pictures, music, etc.)

There are mainly so many characters, spaceships (including our main ship, enemy spaceships and other different shapes), bullets, missiles and other props (different shapes).

type).

3. Code display

Mainly for two pieces of py, I will post them for you below👇👇

1) main.py main program

from mySprite import *

# Initialize 
def  init () :
    pygame.init()
    global isRun
    isRun = True
    global text_font, over_font, over_render, restar_render, star
    text_font = pygame.font.SysFont('arial', 20)
    over_font = pygame.font.SysFont('arial', 40)
    over_render = over_font.render('Game Over', 1, (255, 20, 20), None)
    restar_render = text_font.render('Press R key for restart game', 1, (80, 209, 80), None)
    star = pygame.image.load('./img/star.png')
    bg0 = Background()
    bg1 = Background(True)
    global back_group, fly, fly_group, score_render, enemy_group, boom_group
    back_group = sprite.Group(bg0, bg1)
    fly = Fly()
    fly_group = sprite.Group(fly)
    score_render = text_font.render(str(fly.score), 1, (172, 209, 204), None)
    enemy_group = sprite.Group()
    boom_group = sprite.Group()
    pygame.time.set_timer(10, 1000)
    pygame.time.set_timer(15, 500)

def dealEvent():
    GAMECLOCK.tick(70)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if isRun:
            if event.type == 10:
                enemy = Enemy()
                enemy_group.add(enemy)
            elif event.type == 15:
                fly.fire()
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_r]:
        main_game()
    if keys_pressed[pygame.K_RIGHT]:
        fly.speed = 2
    elif keys_pressed[pygame.K_LEFT]:
        fly.speed = -2
    else:
        fly.speed = 0


# collision detection - between sprite groups - 
def between sprites and sprite groups  checkCollide () : 
    bullet_shot_enemy = pygame.sprite.groupcollide(fly.bullets, enemy_group, True , True )
     if bullet_shot_enemy:
        pygame.mixer.init()
        for bullet in bullet_shot_enemy:
            rect = bullet.rect
            boom = Boom(rect.centerx, rect.centery)
            boom_group.add(boom)
            pygame.mixer.music.load('./img/bao.mp3')
            pygame.mixer.music.play(1)
            fly.score += 3
            global score_render
            score_render = text_font.render(str(fly.score), 1, (172, 209, 204), None)
    enemies = pygame.sprite.spritecollide(fly, enemy_group, True)
    if enemies:
        global isRun
        isRun = False
        SCREEN.blit(over_render,
                    ((WINDOW.width - over_render.get_width()) / 2,
                     (WINDOW.height - over_render.get_height()) / 2 - 10))
        SCREEN.blit(restar_render, ((WINDOW.width - restar_render.get_width()) / 2,
                                    (WINDOW.height - restar_render.get_height()) / 2 + over_render.get_height()))
        pygame.display.update()

# sprite drawing update 
def  update () : 
    if isRun:
        back_group.draw(SCREEN)
        back_group.update()
        fly_group.draw(SCREEN)
        fly_group.update()
        fly.bullets.draw(SCREEN)
        fly.bullets.update()
        enemy_group.draw(SCREEN)
        enemy_group.update()
        boom_group.draw(SCREEN)
        boom_group.update()
        boom_group.empty()
        SCREEN.blit(score_render, (WINDOW.width - 60, 20))
        SCREEN.blit(star, (WINDOW.width - 85, 20))
        pygame.display.update()

# Main function 
def  main_game () :
    init()
    while True:
        dealEvent()
        checkCollide()
        update()

main_game()

2)MySprite.py

import pygame
from pygame import sprite
import random
WINDOW = pygame.Rect(0, 0, 466, 700)
SCREEN = pygame.display.set_mode(WINDOW.size)
pygame.display.set_caption('FlyFight')
GAMECLOCK = pygame.time.Clock()    # game clock

class MySprite(sprite.Sprite):
    def __init__(self, img_name, speed=1):
        super().__init__()
        self.image = pygame.image.load(img_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):
        self.rect.y += self.speed


class Background(MySprite):
    def __init__(self, next=False):
        super().__init__('./img/background.png')
        if next:
            self.rect.y = -self.rect.height

    def update(self):
        super().update()
        if self.rect.y >= 700:
            self.rect.y = -self.rect.height

# Bullet sprite 
class  Bullet ( MySprite ): 
    def  __init__ ( self ) :
         super ().__init_ _ ( './img/bullet.png' , - 2 )

    def update(self):
        super().update()
        if self.rect.bottom < 0:
            self.kill()

# Spy plane sprite 
class  Fly ( MySprite ): 
    def  __init__ ( self ) :
         super ().__init_ _ ( './img/fly.png' , 0 )
         # Set the initial position of the spy plane 
        self .rect.centerx = WINDOW .centerx
         self .rect.bottom = WINDOW.bottom- 10 
        # Define the bullet sprite group 
        self .bullets = pygame.sprite.Group()
         self .score = 0

    def update(self):
        self.rect.x += self.speed
        if self.rect.right > WINDOW.width:
            self.rect.right = WINDOW.width
        elif self.rect.x < 0:
            self.rect.x = 0

    def fire(self):
        bullet = Bullet()
        # Set the initial position of the bullet 
        bullet.rect.centerx = self .rect.centerx
        bullet.rect.bottom = self.rect.y
        self.bullets.add(bullet)


# Enemy sprite 
class  Enemy ( MySprite ): 
    def  __init__ ( self ) :
         super ().__init_ _ ( './img/enemy.png' )
         # Clone magic, set speed and position 
        self .speed = random.randint ( 2 , 3 )
         self .rect.bottom = 0 
        x = WINDOW.width - self .rect.width
         self .rect.x = random.randint( 0 , x)

    def update(self):
        super().update()
        if self.rect.y >= WINDOW.height:
            self.kill()

# Boom sprite 
class  Boom ( MySprite ): 
    def  __init__ ( self , x, y) :
         super ().__init_ _ ( './img/boom.png' )
         # Set the position of the explosion 
        self .rect.centerx = x
         self .rect.centery = y


1) Different backgrounds

2) End of impact

Game rules: Arrow keys to move left and right, R to replay.

​Summary

💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮💮

The news that the game will be online within three seconds of the countdown, I know 🤭 Chestnut knows 🤭 Muzi knows 🤭 Lizi knows 🤭

Gu Muzi knows🤭You are waiting for all the above personnel to enter the venue before you know that you have entered? 🐷🐷🐷

[The Metaverse Interstellar Cosmic Battle Mini-Game Officially Opens]………… Ten Thousand Times Sailing (Announcement)💮💮💮

The Book of Changes of Wisdom and Practice is in secret, and all that is said is fulfilled. To be honest, I don’t understand, it’s just that I’m not mistaken.

Hurry up and find me to get the source code to try it! I’m waiting for you at 100,000 fire 👇👇👇👇👇👇

🎯Complete free source code collection: find me! At the end of the article, the public can get it by themselves, and I can too!

🔨Recommend previous articles——

Project 1.0 Super Mario

[Programmer’s homemade game: Super Mario 100% real version, it can make you cry~ [with source code]]

Project 1.1 Minesweeper

Pygame combat: It is said that this is the most difficult minesweeper game in history, there is no one, you can feel it…

Project 1.3 Space mech game

Pygame actual combat: cattle, thousands of lines of code to achieve “Mecha Adventure Game”, too good (keep it and learn it slowly)

Project 1.4 Fruit Ninja

[Pygame actual combat] The upgraded version of the popular fruit cutting game “Fruit Ninja” is online, do you dare to come to PK?

Project 2.0 Networking, man-machine integrated backgammon game

Pygame combat: play backgammon? Believe it or not, you can’t win even if I give you a few steps?

🎄Article Summary——

Project 1.0  Python—2021 |Summary of existing articles | Continuously updated, just read this article

(More content + source code are summarized in the article!! Welcome to read~)

You may also like...

Leave a Reply

Your email address will not be published.