質問編集履歴
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -41,4 +41,145 @@
|
|
41
41
|
バーがあるx軸全体が反転の対象になってしまいました。
|
42
42
|
|
43
43
|
こちらに現在のソースコードの写真をアップしました。
|
44
|
-
https://d.kuku.lu/60e92a2e4
|
44
|
+
https://d.kuku.lu/60e92a2e4
|
45
|
+
|
46
|
+
```Qt
|
47
|
+
|
48
|
+
#include "ofApp.h"
|
49
|
+
float fx; //長方形(枠)の中心のx座標
|
50
|
+
float fy; //長方形(枠)の中心のy座標
|
51
|
+
float fw; //長方形(枠)の幅
|
52
|
+
float fh; //長方形(枠)の高さ
|
53
|
+
|
54
|
+
float bx; //円の中心のx座標
|
55
|
+
float by; //円の中心のy座標
|
56
|
+
float radius; //円の半径
|
57
|
+
|
58
|
+
|
59
|
+
float rx; //パドルの中心のx座標
|
60
|
+
float ry; //パドルの中心のy座標
|
61
|
+
float rw; //パドルの幅
|
62
|
+
float rh; //パドルの高さ
|
63
|
+
|
64
|
+
float x_speed; //パドルのx移動速度
|
65
|
+
float y_speed; //パドルのy移動速度
|
66
|
+
|
67
|
+
float bx_speed; //円のx方向速度
|
68
|
+
float by_speed; //円のy方向速度
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
//--------------------------------------------------------------
|
73
|
+
void ofApp::setup(){
|
74
|
+
ofBackground(0, 0, 0); //背景を黒
|
75
|
+
ofSetCircleResolution(64); //円の解像度
|
76
|
+
|
77
|
+
//長方形の中心を描画位置にする
|
78
|
+
ofSetRectMode(OF_RECTMODE_CENTER);
|
79
|
+
ofSetFrameRate(30); //フレームレートの設定
|
80
|
+
|
81
|
+
fx = ofGetWidth()/2.0; //枠の描画位置 x座標
|
82
|
+
fy = ofGetHeight()/2.0; //枠の描画位置 y座標
|
83
|
+
fw = 900; //幅
|
84
|
+
fh = 570; //高さ
|
85
|
+
bx = 300; //円の描画位置 x座標
|
86
|
+
by = 300; //円の描画位置 y座標
|
87
|
+
bx_speed = 8; //円のx軸方向の移動速度
|
88
|
+
by_speed = 3; //円のy軸方向の移動速度
|
89
|
+
radius = 25; //円の半径
|
90
|
+
|
91
|
+
rx = ofGetWidth()/2; //パドルの初期位置 x座標
|
92
|
+
ry = ofGetHeight()/2; //パドルの初期位置 y座標
|
93
|
+
rw = 100; //パドルの幅
|
94
|
+
rh = 10; //パドルの高さ
|
95
|
+
|
96
|
+
x_speed = 50; //パドルのx移動速度
|
97
|
+
y_speed = 25; //パドルのy移動速度
|
98
|
+
|
99
|
+
|
100
|
+
}
|
101
|
+
|
102
|
+
//--------------------------------------------------------------
|
103
|
+
void ofApp::update(){
|
104
|
+
bx += bx_speed ; //x軸方向の移動
|
105
|
+
by += by_speed ; //y軸方向の移動
|
106
|
+
|
107
|
+
//円のx軸方向の範囲制限
|
108
|
+
if ((bx<fx-fw/2+radius)||
|
109
|
+
|
110
|
+
(bx>fx+fw/2-radius)){
|
111
|
+
|
112
|
+
bx_speed *= -1 ; //円のy軸方向の移動方向反転
|
113
|
+
}
|
114
|
+
//円のy軸方向の範囲制限
|
115
|
+
if ((by<fy-fh/2+radius)||
|
116
|
+
|
117
|
+
(by>fy+fh/2-radius)){
|
118
|
+
|
119
|
+
by_speed *= -1 ; //円のy軸方向の移動方向反転
|
120
|
+
}
|
121
|
+
|
122
|
+
//パドルのx軸方向の移動範囲制限
|
123
|
+
if(rx<fx-fw/2+rw/2){//パドル左端の制限
|
124
|
+
rx=fx-fw/2+rw/2;
|
125
|
+
}
|
126
|
+
if(rx>fx+fw/2-rw/2){//右端の制限
|
127
|
+
rx=fx+fw/2-rw/2;
|
128
|
+
}
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
//y軸方向の移動範囲制限
|
133
|
+
if(ry<fy-fh/2+rh/2){//パドル上の制限
|
134
|
+
ry=fy-fh/2+rh/2;
|
135
|
+
}
|
136
|
+
if(ry>fy+fh/2-rh/2){//パドル下の制限
|
137
|
+
ry=fy+fh/2-rh/2;
|
138
|
+
}
|
139
|
+
|
140
|
+
|
141
|
+
if(by+radius>ry){
|
142
|
+
by_speed*=-1;
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
//--------------------------------------------------------------
|
148
|
+
void ofApp::draw(){
|
149
|
+
ofNoFill();//塗りつぶしなし
|
150
|
+
ofSetLineWidth(4);//線の太さを4
|
151
|
+
ofSetColor(255,255,255);//白色に
|
152
|
+
ofDrawRectangle(fx,fy,fw,fh);//長方形を描く
|
153
|
+
|
154
|
+
ofFile();//塗りつぶしあり
|
155
|
+
ofSetColor(255,0,0);//赤色
|
156
|
+
ofDrawCircle(bx,by,radius);//円を描く
|
157
|
+
|
158
|
+
//パドルの描画
|
159
|
+
ofFile();
|
160
|
+
ofSetColor(255,255,255);//描画色 白
|
161
|
+
ofDrawRectangle(rx,ry,rw,rh);//パドルの描画
|
162
|
+
|
163
|
+
|
164
|
+
}
|
165
|
+
|
166
|
+
//--------------------------------------------------------------
|
167
|
+
void ofApp::keyPressed(int key){
|
168
|
+
|
169
|
+
//左矢印キー
|
170
|
+
if(key==OF_KEY_LEFT){
|
171
|
+
rx-=x_speed;
|
172
|
+
}
|
173
|
+
//右矢印キー
|
174
|
+
if(key==OF_KEY_RIGHT){
|
175
|
+
rx+=x_speed;
|
176
|
+
}
|
177
|
+
if(key==OF_KEY_UP){
|
178
|
+
ry-=y_speed;
|
179
|
+
}
|
180
|
+
if(key==OF_KEY_DOWN){
|
181
|
+
ry+=y_speed;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
```
|