Repository navigation
Expand file tree
/
Copy pathsnake.cpp
More file actions
982 lines (886 loc) · 19.1 KB
/
Copy pathsnake.cpp
File metadata and controls
982 lines (886 loc) · 19.1 KB
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
// 헤더 파일
#include <iostream>
#include <string>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <fcntl.h>
#include <errno.h>
#include <cstring>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/ioctl.h>
using namespace std;
#include "buzzer.h"
// 게임 보드 가로/세로 크기
const int width = 80;
const int height = 20;
// FPGA 푸시 스위치 디바이스
int fd_fpga_switch = -1;
bool use_fpga_switch = false;
// FPGA FND 디바이스
int fd_fpga_fnd = -1;
// 인터럽트 스위치 디바이스
int fd_sw = -1;
bool use_interrupt_switch = false;
// 버저 디바이스
int fd_buzzer = -1;
// 뱀 머리 좌표 (x축, y축)
int x, y;
// 벽 통과 모드 여부 (true: 반대편 출현, false: 게임 오버)
bool wrapWalls = false;
// 일반 과일 좌표
int fruitCordX, fruitCordY;
// 느린 과일(*) 좌표 및 상태
bool slowFruitActive = false;
int slowFruitX, slowFruitY;
int slowEffectTicks = 0;
const int SLOW_DURATION = 20;
// 점수 2배 과일($) 좌표 및 상태
bool doubleFruitActive = false;
int doubleFruitX, doubleFruitY;
bool scoreDoubled = false;
// 플레이어 점수
int playerScore;
// 최고 점수
int highScore = 0;
string highScoreName = "";
// 뱀 꼬리 좌표 배열 (x축, y축)
int snakeTailX[100], snakeTailY[100];
// 뱀 꼬리 길이
int snakeTailLen;
// 뱀 이동 방향
enum snakesDirection
{
STOP = 0,
LEFT,
RIGHT,
UP,
DOWN
};
// 현재 이동 방향 변수
snakesDirection sDir;
// 게임 오버 여부
bool isGameOver;
bool gamePaused = false;
bool pauseMenuActive = false;
void disableRawMode();
void enableRawMode();
void UpdateFNDScore(int score)
{
if (fd_fpga_fnd < 0)
return;
unsigned char fnd_value[4];
if (score < 0)
score = 0;
if (score > 9999)
score = 9999;
fnd_value[0] = (score / 1000) % 10;
fnd_value[1] = (score / 100) % 10;
fnd_value[2] = (score / 10) % 10;
fnd_value[3] = score % 10;
write(fd_fpga_fnd, fnd_value, 4);
}
static void PlayBuzzerNote(const char *note_name, long duration_ms)
{
if (fd_buzzer < 0)
return;
int freq = 0;
if (note_name_to_frequency(note_name, &freq) == 0)
play_tone(fd_buzzer, (unsigned int)freq, duration_ms);
}
static void PlayStartSound()
{
PlayBuzzerNote("G5", 90);
PlayBuzzerNote("B5", 90);
}
static void PlayGameOverSound()
{
PlayBuzzerNote("G4", 120);
PlayBuzzerNote("D4", 120);
PlayBuzzerNote("G3", 180);
}
// 게임 변수 초기화 함수
void GameInit()
{
isGameOver = false;
gamePaused = false;
sDir = STOP;
x = width / 2;
y = height / 2;
fruitCordX = rand() % width;
fruitCordY = rand() % height;
playerScore = 0;
snakeTailLen = 0;
slowFruitActive = false;
slowEffectTicks = 0;
doubleFruitActive = false;
scoreDoubled = false;
memset(snakeTailX, 0, sizeof(snakeTailX));
memset(snakeTailY, 0, sizeof(snakeTailY));
UpdateFNDScore(playerScore);
}
// 난이도 선택 함수
unsigned int SelectDifficulty()
{
cout << endl;
cout << "=== Select Difficulty ===" << endl;
if (use_fpga_switch)
{
cout << "Button 0 = Easy (200ms - 느리게)" << endl;
cout << "Button 1 = Normal (150ms - 보통)" << endl;
cout << "Button 2 = Hard (100ms - 빠르게)" << endl;
// 모든 버튼이 떼어질 때까지 대기
unsigned char sw_state[13];
bool anyPressed = true;
while (anyPressed)
{
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
anyPressed = false;
for (int i = 0; i < 13; i++)
{
if (sw_state[i])
{
anyPressed = true;
break;
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
while (true)
{
unsigned char sw_state[13];
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
if (sw_state[0])
{
cout << ">> Easy 선택!" << endl;
PlayBuzzerNote("A5", 90);
sleep(1);
return 200;
}
if (sw_state[1])
{
cout << ">> Normal 선택!" << endl;
PlayBuzzerNote("C5", 90);
sleep(1);
return 150;
}
if (sw_state[2])
{
cout << ">> Hard 선택!" << endl;
PlayBuzzerNote("E5", 90);
sleep(1);
return 100;
}
}
}
}
else
{
cout << "0 = Easy (200ms - 느리게)" << endl;
cout << "1 = Normal (150ms - 보통)" << endl;
cout << "2 = Hard (100ms - 빠르게)" << endl;
cout << "선택: " << flush;
char choice;
while (true)
{
cin >> choice;
if (choice == '0')
{
cout << ">> Easy 선택!" << endl;
PlayBuzzerNote("A5", 90);
return 200;
}
if (choice == '1')
{
cout << ">> Normal 선택!" << endl;
PlayBuzzerNote("C5", 90);
return 150;
}
if (choice == '2')
{
cout << ">> Hard 선택!" << endl;
return 100;
}
cout << "0, 1, 2 중에 선택해주세요: " << flush;
}
}
}
// 벽 동작 모드 선택 함수
bool SelectMode()
{
cout << endl;
cout << "=== Select Mode ===" << endl;
if (use_fpga_switch)
{
cout << "Button 0 = Normal (벽 닿으면 죽음)" << endl;
cout << "Button 1 = Wrap (벽 닿으면 반대편으로 넘어감)" << endl;
// 버튼 릴리즈 대기
unsigned char sw_state[13];
bool anyPressed = true;
while (anyPressed)
{
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
anyPressed = false;
for (int i = 0; i < 13; i++)
{
if (sw_state[i])
{
anyPressed = true;
break;
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
while (true)
{
unsigned char sw_state[13];
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
if (sw_state[0])
{
cout << ">> Normal 모드 선택!" << endl;
PlayBuzzerNote("A4", 120);
sleep(1);
return false;
}
if (sw_state[1])
{
cout << ">> Wrap 모드 선택!" << endl;
PlayBuzzerNote("C4", 120);
sleep(1);
return true;
}
}
}
}
else
{
cout << "0 = Normal (벽 닿으면 죽음)" << endl;
cout << "1 = Wrap (벽 닿으면 반대편으로 넘어감)" << endl;
cout << "선택: " << flush;
char choice;
while (true)
{
cin >> choice;
if (choice == '0')
{
cout << ">> Normal 모드 선택!" << endl;
return false;
}
if (choice == '1')
{
cout << ">> Wrap 모드 선택!" << endl;
return true;
}
cout << "0 또는 1 중에 선택해주세요: " << flush;
}
}
}
bool PromptRestart()
{
disableRawMode();
string currentPlayerName;
PlayGameOverSound();
cout << "Enter your name to record this score: ";
cin >> currentPlayerName;
cout << endl;
cout << "Game Over! Final Score: " << playerScore << endl;
// 최고 점수 갱신 확인
if (playerScore > highScore)
{
highScore = playerScore;
highScoreName = currentPlayerName;
cout << "*** NEW HIGH SCORE! ***" << endl;
}
else
{
cout << "Current Score: " << playerScore << " (" << currentPlayerName << ")" << endl;
cout << "High Score: " << highScore << " (" << highScoreName << ")" << endl;
}
cout << endl;
if (use_fpga_switch)
{
cout << "Restart? FPGA Switch 0 = restart, 2 = exit" << endl;
}
else
{
cout << "Restart? (y/n): " << flush;
}
while (true)
{
if (use_fpga_switch)
{
unsigned char sw_state[13];
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
if (sw_state[0])
{
PlayStartSound();
return true;
}
if (sw_state[2])
{
PlayGameOverSound();
return false;
}
}
}
else
{
char choice;
if (!(cin >> choice))
{
return false;
}
if (choice == 'y' || choice == 'Y')
{
return true;
}
if (choice == 'n' || choice == 'N')
{
PlayGameOverSound();
return false;
}
cout << "Please enter y or n." << endl;
}
}
}
// 게임 보드 렌더링 함수
void GameRender(string playerName, unsigned int frameMs)
{
std::system("clear"); // 화면 지우기
// 상단 벽 '-' 출력
for (int i = 0; i < width + 2; i++)
cout << "-";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j <= width; j++)
{
// 좌우 벽 '|' 출력
if (j == 0 || j == width)
cout << "|";
// 뱀 머리 'O' 출력
if (i == y && j == x)
cout << "O";
// 일반 과일 '#' 출력
else if (i == fruitCordY && j == fruitCordX)
cout << "#";
// 느린 과일 '*' 출력
else if (slowFruitActive && i == slowFruitY && j == slowFruitX)
cout << "*";
// 점수 2배 과일 '$' 출력
else if (doubleFruitActive && i == doubleFruitY && j == doubleFruitX)
cout << "$";
else
{
bool prTail = false;
for (int k = 0; k < snakeTailLen; k++)
{
if (snakeTailX[k] == j && snakeTailY[k] == i)
{
cout << "o";
prTail = true;
}
}
if (!prTail)
cout << " ";
}
}
cout << endl;
}
// 하단 벽 '-' 출력
for (int i = 0; i < width + 2; i++)
cout << "-";
cout << endl;
// 점수 및 난이도 표시
cout << playerName << "'s Score: " << playerScore;
if (highScore > 0)
cout << " | High Score: " << highScore << " (" << highScoreName << ")";
cout << endl;
// 난이도 표시
string diffLabel = (frameMs == 200) ? "Easy" : (frameMs == 150) ? "Normal"
: "Hard";
cout << "Difficulty: " << diffLabel << endl;
if (slowEffectTicks > 0)
cout << "** SLOW! (" << slowEffectTicks << " ticks 남음) **" << endl;
if (scoreDoubled)
cout << "** 점수 2배 활성화! 다음 과일 +20pts **" << endl;
cout << "Fruit: # = +10pts | * = +5pts + 속도 감소 | $ = 다음 과일 점수 2배" << endl;
// 모드 표시
string modeLabel = wrapWalls ? "Wrap (벽 닿으면 반대편으로)" : "Normal (벽 닿으면 죽음)";
cout << "Mode: " << modeLabel << endl;
if (gamePaused)
cout << "Game Paused: Interrupt switch to resume" << endl;
if (use_fpga_switch)
cout << "Control: Button 1=Up / 3=Left / 5=Right / 7=Down (FPGA Switch)" << endl;
else if (use_interrupt_switch)
cout << "Control: Interrupt Switch (press to pause/resume)" << endl;
else
cout << "Control: Up: 8 / Down: 2 / Left: 4 / Right: 6 (Keyboard)" << endl;
}
// 게임 상태 업데이트 함수
void UpdateGame()
{
int headX = x;
int headY = y;
int prevX = snakeTailX[0];
int prevY = snakeTailY[0];
int prev2X, prev2Y;
switch (sDir)
{
case LEFT:
x--;
if (x < 0)
{
if (wrapWalls)
x = width - 1;
else
{
PlayBuzzerNote("A2", 300);
isGameOver = true;
return;
}
}
break;
case RIGHT:
x++;
if (x >= width)
{
if (wrapWalls)
x = 0;
else
{
PlayBuzzerNote("A2", 300);
isGameOver = true;
return;
}
}
break;
case UP:
y--;
if (y < 0)
{
if (wrapWalls)
y = height - 1;
else
{
PlayBuzzerNote("A2", 300);
isGameOver = true;
return;
}
}
break;
case DOWN:
y++;
if (y >= height)
{
if (wrapWalls)
y = 0;
else
{
PlayBuzzerNote("A2", 300);
isGameOver = true;
return;
}
}
break;
}
snakeTailX[0] = headX;
snakeTailY[0] = headY;
for (int i = 1; i < snakeTailLen; i++)
{
prev2X = snakeTailX[i];
prev2Y = snakeTailY[i];
snakeTailX[i] = prevX;
snakeTailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
// 꼬리 충돌 확인
for (int i = 0; i < snakeTailLen; i++)
{
if (snakeTailX[i] == x && snakeTailY[i] == y)
{
PlayBuzzerNote("A2", 400);
isGameOver = true;
}
}
// 일반 과일('#') 충돌 확인
if (x == fruitCordX && y == fruitCordY)
{
playerScore += scoreDoubled ? 20 : 10;
scoreDoubled = false;
snakeTailLen++;
UpdateFNDScore(playerScore);
fruitCordX = rand() % width;
fruitCordY = rand() % height;
// 30% 확률로 slow fruit 생성
if (!slowFruitActive && (rand() % 10) < 3)
{
slowFruitActive = true;
slowFruitX = rand() % width;
slowFruitY = rand() % height;
}
// 20% 확률로 점수 2배 과일 생성
if (!doubleFruitActive && (rand() % 10) < 2)
{
doubleFruitActive = true;
doubleFruitX = rand() % width;
doubleFruitY = rand() % height;
}
// 과일 획득 효과음 재생
if (fd_buzzer >= 0)
{
PlayBuzzerNote("A5", 100);
PlayBuzzerNote("C#6", 80);
}
}
// 느린 과일('*') 충돌 확인
if (slowFruitActive && x == slowFruitX && y == slowFruitY)
{
playerScore += 5;
snakeTailLen++;
UpdateFNDScore(playerScore);
slowFruitActive = false;
slowEffectTicks = SLOW_DURATION;
// 느린 과일 효과음 재생 (낮은 음)
PlayBuzzerNote("E5", 140);
}
// 점수 2배 과일('$') 충돌 확인
if (doubleFruitActive && x == doubleFruitX && y == doubleFruitY)
{
doubleFruitActive = false;
scoreDoubled = true;
PlayBuzzerNote("C6", 80);
PlayBuzzerNote("E6", 80);
}
// 속도 감소 효과 틱 감소
if (slowEffectTicks > 0)
slowEffectTicks--;
}
// 키보드 입력 처리 함수
void UserInput()
{
fd_set set;
struct timeval tv;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select(STDIN_FILENO + 1, &set, NULL, NULL, &tv) > 0)
{
int key = getchar();
switch (key)
{
case '4':
sDir = LEFT;
break;
case '6':
sDir = RIGHT;
break;
case '8':
sDir = UP;
break;
case '2':
sDir = DOWN;
break;
case '0':
isGameOver = true;
break;
}
}
}
// FPGA 푸시 스위치 입력 처리 함수
void FPGASwitchInput()
{
if (fd_fpga_switch < 0)
return;
unsigned char sw_state[13];
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
// 버튼 매핑: 1=위, 3=왼쪽, 5=오른쪽, 7=아래
if (sw_state[1]){
PlayBuzzerNote("G4", 120);
sDir = UP;
}
if (sw_state[3]){
PlayBuzzerNote("C4", 120);
sDir = LEFT;
}
if (sw_state[5]){
PlayBuzzerNote("E4", 120);
sDir = RIGHT;
}
if (sw_state[7]){
PlayBuzzerNote("D4", 120);
sDir = DOWN;
}
}
}
// 인터럽트 스위치 입력 처리 함수
void InterruptSwitchInput()
{
if (fd_sw < 0)
return;
static unsigned char prev_state = 0;
unsigned char sw_state = 0;
if (read(fd_sw, &sw_state, 1) > 0)
{
if (sw_state == 1 && prev_state == 0)
{
gamePaused = !gamePaused;
}
prev_state = sw_state;
}
}
// 인터럽트로 일시정지 시 일회성 메뉴 표시
// 재개 시 true, 종료 시 false 반환
bool PauseMenu()
{
disableRawMode();
cout << "\n=== Game Paused ===" << endl;
PlayGameOverSound();
if (use_fpga_switch)
{
cout << "FPGA: Button 0 = Resume, Button 2 = Exit" << endl;
}
if (use_interrupt_switch)
{
cout << "Interrupt Switch: toggle back to resume" << endl;
}
if (!use_fpga_switch && !use_interrupt_switch)
{
cout << "Keyboard: r = resume" << endl;
}
unsigned char prev_fpga[13] = {0};
bool prev_interrupt_state = false;
if (fd_sw >= 0)
{
unsigned char sw_state = 0;
if (read(fd_sw, &sw_state, 1) > 0)
{
prev_interrupt_state = (sw_state != 0);
}
}
if (fd_fpga_switch >= 0)
{
unsigned char sw_state[13];
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
memcpy(prev_fpga, sw_state, 13);
}
}
while (true)
{
fd_set set;
FD_ZERO(&set);
int maxFd = -1;
if (fd_fpga_switch >= 0)
{
FD_SET(fd_fpga_switch, &set);
if (fd_fpga_switch > maxFd)
maxFd = fd_fpga_switch;
}
if (fd_sw >= 0)
{
FD_SET(fd_sw, &set);
if (fd_sw > maxFd)
maxFd = fd_sw;
}
FD_SET(STDIN_FILENO, &set);
if (STDIN_FILENO > maxFd)
maxFd = STDIN_FILENO;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
int ready = select(maxFd + 1, &set, NULL, NULL, &tv);
if (ready > 0)
{
if (fd_fpga_switch >= 0 && FD_ISSET(fd_fpga_switch, &set))
{
unsigned char sw_state[13];
if (read(fd_fpga_switch, sw_state, 13) > 0)
{
//continue
if (sw_state[0] && !prev_fpga[0])
{
PlayStartSound();
enableRawMode();
return true;
}
//game over if button 2 is pressed
if (sw_state[2] && !prev_fpga[2])
{
PlayGameOverSound();
return false;
}
memcpy(prev_fpga, sw_state, 13);
}
}
if (fd_sw >= 0 && FD_ISSET(fd_sw, &set))
{
unsigned char sw_state = 0;
if (read(fd_sw, &sw_state, 1) > 0)
{
bool current_interrupt_state = (sw_state != 0);
if (current_interrupt_state && !prev_interrupt_state)
{
enableRawMode();
return true;
}
prev_interrupt_state = current_interrupt_state;
}
}
if (FD_ISSET(STDIN_FILENO, &set))
{
int c = getchar();
if (c == 'r' || c == 'R')
{
enableRawMode();
return true;
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
static struct termios orig_termios;
void disableRawMode()
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void enableRawMode()
{
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
return;
struct termios raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
atexit(disableRawMode);
}
// 메인 함수 / 게임 루프
int main()
{
// FPGA 푸시 스위치 디바이스 열기
fd_fpga_switch = open("/dev/fpga_push_switch", O_RDWR);
if (fd_fpga_switch >= 0)
{
use_fpga_switch = true;
cout << "FPGA push switch device opened successfully!" << endl;
}
else
{
cout << "FPGA push switch device not found. Using keyboard input." << endl;
}
// FPGA FND 디바이스 열기
fd_fpga_fnd = open("/dev/fpga_fnd", O_RDWR);
if (fd_fpga_fnd >= 0)
{
cout << "FPGA FND device opened successfully!" << endl;
}
else
{
cout << "FPGA FND device not found. Score will be shown on console only." << endl;
}
// 인터럽트 스위치 디바이스 열기
fd_sw = open("/dev/my_led_dev", O_RDWR);
if (fd_sw >= 0)
{
use_interrupt_switch = true;
cout << "Interrupt switch device opened successfully!" << endl;
}
else
{
cout << "Interrupt switch device not found." << endl;
}
// 버저 디바이스 열기 (선택)
fd_buzzer = open("/dev/fpga_buzzer", O_RDWR);
if (fd_buzzer >= 0)
{
cout << "FPGA buzzer device opened successfully!" << endl;
}
else
{
cout << "FPGA buzzer device not found (sound disabled)." << endl;
}
GameInit();
// 게임 시작 전 난이도 및 모드 선택
unsigned int frameMs = SelectDifficulty();
wrapWalls = SelectMode();
enableRawMode();
PlayStartSound();
while (true)
{
while (!isGameOver)
{
unsigned int currentFrameMs = (slowEffectTicks > 0) ? frameMs * 2 : frameMs;
GameRender("Player", frameMs);
// 인터럽트 스위치 우선 확인 (일시정지/재개)
if (use_interrupt_switch)
{
InterruptSwitchInput();
// 일시정지 시 일회성 메뉴 표시 (반복 출력 방지)
if (gamePaused && !pauseMenuActive)
{
pauseMenuActive = true;
bool resume = PauseMenu();
pauseMenuActive = false;
if (!resume)
{
isGameOver = true;
break;
}
else
{
gamePaused = false;
continue;
}
}
}
// 일시정지 중이 아닐 때만 이동 입력 처리
if (!gamePaused && use_fpga_switch)
{
FPGASwitchInput();
}
else if (!gamePaused)
{
UserInput();
}
if (!gamePaused)
{
UpdateGame();
}
std::this_thread::sleep_for(std::chrono::milliseconds(currentFrameMs));
}
if (!PromptRestart())
{
break;
}
GameInit();
// 재시작 시 난이도 및 모드 재선택
frameMs = SelectDifficulty();
wrapWalls = SelectMode();
enableRawMode();
PlayStartSound();
}
// 열린 디바이스 닫기
if (fd_fpga_switch >= 0)
close(fd_fpga_switch);
if (fd_fpga_fnd >= 0)
close(fd_fpga_fnd);
if (fd_sw >= 0)
close(fd_sw);
if (fd_buzzer >= 0)
close(fd_buzzer);
return 0;
}