python编写飞机大战小游戏+源码

前言

大家之前用python编写过飞机大战的部分代码,只能够展示英雄飞机,背景,敌机和发射子弹,今天把背景音乐,击毁敌机,爆

炸特效,得分等等相关功能一并加入进来,代码有点长,三百多行,你们要的代码来了哦?

在这里插入图片描述

编程思路

主要使用pygame库,类的创建,函数的调用等等来实现,话不多说,上程序。

在这里插入图片描述

编程实现

Python学习交流Q群:906715085####
import pygame  # 导入动态模块(.dll .pyd .so) 不需要在包名后边跟模块名

from pygame.locals import *

import time

import random

import sys




# 定义常量(定义后,不再改值)

WINDOW_HEIGHT = 768

WINDOW_WIDTH = 512




enemy_list = []

score = 0

is_restart = False





class Map:

def __init__(self, img_path, window):

self.x = 0

self.bg_img1 = pygame.image.load(img_path)

self.bg_img2 = pygame.image.load(img_path)

self.bg1_y = - WINDOW_HEIGHT

self.bg2_y = 0

self.window = window




def move(self):

# 当地图1的 y轴移动到0,则重置

if self.bg1_y >= 0:

self.bg1_y = - WINDOW_HEIGHT




# 当地图2的 y轴移动到 窗口底部,则重置

if self.bg2_y >= WINDOW_HEIGHT:

self.bg2_y = 0




# 每次循环都移动1个像素

self.bg1_y += 3

self.bg2_y += 3




def display(self):

"""贴图"""

self.window.blit(self.bg_img1, (self.x, self.bg1_y))

self.window.blit(self.bg_img2, (self.x, self.bg2_y))





class HeroBullet:

"""英雄子弹类"""

def __init__(self, img_path, x, y, window):

self.img = pygame.image.load(img_path)

self.x = x

self.y = y

self.window = window




def display(self):

self.window.blit(self.img, (self.x, self.y))




def move(self):

"""子弹向上飞行距离"""

self.y -= 6




def is_hit_enemy(self, enemy):

if pygame.Rect.colliderect(

pygame.Rect(self.x, self.y, 20, 31),

pygame.Rect(enemy.x, enemy.y, 100, 68)

):  # 判断是否交叉

return True

else:

return False





class EnemyPlane:

"""敌人飞机类"""

def __init__(self, img_path, x, y, window):

self.img = pygame.image.load(img_path)  # 图片对象

self.x = x  # 飞机坐标

self.y = y

self.window = window  # 飞机所在的窗口

self.is_hited = False

self.anim_index = 0

self.hit_sound = pygame.mixer.Sound("E:/飞机大战/baozha.ogg")




def move(self):

self.y += 10

# 到达窗口下边界,回到顶部

if self.y >= WINDOW_HEIGHT:

self.x = random.randint(0, random.randint(0, WINDOW_WIDTH - 100))

self.y = 0




def plane_down_anim(self):

"""敌机被击中动画"""

if self.anim_index >= 21:  # 动画执行完

self.anim_index = 0

self.img = pygame.image.load(

"E:/飞机大战/img-plane_%d.png" % random.randint(1, 7))

self.x = random.randint(0, WINDOW_WIDTH - 100)

self.y = 0

self.is_hited = False

return

elif self.anim_index == 0:

self.hit_sound.play()

self.img = pygame.image.load(

"E:/飞机大战/bomb-%d.png" % (self.anim_index // 3 + 1))

self.anim_index += 1






def display(self):

"""贴图"""

if self.is_hited:

self.plane_down_anim()




self.window.blit(self.img, (self.x, self.y))





class HeroPlane:

def __init__(self, img_path, x, y, window):

self.img = pygame.image.load(img_path)  # 图片对象

self.x = x  # 飞机坐标

self.y = y

self.window = window  # 飞机所在的窗口

self.bullets = []  # 记录该飞机发出的所有子弹

self.is_hited = False

self.is_anim_down = False

self.anim_index = 0




def is_hit_enemy(self, enemy):

if pygame.Rect.colliderect(

pygame.Rect(self.x, self.y, 120, 78),

pygame.Rect(enemy.x, enemy.y, 100, 68)

):  # 判断是否交叉

return True

else:

return False




def plane_down_anim(self):

"""敌机被击中动画"""

if self.anim_index >= 21:  # 动画执行完

self.is_hited = False

self.is_anim_down = True

return




self.img = pygame.image.load(

"E:/飞机大战/bomb-%d.png" % (self.anim_index // 3 + 1))

self.anim_index += 1




def display(self):

"""贴图"""

for enemy in enemy_list:

if self.is_hit_enemy(enemy):

enemy.is_hited = 
  • 3
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您好!以下是一个简单的飞机大战小游戏Python 代码示例: ```python import pygame import random # 初始化游戏 pygame.init() # 游戏窗口大小 screen_width = 480 screen_height = 600 # 创建游戏窗口 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置窗口标题 pygame.display.set_caption("飞机大战") # 加载背景图片 background = pygame.image.load("background.png") # 加载玩家飞机图片 player = pygame.image.load("player.png") player_rect = player.get_rect() player_rect.centerx = screen_width // 2 player_rect.bottom = screen_height - 10 # 加载敌机图片 enemy = pygame.image.load("enemy.png") enemy_rect = enemy.get_rect() # 计时器,用于控制敌机的出现频率 enemy_timer = pygame.time.get_ticks() # 子弹的初始速度 bullet_speed = 5 # 子弹列表 bullets = [] # 敌机列表 enemies = [] # 游戏结束标志位 game_over = False # 游戏时钟 clock = pygame.time.Clock() # 游戏循环 while not game_over: # 设置帧率 clock.tick(60) # 监听事件 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # 监听键盘事件,控制玩家飞机移动和发射子弹 if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bullet = pygame.Rect(player_rect.centerx, player_rect.top, 5, 11) bullets.append(bullet) # 移动玩家飞机 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_rect.x -= 5 if keys[pygame.K_RIGHT]: player_rect.x += 5 if keys[pygame.K_UP]: player_rect.y -= 5 if keys[pygame.K_DOWN]: player_rect.y += 5 # 控制玩家飞机在窗口范围内移动 if player_rect.left < 0: player_rect.left = 0 if player_rect.right > screen_width: player_rect.right = screen_width if player_rect.top < 0: player_rect.top = 0 if player_rect.bottom > screen_height: player_rect.bottom = screen_height # 发射子弹 for bullet in bullets: bullet.y -= bullet_speed if bullet.y < 0: bullets.remove(bullet) # 控制敌机的出现频率 now = pygame.time.get_ticks() if now - enemy_timer >= 1000: enemy_rect.x = random.randint(0, screen_width - enemy_rect.width) enemy_rect.y = -enemy_rect.height enemies.append(enemy_rect) enemy_timer = now # 移动敌机 for enemy in enemies: enemy.y += bullet_speed if enemy.y > screen_height: enemies.remove(enemy) # 碰撞检测 for bullet in bullets: for enemy in enemies: if bullet.colliderect(enemy): bullets.remove(bullet) enemies.remove(enemy) # 绘制背景 screen.blit(background, (0, 0)) # 绘制玩家飞机 screen.blit(player, player_rect) # 绘制子弹 for bullet in bullets: pygame.draw.rect(screen, (255, 255, 255), bullet) # 绘制敌机 for enemy in enemies: screen.blit(enemy, enemy) # 更新屏幕 pygame.display.update() # 退出游戏 pygame.quit() ``` 请注意,以上代码只是一个简单的示例,您可以根据自己的需求进行修改和完善。同时,您需要准备好相应的图片资源,并将其与代码放在同一个目录下。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值