1.用python做一个飞机大战(一)
2.添加自己飞机的生命,击落敌机的分数,背景音乐(二)
用到的一个python插件,pygame,想要深入学习python游戏开发可访问官网:https://www.pygame.org/news
实现后的飞机大战:
1.创建一个飞机的精灵
import pygame
import random
SCREEN_SIZE = None
CLOCK_TICK = 60
EMENY_FLY_MAX_SPEED = 12
#背景类
class BackGround(pygame.sprite.Sprite):
def __init__(self,bg_pathname,speed = 1,is_first = True):
global SCREEN_SIZE
# print(SCREEN_SIZE)
super().__init__()
self.image = pygame.image.load(bg_pathname)
self.rect = self.image.get_rect() # 0 0 width height
self.speed = speed
if not is_first:
self.rect.y = -SCREEN_SIZE.height
def update(self, *args):
self.rect.y += self.speed
if self.rect.y >SCREEN_SIZE.height:
self.rect.y = -SCREEN_SIZE.height
#敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self,image_path,plane_size = 1):
super().__init__()
self.image = pygame.image.load(image_path)
self.rect = self.image.get_rect()
self.rect.bottom = 0
# self.hp = 1
self.rect.x = random.randint(0,SCREEN_SIZE.width-self.rect.width)
if plane_size == 1:
self.speed = random.randint(1,EMENY_FLY_MAX_SPEED)
# self.hp = 1
elif plane_size == 2:
self.speed = random.randint(1,EMENY_FLY_MAX_SPEED/2)
# self.hp = EMENY_FLY_MAX_SPEED/2
else:
self.speed = random.randint(1, EMENY_FLY_MAX_SPEED / 3)
# self.hp = EMENY_FLY_MAX_SPEED * 2
def update(self, *args):
self.rect.y += self.speed
if self.rect.y >= SCREEN_SIZE.height:
self.kill()
def __del__(self):
#print('over...')
pass
#英雄类
class Hero(pygame.sprite.Sprite):
def __init__(self,pathname1,pathname2):
super().__init__()
self.image = None
self.image_flag = False
self.speed = {'x':0,'y':0}
self.image_all = [pygame.image.load(pathname1),pygame.image.load(pathname2)]
self.rect = self.image_all[0].get_rect()
self.rect.centerx = SCREEN_SIZE.centerx
self.rect.bottom = SCREEN_SIZE.height - 100
self.bullets = pygame.sprite.Group()
def update(self):
self.image = self.image_all[int(self.image_flag)]
self.image_flag = not self.image_flag
self.rect.x += self.speed['x']
self.rect.y += self.speed['y']
if self.rect.centerx < 0:
self.rect.centerx = 0
if self.rect.centerx >SCREEN_SIZE.width:
self.rect.centerx = SCREEN_SIZE.width
if self.rect.top > SCREEN_SIZE.height-self.rect.height:
self.rect.bottom = SCREEN_SIZE.height
if self.rect.bottom < 0:
self.rect.bottom = 0
class Bullet(pygame.sprite.Sprite):
def __init__(self,pathname,centerx,top,speed = -3):
super().__init__()
self.speed = speed
self.image = pygame.image.load(pathname)
self.rect = self.image.get_rect()
self.rect.centerx = centerx
self.rect.bottom = top
def update(self, *args):
self.rect.y += self.speed
if self.rect.bottom < 0:
self.kill()
def __del__(self):
print("子弹被销毁...")
2.创建主类
强调:
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021061416545376.png#pic_center)
```python
import pygame
import plane_sprites
ENEMY_SMALL_ENVENT1 = pygame.USEREVENT + 1
ENEMY_SMALL_ENVENT2 = pygame.USEREVENT + 2
class PlaneGame(object):
def __init__(self):
# 模块初始化,backgroud.png是背景图
pygame.init()
plane_sprites.SCREEN_SIZE = pygame.image.load('./images/background.png').get_rect()
self.screen = pygame.display.set_mode(plane_sprites.SCREEN_SIZE.size)
# 游戏时钟
self.clock = pygame.time.Clock()
# 各种各样精灵和精灵组
self.__create_sprites()
def start_game(self):
while True:
# 设置游戏的刷新频率
self.clock.tick(plane_sprites.CLOCK_TICK)
# 事件检测
self.__event_handler()
# 碰撞的检测
self.__check_collide()
# 重新绘制元素
self.__sprites_update()
# 游戏屏幕更新
pygame.display.update()
def __game_exit(self):
pygame.quit()
exit(0)
def __event_handler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.__game_exit()
elif event.type == ENEMY_SMALL_ENVENT1:
e1 = plane_sprites.Enemy('./images/enemy1.png')
self.enemy1_group.add(e1)
elif event.type == ENEMY_SMALL_ENVENT2:
e1 = plane_sprites.Bullet('./images/bullet2.png',self.hero.rect.centerx,self.hero.rect.top,-8)
self.bullet_group.add(e1)
keys_pressed = pygame.key.get_pressed()
# print(len(keys_pressed))
if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
self.hero.speed['x'] = 4
elif keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
self.hero.speed['x'] = -4
elif keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
self.hero.speed['y'] = -4
elif keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
self.hero.speed['y'] = 4
else:
self.hero.speed['x'] = 0
self.hero.speed['y'] = 0
def __check_collide(self):
# 1.子弹摧毁飞机
pygame.sprite.groupcollide(self.bullet_group, self.enemy1_group, True, True)
# 2.敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy1_group, True)
if len(enemies) > 0:
self.hero.kill()
PlaneGame.__game_over()
# 让英雄牺牲
def __sprites_update(self):
for grp in [self.bg_group,self.enemy1_group,self.hero_group,self.bullet_group]:
grp.update()
grp.draw(self.screen)
def __create_sprites(self):
# 构建背景图的精灵和精灵组
bg1 = plane_sprites.BackGround('./images/background.png',2)
bg2 = plane_sprites.BackGround('./images/background.png',2,False)
self.bg_group = pygame.sprite.Group()
self.bg_group.add(bg1,bg2)
#敌方飞机
pygame.time.set_timer(ENEMY_SMALL_ENVENT1,800)
self.enemy1_group = pygame.sprite.Group()
# 我方飞机
self.hero = plane_sprites.Hero('./images/me1.png','./images/me2.png')
self.hero_group = pygame.sprite.Group()
self.hero_group.add(self.hero)
# 子弹
pygame.time.set_timer(ENEMY_SMALL_ENVENT2, 100)
self.bullet_group = pygame.sprite.Group()
if __name__ == '__main__':
p1 = PlaneGame()
p1.start_game()
图片:
enemy1.png
background.png
me1.png
me2.png