ソフトロックを無理にした

諏訪子 <suwako@076.moe>、2025-11-26 01:40
コミット c154d6cca660bab6ec4e5dd0aedd87de02884fa2 ・親 755b579 ・ファイルを見る

7個のファイル、+102 -11

追加 .gitignore
@@ -0,0 +1,1 @@
1+brickbreaker
変更 main.cc
@@ -60,7 +60,7 @@
6060 if (!r.gameover) {
6161 r.update(r, p, bl, br);
6262 }
63- r.input(p);
63+ r.input(r, p, bl, br);
6464 r.render(p, bl, br);
6565 }
6666
変更 src/ball.hh
@@ -13,6 +13,8 @@
1313
1414 float xvelocity;
1515 float yvelocity;
16+ float xvelSave;
17+ float yvelSave;
1618
1719 SDL_Rect ball;
1820
変更 src/brick.cc
@@ -29,7 +29,6 @@
2929 r.c6g = rand() % 256;
3030 r.c6b = rand() % 256;
3131
32-
3332 if (r.level > 1) {
3433 Mix_HaltMusic();
3534 }
@@ -51,13 +50,42 @@
5150 Brick::bricks[i] = 1;
5251 }
5352
54- bl.size -= 2;
55- bl.ballspeed += 1.4f;
56- p.player.w -= 1.4f;
53+ int pw = 620 / 4;
54+
55+ if (r.level == 1) {
56+ bl.size = 16;
57+ bl.ballspeed = 8.f;
58+ p.player.w = pw;
59+ } else if (r.level == 2) {
60+ bl.size = 14;
61+ bl.ballspeed = 9.4f;
62+ p.player.w = pw - 1.4;
63+ } else if (r.level == 3) {
64+ bl.size = 12;
65+ bl.ballspeed = 10.8f;
66+ p.player.w = pw - (1.4 * 2);
67+ } else if (r.level == 4) {
68+ bl.size = 10;
69+ bl.ballspeed = 12.2f;
70+ p.player.w = pw - (1.4 * 3);
71+ } else if (r.level == 5) {
72+ bl.size = 8;
73+ bl.ballspeed = 13.6f;
74+ p.player.w = pw - (1.4 * 4);
75+ } else if (r.level == 6) {
76+ bl.size = 6;
77+ bl.ballspeed = 15.f;
78+ p.player.w = pw - (1.4 * 5);
79+ } else if (r.level == 7) {
80+ bl.size = 4;
81+ bl.ballspeed = 16.4f;
82+ p.player.w = pw - (1.4 * 6);
83+ }
84+
5785 p.player.x = (r.width / 2) - (p.player.w / 2);
58- bl.ball.y = p.player.y - (p.player.h * 4);
5986 bl.yvelocity = bl.ballspeed / 2;
6087 bl.xvelocity = 0;
88+ bl.ball.y = p.player.y - (p.player.h * 4);
6189 bl.ball.x = r.width / 2 - (bl.size / 2);
6290 }
6391
変更 src/player.hh
@@ -14,7 +14,7 @@
1414 int scoreMultiplier = 1;
1515 int livesCount = 3;
1616
17- Player(Render& r) {
17+ Player(Render &r) {
1818 player.h = 12;
1919 player.y = r.height - player.h - 32;
2020 player.w = r.width / 4;
変更 src/render.cc
@@ -3,7 +3,7 @@
33 #include "player.hh"
44 #include "brick.hh"
55
6-#define PI 3.14
6+#define DEBUG false
77
88 void Render::gameOver(Player &p, Brick &br, bool winner) {
99 if (winner) {
@@ -41,10 +41,16 @@
4141 if (SDL_HasIntersection(&bl.ball, &p.player)) {
4242 double rel = (p.player.x + ((float)p.player.w/2)) - ((float)bl.ball.x + ((float)bl.size/2));
4343 double norm = rel / ((float)p.player.w/2);
44- double bounce = norm * (5*PI/12);
44+ double bounce = norm * (5 * M_PI / 12);
45+ if (fabs(bounce - bounceSave) <= .04) bounce += .4;
4546
4647 bl.yvelocity = -bl.ballspeed * cos(bounce);
4748 bl.xvelocity = bl.ballspeed * -sin(bounce);
49+
50+ if (bl.yvelocity == bl.yvelSave || -bl.yvelocity == bl.yvelSave) bl.xvelocity += .4f;
51+ bl.yvelSave = bl.yvelocity;
52+
53+ bounceSave = bounce;
4854 p.scoreMultiplier = 1;
4955 }
5056
@@ -58,9 +64,12 @@
5864 }
5965 if (bl.ball.x <= 0 || bl.ball.x + bl.size >= Render::width) {
6066 bl.xvelocity = -bl.xvelocity;
67+ if (bl.xvelocity == bl.xvelSave || -bl.xvelocity == bl.xvelSave) bl.yvelocity += .4f;
68+ bl.xvelSave = bl.xvelocity;
6169 }
6270 bl.ball.x += bl.xvelocity;
6371 bl.ball.y += bl.yvelocity;
72+
6473 if (p.player.x < 0) {
6574 p.player.x = 0;
6675 }
@@ -112,7 +121,7 @@
112121 }
113122 }
114123
115-void Render::input(Player &p) {
124+void Render::input(Render &r, Player &p, Ball &bl, Brick &br) {
116125 SDL_Event e;
117126 const Uint8 *keystates = SDL_GetKeyboardState(NULL);
118127
@@ -123,6 +132,56 @@
123132 }
124133
125134 if (keystates[SDL_SCANCODE_Q]) Render::running = false;
135+#if DEBUG
136+ if (keystates[SDL_SCANCODE_E]) p.livesCount = 3;
137+ if (keystates[SDL_SCANCODE_R]) {
138+ if (level <= 7) {
139+ br.resetBricks(r, p, bl);
140+ bl.resetBall(r, p);
141+ gameover = false;
142+ }
143+ }
144+ if (keystates[SDL_SCANCODE_O]) {
145+ bl.ball.x = 250.f;
146+ bl.ball.y = 400.f;
147+ bl.xvelocity = 0.f;
148+ bl.yvelocity = 8.f;
149+ }
150+ if (keystates[SDL_SCANCODE_P]) {
151+ bl.ball.x = 250.f;
152+ bl.ball.y = 400.f;
153+ bl.xvelocity = 8.f;
154+ bl.yvelocity = 0.f;
155+ }
156+ if (keystates[SDL_SCANCODE_1]) {
157+ Render::level = 1;
158+ br.resetBricks(r, p, bl);
159+ }
160+ if (keystates[SDL_SCANCODE_2]) {
161+ Render::level = 2;
162+ br.resetBricks(r, p, bl);
163+ }
164+ if (keystates[SDL_SCANCODE_3]) {
165+ Render::level = 3;
166+ br.resetBricks(r, p, bl);
167+ }
168+ if (keystates[SDL_SCANCODE_4]) {
169+ Render::level = 4;
170+ br.resetBricks(r, p, bl);
171+ }
172+ if (keystates[SDL_SCANCODE_5]) {
173+ Render::level = 5;
174+ br.resetBricks(r, p, bl);
175+ }
176+ if (keystates[SDL_SCANCODE_6]) {
177+ Render::level = 6;
178+ br.resetBricks(r, p, bl);
179+ }
180+ if (keystates[SDL_SCANCODE_7]) {
181+ Render::level = 7;
182+ br.resetBricks(r, p, bl);
183+ }
184+#endif
126185 if (!Render::gameover) {
127186 if (keystates[SDL_SCANCODE_LEFT]) p.player.x -= Render::xpos;
128187 if (keystates[SDL_SCANCODE_RIGHT]) p.player.x += Render::xpos;
変更 src/render.hh
@@ -35,7 +35,7 @@
3535 Mix_Music* music;
3636
3737 void update(Render &r, Player &p, Ball &bl, Brick &br);
38- void input(Player &p);
38+ void input(Render &r, Player &p, Ball &bl, Brick &br);
3939 void render(Player &p, Ball &bl, Brick &br);
4040
4141 Render() {
@@ -47,6 +47,7 @@
4747 private:
4848 void gameOver(Player &p, Brick &br, bool winner);
4949 void write(Player &p, Brick &br, std::string text, int x, int y);
50+ double bounceSave;
5051 };
5152
5253 #endif