抛物线的精灵

本文介绍如何利用Cocos2d-x引擎实现一个精灵沿抛物线路径移动,并在移动过程中进行旋转的效果。通过创建贝塞尔曲线和动作组合,实现动态的视觉体验。

//  抛物线

//mSprite:需要做抛物线的精灵

//startPoint:起始位置

//endPoint:中止位置

//dirTime:起始位置到中止位置的所需时间

- (void) moveWithParabola:(CCSprite*)mSprite startP:(CGPoint)startPoint endP:(CGPoint)endPoint dirTime:(float)time{

    float sx = startPoint.x;

    float sy = startPoint.y;

    float ex =endPoint.x+50;

    float ey =endPoint.y+150;

    int h = [mSprite contentSize].height*0.5;

    ccBezierConfig bezier; // 创建贝塞尔曲线

    bezier.controlPoint_1 = ccp(sx, sy); // 起始点

    bezier.controlPoint_2 = ccp(sx+(ex-sx)*0.5, sy+(ey-sy)*0.5+200); //控制点

    bezier.endPosition = ccp(endPoint.x-30, endPoint.y+h); // 结束位置

    CCBezierTo *actionMove = [CCBezierToactionWithDuration:time bezier:bezier];

    [mSprite runAction:actionMove];

}

 

 

//  抛物线运动并同时旋转

//mSprite:需要做抛物线的精灵

//startPoint:起始位置

//endPoint:中止位置

//startA:起始角度

//endA:中止角度

//dirTime:起始位置到中止位置的所需时间

- (void) moveWithParabola:(CCSprite*)mSprite startP:(CGPoint)startPoint endP:(CGPoint)endPoint startA:(float)startAngle endA:(float)endAngle dirTime:(float)time{

    float sx = startPoint.x;

    float sy = startPoint.y;

    float ex =endPoint.x+50;

    float ey =endPoint.y+150;

    int h = [mSprite contentSize].height*0.5;

    //设置精灵的起始角度

    mSprite.rotation=startAngle;

    ccBezierConfig bezier; // 创建贝塞尔曲线

    bezier.controlPoint_1 = ccp(sx, sy); // 起始点

    bezier.controlPoint_2 = ccp(sx+(ex-sx)*0.5, sy+(ey-sy)*0.5+200); //控制点

    bezier.endPosition = ccp(endPoint.x-30, endPoint.y+h); // 结束位置

    CCBezierTo *actionMove = [CCBezierToactionWithDuration:time bezier:bezier];

    //创建精灵旋转的动作

    CCRotateTo *actionRotate =[CCRotateToactionWithDuration:time angle:endAngle];

    //将两个动作封装成一个同时播放进行的动作

    CCAction * action = [CCSpawn actions:actionMove, actionRotate, nil];

    [mSprite runAction:action];

 

}

好的,下面是制作炮弹类并实现抛物线运动的示例代码: ```python import pygame import math pygame.init() width, height = 640, 480 screen = pygame.display.set_mode((width, height)) class Bullet(pygame.sprite.Sprite): def __init__(self, pos, target): super().__init__() self.image = pygame.image.load('bullet.png').convert_alpha() self.rect = self.image.get_rect(center=pos) self.start_pos = pos self.target_pos = target self.speed = 10 self.gravity = 0.5 self.angle = math.atan2(target[1]-pos[1], target[0]-pos[0]) self.vx = self.speed * math.cos(self.angle) self.vy = self.speed * math.sin(self.angle) self.time = 0 def update(self): self.time += 1 self.rect.x = self.start_pos[0] + self.vx * self.time self.rect.y = self.start_pos[1] + (self.vy * self.time + 0.5 * self.gravity * self.time ** 2) if self.rect.left > width or self.rect.top > height: self.kill() class Enemy(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((50, 50)) self.image.fill((255, 0, 0)) self.rect = self.image.get_rect(center=(width-50, height-50)) all_sprites = pygame.sprite.Group() enemy = Enemy() all_sprites.add(enemy) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.fill((255, 255, 255)) all_sprites.update() all_sprites.draw(screen) if pygame.mouse.get_pressed()[0]: bullet = Bullet((0, height//2), pygame.mouse.get_pos()) all_sprites.add(bullet) pygame.display.update() clock.tick(60) ``` 在这个示例中,我们先定义了一个 `Bullet` 类和一个 `Enemy` 类。`Bullet` 类继承自 `pygame.sprite.Sprite` 类,并在其构造函数中加载了炮弹图片,并计算了炮弹的初速度和运动角度,以及添加了重力加速度,用于实现抛物线运动。在 `update` 方法中根据时间更新炮弹位置,如果炮弹超出屏幕边界,则从精灵组中删除。`Enemy` 类则仅继承自 `pygame.sprite.Sprite` 类,并在其构造函数中创建了一个红色的矩形作为敌人。 在主循环中,我们创建了一个 `all_sprites` 精灵组,并将敌人添加到其中。每帧都更新精灵组中的所有精灵,并将它们绘制到屏幕上。当玩家按下鼠标左键时,创建一个新的炮弹,并将其添加到精灵组中。 注意,这个示例中的炮弹运动轨迹是一条简单的抛物线,如果需要更精确的抛物线运动,可以考虑使用更高级的物理引擎,如 Box2D 等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值