敌机也是机,又不是象棋里的兵和卒,他也会走八字,跳广场舞的,只要你想,没有什么轨迹是做不到的。
贝塞尔曲线的基本公式上篇日志已经实现了,现在来利用到游戏里。
新建一个飞机类enemyplane.py,为了比较,实现了两个模式,一个是基本的直接往下,一个是贝塞尔曲线。setting.py,bezier.py见前面的日志。
from setting import *
from bezier import *
# 敌人飞机
class EnemyPlane(pygame.sprite.Sprite):
# 初始化敌人飞机
def __init__(self):
self._layer = 2
self.groups = allgroup
pygame.sprite.Sprite.__init__(self, self.groups)
self.image = pygame.image.load('./images/enemy/enemy0.png')
self.rect = self.image.get_rect()
self.HP = 1
#飞机的速度
self.speed = 2
self.x_speed = 0
self.y_speed = 2
#当采用路径时,输入的控制点
self.points= []
#飞机路径上的点 point(x,y)
self.path_points = []
#飞机正常方式
self.path_type = 'normal'
#飞机用路径描述的最大的点,当前点
self.max_point = 500
self.current_point = 0
# 设置飞机位置
def set_pos(self, x, y):
self.rect.x = x
self.rect.y = y
def set_bezier_path(self, points):
self.path_type = 'bezier'
self.points = points
for v in range(0, self.max_point+1):
t = v / self.max_point
self.path_points.append(bezier(self.points, t))
def update(self):
"""
更新位置
计算位置,精灵组自动调用
超出屏幕,自毁
"""
if self.path_type == 'bezier':
if self.current_point == self.max_point:
self.current_point = 0
# print(self.path_points[self.current_point][0])
self.rect.x = self.path_points[self.current_point][0]
self.rect.y = self.path_points[self.current_point][1]
self.current_point += 1
else:
self.rect.x += self.x_speed
self.rect.y += self.y_speed
if self.HP <= 0:
self.kill()
if not self.rect.colliderect(SCENERECT):
self.kill()
在来创建一个飞机组enemyunit.py,最多包括6加飞机。
from setting import *
from enemyplane import *
class EnemyUnit(pygame.sprite.Sprite):
def __init__(self):
self._layer = 2
self.groups = allgroup, enemygroup
pygame.sprite.Sprite.__init__(self, self.groups)
self.image = pygame.image.load('./images/enemy/enemy0.png')
self.rect = self.image.get_rect()
#计数器
self.count = 0
#设置飞机出现间隔用
self.interval = 1000
self.start_time = pygame.time.get_ticks()
#用了sprite必须有个image ,用了个投机的办法,放到屏幕外面去了。
self.set_pos(-300,-300)
#控制飞行轨迹的点
self.points = [(-10, -10), (200, 800), (300, 200), (1000, 200)]
def set_pos(self,x,y):
self.rect.x = x
self.rect.y = y
def update(self):
cur_time = pygame.time.get_ticks()
if cur_time - self.start_time < self.interval:
return
self.start_time = cur_time
enemy = EnemyPlane()
enemy.set_bezier_path(self.points)
enemy.set_pos(-10,-10)
self.count +=1
#大于6架,就退出了
if self.count > 6:
self.kill()
新建一个简单的main.py来测试
from setting import *
from sys import exit
from pygame import gfxdraw
# from demo import *
from enemyunit import *
class MainScene(object):
#初始化
def __init__(self):
pygame.mixer.pre_init()
pygame.init()
pygame.mixer.init()
pygame.font.init()
# pygame.mouse.set_visible(False)
self.scene = pygame.display.set_mode((SCENEWIDTH, SCENEHEIGHT), 1, 32)
# self.scene = pygame.display.set_mode((SCENEWIDTH, SCENEHEIGHT), pygame.FULLSCREEN, 32)
pygame.display.set_caption("自学飞机大战--精灵组版")
self.scene.fill((0,0,0))
enemy = EnemyPlane()
enemy.set_pos(200,0)
enemyunit = EnemyUnit()
# ene.set_path()
#绘制各个元素
def draw(self):
self.scene.fill((0,0,0))
allgroup.draw(self.scene)
def update(self):
allgroup.update()
def collideEvent(self):
pass
#处理键盘和鼠标相应
def handleEvent(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_j:
pass
else:
pass
def runScene(self):
# pygame.mixer.music.load('music/bgm.mp3')
# pygame.mixer.music.play(-1)
clock = pygame.time.Clock()
while True:
clock.tick(60)
self.draw()
self.update()
self.collideEvent()
self.handleEvent()
pygame.display.update()
# pygame.time.delay(100)
if __name__ == "__main__":
play = MainScene()
play.runScene()
else:
print("i was imported by", __name__)
最终效果如下:
定义不同的控制点,就可以做出形形色色的飞行轨迹了