-
Notifications
You must be signed in to change notification settings - Fork 0
/
slot_game.cpp
597 lines (544 loc) · 14.4 KB
/
slot_game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <glut.h>
#include <Windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define COINS 30
GLfloat lightpos0[] = { -0.75 , 0.0 , 0.0 , 1.0 };
GLfloat lightpos1[] = { 0.0 , 0.0 , 0.0 , 1.0 };
GLfloat lightpos2[] = { 0.75 , 0.0 , 0.0 , 1.0 };
void MyGround(double height);
/* グローバル変数宣言 */
int game_start_flag = 0;
int start_flag = 0; /* 1ゲーム開始フラグ */
int r[3]; /* リール1,2,3の回転角 */
int empty_r[3];
int stop_r[3]; /* リール1,2,3の回転停止角 */
int stop_color_no[3] = { 7 , 7 , 7 }; /* リール1,2,3の回転停止角を示すReelColor構造体の配列番号 */
int stop_flag[3] = { 0 , 0 , 0 }; /* リール1,2,3の回転状態を示すフラグ */
int result_view_flag = 0; /* 結果表示の完了を示すフラグ */
int click_count = 0; /* 左クリックの回数 */
int speed = 1; /* 回転速度 */
int Coin; /* コイン */
int AddCoin; /* 加算されるコイン */
GLdouble vertex[][3] = {
{ -0.5 , 0.5 , 1.538935 }, /* A */
{ -0.5 , -0.5 , 1.538935 }, /* B */
{ 0.5 , -0.5 , 1.538935 }, /* C */
{ 0.5 , 0.5 , 1.538935 }, /* D */
{ -0.5 , 1.309122 , 0.9511 }, /* E */
{ 0.5 , 1.309122 , 0.9511 }, /* F */
{ -0.5 , 1.618122 , 0 }, /* G */
{ 0.5 , 1.618122 , 0 }, /* H */
{ -0.5 , 1.309122 , -0.9511 }, /* I */
{ 0.5 , 1.309122 , -0.9511 }, /* J */
{ -0.5 , 0.5 , -1.538935 }, /* K */
{ -0.5 , -0.5 , -1.538935 }, /* L */
{ 0.5 , -0.5 , -1.538935 }, /* M */
{ 0.5 , 0.5 , -1.538935 }, /* N */
{ -0.5 , -1.309122 , -0.9511 }, /* O */
{ 0.5 , -1.309122 , -0.9511 }, /* P */
{ -0.5 , -1.618122 , 0 }, /* Q */
{ 0.5 , -1.618122 , 0 }, /* R */
{ -0.5 , -1.309122 , 0.9511 }, /* S */
{ 0.5 , -1.309122 , 0.9511 } /* T */
};
int face[][4] = {
{ 8 , 6 , 7 , 9 }, /* I-G-H-J 0~35 */
{ 10 , 8 , 9 , 13 }, /* K-I-J-N 36~71 */
{ 11 , 10 , 13 , 12 }, /* L-K-N-M 72~107 */
{ 14 , 11 , 12 , 15 }, /* O-L-M-P 108~143 */
{ 16 , 14 , 15 , 17 }, /* Q-O-P-R 144~179 */
{ 18 , 16 , 17 , 19 }, /* S-Q-R-T 180~215 */
{ 1 , 18 , 19 , 2 }, /* B-S-T-C 216~251 */
{ 0 , 1 , 2 , 3 }, /* A-B-C-D 252~287 */
{ 4 , 0 , 3 , 5 }, /* E-A-D-F 288~323 */
{ 6 , 4 , 5 , 7 } /* G-E-F-H 324~360 */
};
GLdouble normal[][3] = {
{ 0.0 , -1.0 , 1.0 },
{ 0.0 , -1.0 , 1.0 },
{ 0.0 , 0.0 , 1.0 },
{ 0.0 , 1.0 , 1.0 },
{ 0.0 , 1.0 , 1.0 },
{ 0.0 , 1.0 , -1.0 },
{ 0.0 , 1.0 , -1.0 },
{ 0.0 , 0.0 , -1.0 },
{ 0.0 , -1.0 , -1.0 },
{ 0.0 , -1.0 , -1.0 }
};
GLfloat black[] = { 0.1 , 0.1 , 0.1 };
GLfloat spot[] = { 0.0 , 0.0 , 1.0 };
/* リールカラーの構造体宣言 */
struct ReelColor {
GLfloat color[4];
int color_no;
};
/* 赤:1 紫:2 緑:3 青:4 */
/* リール1のカラーレイアウト */
struct ReelColor color1[10] = {
{ { 1.0 , 0.0 , 0.0 , 1.0 } , 1 }, // R 108
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 144
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 180
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 216
{ { 1.0 , 0.0 , 1.0 , 1.0 } , 4 }, // P 252
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 288
{ { 1.0 , 0.0 , 1.0 , 1.0 } , 4 }, // P 324
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 360(0)
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 36
{ { 0.0 , 0.0 , 1.0 , 1.0} , 3 }, // B 72
};
/* リール2のカラーレイアウト */
struct ReelColor color2[10] = {
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 108
{ { 1.0 , 0.0 , 1.0 , 1.0 } , 4 }, // P 144
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 180
{ { 1.0 , 0.0 , 0.0 , 1.0 } , 1 }, // R 216
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 252
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 288
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 324
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 360(0)
{ { 1.0 , 0.0 , 1.0 , 1.0 } , 4 }, // P 36
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 72
};
/* リール3のカラーレイアウト */
struct ReelColor color3[10] = {
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 108
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 144
{ { 1.0 , 0.0 , 1.0 , 1.0 } , 4 }, // P 180
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 216
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 252
{ { 1.0 , 0.0 , 1.0 , 1.0 } , 4 }, // P 288
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 324
{ { 1.0 , 0.0 , 0.0 , 1.0 } , 1 }, // R 360(0)
{ { 0.0 , 0.0 , 1.0 , 1.0 } , 3 }, // B 36
{ { 0.0 , 1.0 , 0.0 , 1.0 } , 2 }, // G 72
};
/* ゲーム開始時の説明文 */
void IntroductionMsg(void)
{
int load_key;
FILE *fp;
printf("--------------------------------------------------------------------------------\n");
printf("スロットゲームへようこそ!\n\n");
printf("3つのリールの真ん中の絵柄を揃えましょう。\n");
printf("左クリックで左から順にリールの回転を止められます。\n");
printf("右クリックでリールの回転速度調整が可能です。\n");
printf("EscキーかQキーを押すとゲームを終了します。\n\n");
printf("絵柄を揃えてコインを増やしましょう。\n\n");
printf("赤: Wonderful! コイン+10\n");
printf("ピンク: Excellent! コイン+7\n");
printf("緑: Great! コイン+4\n");
printf("青: Good! コイン+2\n");
printf("一致なし: Miss! コイン-1\n\n");
printf("稼いだコインはSキーを押してセーブして保存できます。\n");
printf("Good Luck!!\n\n");
printf("(ヒント)回転速度が速い状態で絵柄を揃えると通常よりも獲得できるコイン数がUP!\n");
printf(" ただしミスした時のコイン減少量も大きくなるので注意!\n\n");
printf("前回セーブしたコイン枚数をロードしますか?\n");
printf("1:はい or 2:いいえ\n");
printf(">>");
scanf("%d",&load_key);
if(load_key == 1)
{
fp = fopen("save.txt" , "r");
if(fp == NULL)
{
printf("セーブファイル読み込みに失敗しました。\n");
Coin = COINS;
}
else
{
fscanf(fp , "%d" , &Coin);
fclose(fp);
}
}
else if(load_key == 2)
{
Coin = COINS;
}
printf("\n");
printf("ゲーム開始時の所持コインは%dコインです!\n",Coin);
printf("--------------------------------------------------------------------------------\n");
PlaySound("BGM\\Start_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
}
/* リール1の描画 */
void Reel1(void)
{
int i, j;
glLightfv(GL_LIGHT0 , GL_POSITION , lightpos0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot);
glEnable(GL_LIGHT0);
glBegin(GL_QUADS);
for(j=0; j<10; j++)
{
glNormal3dv(normal[j]);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color1[j].color);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color1[j].color);
for(i=0; i<4; i++)
{
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
}
/* リール2の描画 */
void Reel2(void)
{
int i, j;
glLightfv(GL_LIGHT1 , GL_POSITION , lightpos1);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot);
glEnable(GL_LIGHT1);
glBegin(GL_QUADS);
for(j=0; j<10; j++)
{
glNormal3dv(normal[j]);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color2[j].color);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color2[j].color);
for(i=0; i<4; i++)
{
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
}
/* リール3の描画 */
void Reel3(void)
{
int i, j;
glLightfv(GL_LIGHT2 , GL_POSITION , lightpos2);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, spot);
glEnable(GL_LIGHT2);
glBegin(GL_QUADS);
for(j=0; j<10; j++)
{
glNormal3dv(normal[j]);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color3[j].color);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color3[j].color);
for(i=0; i<4; i++)
{
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
}
/* 1ゲーム開始時の3つのリールの初期位置をランダムで決定 */
void ReelSet(void)
{
if(game_start_flag == 0)
{
srand((unsigned)time(NULL));
r[0] = rand()%361;
while(r[0]%36 != 0)
r[0]++;
r[1] = rand()%361;
while(r[1]%36 != 0)
r[1]++;
r[2] = rand()%361;
while(r[2]%36 != 0)
r[2]++;
game_start_flag = 1;
start_flag = 1;
}
else if((game_start_flag == 1)&&(start_flag == 0))
{
r[0] = stop_r[0];
r[1] = stop_r[1];
r[2] = stop_r[2];
start_flag = 1;
}
else;
}
/* 縦回転アニメーション */
void ReelAnime(int reel_num)
{
int empty;
if(click_count < (reel_num+1))
{
glRotated((double)r[reel_num] , 1.0 , 0.0 , 0.0);
}
else if((click_count==(reel_num+1))&&(stop_flag[reel_num]==0))
{
stop_r[reel_num] = r[reel_num];
while(((stop_r[reel_num])%36) != 0)
stop_r[reel_num]++;
empty = stop_r[reel_num];
while(empty != 0)
{
empty = empty-36;
stop_color_no[reel_num]++;
if(stop_color_no[reel_num] >= 10)
stop_color_no[reel_num] = stop_color_no[reel_num] - 10;
}
stop_flag[reel_num] = 1;
glRotated((double)stop_r[reel_num] , 1.0 , 0.0 , 0.0);
}
else if((click_count>=(reel_num+1))&&(stop_flag[reel_num]==1))
{
glRotated((double)stop_r[reel_num] , 1.0 , 0.0 , 0.0);
}
}
/* 絵柄の一致判定 */
void CheckReelColor(void)
{
char *GoodMsg1 = "Wonderful! ";
char *GoodMsg2 = "Excellent! ";
char *GoodMsg3 = "Great! ";
char *GoodMsg4 = "Good! ";
char *BadMsg = "Miss! ";
if((click_count==3)&&(stop_flag[2]==1)&&(result_view_flag==0))
{
int c1_no, c2_no, c3_no;
c1_no = color1[stop_color_no[0]].color_no;
c2_no = color2[stop_color_no[1]].color_no;
c3_no = color3[stop_color_no[2]].color_no;
if((c1_no==c2_no)&&(c2_no==c3_no)&&(c1_no==c3_no))
{
switch(c1_no)
{
case 1:
PlaySound("BGM\\Red_Stop_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
printf(GoodMsg1);
AddCoin = 10 * speed;
Coin += AddCoin;
printf("コイン: +%d %d\n",AddCoin,Coin);
break;
case 4:
PlaySound("BGM\\Pink_Stop_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
printf(GoodMsg2);
AddCoin = 7 * speed;
Coin += AddCoin;
printf("コイン: +%d %d\n",AddCoin,Coin);
break;
case 2:
PlaySound("BGM\\Green_Stop_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
printf(GoodMsg3);
AddCoin = 4 * speed;
Coin += AddCoin;
printf("コイン: +%d %d\n",AddCoin,Coin);
break;
case 3:
PlaySound("BGM\\Blue_Stop_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
printf(GoodMsg4);
AddCoin = 2 * speed;
Coin += AddCoin;
printf("コイン: +%d %d\n",AddCoin,Coin);
break;
}
}
else
{
printf(BadMsg);
AddCoin = speed;
Coin -= AddCoin;
if(Coin<0)
printf("コイン: -%d %d\n",AddCoin,0);
else
printf("コイン: -%d %d\n",AddCoin,Coin);
if(Coin <= 0)
{
int Key;
printf("\n\n");
printf("コインがなくなりました!\n");
printf("キーを押してください。\n\n");
printf("もう一度ゲームを遊ぶ: 1\n");
printf("ゲームを終了する: 2\n");
printf(">>");
scanf("%d",&Key);
if(Key == 1)
{
PlaySound("BGM\\Enter_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
printf("\n");
printf("コインの枚数がリセットされました!\n\n");
Coin = 30;
}
else if(Key == 2)
exit(0);
}
}
stop_color_no[0] = stop_color_no[1] = stop_color_no[2] = 7;
result_view_flag = 1;
}
}
void idle(void)
{
glutPostRedisplay();
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0 , 0.0 , 10.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0);
MyGround(-2.2);
/* リールの回転角の初期値設定 */
ReelSet();
/* リール1に関するアニメーション描画 */
glPushMatrix();
ReelAnime(0);
glTranslated(-1.5 , 0.0 , 0.0);
Reel1();
glPopMatrix();
/* リール2に関するアニメーション描画 */
glPushMatrix();
ReelAnime(1);
Reel2();
glPopMatrix();
/* リール3に関するアニメーション描画 */
glPushMatrix();
ReelAnime(2);
glTranslated(1.5 , 0.0 , 0.0);
Reel3();
glPopMatrix();
glutSwapBuffers();
/* リールの絵柄の判定 */
CheckReelColor();
/* 1周回ったら回転角を0に戻す */
for(i=0; i<=2; i++)
{
if((r[i] += speed) >= 360)
{
r[i] = 0;
}
}
}
void resize(int w,int h)
{
glViewport(0 , 0 , w , h);
/* 透視変換行列の設定 */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0 , (double)w / (double)h , 1.0 , 100.0);
/* モデルビュー変換行列の設定 */
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button,int state,int x,int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN)
{
click_count++;
if(click_count <= 3)
PlaySound("BGM\\Stop_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
else if(click_count == 4)
{
/* 各変数の初期化 */
click_count = 0;
stop_flag[0] = stop_flag[1] = stop_flag[2] = 0;
start_flag = 0;
result_view_flag = 0;
PlaySound("BGM\\Start_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
}
}
break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN)
{
printf("--------------------------------------------------------------------------------\n");
printf("[速度調整モード]\n");
printf("リールの回転速度を設定します。\n");
printf("お好みの速度の数字キーを押してください。\n\n");
printf("1:普通(デフォルトの速度)\n");
printf("2:速い\n");
printf("3:かなり速い\n");
printf("4:神の領域\n");
printf(">>");
scanf("%d",&speed);
printf("\n");
printf("--------------------------------------------------------------------------------\n");
PlaySound("BGM\\Enter_Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
}
break;
default:
break;
}
}
void keyboard(unsigned char key,int x,int y)
{
int save_key;
FILE *fp;
switch(key)
{
case 'q':
case 'Q':
case '\033': /* '\033'はESCのASCIIコード */
exit(0);
case 's':
case 'S':
printf("\n");
printf("コイン枚数をセーブしますか?\n");
printf("1:はい or 2:いいえ\n");
printf(">>");
scanf("%d",&save_key);
if(save_key == 1)
{
fp = fopen("save.txt" , "w");
fprintf(fp , "%d" , Coin);
fclose(fp);
}
else if(save_key == 2)
{
printf("\n");
}
break;
default:
break;
}
}
void init(void)
{
glClearColor(1.0 , 1.0 , 1.0 , 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_LIGHTING);
}
void MyGround(double height)
{
static GLfloat ground[][4] = {
{ 0.6 , 0.6 , 0.6 , 1.0 },
{ 0.3 , 0.3 , 0.3 , 1.0 },
};
int i, j;
glBegin(GL_QUADS);
glNormal3d(0.0, 1.0, 0.0);
for(j=-5; j<5; ++j)
{
for(i=-5; i<5; ++i)
{
glMaterialfv(GL_FRONT, GL_DIFFUSE, ground[(i+j) & 1]);
glMaterialfv(GL_FRONT, GL_SPECULAR, ground[(i+j) & 1]);
glMaterialfv(GL_FRONT, GL_AMBIENT, black);
glVertex3d((GLdouble)i, height, (GLdouble)j);
glVertex3d((GLdouble)i, height, (GLdouble)(j+1));
glVertex3d((GLdouble)(i+1), height, (GLdouble)(j+1));
glVertex3d((GLdouble)(i+1), height, (GLdouble)j);
}
}
glEnd();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(250,100);
glutInitWindowSize(1000,800);
glutCreateWindow(argv[0]);
IntroductionMsg();
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}