-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
214 lines (191 loc) · 3.84 KB
/
main.c
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
#include "zurapce/zurapce.h"
#include "grid.h"
static BOOL s_initialize_succeed = FALSE;
extern BYTE PANEL[], PANEL_ANIM[];
static UnitedPieceBmp s_panel_bmp, s_panel_anim_bmp;
static PieceBmpAnimation s_panel_anim;
static int const s_panel_width = 22;
static int const s_period = 33;
static int s_adding_panel;
enum Phase
{
Phase_Title,
Phase_Game,
Phase_GameOver,
Phase_Win,
};
typedef enum Phase Phase;
static Phase s_phase;
static void SetupUnitedPieceBmp( UnitedPieceBmp* p, BYTE* source )
{
UnitedPieceBmp_Construct( p, source, s_panel_width, s_panel_width );
}
static void DrawGrid( void )
{
int i;
for( i = 0; i < GRID_WIDTH * GRID_WIDTH; i++ )
{
int const x = DISP_X / 2 + s_panel_width * ( ( i % GRID_WIDTH ) - GRID_WIDTH / 2 );
int const y = s_panel_width * ( i / GRID_WIDTH );
if( i == s_adding_panel
&& PieceBmpAnimation_Playing( &s_panel_anim ) )
{
PieceBmpAnimation_Draw( &s_panel_anim, x, y, DRW_CLR( COLOR_BLACK, COLOR_WHITE ) );
}
else
{
UnitedPieceBmp_Draw( &s_panel_bmp, x, y, g_grid[i], DRW_NOMAL );
}
}
}
static void DrawScore( void )
{
FontProxy_SetType( 2 );
pceFontSetPos( 0, 0 );
pceFontPutStr( "SCORE\n" );
pceFontPrintf( "%5d", g_score );
}
static void StartGame( void )
{
int i;
InitGrid();
for( i = 0; i < 2; i++ )
{
AddRandomPanel();
}
PieceBmpAnimation_Clear( &s_panel_anim );
s_phase = Phase_Game;
}
static int CenteringX( char const* message, int font_width )
{
return ( DISP_X - strlen( message ) * font_width ) / 2;
}
static void DrawTitle( char const* message )
{
FontFuchi_SetType( 1 );
FontFuchi_SetPos( CenteringX( message, 8 ), 24 );
FontFuchi_PutStr( message );
}
static void DrawPushButton( char const* message )
{
FontFuchi_SetType( 0 );
FontFuchi_SetPos( CenteringX( message, 5 ), 54 );
FontFuchi_PutStr( message );
}
static void DrawRestartMessage( void )
{
DrawPushButton( "PUSH A BUTTON TO RESTART" );
}
/// 初期化.
void pceAppInit( void )
{
FramObject_Init();
pceAppSetProcPeriod( s_period );
Configure_Init();
FontProxy_Hook_Set();
if( Lcd_Init() )
{
SetupUnitedPieceBmp( &s_panel_bmp, PANEL );
SetupUnitedPieceBmp( &s_panel_anim_bmp, PANEL_ANIM );
InitGrid();
s_phase = Phase_Title;
DrawGrid();
DrawScore();
s_initialize_succeed = TRUE;
}
}
/// メイン.
void pceAppProc( int cnt )
{
BOOL moved = FALSE;
if( !s_initialize_succeed || pcePadGet() & TRG_D )
{
pceAppReqExit( 0 );
}
switch( s_phase )
{
case Phase_Title:
case Phase_GameOver:
case Phase_Win:
if( pcePadGet() & TRG_A )
{
StartGame();
}
break;
case Phase_Game:
if( PieceBmpAnimation_Playing( &s_panel_anim ) )
{
if( PieceBmpAnimation_IsEnd( &s_panel_anim ) )
{
PieceBmpAnimation_Clear( &s_panel_anim );
if( !Movable() )
{
s_phase = Phase_GameOver;
}
}
PieceBmpAnimation_Update( &s_panel_anim, s_period );
}
else
{
if( pcePadGet() & TRG_LF )
{
moved = MoveLeft();
}
else if( pcePadGet() & TRG_RI )
{
moved = MoveRight();
}
else if( pcePadGet() & TRG_UP )
{
moved = MoveUp();
}
else if( pcePadGet() & TRG_DN )
{
moved = MoveDown();
}
if( moved )
{
if( Win() )
{
s_phase = Phase_Win;
}
else
{
s_adding_panel = AddRandomPanel();
PieceBmpAnimation_StartToEnd( &s_panel_anim, &s_panel_anim_bmp
, s_period, FALSE );
}
}
}
break;
}
pceLCDPaint( 0, 0, 0, DISP_X, DISP_Y );
DrawGrid();
DrawScore();
switch( s_phase )
{
case Phase_Title:
DrawTitle( "2048 for P/ECE" );
DrawPushButton( "PUSH A BUTTON TO START" );
break;
case Phase_Game:
break;
case Phase_GameOver:
DrawTitle( "GAME OVER" );
DrawRestartMessage();
break;
case Phase_Win:
DrawTitle( "YOU WIN !!" );
DrawRestartMessage();
break;
}
Lcd_Update();
Lcd_Trans();
rand(); // 空回し
}
/// 終了.
void pceAppExit( void )
{
FontProxy_Unhook_Set();
Configure_Exit();
}