master
ブランチ master
Pong / main.cc
7.1 KB、268行 生 Blame 履歴
1#define SDL_MAIN_HANDLED
2#include <SDL2/SDL.h>
3#include <SDL2/SDL_ttf.h>
4#include <iostream>
5#include <string>
6
7#include "Peepo.h"
8
9#define WIDTH 720
10#define HEIGHT 720
11#define FONTSIZE 32
12#define BALLSIZE 16
13#define BALLSPEED 16
14#define SPEED 9
15#define PI 3.1415926535897323846
16
17SDL_Renderer *renderer;
18SDL_Window *window;
19TTF_Font *font;
20SDL_Color color;
21bool running;
22int frameCount, timerFPS, lastFrame, fps;
23
24SDL_Rect player, npc, ball, scoreboard, leveldisplay;
25float xvelocity, yvelocity, ballvelocity, ballsize;
26std::string score, slevel;
27int lscore, rscore, level, games;
28bool turn, isgameover;
29std::string version = "1.0.1";
30
31void serve() {
32 player.y = npc.y = (HEIGHT/2) - (player.h/2);
33 if (turn) {
34 ball.x = player.x + (player.w * 4);
35 xvelocity = (BALLSPEED*ballvelocity)/2;
36 turn = false;
37 } else {
38 ball.x = npc.x - (player.w * 4);
39 xvelocity = -(BALLSPEED*ballvelocity)/2;
40 turn = true;
41 }
42
43 yvelocity = 0;
44 ball.y = HEIGHT / 2 - ((BALLSIZE-static_cast<int>(ballsize))/2);
45}
46
47void write(std::string text, int x, int y) {
48 SDL_Surface *surface;
49 SDL_Texture *texture;
50 const char* t = text.c_str();
51
52 surface = TTF_RenderText_Solid(font, t, color);
53 texture = SDL_CreateTextureFromSurface(renderer, surface);
54
55 // スコアボード
56 scoreboard.w = surface->w;
57 scoreboard.h = surface->h;
58 scoreboard.x = x - scoreboard.w;
59 scoreboard.y = y - scoreboard.h;
60
61 // レベル表示
62 leveldisplay.w = surface->w;
63 leveldisplay.h = surface->h;
64 leveldisplay.x = x - scoreboard.w + 8;
65 leveldisplay.y = y - scoreboard.h + 8;
66
67 // 掃除
68 SDL_FreeSurface(surface);
69 SDL_RenderCopy(renderer, texture, NULL, &scoreboard);
70 SDL_DestroyTexture(texture);
71}
72
73void gameover() {
74 xvelocity = yvelocity = ballvelocity = 0;
75}
76
77void update() {
78 // ボールとコンピュータの衝突検出
79 if (SDL_HasIntersection(&ball, &npc)) {
80 double rel = (npc.y + (npc.h / 2)) - (ball.y + ((BALLSIZE-ballsize) / 2));
81 double norm = rel / (npc.h / 2);
82 double bounce = norm * (5 * PI / 12);
83 xvelocity = -(BALLSPEED*ballvelocity) * cos(static_cast<float>(bounce));
84 yvelocity = (BALLSPEED*ballvelocity) * -sin(static_cast<float>(bounce));
85 }
86
87 // ボールとプレイヤーの衝突検出
88 if (SDL_HasIntersection(&ball, &player)) {
89 double rel = (player.y + (player.h / 2)) - (ball.y + ((BALLSIZE-ballsize) / 2));
90 double norm = rel / (player.h / 2);
91 double bounce = norm * (5 * PI / 12);
92 xvelocity = (BALLSPEED*ballvelocity) * cos(static_cast<float>(bounce));
93 yvelocity = (BALLSPEED*ballvelocity) * -sin(static_cast<float>(bounce));
94 }
95
96 // コンピュータがボールの方向を付いてくる
97 if (ball.y > npc.y + (npc.h/2)) npc.y += ((SPEED+level*0.5f)*static_cast<int>(ballvelocity));
98 if (ball.y < npc.y + (npc.h/2)) npc.y -= ((SPEED+level*0.5f)*static_cast<int>(ballvelocity));
99
100 // ボールが左右に逃げたら、スコアアップにして、最初フレームから再開
101 if (ball.x <= 0) {
102 rscore++;
103 serve();
104 } else if (ball.x + (BALLSIZE-ballsize) >= WIDTH) {
105 lscore++;
106 serve();
107 games++;
108 }
109
110 // コンピュータさんを十分倒したら、レベルアップにする
111 if (games == ((level + 1) * 2)) {
112 level++;
113 ballvelocity += 0.25f;
114 ballsize -= 0.2f;
115 games = 0;
116 }
117
118 // コンピュータのスコアが10になったら、ゲームオーバーだ
119 if (rscore > 9) {
120 isgameover = true;
121 }
122
123 // ボールが上下に逃げる事に対して守る
124 if (ball.y <= 0 || ball.y + (BALLSIZE-ballsize) >= HEIGHT) yvelocity = -yvelocity;
125
126 // ボールの移動
127 ball.x += static_cast<int>(xvelocity);
128 ball.y += static_cast<int>(yvelocity);
129
130 // スコアやレベルの文字
131 score = std::to_string(lscore) + " " + std::to_string(rscore);
132 slevel = "Lv" + std::to_string(level);
133
134 // プレイヤーを上下に逃げられない為
135 if (player.y < 0) player.y = 0;
136 if (player.y + player.h > HEIGHT) player.y = HEIGHT-player.h;
137
138 // コンピューターを上下に逃げられない為
139 if (npc.y < 0) npc.y = 0;
140 if (npc.y + npc.h > HEIGHT) npc.y = HEIGHT-npc.h;
141}
142
143void input() {
144 SDL_Event e;
145 const Uint8 *keystates = SDL_GetKeyboardState(NULL);
146
147 while (SDL_PollEvent(&e)) {
148 if (e.type == SDL_QUIT) {
149 running = false;
150 }
151 }
152
153 if (keystates[SDL_SCANCODE_Q]) running = false; // Qでゲームを終了
154 if (!isgameover) {
155 if (keystates[SDL_SCANCODE_UP]) player.y -= (SPEED*static_cast<int>(ballvelocity)); // ↑でプレイヤーを上がる
156 if (keystates[SDL_SCANCODE_DOWN]) player.y += (SPEED*static_cast<int>(ballvelocity)); // ↓でプレイヤーを下げる
157 }
158}
159
160void render() {
161 // 黒色背景
162 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 255);
163 SDL_RenderClear(renderer);
164
165 frameCount++;
166 timerFPS = SDL_GetTicks() - lastFrame;
167 if (timerFPS < (1000/60)) {
168 SDL_Delay((1000/60) - timerFPS);
169 }
170
171 // 背景以外もレンダーして
172 SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
173
174 // プレイヤー、コンピュータ、とボールを創作
175 if (!isgameover) {
176 SDL_RenderFillRect(renderer, &player);
177 SDL_RenderFillRect(renderer, &npc);
178 SDL_RenderFillRect(renderer, &ball);
179 }
180
181 write(score, WIDTH/2+FONTSIZE, FONTSIZE*2);
182 write(slevel, WIDTH/2+FONTSIZE, FONTSIZE*3);
183 if (isgameover) {
184 write("GAME OVER", WIDTH/2+84, HEIGHT/2);
185 }
186 write("076.moe SUWAKO NO PONG v" + version, WIDTH-150, HEIGHT-20);
187 SDL_RenderPresent(renderer);
188}
189
190#if defined(_WIN32) || defined(_WIN64)
191int WinMain(void) {
192#else
193int main(void) {
194#endif
195 // SDLを初期化
196 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
197 std::cout << "SDL_Init()に失敗" << std::endl;
198 return -1;
199 }
200
201 // ウィンドウとレンダーを創作
202 if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) < 0) {
203 std::cout << "SDL_CreateWindowAndRenderer()に失敗" << std::endl;
204 SDL_Quit();
205 return -1;
206 }
207
208 std::string tit = "Pong " + std::string(version);
209
210 SDL_SetWindowTitle(window, tit.c_str());
211
212 // フォントを読込
213 TTF_Init();
214 font = TTF_OpenFontRW(SDL_RWFromMem(Peepo_ttf, Peepo_ttf_len), 1, FONTSIZE);
215 if (!font) {
216 std::cout << "フォントを読込に失敗" << std::endl;
217 SDL_DestroyRenderer(renderer);
218 SDL_DestroyWindow(window);
219 SDL_Quit();
220 return -1;
221 }
222
223 // 初期設定
224 running = true;
225 static int lastTime = 0;
226
227 color.r = color.g = color.b = 255;
228 lscore = rscore = games = 0;
229 ballvelocity = ballsize = 1.0f;
230 level = 1;
231 player.x = 32;
232 player.h = (HEIGHT/4)/(static_cast<int>(ballsize)*1.15f);
233 player.y = (HEIGHT/2) - (player.h/2);
234 player.w = 12;
235 npc = player;
236 npc.x = WIDTH - npc.w - 32;
237 ball.w = ball.h = (BALLSIZE-static_cast<int>(ballsize));
238
239 serve();
240
241 // ゲームループ
242 while (running) {
243 lastFrame = SDL_GetTicks();
244 if (lastFrame >= (lastTime + 1000)) {
245 lastTime = lastFrame;
246 fps = frameCount;
247 frameCount = 0;
248 }
249
250 if (!isgameover) {
251 update();
252 input();
253 render();
254 } else {
255 gameover();
256 input();
257 render();
258 }
259 }
260
261 // 掃除
262 TTF_CloseFont(font);
263 SDL_DestroyRenderer(renderer);
264 SDL_DestroyWindow(window);
265 SDL_Quit();
266
267 return 0;
268}