回答編集履歴

5

さらにさらに更新

2022/09/30 09:51

投稿

TN8001
TN8001

スコア9242

test CHANGED
@@ -70,7 +70,8 @@
70
70
  int startMS; // ゲーム開始時間
71
71
  int countDown;
72
72
  int score;
73
- PImage photo;
73
+ PImage photo1;
74
+ PImage photo2;
74
75
 
75
76
  Scene scene = Scene.TITLE; // 現在の画面
76
77
  enum Scene { // 画面を表すenum
@@ -91,7 +92,8 @@
91
92
 
92
93
  // 画像読み込みは初回1回でよい(同じ画像を使いまわす)
93
94
  //photo = loadImage("2.png");
94
- photo = loadImage("https://www.gravatar.com/avatar/65adad388f02d47ab7fb7e48579efe41?d=identicon", "png");
95
+ photo1 = loadImage("https://www.gravatar.com/avatar/65adad388f02d47ab7fb7e48579efe41?d=identicon", "png");
96
+ photo2 = loadImage("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg");
95
97
  }
96
98
 
97
99
  void draw() {
@@ -179,26 +181,44 @@
179
181
 
180
182
  void mousePressed() {
181
183
  FBody body = world.getBody(mouseX, mouseY);
182
- if (body instanceof FCircle) {
184
+ if(body == null) return;
185
+
186
+ String name = body.getName();
187
+ if ("aaa".equals(name)) {
183
188
  world.remove(body);
184
189
  score += 500;
185
190
  }
191
+ if ("bbb".equals(name)) {
192
+ world.remove(body);
193
+ score += 300;
194
+ }
186
195
  }
187
196
 
188
197
  void add() {
198
+ FBody body;
199
+
200
+ if (1 < random(2)) {
189
- FCircle circle = new FCircle(135);
201
+ body = new FCircle(135); // (丸い当たり判定)
190
- circle.attachImage(photo);
202
+ body.attachImage(photo1);
203
+ body.setName("aaa"); // 区別できればなんでもいい
204
+ } else {
205
+ body = new FBox(95, 95); // (四角い当たり判定)
206
+ body.attachImage(photo2);
207
+ body.setName("bbb");
208
+ }
209
+
191
210
  float x = random(width - 100) + 50;
192
211
  float y = random(height - 100) + 50;
193
- circle.setPosition(x, y);
212
+ body.setPosition(x, y);
194
213
 
195
214
  PVector p = PVector.random2D().mult(10000);
196
- circle.addForce(p.x, p.y);
215
+ body.addForce(p.x, p.y);
197
- circle.setDamping(0);
216
+ body.setDamping(0);
198
- circle.setDensity(0.2);
217
+ body.setDensity(0.2);
199
- circle.setFriction(0);
218
+ body.setFriction(0);
200
- circle.setRestitution(1);
219
+ body.setRestitution(1);
201
-
220
+ body.setStroke(1);
221
+
202
- world.add(circle);
222
+ world.add(body);
203
223
  }
204
224
  ```

4

用語統一

2022/09/23 07:17

投稿

TN8001
TN8001

スコア9242

test CHANGED
@@ -72,8 +72,8 @@
72
72
  int score;
73
73
  PImage photo;
74
74
 
75
- Scene scene = Scene.TITLE; // 現在のシーン
75
+ Scene scene = Scene.TITLE; // 現在の画面
76
- enum Scene { // シーンを表すenum
76
+ enum Scene { // 画面を表すenum
77
77
  TITLE, PLAY, GAMEOVER,
78
78
  }
79
79
 
@@ -131,7 +131,7 @@
131
131
 
132
132
  countDown = timeLimit - ms;
133
133
  if (countDown <= 0) {
134
- scene = Scene.GAMEOVER;
134
+ scene = Scene.GAMEOVER; // 結果画面に遷移
135
135
  }
136
136
  if (countDown <= 10) {
137
137
  text("COUNT DOWN :" + countDown, 100, 100);
@@ -162,11 +162,11 @@
162
162
  void mouseClicked() {
163
163
  switch(scene) {
164
164
  case TITLE:
165
- scene = Scene.PLAY; // ゲームに遷移
165
+ scene = Scene.PLAY; // ゲーム画面に遷移
166
166
  startMS = millis(); // ゲーム開始時間記録
167
167
  break;
168
168
  case GAMEOVER:
169
- scene = Scene.TITLE; // タイトルに遷移
169
+ scene = Scene.TITLE; // タイトル画面に遷移
170
170
  countDown = 0; // 変数再初期化
171
171
  score = 0;
172
172
  // FCircleだけを取り除くのはそこそこ面倒なので、FWorldごと作り直し(雑いw

3

さらに更新

2022/09/23 07:07

投稿

TN8001
TN8001

スコア9242

test CHANGED
@@ -66,76 +66,131 @@
66
66
  import fisica.*;
67
67
 
68
68
  FWorld world;
69
- color bodyColor = #155AAD;
70
- color hoverColor = #55AA11;
71
-
72
69
  int timeLimit = 15;
70
+ int startMS; // ゲーム開始時間
73
71
  int countDown;
74
72
  int score;
73
+ PImage photo;
74
+
75
+ Scene scene = Scene.TITLE; // 現在のシーン
76
+ enum Scene { // シーンを表すenum
77
+ TITLE, PLAY, GAMEOVER,
78
+ }
75
79
 
76
80
 
77
81
  void setup() {
78
82
  fullScreen();
83
+ background(255, 255, 0);
84
+ fill(0);
85
+ textSize(90);
79
86
 
80
87
  Fisica.init(this);
81
88
  world = new FWorld();
82
89
  world.setEdges();
83
90
  world.setGravity(0, 0);
84
91
 
92
+ // 画像読み込みは初回1回でよい(同じ画像を使いまわす)
85
- textSize(90);
93
+ //photo = loadImage("2.png");
94
+ photo = loadImage("https://www.gravatar.com/avatar/65adad388f02d47ab7fb7e48579efe41?d=identicon", "png");
86
95
  }
87
96
 
88
97
  void draw() {
98
+ switch(scene) {
99
+ case TITLE:
100
+ title();
101
+ break;
102
+ case PLAY:
103
+ play();
104
+ break;
105
+ case GAMEOVER:
106
+ gameover();
107
+ break;
108
+ }
109
+ }
110
+
111
+ void title() {
112
+ background(255, 255, 0);
113
+
114
+ push(); // textAlignやtextSizeの変更がほかに影響しないように、push popで閉じ込める
115
+ textAlign(CENTER, CENTER); // センタリング
116
+ text("HOGE GAME", 0, 0, width, height); // 画面中央
117
+
118
+ if (frameCount % 60 < 30) { // 点滅
119
+ textSize(44);
120
+ text("Mouse Click to Play", 0, height / 2, width, height / 2); // 画面下半分中央
121
+ }
122
+ pop();
123
+ }
124
+
125
+ void play() {
89
126
  background(255);
90
127
 
91
- fill(0);
92
- text("SCORE:" + score, width - 400, 100);
128
+ text("SCORE:" + score, width - 600, 100);
93
-
129
+
94
- int ms = millis() / 1000; //制限時間
130
+ int ms = (millis() - startMS) / 1000; // 開始時間を引く
95
- println(ms);
96
131
 
97
132
  countDown = timeLimit - ms;
98
- if (countDown > 0) {
133
+ if (countDown <= 0) {
134
+ scene = Scene.GAMEOVER;
135
+ }
99
- if (countDown <= 10) {
136
+ if (countDown <= 10) {
100
- text("COUNT DOWN :" + countDown, 100, 100);
137
+ text("COUNT DOWN :" + countDown, 100, 100);
101
- }
102
- } else {
103
- background(255, 255, 0);
104
- text("TIME OVER", 300, 400);
105
- return;
106
138
  }
107
139
 
108
140
  if (frameCount % 60 == 1) {
109
141
  add();
110
142
  }
111
143
 
112
- for (Object body : world.getBodies()) {
113
- ((FBody)body).setFillColor(bodyColor);
114
- }
115
-
116
- FBody hovered = world.getBody(mouseX, mouseY);
117
- if (hovered != null) {
118
- hovered.setFillColor(hoverColor);
119
- }
120
-
121
144
  world.step();
122
145
  world.draw();
146
+ }
147
+
148
+ void gameover() {
149
+ background(255, 255, 0);
150
+
151
+ push();
152
+ textAlign(CENTER, CENTER);
153
+ text("TIME OVER\nYOUR SCORE " + score, 0, 0, width, height);
154
+
155
+ if (frameCount % 60 < 30) {
156
+ textSize(44);
157
+ text("Mouse Click to Title", 0, height / 2, width, height / 2);
158
+ }
159
+ pop();
160
+ }
161
+
162
+ void mouseClicked() {
163
+ switch(scene) {
164
+ case TITLE:
165
+ scene = Scene.PLAY; // ゲームに遷移
166
+ startMS = millis(); // ゲーム開始時間記録
167
+ break;
168
+ case GAMEOVER:
169
+ scene = Scene.TITLE; // タイトルに遷移
170
+ countDown = 0; // 変数再初期化
171
+ score = 0;
172
+ // FCircleだけを取り除くのはそこそこ面倒なので、FWorldごと作り直し(雑いw
173
+ world = new FWorld();
174
+ world.setEdges();
175
+ world.setGravity(0, 0);
176
+ break;
177
+ }
123
178
  }
124
179
 
125
180
  void mousePressed() {
126
181
  FBody body = world.getBody(mouseX, mouseY);
127
182
  if (body instanceof FCircle) {
128
183
  world.remove(body);
129
- score++;
184
+ score += 500;
130
185
  }
131
186
  }
132
187
 
133
188
  void add() {
134
- FCircle circle = new FCircle(50);
189
+ FCircle circle = new FCircle(135);
190
+ circle.attachImage(photo);
135
191
  float x = random(width - 100) + 50;
136
192
  float y = random(height - 100) + 50;
137
193
  circle.setPosition(x, y);
138
- circle.setFillColor(bodyColor);
139
194
 
140
195
  PVector p = PVector.random2D().mult(10000);
141
196
  circle.addForce(p.x, p.y);

2

コード更新

2022/09/18 14:25

投稿

TN8001
TN8001

スコア9242

test CHANGED
@@ -69,17 +69,43 @@
69
69
  color bodyColor = #155AAD;
70
70
  color hoverColor = #55AA11;
71
71
 
72
+ int timeLimit = 15;
73
+ int countDown;
74
+ int score;
75
+
76
+
72
77
  void setup() {
73
- size(400, 400);
78
+ fullScreen();
74
79
 
75
80
  Fisica.init(this);
76
81
  world = new FWorld();
82
+ world.setEdges();
83
+ world.setGravity(0, 0);
84
+
85
+ textSize(90);
77
86
  }
78
87
 
79
88
  void draw() {
80
89
  background(255);
81
90
 
91
+ fill(0);
92
+ text("SCORE:" + score, width - 400, 100);
93
+
94
+ int ms = millis() / 1000; //制限時間
95
+ println(ms);
96
+
97
+ countDown = timeLimit - ms;
98
+ if (countDown > 0) {
99
+ if (countDown <= 10) {
100
+ text("COUNT DOWN :" + countDown, 100, 100);
101
+ }
102
+ } else {
103
+ background(255, 255, 0);
104
+ text("TIME OVER", 300, 400);
105
+ return;
106
+ }
107
+
82
- if (frameCount % 150 == 1) {
108
+ if (frameCount % 60 == 1) {
83
109
  add();
84
110
  }
85
111
 
@@ -100,6 +126,7 @@
100
126
  FBody body = world.getBody(mouseX, mouseY);
101
127
  if (body instanceof FCircle) {
102
128
  world.remove(body);
129
+ score++;
103
130
  }
104
131
  }
105
132
 
@@ -109,19 +136,13 @@
109
136
  float y = random(height - 100) + 50;
110
137
  circle.setPosition(x, y);
111
138
  circle.setFillColor(bodyColor);
112
- circle.setStatic(true);
113
139
 
114
-
115
- // 以下をコメント解除すると丸が動き回るハードモードだぞ!
116
- //PVector p = PVector.random2D().mult(10000);
140
+ PVector p = PVector.random2D().mult(10000);
117
- //circle.addForce(p.x, p.y);
141
+ circle.addForce(p.x, p.y);
118
- //circle.setDamping(0);
142
+ circle.setDamping(0);
119
- //circle.setDensity(0.2);
143
+ circle.setDensity(0.2);
120
- //circle.setFriction(0);
144
+ circle.setFriction(0);
121
- //circle.setRestitution(1);
145
+ circle.setRestitution(1);
122
- //circle.setStatic(false);
123
- //world.setEdges();
124
- //world.setGravity(0, 0);
125
146
 
126
147
  world.add(circle);
127
148
  }

1

さんのさんの

2022/09/18 10:11

投稿

TN8001
TN8001

スコア9242

test CHANGED
@@ -55,7 +55,7 @@
55
55
  > プログラミング初心者なので、参考のためにも「前提」に書いてあるようなゲームを作るためのコードを作っていただけたらめちゃめちゃありがたいです!!
56
56
 
57
57
  代わりに作ってしまっては、それは「プログラミング」と言えるのでしょうか?
58
- claus__さんのさんの理解度もわかりませんし、かといって0から説明するわけにもいきません。
58
+ claus__さんの理解度もわかりませんし、かといって0から説明するわけにもいきません。
59
59
 
60
60
  求めている回答ではないでしょうが、比較的簡単にできそうなライブラリを使用した例を出しておきます。
61
61