コミット

諏訪子 <suwako@076.moe>、2025-11-25 20:04
コミット 97888084e8e0d485576e545bab7a26b4c8295bde ・ファイルを見る

5個のファイル、+319 -0

追加 CHANGELOG.md
@@ -0,0 +1,2 @@
1+# 1.0.0
2+* 最初リリース
追加 Makefile
@@ -0,0 +1,24 @@
1+NAME=pong
2+VERSION=1.0.0
3+# Linux、Haiku、かIllumos = /usr、FreeBSDかOpenBSD = /usr/local、NetBSD = /usr/pkg
4+PREFIX=/usr/local
5+CC=clang++
6+CFLAGS=-I${PREFIX}/include -L${PREFIX}/lib
7+LDFLAGS=-lSDL2 -lSDL2_ttf
8+FILES=main.cc
9+
10+all:
11+ ${CC} ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS}
12+
13+clean:
14+ rm -f ${NAME}
15+
16+install: all
17+ mkdir -p ${DESTDIR}${PREFIX}/bin
18+ cp -f ${NAME} ${DESTDIR}${PREFIX}/bin
19+ chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME}
20+
21+uninstall:
22+ rm -f ${DESTDIR}${PREFIX}/bin/${NAME}
23+
24+.PHONY: all clean install uninstall
追加 Peepo.ttf

バイナリファイル

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