質問編集履歴

2

コードの修正

2023/12/01 03:00

投稿

HERO
HERO

スコア1

test CHANGED
File without changes
test CHANGED
@@ -13,20 +13,131 @@
13
13
  ### 該当のソースコード
14
14
 
15
15
  ```Processing(java)
16
+ //dx,dyをvelocityに変更した影響でdx,dyがコメントアウトがしてあります
17
+ class Chara {
18
+ float[][] position = new float[4][2];
19
+ float[] dx = new float[4];
20
+ float[] dy = new float[4];
21
+ float[][] velocity=new float[4][2];
22
+ float[] start_x = new float[4];
23
+ float[] start_y = new float[4];
24
+ float[] speed = new float[4];
25
+ int[] attack = new int[4];
26
+ int[] HP = new int[4];
27
+ int allHP;
28
+ int currentHP;
29
+ int damage;
30
+ float r = 80;
31
+ boolean[] isDrag = new boolean[4];
32
+ boolean[] isStopped = new boolean[4];
33
+
34
+ int currentPhase;
35
+ int nowCP;
36
+
37
+ Chara() {
38
+ nowCP=0;
39
+ currentPhase = 0;
40
+ for (int i = 0; i < 4; i++) {
41
+ position[i][0] = (i + 1) * 128;
42
+ position[i][1] = 600;
43
+ // dx[i] = 0;
44
+ velocity[i][0]=0;
45
+ // dy[i] = 0;
46
+ velocity[i][1]=0;
47
+ start_x[i] = 0;
48
+ start_y[i] = 0;
49
+ speed[i] = 30;
50
+ attack[i]=30000;
51
+ HP[i]=25000;
52
+ allHP+=HP[i];
53
+ isDrag[i] = false;
54
+ isStopped[i] = false;
55
+ }
56
+ currentHP=allHP;
57
+ }
58
+
59
+ void update() {
60
+ switch (nowCP) {
61
+ case 0:
62
+ updateBall(0);
63
+ break;
64
+ case 1:
65
+ updateBall(1);
66
+ break;
67
+ case 2:
68
+ updateBall(2);
69
+ break;
70
+ case 3:
71
+ updateBall(3);
72
+ break;
73
+ }
74
+ }
75
+
76
+
16
- void updateBall(int index) {
77
+ void updateBall(int index) {
17
- dx[index] *= 0.99;
78
+ // dx[index] *= 0.99;
79
+ // dy[index] *= 0.99;
18
- dy[index] *= 0.99;
80
+ velocity[index][0]*=0.99;
19
-
81
+ velocity[index][1]*=0.99;
82
+
20
- if (abs(dx[index]) < 0.5 && abs(dy[index]) < 0.5) {
83
+ if (abs(velocity[index][0]) < 0.5 && abs(velocity[index][1]) < 0.5) {
21
- dx[index] = 0;
22
- dy[index] = 0;
84
+ velocity[index][0] = 0;
85
+ velocity[index][1] = 0;
23
86
  stop(index);
24
87
  }
25
- move(index, position[index][0], position[index][1], dx[index], dy[index]);
88
+ move(index, position[index][0], position[index][1], velocity[index][0], velocity[index][1]);
26
89
  bound(index, position[index][0], position[index][1]);
27
90
 
28
91
  }
92
+
93
+ void display() {
94
+ showChara();
95
+ showArrow();
96
+ showHP();
97
+ }
98
+
99
+
100
+
101
+ void mousePressed() {
102
+
103
+ if (scene_number == SCENE_GAME&&isStopped[nowCP]&&!m.phase) {
104
+ nowCP=currentPhase;
105
+ currentPhase++;
29
- ーーーーーーーーーーーーーーーーーーーーーーーーーーー
106
+ if (currentPhase >= 4) {
107
+ currentPhase = 0;
108
+ }
109
+ }
110
+ if(!m.phase){
111
+ start_x[nowCP] = mouseX;
112
+ start_y[nowCP] = mouseY;
113
+ isDrag[nowCP] = true;
114
+ }
115
+ }
116
+
117
+
118
+ void mouseReleased() {
119
+ if (scene_number == SCENE_GAME && isStopped[nowCP]&&!m.phase) {
120
+ resume(nowCP);
121
+ m.phase=true;
122
+ arrow(position[nowCP][0], position[nowCP][1], start_x[nowCP], start_y[nowCP]);
123
+ float l = mag(start_x[nowCP] - mouseX,start_y[nowCP] - mouseY);
124
+
125
+ //dx[nowCP] = (start_x[nowCP] - mouseX) * speed[nowCP]/l;
126
+ // dy[nowCP] = (start_y[nowCP] - mouseY) * speed[nowCP]/l;
127
+ velocity[nowCP][0] = (start_x[nowCP] - mouseX) * speed[nowCP]/l;
128
+ velocity[nowCP][1] = (start_y[nowCP] - mouseY) * speed[nowCP]/l;
129
+ isDrag[nowCP] = false;
130
+
131
+ if (l < r/2) {
132
+ // dx[nowCP] = 0;
133
+ // dy[nowCP] = 0;
134
+ velocity[nowCP][0]=0;
135
+ velocity[nowCP][1]=0;
136
+ currentPhase--;
137
+ m.phase=false;
138
+ }
139
+ }
140
+ }
30
141
  void showChara(){
31
142
  fill(0);
32
143
  for (int i = 0; i < 4; i++) {
@@ -35,44 +146,111 @@
35
146
  circle(position[i][0], position[i][1], r);
36
147
  fill(63*i);
37
148
  }
149
+ // text(nowCP,width/2,height/2,0);
38
- }
150
+ }
151
+
152
+ void showArrow(){
153
+ for (int i = 0; i < 4; i++) {
154
+ if (isDrag[i]) {
155
+ arrow(position[i][0], position[i][1], start_x[i], start_y[i]);
156
+ }
157
+ }
158
+ }
159
+ void arrow(float x, float y, float start_x, float start_y) {
160
+ PVector arrow = new PVector(x + start_x - mouseX, y + start_y - mouseY);
161
+ if (isStopped[nowCP]) {
162
+ stroke(0);
163
+ strokeWeight(10);
164
+ line(arrow.x, arrow.y, x, y);
165
+
166
+ }
167
+ }
168
+
169
+ void showHP(){
170
+ float HPbar;
171
+ HPbar=map(currentHP,0,allHP,0,width);
172
+ textSize(50);
173
+ strokeWeight(10);
174
+ stroke(255,255,0);
39
- ーーーーーーーーーーーーーーーーーーーーーーーーーーー
175
+ line(0,height,HPbar,height);
176
+ textAlign(RIGHT);
177
+ text(allHP,width,height);
178
+ textAlign(LEFT);
179
+ text("/",width-155,height);
180
+ textAlign(RIGHT);
181
+ text(currentHP,width-150,height);
182
+ }
183
+
184
+
185
+
40
- void bound(int index, float x, float y) {
186
+ void bound(int index, float x, float y) {
41
-
187
+ boolean collision[]=new boolean[6];
42
- if (x > width && dx[index] > 0) {
188
+ for(int i=0;i<m.M;i++){
189
+ if(m.HP[i]>=0){
43
- dx[index] *= -1;
190
+ collision[i]=false;
44
- }
191
+ }
45
- if (x < 0 && dx[index] < 0) {
46
- dx[index] *= -1;
47
- }
192
+ }
193
+
48
- if (y > height && dy[index] > 0) {
194
+ if (x > width && velocity[index][0] > 0) {
195
+ //dx[index] *= -1;
49
- dy[index] *= -1;
196
+ velocity[index][0]*=-1;
50
- }
197
+ }
198
+ if (x < 0 && velocity[index][0] < 0) {
199
+ // dx[index] *= -1;
200
+ velocity[index][0]*=-1;
201
+ }
202
+ if (y > height && velocity[index][1] > 0) {
203
+ // dy[index] *= -1;
204
+ velocity[index][1]*=-1;
205
+ }
51
- if (y < 0 && dy[index] < 0) {
206
+ if (y < 0 && velocity[index][1] < 0) {
52
- dy[index] *= -1;
207
+ //dy[index] *= -1;
208
+ velocity[index][1]*=-1;
53
209
  }
54
210
 
55
211
  for(int i=0;i<m.M;i++){
56
212
  if(m.HP[i]>=0){
213
+
57
214
  float d=distance(m.position[i][0],m.position[i][1],x,y);
58
215
  if(d<r/2+m.r/2){
216
+ collision[i]=true;
217
+ text("collision",width/2,100);
59
- if(abs(dx[index])>=abs(dy[index])){
218
+ if(abs(velocity[index][0])>=abs(velocity[index][1])&&collision[i]){
60
- dx[index] *= -1;
219
+ velocity[index][0] *= -1;
61
220
  m.HP[i]-=attack[nowCP];
62
221
  }
63
- if(abs(dx[index])<abs(dy[index])){
222
+ if(abs(velocity[index][0])<abs(velocity[index][1])&&collision[i]){
64
- dy[index]*=-1;
223
+ velocity[index][1]*=-1;
65
224
  m.HP[i]-=attack[nowCP];
66
225
  }
67
226
  }
68
227
  }
69
228
  }
70
229
  }
230
+
231
+
232
+ float distance(float x1, float y1, float x2, float y2) {
233
+ float dx = x2 - x1;
234
+ float dy = y2 - y1;
71
- ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
235
+ return sqrt(dx * dx + dy * dy);
236
+ }
237
+
72
- void move(int index, float x, float y, float dx, float dy) {
238
+ void move(int index, float x, float y, float dx, float dy) {
73
239
  position[index][0] = x + dx;
74
240
  position[index][1] = y + dy;
75
241
  }
242
+
243
+
244
+
245
+ void stop(int index) {
246
+ isStopped[index] = true;
247
+
248
+ }
249
+
250
+ void resume(int index) {
251
+ isStopped[index] = false;
252
+ }
253
+ }
76
254
  ```
77
255
 
78
256
  ### 試したこと

1

ソースコード追加、試したこと追加

2023/11/28 05:56

投稿

HERO
HERO

スコア1

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 実現したいこと
2
2
 
3
3
  円同士が衝突したときにモンストのような反射の挙動をしてほしい
4
- ~~打ち消し線~~
4
+
5
5
  ### 前提
6
6
 
7
7
  Processingでモンストのようなゲームを作っています。
@@ -13,19 +13,70 @@
13
13
  ### 該当のソースコード
14
14
 
15
15
  ```Processing(java)
16
+ void updateBall(int index) {
16
- //dは自分と敵との距離
17
+ dx[index] *= 0.99;
18
+ dy[index] *= 0.99;
19
+
20
+ if (abs(dx[index]) < 0.5 && abs(dy[index]) < 0.5) {
21
+ dx[index] = 0;
22
+ dy[index] = 0;
23
+ stop(index);
24
+ }
25
+ move(index, position[index][0], position[index][1], dx[index], dy[index]);
26
+ bound(index, position[index][0], position[index][1]);
27
+
28
+ }
29
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーー
30
+ void showChara(){
31
+ fill(0);
32
+ for (int i = 0; i < 4; i++) {
33
+ strokeWeight(1);
34
+ stroke(0);
35
+ circle(position[i][0], position[i][1], r);
36
+ fill(63*i);
37
+ }
38
+ }
39
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーー
40
+ void bound(int index, float x, float y) {
41
+
42
+ if (x > width && dx[index] > 0) {
43
+ dx[index] *= -1;
44
+ }
45
+ if (x < 0 && dx[index] < 0) {
46
+ dx[index] *= -1;
47
+ }
48
+ if (y > height && dy[index] > 0) {
49
+ dy[index] *= -1;
50
+ }
51
+ if (y < 0 && dy[index] < 0) {
52
+ dy[index] *= -1;
53
+ }
54
+
55
+ for(int i=0;i<m.M;i++){
56
+ if(m.HP[i]>=0){
17
- float d=distance(m.position[i][0],m.position[i][1],x,y);
57
+ float d=distance(m.position[i][0],m.position[i][1],x,y);
18
- if(d<r/2+m.r/2){//rは自分の円の直径,m.rは敵の円の直径
58
+ if(d<r/2+m.r/2){
19
59
  if(abs(dx[index])>=abs(dy[index])){
20
- dx[index] *= -1;//dxは速度のx成分
60
+ dx[index] *= -1;
61
+ m.HP[i]-=attack[nowCP];
21
62
  }
22
63
  if(abs(dx[index])<abs(dy[index])){
23
- dy[index]*=-1; //dyは速度のy成分
64
+ dy[index]*=-1;
65
+ m.HP[i]-=attack[nowCP];
24
66
  }
25
67
  }
68
+ }
69
+ }
70
+ }
71
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
72
+ void move(int index, float x, float y, float dx, float dy) {
73
+ position[index][0] = x + dx;
74
+ position[index][1] = y + dy;
75
+ }
26
76
  ```
27
77
 
28
-
78
+ ### 試したこと
79
+ bound関数の中身をずっといじっているのですが見当違いなのでしょうか?
29
80
 
30
81
  ### 補足情報(FW/ツールのバージョンなど)
31
82