FXGL学习2
最编程
2024-07-23 21:38:50
...
public class MainPlay extends GameApplication {
protected Entity shooter;
protected Entity normalizedZomba;
protected Entity bullet;
int z_hp=12;//僵尸血条
int s_hp=10;//植物血条
@Override
protected void initSettings(GameSettings gameSettings) {
gameSettings.setWidth(680);
gameSettings.setHeight(480);
gameSettings.setVersion("1.0v");
gameSettings.setTitle("植物练习射击僵尸");
}
@Override
protected void initGame() {
getGameWorld().addEntityFactory(new PlantFactory());
shooter=spawn("shooter");
normalizedZomba=spawn("zomba");
normalizedZomba.setPosition(600,100);
generate();
}
private void generate(){
bullet=spawn("pea");
bullet.setPosition(shooter.getX()+10, shooter.getY());
}
@Override
protected void initInput() {
Input input=getInput();
input.addEventHandler(MouseEvent.MOUSE_CLICKED,event -> {
shooter.setPosition(event.getX()-40, event.getY()-20);
});
}
@Override
protected void initPhysics() {
getPhysicsWorld().addCollisionHandler(new CollisionHandler(PlantEntity.PLANT,PlantEntity.ZOMBA) {
@Override
protected void onCollisionBegin(Entity plant, Entity zomba) {
s_hp--;
if (s_hp<=0){
shooter.removeFromWorld();
}
}
});
getPhysicsWorld().addCollisionHandler(new CollisionHandler(PlantEntity.BULLET,PlantEntity.ZOMBA) {
@Override
protected void onCollision(Entity bullet, Entity zomba) {
z_hp--;
if (z_hp<=0){
zomba.removeFromWorld();
}
bullet.removeFromWorld();//开始碰撞后移除bullet
}
});
}
@Override
protected void onUpdate(double tpf) {
normalizedZomba.translate(new Vec2(-tpf*10,0));//僵尸移动
bullet.translate(new Vec2(6,0));//子弹发射
if (bullet.getX()>getAppWidth()){
bullet.removeFromWorld();
generate();
}
}
public static void main(String[] args) {
launch(args);
}
}
上一篇: 用JavaFX和FxGL引擎打造的酷炫飞行射击游戏
下一篇: 如何在FXGL中设置窗口透明度步骤指南
推荐阅读
-
计算机 毕业设计 Python 深度学习 房价预测 房源可视化 房源爬虫 二手房可视化 二手房爬虫 递归决策树模型 机器学习 深度学习 大数据 毕业设计
-
STM32 I2C 通信协议详解
-
[原版] IDA Pro 9.0.240925 v2 官方原版 官方原版 全平台(Win Mac Linux)全功能完美运行
-
Idea-Maven 的设置文件 Disk Configuration-2.Setting.xml
-
[机器学习和神经网络荣获诺贝尔奖] 科学边界的扩展与技术创新 - IV.个人对机器学习和神经网络的看法
-
SSM 框架学习(七、MyBatis-Plus 高级用法:优化持久层开发)
-
多媒体(2)
-
【论文阅读】分离与联合:用于少量学习的简单元传输-摘要
-
JAVA 就业说明 8 - 第 2 阶段 (5)
-
机器学习捷径:自动特征工程综合分析(附代码示例)