欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pygame制作飞机大战(敌机出现、碰撞检测、增加声音、分数记录)

程序员文章站 2022-03-27 22:26:35
1、歼灭敌机(1)增加敌机:设置敌机,随机从窗口顶部出现,往底部移动,随机从窗口顶部出现只需要设置初始纵坐标为顶部的一个固定值:#敌机初始化k = 0enemy_x1 = 0enemy_y1 = 0(2)敌机移动敌机随机出现设定敌机速度敌机随机出现,可以将初始位置设定在y = -24的位置(稍高于窗口顶部y = 0位置)设定敌机速度,要确定速度就要设定时间t和路程s,时间设定为每执行一次程序t0,时间参数k,设定每执行一次程序移动的像素为5,根据实际效果调整参数,飞机从上到...

1、歼灭敌机

(1)增加敌机:设置敌机,随机从窗口顶部出现,往底部移动,随机从窗口顶部出现只需要设置初始纵坐标为顶部的一个固定值:

#敌机初始化
k = 0
enemy_x1 = 0
enemy_y1 = 0

(2)敌机移动

  • 敌机随机出现
  • 设定敌机速度

敌机随机出现,可以将初始位置设定在y = -24的位置(稍高于窗口顶部y = 0位置)

设定敌机速度,要确定速度就要设定时间t和路程s,时间设定为每执行一次程序t0,时间参数k,设定每执行一次程序移动的像素为5,根据实际效果调整参数,飞机从上到下运行总时间为(1000 + 90)/10 = 109*t0,总路程s = (109*t0)*5:

 #随机出现敌人
    #敌人1
    if k == 0:
        enemy_x1 = random.randint(-24, 426)
        enemy_y1 = -24
    elif k == 90:
        k = -1000
    k += 10
    enemy_y1 += 5

同理,可以再增加一个敌机,但是飞机初始出现的位置需要和前一个区别:

 #敌人2
    if l == 80:
        enemy_x2 = random.randint(-24, 426)
        enemy_y2 = -24
    elif l == 170:
        l = -1170
    l += 10
    enemy_y2 += 5

2、碰撞检测

只要检测到子弹“左边缘”横坐标在敌机“左边缘”和“右边缘”之间且子弹“顶部”纵坐标在敌机“底部坐标之上”,或者子弹“右边缘”横坐标在敌机“左边缘”和“右边缘”之间且子弹“顶部”纵坐标在敌机“底部坐标之上”,则判定子弹会和敌机发生碰撞:(由于设定的子弹速度很快,暂时没有判断子弹“顶部”纵坐标在敌机“底部坐标之上”

pygame制作飞机大战(敌机出现、碰撞检测、增加声音、分数记录)pygame制作飞机大战(敌机出现、碰撞检测、增加声音、分数记录)

#检测碰撞
def collide(button_x1, enemy_x1, enemy_y1, score_count):        # x1子弹中心横坐标,X2敌机中心横坐标
    collide_x1 = enemy_x1
    collide_y1 = enemy_y1
    if ((enemy_x1 - 15) <= (button_x1 - 4) <= (enemy_x1 + 15)) or ((enemy_x1 - 15) <= (button_x1 + 4) <= (enemy_x1 + 15)):
        collide_x1 = random.randint(-24, 426)
        collide_y1 = -65
        score_count +=1
    print(score_count)
    return collide_x1, collide_y1, score_count

3、增加声音

初始化及加载:

#音乐初始化及加载
pygame.mixer.init()
pygame.mixer_music.load('button.wav')

显示子弹声音:

 #显示子弹声音
    pygame.mixer_music.play()

4、计算分数

计算分数比较简单,这里只选择了三位数,即最大为999,但是提前需要准备好1-9的数字图片:

 #计算并显示分数
    units = score_count//1%10
    tens = score_count//10%10
    hundreds = score_count//100%10

    units_picture = score_picture_list[units]
    tens_picture = score_picture_list[tens]
    hundreds_picture = score_picture_list[hundreds]

5、完整代码

#参数初始化
background_image_filename = 'background.png'
mouse_image_filename = 'airplane.png'
button_image_filename = 'button1.png'
enemy1_image_filename = 'enemy.png'
enemy2_image_filename = 'enemy.png'
score_board = 'score_board.png'
number_0 = 'zero.png'
number_1 = 'one.png'
number_2 = 'two.png'
number_3 = 'three.png'
number_4 = 'four.png'
number_5 = 'five.png'
number_6 = 'six.png'
number_7 = 'seven.png'
number_8 = 'eight.png'
number_9 = 'nine.png'

#导入函数
import pygame
from pygame.locals import *
from sys import exit
import random

#音乐初始化及加载
pygame.mixer.init()
pygame.mixer_music.load('button.wav')

#加载并转换图像
background = pygame.image.load(background_image_filename)
mouse_cursor = pygame.image.load(mouse_image_filename)
button_picture1 = pygame.image.load(button_image_filename)
button_picture2 = pygame.image.load(button_image_filename)
enemy_picture1 = pygame.image.load(enemy1_image_filename)
enemy_picture2 = pygame.image.load(enemy2_image_filename)
score_board_picture = pygame.image.load(score_board)
score_zero_picture = pygame.image.load(number_0)
score_one_picture = pygame.image.load(number_1)
score_two_picture = pygame.image.load(number_2)
score_three_picture = pygame.image.load(number_3)
score_four_picture = pygame.image.load(number_4)
score_five_picture = pygame.image.load(number_5)
score_six_picture = pygame.image.load(number_6)
score_seven_picture = pygame.image.load(number_7)
score_eight_picture = pygame.image.load(number_8)
score_nine_picture = pygame.image.load(number_9)
score_picture_list = [score_zero_picture, score_one_picture, score_two_picture, score_three_picture, score_four_picture,
                      score_five_picture, score_six_picture, score_seven_picture, score_eight_picture, score_nine_picture]

#创建窗口及标题
screen = pygame.display.set_mode((450, 550), 0, 32)
pygame.display.set_caption("hello world")

#初始化pygame,为硬件使用作准备
pygame.init()

x1 = 225
y1 = 275
#子弹坐标初始化
button_x1 = 0
button_y1 = 0
button_x2 = 0
button_y2 = 0
v = 4
i = 0
j = 0
#敌机初始化
k = 0
enemy_x1 = 0
enemy_y1 = 0
#分数初始化
score_count = 0
board_x = 280
board_y = 20
number_x = list(range(10))

l = 0
enemy_x2 = 0
enemy_y2 = 0
units_x1 = 60
units_y1 = 0
tens_x1 = 30
tens_y1 = 0
hundreds_x1 = 0
hundreds_y1 = 0

# 检测边缘
def judge(potx, poty):
    if potx >= 426:
        potx = 426
    elif potx <= -24:
        potx = -24
    elif poty >= 526:
        poty = 526
    elif poty <= -24:
        poty = -24
    return potx, poty

#检测碰撞
def collide(button_x1, enemy_x1, enemy_y1, score_count):        # x1子弹中心横坐标,X2敌机中心横坐标
    collide_x1 = enemy_x1
    collide_y1 = enemy_y1
    if ((enemy_x1 - 15) <= (button_x1 - 4) <= (enemy_x1 + 15)) or ((enemy_x1 - 15) <= (button_x1 + 4) <= (enemy_x1 + 15)):
        collide_x1 = random.randint(-24, 426)
        collide_y1 = -65
        score_count +=1
    print(score_count)
    return collide_x1, collide_y1, score_count

#游戏主循环
while True:
    # 检测退出事件,判断是否执行退出
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    #将背景画上去
    screen.blit(background, (0, 0))

    #获取按键状态并完成移动
    press = pygame.key.get_pressed()

    if press[K_LEFT] == True:
        if press[K_UP] == True:
            x1 = x1 - v
            y1 = y1 - v
        elif press[K_DOWN] == True:
            x1 = x1 - v
            y1 = y1 + v
        else:
            x1 = x1 - v
    elif press[K_RIGHT] == True:
        if press[K_UP] == True:
            x1 = x1 + v
            y1 = y1 - v
        elif press[K_DOWN] == True:
            x1 = x1 + v
            y1 = y1 + v
        else:
            x1 = x1 + v
    elif press[K_UP] == True:
        y1 = y1 - v
    elif press[K_DOWN] == True:
        y1 = y1 + 4
    # 获取鼠标位置
    #x, y = pygame.mouse.get_pos()
    #计算光标在左上角的位置
    #x -= mouse_cursor.get_width() / 2
    #y -= mouse_cursor.get_height() / 2

    #子弹移动
    #子弹1移动
    if i == 0:
        button_x1 = x1 + 3
        button_y1 = y1 + 60
    elif i == 90:
        i = -10
    i += 10
    button_y1 -= 65
    #子弹2移动
    if j == 0:
        button_x2 = x1 + 27
        button_y2 = y1 + 60
    elif j == 90:
        j = -10
    j += 10
    button_y2 -= 65

    #判断边缘
    x1, y1 = judge(potx = x1, poty = y1)

    #随机出现敌人
    #敌人1
    if k == 0:
        enemy_x1 = random.randint(-24, 426)
        enemy_y1 = -24
    elif k == 90:
        k = -1000
    k += 10
    enemy_y1 += 5

    #敌人2
    if l == 80:
        enemy_x2 = random.randint(-24, 426)
        enemy_y2 = -24
    elif l == 170:
        l = -1170
    l += 10
    enemy_y2 += 5

    #子弹击中
    enemy_x1, enemy_y1, score_count = collide(button_x1, enemy_x1, enemy_y1, score_count)
    enemy_x2, enemy_y2, score_count = collide(button_x2, enemy_x2, enemy_y2,score_count)

    #显示子弹声音
    pygame.mixer_music.play()

    #计算并显示分数
    units = score_count//1%10
    tens = score_count//10%10
    hundreds = score_count//100%10

    units_picture = score_picture_list[units]
    tens_picture = score_picture_list[tens]
    hundreds_picture = score_picture_list[hundreds]

    #将光标画上去
    screen.blit(mouse_cursor, (x1, y1))
    screen.blit(button_picture1, (button_x1, button_y1))
    screen.blit(button_picture2, (button_x2, button_y2))

    screen.blit(enemy_picture1, (enemy_x1, enemy_y1))
    screen.blit(enemy_picture2, (enemy_x2, enemy_y2))

    screen.blit(units_picture, (units_x1, units_y1))
    screen.blit(tens_picture, (tens_x1, tens_y1))
    screen.blit(hundreds_picture, (hundreds_x1, hundreds_y1))


    # 在新的位置上画图
    pygame.display.update() 

PS:可以看出,这样的写法代码量很大,下一步会基于“面向对象”和“程序提炼”来优化一遍上述的程序。

本文地址:https://blog.csdn.net/F_MILK_/article/details/109030183