Python self-study-simple realization of aircraft war (with related audio and picture materials)

Airplane War Audio Picture Package (Baidu Netdisk)

Link: https://pan.baidu.com/s/1MY_oHjB5vr-5oAWMeLnTWQ

Extraction code: txm0

1. Create a game window

import pygame
screen=pygame.display.set_mode((800,600),0,32)  #创建一个游戏窗体
while True:
    pygame.display.update()  #消息循环,更新消息

Insert picture description here

2. Load background

import pygame
screen=pygame.display.set_mode((480,650),0,32)  #创建一个游戏窗体
#bk=pygame.image.load("D:\\Python代码\\class28\\images\\background.png").convert()  #载入文件图片到内存
bk=pygame.image.load("images\\background.png").convert()#相对路径

while True:
    screen.blit(bk,(0,0))#(0,0)表示窗体位置
    pygame.display.update()  #消息循环,更新消息

Insert picture description here
3. Processing messages is
mainly to bind keyboard messages to control the movement of the aircraft

import pygame
import pygame.locals
screen=pygame.display.set_mode((480,650),0,32)  #创建一个游戏窗体
#bk=pygame.image.load("D:\\Python代码\\class28\\images\\background.png").convert()  #载入文件图片到内存
bk=pygame.image.load("images\\background.png").convert()#相对路径

while True:
    screen.blit(bk,(0,0))#(0,0)表示窗体位置

    for event in pygame.event.get(): #抓取每一个事件
        if event.type==pygame.locals.QUIT: #退出关闭
            print("exit")
            exit()
        elif event.type==pygame.locals.KEYDOWN: #处理键盘消息
            if event.key==pygame.locals.K_k:
                print("K")
            elif event.key==pygame.locals.K_SPACE:
                print("SPACE")
    pygame.display.update()  #消息循环,更新消息

4. Load the aircraft

import pygame
import pygame.locals
screen=pygame.display.set_mode((480,650),0,32)  #创建一个游戏窗体
#bk=pygame.image.load("D:\\Python代码\\class28\\images\\background.png").convert()  #载入文件图片到内存
bk=pygame.image.load("images\\background.png").convert()#相对路径
plane=pygame.image.load("images\\me1.png").convert_alpha()#alpha可以消除背景

x=0
y=0

while True:
    screen.blit(bk,(0,0))#(0,0)表示窗体位置
    screen.blit(plane,(x,y))
    for event in pygame.event.get(): #抓取每一个事件
        if event.type==pygame.locals.QUIT: #退出关闭
            print("exit")
            exit()
        elif event.type==pygame.locals.KEYDOWN: #处理键盘消息
            if event.key==pygame.locals.K_w:
                if y-10>=0:
                    y-=10
            elif event.key==pygame.locals.K_s:
                if y+10<=530:
                    y+=10
            elif event.key==pygame.locals.K_a:
                if x-10>=0:
                    x-=10
            elif event.key==pygame.locals.K_d:
                if x+10<=370:
                    x+=10
    pygame.display.update()  #消息循环,更新消息

Insert picture description here
5. Load background music

import pygame
import pygame.midi
import pygame.locals

pygame.init() #初始化
pygame.mixer.init()  #初始化用于播放音乐
bg_size=width,heigth=480,650  #设置窗体大小
screen=pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")

bk=pygame.image.load(r"..\images\background.png").convert()#载入图片相对路径

pygame.mixer.music.load(r"..\sound\飞机大战多人联机版_爱给网_aigei_com\声音\游戏的音乐.mp3") #载入音乐
pygame.mixer.music.set_volume(0.2)  #播放速度
pygame.mixer.music.play(-1)  #循环播放-1死循环
while True:
    screen.blit(bk,(0,0))#(0,0)表示窗体位置
    pygame.display.update()  #消息循环,更新消息

    for event in pygame.event.get(): #抓取每一个事件
        if event.type==pygame.locals.QUIT: #退出关闭
            print("exit")
            exit()

The above is the basic knowledge of realization. After understanding these basic knowledge, you can get the desired effect by continuously refining it according to your own ideas.

My renderings are as follows:
Insert picture description here
Finally:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/114238699