Python 游戏编程之实现飞机大战(含源代码)( 二 )
self.destroy_images =
self.destroy_images.extend([ \
pygame.image.load("images/enemy1_down1.png").convert_alpha(), \
pygame.image.load("images/enemy1_down2.png").convert_alpha(), \
pygame.image.load("images/enemy1_down3.png").convert_alpha(), \
pygame.image.load("images/enemy1_down4.png").convert_alpha() \
])
self.rect = self.image.get_rect
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.active = True
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-5 * self.height, 0)
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset
def reset(self):
self.active = True
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-5 * self.height, 0)
class MidEnemy(pygame.sprite.Sprite):
energy = 8
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/enemy2.png").convert_alpha
self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha
self.destroy_images =
self.destroy_images.extend([ \
pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
pygame.image.load("images/enemy2_down4.png").convert_alpha() \
])
self.rect = self.image.get_rect
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 1
self.active = True
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-10 * self.height, -self.height)
self.mask = pygame.mask.from_surface(self.image)
self.energy = MidEnemy.energy
self.hit = False
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset
def reset(self):
self.active = True
self.energy = MidEnemy.energy
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-10 * self.height, -self.height)
class BigEnemy(pygame.sprite.Sprite):
energy = 20
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha
self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha
self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha
self.destroy_images =
self.destroy_images.extend([ \
pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
pygame.image.load("images/enemy3_down6.png").convert_alpha() \
])
self.rect = self.image1.get_rect
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 1
self.active = True
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-15 * self.height, -5 * self.height)
self.mask = pygame.mask.from_surface(self.image1)
self.energy = BigEnemy.energy
self.hit = False
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset
def reset(self):
self.active = True
self.energy = BigEnemy.energy
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-15 * self.height, -5 * self.height)
四.发射子弹现在的情况是我方飞机处于落后挨打的状态,敌强我弱,所以应该拿起武器进行反击! 接下来定义子弹,子弹分为两种: 一种是普通子弹一次只发射一颗; 另一种是补给发放的超级子弹一次可以发射两颗 。
文章插图
我们将子弹定义为独立的模块bullet.py:
import pygame
class Bullet1(pygame.sprite.Sprite):
- 王储|壹周游闻第20期:直播打赏实行实名制;沙特王储收购SNK
- 环境|环境标识认知转盘游戏
- realme X|手机打游戏太卡?行家:那是你还没换这几款
- Play|Google Play公布2020年度最佳应用和游戏排行榜
- 第一款骁龙888游戏手机:红魔6官宣
- 告诉|阿里大佬告诉你如何一分钟利用Python在家告别会员看电影
- Python源码阅读-基础1
- Linux(服务器编程):百万并发服务器系统参数调优
- Python调用时使用*和**
- 如何基于Python实现自动化控制鼠标和键盘操作