python--星际大战(基础版)
原创要实现该功能:
运用python的pygame该模块意识到一组敌机出现在顶部。每架敌机都随机不规则地发射子弹。底部是球员平面。它通过控制方向和发射子弹摧毁所有敌机。比赛开始前会有计时器(3秒)计时结束,游戏开始。如果你的生命值被消耗了,并且仍然有敌机库存,那么它将显示失败。如果你的生命值没有被消耗,并且所有敌机都被摧毁,那么成功界面将会显示!
代码如下:
import pygame from pygame import mixer from pygame.locals import * import sys import random
定义帧
clock = pygame.time.Clock() fps = 60
pygame.mixer.pre_init(44100, -16, 2, 512) pygame.init() screen_width = 400 screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("太空入侵者")
定义字体
font30 = pygame.font.SysFont("Constantia", 30) font40 = pygame.font.SysFont("Constantia", 40)
加载音效
explosion_fx = pygame.mixer.Sound("img/explosion.wav") explosion_fx.set_volume(0.25)
explosion2_fx = pygame.mixer.Sound("img/explosion2.wav") explosion2_fx.set_volume(0.25)
laser_fx = pygame.mixer.Sound("img/laser.wav") laser_fx.set_volume(0.25)
explosion_fx_ship = pygame.mixer.Sound("img/explosion.wav") explosion_fx_ship.set_volume(1)
定义游戏变量
rows = 5 cols = 5 alien_cooldown = 1000 last_alien_shot = pygame.time.get_ticks() countdown = 3 last_count = pygame.time.get_ticks() game_over = 0 # 0比赛还没结束 1是玩赢了 -1 是输了
定义健康栏颜色
red = (255, 0, 0) green = (0, 255, 0) white = (255, 255, 255)
加载图片
bg = pygame.image.load("img/bg.png")
def draw_bg(): screen.blit(bg, (0, 0))
定义文本提示的函数
def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y))
创建一类太空船
class Spaceship(pygame.sprite.Sprite): def __init__(self, x, y, health): super().__init__() self.image = pygame.image.load("img/spaceship.png") self.rect = self.image.get_rect() self.rect.center = [x, y] self.health_start = health self.health_remaining = health self.last_shot = pygame.time.get_ticks()
def update(self):
# 设置移动速度
speed = 8
# 设置冷却时间变量
cooldown = 500 # 毫秒
game\_over = 0
# 按键
key = pygame.key.get\_pressed()
if key[pygame.K\_LEFT] and self.rect.left > 0:
self.rect.x -= speed
if key[pygame.K\_RIGHT] and self.rect.right < screen\_width:
self.rect.x += speed
# 记录当前时间
time\_now = pygame.time.get\_ticks()
# 发射子弹
if key[pygame.K\_SPACE] and time\_now - self.last\_shot > cooldown:
laser\_fx.play()
bullet = Bullets(self.rect.centerx, self.rect.top)
bullet\_group.add(bullet)
self.last\_shot = time\_now
# 创建蒙版
self.mask = pygame.mask.from\_surface(self.image)
# 画健康条
pygame.draw.rect(screen, red, (self.rect.x, (self.rect.bottom + 10), self.rect.width, 15))
if self.health\_remaining > 0:
pygame.draw.rect(screen, green, (self.rect.x, (self.rect.bottom + 10), int(self.rect.width * (self.health\_remaining / self.health\_start)), 15))
elif self.health\_remaining <= 0:
explosion = Explosion(self.rect.centerx, self.rect.centery, 3)
explosion\_group.add(explosion)
explosion\_fx\_ship.play()
self.kill()
game\_over = -1
return game\_over
创建子弹
class Bullets(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load("img/bullet.png") self.rect = self.image.get_rect() self.rect.center = [x, y] def update(self): self.rect.y -= 5 if self.rect.bottom < 0: self.kill()
if pygame.sprite.spritecollide(self, alien\_group, True):
self.kill()
explosion\_fx.play()
explosion = Explosion(self.rect.centerx, self.rect.centery, 2)
explosion\_group.add(explosion)
创建外星人
class Aliens(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load("img/alien" + str(random.randint(1, 5)) + ".png") self.rect = self.image.get_rect() self.rect.center = [x, y] self.move_counter = 0 self.move_direction = 1
def update(self):
self.rect.x += self.move\_direction
self.move\_counter += 1
if abs(self.move\_counter) > 40:
self.move\_direction *= -1
self.move\_counter *= self.move\_direction
异形项目符号
class Alien_Bullets(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load("img/alien_bullet.png") self.rect = self.image.get_rect() self.rect.center = [x, y] def update(self): self.rect.y += 2 if self.rect.top > screen_height: self.kill()
if pygame.sprite.spritecollide(self, spaceship\_group, False, pygame.sprite.collide\_mask):
self.kill()
explosion2\_fx.play()
# 减少健康条
spaceship.health\_remaining -= 1
explosion = Explosion(self.rect.centerx, self.rect.centery, 1)
explosion\_group.add(explosion)
创建爆炸类
class Explosion(pygame.sprite.Sprite): def __init__(self, x, y, size): super().__init__() self.images = [] for num in range(1, 6): img = pygame.image.load(f"img/exp{num}.png")
根据大小缩放
if size == 1:
img = pygame.transform.scale(img, (20, 20))
if size == 2:
img = pygame.transform.scale(img, (40, 40))
if size == 3:
img = pygame.transform.scale(img, (160, 160))
# 将图像添加到列表
self.images.append(img)
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get\_rect()
self.rect.center = [x, y]
self.counter = 0
def update(self):
explosion\_speed = 3
# 更新爆炸动画
self.counter += 1
if self.counter >= explosion\_speed and self.index < len(self.images) - 1:
self.counter = 0
self.index += 1
self.image = self.images[self.index]
# 如果动画完成,请移除爆炸
if self.index >= len(self.images) - 1 and self.counter >= explosion\_speed:
self.kill()
create sprite groups
spaceship_group = pygame.sprite.Group() bullet_group = pygame.sprite.Group() alien_group = pygame.sprite.Group() alien_bullet_group = pygame.sprite.Group() explosion_group = pygame.sprite.Group()
创建飞船
spaceship = Spaceship(screen_width // 2, screen_height - 80, 3) spaceship_group.add(spaceship)
def create_aliens(): for row in range(rows): for item in range(cols): alien = Aliens(item 70 + 50, row 60 + 50) alien_group.add(alien)
create_aliens() run = True while run: clock.tick(fps)
画背景色
draw\_bg()
if countdown == 0:
# 创建随机的异形项目符号
# 记录当前时间
time\_now = pygame.time.get\_ticks()
# 发射子弹
if time\_now - last\_alien\_shot > alien\_cooldown and len(alien\_bullet\_group) < 5 and len(alien\_group) > 0:
attacking\_alien = random.choice(alien\_group.sprites())
alien\_bullet = Alien\_Bullets(attacking\_alien.rect.centerx, attacking\_alien.rect.bottom)
alien\_bullet\_group.add(alien\_bullet)
last\_alien\_shot = time\_now
# 检查是否所有敌人都被摧毁
if len(alien\_group) == 0:
game\_over = 1
if game\_over == 0:
# 更新飞船
game\_over = spaceship.update()
# 更新子弹
bullet\_group.update()
# 更新外国人
alien\_group.update()
# 更新异形项目符号
alien\_bullet\_group.update()
else:
if game\_over == -1:
draw\_text("GET OVER!", font40, white, int(screen\_width / 2 - 110), int(screen\_height / 2 + 50))
if game\_over == 1:
draw\_text("YOU WIN!", font40, white, int(screen\_width / 2 - 110), int(screen\_height / 2 + 50))
if countdown > 0:
draw\_text("GET READY!", font40, white, int(screen\_width / 2 - 110), int(screen\_height / 2 + 50))
draw\_text(str(countdown), font40, white, int(screen\_width / 2 - 10), int(screen\_height / 2 + 100))
count\_timer = pygame.time.get\_ticks()
if count\_timer - last\_count > 1000:
countdown -= 1
last\_count = count\_timer
# 更新爆炸
explosion\_group.update()
spaceship\_group.draw(screen)
bullet\_group.draw(screen)
alien\_group.draw(screen)
alien\_bullet\_group.draw(screen)
explosion\_group.draw(screen)
# event handlers
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
pygame.display.update()
以下屏幕截图(部分):
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除