上部にボス的なものを配置する予定
こういう場合(文章中に主語が出てきた場合)は専用のクラスを作成するとコードの管理が楽になります。
今回の場合はBoss
クラスになるかと。
Boss
クラスは任意のタイミングで、弾幕を貼る。そのようなコードになります。
発射した弾の生存管理方法はいろいろな手法がありますが、
描画範囲外になったら生存フラグをfalse
にして、描画時にリストから削除する形が楽かと。
あと変数の型をdouble
型ではなくfloat
型で宣言するのは、
宗教上の理由などがある場合を除き、float
型の精度に起因する余計なトラブルを持ち込みむため、避けたほうが無難です。
この精度についての問題について、日本語で体系的に纏まっている情報としては2008年に出版された本ですが、「ゲームプログラマになる前に覚えておきたい技術」が纏まっています。
「ゲームプログラマになる前に覚えておきたい技術 float 中途半端な数による誤差」でキーワード検索してみてくださいな。
サンプルコードです。ご参考まで。
Java
1Bullet bullet;
2private Boss boss = new Boss(100, 150);
3private ArrayList<Bullet> danmaku = new ArrayList<Bullet>();
4
5void setup() {
6 size(320, 480);
7
8 frameRate(30);
9
10 noCursor(); // clear mouse cursor
11
12 bullet = new Bullet(width/2, height/2, 10, 0, 0 ); //クラスからインスタンスを生成
13
14}
15// my ship function
16
17void ship(int x, int y) {
18
19 stroke(255, 255, 255);
20
21 noFill();
22
23 triangle(x, y -7, x - 10, y + 7, x + 10, y + 7);
24
25 if (mousePressed) {
26 line(x, y - 7, x, 0);
27 }
28}
29class Boss {
30 private int tx, ty;
31 private int dx, dy;
32 private long routine = 0;
33 Boss(int x, int y) {
34 tx = x;
35 ty = y;
36 dx = 1;
37 dy = -5;
38 }
39 void move(){
40 tx += dx;
41 ty += dy;
42 if(tx < 0 || tx > width){
43 dx*= -1;
44 }
45 if(ty < 0 || ty > height){
46 dy*= -1;
47 }
48 }
49 void doShinking(){
50 routine++;
51 if(routine % 10 == 0){
52 move();
53 }
54 if(routine % 30 == 0){
55 danmaku.addAll(this.shot());
56 println("#shot!!" +routine + ":" + tx + "," + ty);
57 }
58 }
59 ArrayList<Bullet> shot() {
60 ArrayList<Bullet> danmaku = new ArrayList();
61 for (int i = 0; i < 360; i+= 10) {
62 double rad = radians(i);
63 danmaku.add(new Bullet(this.tx, this.ty, 10, Math.cos(rad), Math.sin(rad)));
64 }
65 return danmaku;
66 }
67}
68// Bullet class
69
70class Bullet {
71
72 private double tx, ty;
73 private final double tr;
74 private double dx, dy;
75 private boolean is_alive = true;
76 Bullet(double x, double y, double r, double temp_dx, double temp_dy ) {
77 tx = x;
78 ty = y;
79 tr = r;
80 dx = temp_dx;
81 dy = temp_dy;
82 }
83 boolean isAlive(){
84 return is_alive;
85 }
86 void update() {
87 tx += dx;
88 ty += dy;
89 if(Math.min(tx, ty) < 0){
90 is_alive = false;
91 //println("#" + tx + "," + ty);
92 return ;
93 }
94 if(tx > width || ty > height){
95 is_alive = false;
96 return ;
97 }
98 stroke(255, 0, 0);
99
100 ellipse((float)tx, (float)ty, (float)tr, (float)tr);
101 }
102}
103 void draw() {
104 background(0); // clear
105
106 ship(mouseX, mouseY);
107 boss.doShinking();
108
109 for (int i = danmaku.size() -1; i >= 0; i--) {
110 Bullet b = (Bullet)danmaku.get(i);
111 if(!b.isAlive()){
112 danmaku.remove(i);
113 continue;
114 }
115 b.update();
116 }
117 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/04 05:51
2018/05/04 07:32 編集
2018/05/04 07:20 編集
2018/05/04 07:36
2018/05/04 07:52