diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..9d06069 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,4 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + + diff --git a/src/__init__.pyc b/src/__init__.pyc new file mode 100644 index 0000000..3f8bbe7 Binary files /dev/null and b/src/__init__.pyc differ diff --git a/src/__pycache__/__init__.cpython-38.pyc b/src/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..b1d6871 Binary files /dev/null and b/src/__pycache__/__init__.cpython-38.pyc differ diff --git a/src/__pycache__/bullet.cpython-38.pyc b/src/__pycache__/bullet.cpython-38.pyc new file mode 100644 index 0000000..d361045 Binary files /dev/null and b/src/__pycache__/bullet.cpython-38.pyc differ diff --git a/src/__pycache__/enemy.cpython-38.pyc b/src/__pycache__/enemy.cpython-38.pyc new file mode 100644 index 0000000..a203f3b Binary files /dev/null and b/src/__pycache__/enemy.cpython-38.pyc differ diff --git a/src/__pycache__/plane.cpython-38.pyc b/src/__pycache__/plane.cpython-38.pyc new file mode 100644 index 0000000..1e9bed6 Binary files /dev/null and b/src/__pycache__/plane.cpython-38.pyc differ diff --git a/src/bullet.py b/src/bullet.py new file mode 100644 index 0000000..85830ac --- /dev/null +++ b/src/bullet.py @@ -0,0 +1,42 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +""" + 子弹的实现 +""" + +import pygame + + +class Bullet(pygame.sprite.Sprite): + + def __init__(self, position): + super(Bullet, self).__init__() + self.image = pygame.image.load("material/image/bullet1.png") + self.rect = self.image.get_rect() + self.rect.left, self.rect.top = position + self.speed = 30 + self.active = True + self.mask = pygame.mask.from_surface(self.image) + + def move(self): + """ + 子弹移动, 超出屏幕范围, 则设置死亡 + :return: + """ + if self.rect.top < 0: + self.active = False + else: + self.rect.top -= self.speed + + def reset(self, position): + """ + 复位函数 + :param position: + :return: + """ + self.rect.left, self.rect.top = position + self.active = True + + + diff --git a/src/bullet.pyc b/src/bullet.pyc new file mode 100644 index 0000000..7550812 Binary files /dev/null and b/src/bullet.pyc differ diff --git a/src/enemy.py b/src/enemy.py new file mode 100644 index 0000000..251fe31 --- /dev/null +++ b/src/enemy.py @@ -0,0 +1,60 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +""" + 定义敌机 +""" + +from random import randint + +import pygame + + +class SmallEnemy(pygame.sprite.Sprite): + """ + 定义小飞机敌人 + """ + energy = 1 + + def __init__(self, bg_size): + super(SmallEnemy, self).__init__() + self.image = pygame.image.load("material/image/enemy1.png") + self.rect = self.image.get_rect() + self.width, self.height = bg_size[0], bg_size[1] + self.mask = pygame.mask.from_surface(self.image) # 获取飞机图像的掩膜用以更加精确的碰撞检测 + self.speed = 2 + self.energy = SmallEnemy.energy + # 定义敌机出现的位置, 保证敌机不会在程序已开始就立即出现 + self.rect.left, self.rect.top = ( + randint(0, self.width - self.rect.width), randint(-5 * self.rect.height, -5), + ) + self.active = True + # 加载飞机损毁图片 + self.destroy_images = [] + self.destroy_images.extend( + [ + + pygame.image.load("material/image/enemy1_down3.png"), + pygame.image.load("material/image/enemy1_down4.png") + ] + ) + + def move(self): + """ + 定义敌机的移动函数 + :return: + """ + if self.rect.top < self.height: + self.rect.top += self.speed + else: + self.reset() + + def reset(self): + """ + 当敌机向下移动出屏幕且飞机是需要进行随机出现的, 以及敌机死亡 + :return: + """ + self.rect.left, self.rect.top = (randint(0, self.width - self.rect.width), randint(-5 * self.rect.height, 0)) + self.active = True + + diff --git a/src/enemy.pyc b/src/enemy.pyc new file mode 100644 index 0000000..301aa6c Binary files /dev/null and b/src/enemy.pyc differ diff --git a/src/plane.py b/src/plane.py new file mode 100644 index 0000000..b384538 --- /dev/null +++ b/src/plane.py @@ -0,0 +1,88 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +""" + 创建飞机 + 在pygame中, 所有可移动的对象均叫可看作一个精灵(sprite) + 该类并实现了碰撞方法 spritecollide + + 我方飞机和敌方飞机指定掩膜属性以及生存状态标志位 添加 self.mask 属性(可以实现更精准的碰撞效果) +""" +from config.settings import BASE_DIR + +import os +# 倒入精灵模块, 使飞机可以动起来 +import pygame + + +class OurPlane(pygame.sprite.Sprite): + + def __init__(self, bg_size): + super(OurPlane, self).__init__() + # 确定我方飞机背景图(有俩张,可以让它们不停的切换,形成动态效果) + self.image_one = pygame.image.load(os.path.join(BASE_DIR, "material/image/hero1.png")) + self.image_two = pygame.image.load(os.path.join(BASE_DIR, "material/image/hero2.png")) + # 获取我方飞机的位置 + self.rect = self.image_one.get_rect() + # 本地化背景图片的尺寸 + self.width, self.height = bg_size[0], bg_size[1] + # 获取飞机图像的掩膜用以更加精确的碰撞检测 + self.mask = pygame.mask.from_surface(self.image_one) + # 定义飞机初始化位置,底部预留60像素 + self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, (self.height - self.rect.height - 60) + # 设置飞机移动速度 + self.speed = 10 + # 设置飞机存活状态(True为存活, False为死亡) + self.active = True + # 加载飞机损毁图片 + self.destroy_images = [] + self.destroy_images.extend( + [ + pygame.image.load(os.path.join(BASE_DIR, "material/image/hero_blowup_n4.png")), + ] + ) + + def move_up(self): + """ + 飞机向上移动的操作函数,其余移动函数方法类似 + """ + if self.rect.top > 0: # 如果飞机尚未移动出背景区域 + self.rect.top -= self.speed + else: # 若即将移动出背景区域,则及时纠正为背景边缘位置 + self.rect.top = 0 + + def move_down(self): + """ + 飞机向下移动 + """ + if self.rect.bottom < self.height - 60: + self.rect.top += self.speed + else: + self.rect.bottom = self.height - 60 + + def move_left(self): + """ + 飞机向左移动 + """ + if self.rect.left > 0: + self.rect.left -= self.speed + else: + self.rect.left = 0 + + def move_right(self): + """ + 飞机向右移动 + """ + if self.rect.right < self.width: + self.rect.right += self.speed + else: + self.rect.right = self.width + + def reset(self): + # 初始化飞机(飞机挂了, 初始化到初始位置) + self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, (self.height - self.rect.height - 60) + # 重置飞机的存活状态 + self.active = True + + + diff --git a/src/plane.pyc b/src/plane.pyc new file mode 100644 index 0000000..5502b9b Binary files /dev/null and b/src/plane.pyc differ