导语
12月的心境:一半优,一半喜。
12月的天空: 一半晴,一半雨。
12月的风 : 已不再那么的轻柔 ,带有无言的烦躁!甚至裹着疼痛的雨。
2022年的尾巴 :已悄悄来临!
临近新的一年,小编为大家准备了一套系列《从简易到豪华版——3个版的“飞机大战”小游戏》
大家可以先自行💭想象~嘿嘿。等我慢慢更新这个系列,今天先给大家更新一个基础版本的飞机大
战吧!
其实说是基础的话也不是很基础:界面特别精致、然后还有相应的防护盾等功能,只是因为第一款
的飞机大战是天上掉下陨石然后敌机的话没得,等我给大家下次升级在给大家写哈!
那么——我们就正式开始干活儿吧👇👇👇👇👇👇
正文
故事背景:(纯虚构,勿认真)
12月30号晚上凌晨,🌏发生陨石坠落。现场视频显示,当晚夜空忽然被照亮,一道红光从空中划
过,几秒后迅速消失。经过研究,预计今天还会有新一波的大量陨石从天而降,甚至这大量陨石的
降落威胁到了人们的生活,造成了多起医疗事故,所以最后出动了保卫战战队进行宇宙的陨石消灭
计划!我们要尽可能的击碎甚至消灭这些大量的陨石...........计划开始........
一、游戏规则
我们设计了一款基于Python的Pygame模块开发的飞机大战游戏。
飞机大战游戏是一款休闲益智类游戏,既简单又耐玩。在初始界面,我们有开始游戏和退出游戏两
个选项。
开始游戏后,玩家可以用上下左右方向键的控制飞机在屏幕上向任意方向移动,通过空格键射击陨
石得分,有护盾加生命以及闪电⚡加攻击炮弹,初始化生命为3次在屏幕右上角可以看到当前生
命,中间可以看得分。
二、准备中
1)素材图片
2)来点儿Music
三、代码演示
from __future__ import division
import pygame
import random
from os import path
img_dir = path.join(path.dirname(__file__), 'assets')
sound_folder = path.join(path.dirname(__file__), 'sounds')
WIDTH = 480
HEIGHT = 600
FPS = 60
POWERUP_TIME = 5000
BAR_LENGTH = 100
BAR_HEIGHT = 10
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
pygame.init()
pygame.mixer.init() ## For sound
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Shooter")
clock = pygame.time.Clock() ## For syncing the FPS
font_name = pygame.font.match_font('arial')
def main_menu():
global screen
menu_song = pygame.mixer.music.load(path.join(sound_folder, "menu.ogg"))
pygame.mixer.music.play(-1)
title = pygame.image.load(path.join(img_dir, "main.png")).convert()
title = pygame.transform.scale(title, (WIDTH, HEIGHT), screen)
screen.blit(title, (0,0))
pygame.display.update()
while True:
ev = pygame.event.poll()
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_RETURN:
break
elif ev.key == pygame.K_q:
pygame.quit()
quit()
elif ev.type == pygame.QUIT:
pygame.quit()
quit()
else:
draw_text(screen, "Press [ENTER] To Begin", 30, WIDTH/2, HEIGHT/2)
draw_text(screen, "or [Q] To Quit", 30, WIDTH/2, (HEIGHT/2)+40)
pygame.display.update()
#pygame.mixer.music.stop()
ready = pygame.mixer.Sound(path.join(sound_folder,'getready.ogg'))
ready.play()
screen.fill(BLACK)
draw_text(screen, "GET READY!", 40, WIDTH/2, HEIGHT/2)
pygame.display.update()
def draw_text(surf, text, size, x, y):
## selecting a cross platform font to display the score
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE) ## True denotes the font to be anti-aliased
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
def draw_shield_bar(surf, x, y, pct):
# if pct < 0:
# pct = 0
pct = max(pct, 0)
## moving them to top
# BAR_LENGTH = 100
# BAR_HEIGHT = 10
fill = (pct / 100) * BAR_LENGTH
outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
pygame.draw.rect(surf, GREEN, fill_rect)
pygame.draw.rect(surf, WHITE, outline_rect, 2)
def draw_lives(surf, x, y, lives, img):
for i in range(lives):
img_rect= img.get_rect()
img_rect.x = x + 30 * i