Código fuente del juego mediano Vc
# include & lttime.h & gt
# include & ltstdio.h & gt
# include & ltstring.h & gt
const int g _ width = 400
const int g _ height = 520
/* Se utiliza para registrar 7 formas y sus 4 variaciones Tabla de cuerpo */
static int g _ TRS _ map[8][4][4][4];
/*Cambiar tabla de números*/
static int g_map_mod[] = {1, 4, 4, 4, 2, 2, 2, 1, 0};
/*Inicializar datos globales y visualización gráfica*/
void initgr() {
initgraph(g_width, g_height);
Setfont(12, 6, "宋体");
int Trs_map[8][ 4][4][4] =
{
{{{0}}},{{
{0,0 ,0,0} ,{1,1,1,0},{0,1,0,0},
},{
{0,1,0, 0},{1 ,1,0,0},{0,1,0,0},
},{
{0,1,0,0}, {1,1, 1,0},
},{
{0,1,0,0},{0,1,1,0},{0, 1,0,0 },
},},{{
{2,2,0,0},{0,2,0,0},{0, 2,0,0 },
},{
{0,0,2,0},{2,2,2,0},
},{
{0,2,0,0},{0,2,0,0},{0,2,2,0},
}, {
{0,0,0,0},{2,2,2,0},{2,0,0,0},
},},{ {
{0,3,3,0},{0,3,0,0},{0,3,0,0},
},{ p>
{0,0,0,0},{3,3,3,0},{0,0,3,0},
},{
{0, 3,0,0},{0,3,0,0},{3,3,0,0},
},{
{ 3,0,0 ,0},{3,3,3,0},
},},{{
{4,4,0,0},{ 0,4,4 ,0},
},{
{0,0,4,0},{0,4,4,0},{0,4 ,0,0} ,
},},{{
{0,5,5,0},{5,5,0,0},
}, {
{0,5,0,0},{0,5,5,0},{0,0,5,0},
},},{ {
{0,0,0,0},{6,6,6,6},
},{
{0,0, 6,0},{0,0,6,0},{0,0,6,0},{0,0,6,0},
},} ,{{ p>
{0,0,0,0},{0,7,7,0},{0,7,7,0},
},},
};
memcpy(g_trs_map, trs_map, sizeof(Trs_map));
}
Juego de clase {
Público :
/*Tabla de estado*/
Enumeración {
ST_START, /*Reinicio del juego*/
ST_NEXT, /*Prepara la siguiente casilla*/
ST_NORMAL, /*Fase de control del jugador*/
ST_OVER /*El juego termina, F2 se reinicia* /
};
Juego(int w, int h, int bw, int bh) {
int colormap[10] = {0, 0xA0, 0x50A0, 0xA0A0, 0xC000,
0xA0A000, 0xC04040, 0xA000A0, 0x808080, 0x ffffff};
memcpy(m_colormap, colormap, sizeof(m_colormap));
int Claves[8] = {VK_F2, VK_LEFT, VK_RIGHT, VK_DOWN, VK_UP, VK_NUMPAD0, VK_SPACE};
memcpy(m_Keys, Keys, sizeof(Keys));
memset (m_KeyState, 0, sizeof(m_key state));
m _ gamepool _ w = w
m _ gamepool _ h = h
m _ base _ w = bw
m _ base _ h = bh
randomize();
m _ CTL _ t =-1 ;
m_pcb = nueva imagen;
for(int I = 0; i<10;++i) {
drawtile(bw * i, 0, bw, bh, 5, mapa de colores [I]);
}
getimage(m_pcb, 0, 0, bw*10, BH
m_estado=ST_START<); /p>
}
/*Procesamiento de transición de estado*/
int deal () {
int nRet = 0;
If (m_state == ST_START) {//Inicialización
m _ siguiente 1 _ s = aleatorio(7)+1;
m _ siguiente 2 _ s = aleatorio( 7)+1;
m _ pausa = 0
memset(m_gamepool, 255, sizeof(m _ grupo de juegos)); p>for(int y = 1; y & lt= m _ juego de billar _ h; ++y) {
for(int x = 1; x & lt= m _ juego de billar _ w; ++x)
m _ grupo de juegos[y][x]= 0
}
m _ prohibir _ abajo = 0; >
m _ CTL _ t =-1;
nRet = 1
m _ estado = ST _ NEXT
} else if ( m_state == ST_NEXT ) {
m _ CTL _ x =(m _ juego billar _ w-4)/2+1
m _ CTL _ y = 1 ; p>
m_CTL_t=0;
m_CTL_s=m_siguiente 1_s;
m_CTL_dy=0;
p>m _ siguiente 1 _ s = m _ siguiente 2
_ s;
m _ siguientes 2 _ s = aleatorio(7)+1;
m _ curtime = m _ droptime
m _ curxtime = 0 ;
nRet = 1;
if ( isCrash() ) {
m _ gris _ y = m _ juego de billar _ h * 2
p>
p>
m _ over _ ST = 0
m _ state = ST _ OVER
} En caso contrario {
m _ estado = ST _ NORMAL
}
} else if (m_state == ST_NORMAL) {
/*Manejar la caída libre*/
int i, j;
if(m_key state[3]= = 0 | | m_body_down){
- m_curtime, m_cursubtime = 1;
}
if ( m_curxtime ) {
if(m _ curxtime & lt; 0)
m _ curxtime++
Otro
m_curxtime-;
}
/*Procesamiento de claves*/
for (i = 1, j = 1;i<=2 ;++i,j-=2) {
for(;m_key flag[I]>0;- m_KeyFlag[i]) {
m _ CTL _ x- = j;
if (isCrash())
m _ CTL _ x+= j
Otros
m _ curxtime =; m _ mov xtime * j;
}
}
m _ CTL _ dx = float(double( m_curxtime)/m_mov xtime);//Procesamiento suavizado en la dirección X
for (i = 4, j = 1; i<=5;++i, j-=2) {
for(int t;m _ indicador clave[I]>0;- m_KeyFlag[i]) {
m _ CTL _ t =((t = m _ CTL_t)+g_map_mod[m_CTL_s]+j)% g_map_mod[m_CTL_s] ;
if(is crash())m_ CTL_t = t;
}
}
if(m_disabled_off== 0 & amp& amp(m_KeyState[3] ) ) {
m _ curtime-= m _ cursubtime++
}
if(m _ curtime & lt; 0 ) {
+ + m _ CTL _ y;
if ( isCrash() ) {
-m _ CTL _ y;
merge();
m _ CTL _ dy = 0; m _ CTL _ dx = 0; m _ CTL _ t =-1; ])
m _forbidden_down = 1
m _ estado = ST _ SIGUIENTE
} más {
m _
curtime+= m _ tiempo de caída;
}
}
if (m_state == ST_NORMAL) {
m _ CTL _ dy = float(double(m _ curtime)/m _ drop time); //Procesando el suavizado de la dirección Y
}
} else if (m_state == ST_OVER) {
if(m _ grey _ y & gt; 0 & amp& amp(m_gray_y % 2) == 0)
for(int x = 1; x & lt= m _ juego de billar _ w;++x)
if(m _ juego de billar[m _ grey _ y & gt;& gt1][x])
m _ juego de billar[m _ grey _ y>& gt1][x]= 8;
m _ gris _ y-;
++ m _ sobre _ ST;
if( m_key flag[0]>0)
m_state = ST_START
}
memset(m_KeyFlag, 0, sizeof (m _ key flag));
Devolver nRet
}
/*Detección de colisiones*/
bool isCrash() {
for( int y = 0; y & lt4;++y) {
for(int x = 0; x & lt4;++x)
if(g _ TRS _ map [m_CTL_s][m_CTL_t][y][x]){
if(m_CTL_y+y & lt; 0 | | m_CTL_x+ x & lt; 0
|
Devuelve verdadero
}
}
Devuelve falso
}
void merge() {
int y, x, cy = m _ game pool _ h
/*Procesamiento de fusión*/
for(y = 0; y & lt4;++y) {
for(x = 0;x & lt4;++x)
if(g _ TRS _ map[m _ CTL _ s][m _ CTL _ t][y][x])
m _ juego billar[m _ CTL _ y+y][m _ CTL _ x+x]
= g_TRS_map[m_CTL_s][m_CTL_t][y][x];
}
/*Cálculo de eliminación de filas*/
for(y = m _ game pool _ h; y & gt= 1;- y) {
for(x = 1 ; x & lt= m _ grupo de juegos _ w; + +x) {
if (m_gamepool[y][x] == 0)
Descanso;
}
if( x <= m_gamepool_w ) {
if (cy! = y ) {
for(x = 1; x & lt= m _ juego de billar _ w; ++x)
m _ g
ame grupo[cy][x]= m_juego grupo[y][x];
}
-cy;
}
}
for(y = cy; y & gt= 1;- y) {
for(x = 1; x & lt= m _ juego de billar _ w ;+ +x)
m _ game pool[y][x]= 0;
}
}
/* Actualización lógica función principal*/
void update(int k) {
And (k & amp& amp(k = getch(1)) ) {
for( int I = 0;i<8;++i) {
if ((k & amp0x ffff)=(m _ Keys[I]& amp;0xFFFF)) {
Si (k & ampKEYMSG_DOWN) {
m _ key flag[I]++;
m _ key state[I]= 1;
} else if (k & KEYMSG_UP) {
m _ indicador de clave[I]= 0
m _ estado de clave[I]=
if (i) == 3)
m_disable_drop = 0;
}
}
}
k = kb hit (1);
}
mientras(acuerdo());
}
borde dibujado nulo(int x, int y, int w, int h, int color, int bdark = 1) {
setcolor(getchangcolor(color, 1.5f));
line(x,y+h,x, y);
lineto(x+w,y);
if (bdark)
setcolor(getchangcolor (color, 0.7f)); p>
lineto(x+w, y+h);
lineto(x, y+h
}
void drawtile( int x, int y, int w, int h, int d, int color) {
w -, h-;
setfillstyle (color)
bar(x+1, y+1, x+w, y+h);
dibujo(x, y, w, h, color
dibujo(x); +1, y+1, w-2, h-2, color);
}
manuar vacío(int x, int y, int w, int h, int d = 0) {
int coll[] = {0x400040, 0x600060, 0xA000A0, 0xFF00FF,
0xA000A0, 0x600060, 0x 400040}; 010101);
bar(x, y, x + w -, y+h-);
for(int I = 0 ;i<7;++i) { p>
- x, - y, w += 2, h+= 2;
dibujo(x, y, w, h, coll [i], 0);
}
}
void draw44(int bx, int by, int
mat[4][4],
float dx=0, float dy=0, int nc=0, int deep=5) {
for(int y = 3; y & gt= 0;- y) {
for (int x = 0, c; x & lt4;++x) {
if ( c = mat[y] [x] ) {
if(NC)c = NC;
dibujar mosaico(int(bx+(x+dx)* m _ base _ w+1000.5)-1000,
int(by+(y-dy)* m _ base _ h+1000.5)-1000,
m_base_w, m_base_h, profundo,
m _ mapa de colores [c]);
}
}
}
}
void drawfps() {
Cadena estática[100];
Static int n_time = 0, fps = 0;
establecer color(0x ffffff);
setbkmode(transparent);
Sprintf(str, "Velocidad de fotogramas %.2f fps", obtener fps());
outtextxy(0, 0, str);
p>
}
/*Función principal de actualización gráfica*/
void render() {
int x, y, c , bx, por , profundo = 5;
/*Dibujar marco de fondo*/
borrar dispositivo();
drawframe( m_base_x + 5 * m_base_w
m_base_y,
m_gamepool_w * m_base_w
m _ game pool _ h * m _ base _ h
manuar (m_base_x, m_base_y, 4* m_base_w, 4*m_base_h);
drawframe(m_base_x, m_base_y + 5*m_base_h, 4*m_base_w, 4*m_base_h);
/* Extrae el juego principal pool*/
bx = m_base_x+4 * m_base_w;
by = m_base_y-1 * m_base_ h;
for(y = m _ juego de billar _ h; y & gt= 1;- y) {
for(x = 1; x & lt= m _ game pool_w;++x) {
if ( c = m_gamepool[y][x])
putimage(bx + x * m_base_w, por + y * m_base_h,
m_base_w, m_base_h, m_pcb,
c * m_base_w, 0);
}
}
/ *Dibujar bloque de control*/
if(m _ CTL _ t & gt;=0) {
bx = m_base_x+(m_CTL_x+4) * m_base_w;
by = m_base_y+(m_CTL_y-1)* m_base_h ;
draw44(bx, por, g_trs_map[m_ct
l_s][m_ctl_t], m_ctl_dx, m_CTL_dy);
}
/*Dibuja una pieza de ajedrez y dos piezas de ajedrez*/
bx = m_base_x;
por = m_base_y;
draw44(bx, por, g_TRS_map[m_next 1_s][ 0]);
bx = m_base_x;
by = m_base_y+5 * m_base_h;
draw44 (bx, by, g_trs_map[m_next2_s][0], 0, 0, 8
establecer color(0x ffffff) ;
If (m_state == ST_OVER) {/ /Finalizar visualización del texto del mensaje.
outtextxy(m_base_x+5*m_base_w, m_base_y, "Presiona F2 para reiniciar el juego");
}
dibuja fps(); >
}
static int reparto(int a, float dt) {
a = int(a * dt);
if (a & gt255) a = 255;
de lo contrario si(a & lt; 0)a = 0
Devuelve a
}
static int getchangcolor(int Color, float t) {
int r = GetRValue(Color), g = GetGValue(Color), b = getb valor(Color);
r = tratobit (r, t);
g = tratobit(g, t);
b = tratobit(b, t
Devolver RGB(); r, g, b);
}
Público:
int m_base_x, m_base_y, m_base_w, m_base_h
int m _; droptime
int m_curtime
int m_cursubtime
int m_movxtime
int m_curxtime
Privado:
int m_gamepool_w, m_game pool_h;
int m_game pool[30][30]; //Comience desde 1, use 0 Detección de colisión de límites.
int m_ctl_x, m_ctl_y, m_ctl_t, m_CTL_s; //Atributos del bloque de control actual
float m_ctl_dx, m_ctl_dy
int m_next1_s, m_next 2_s;
int m_body_down;
int m_colormap[10];
Público:
int m_pause
int m_state //Estado principal del juego
int m _ grey _ y
int m _ over _ st
int m _ Keys[8]; >
int m _ key flag[8];
int m _ key state[8]
Privado:
IMAGEN * m _ pcb;
};
int main() {
int fps = 200
setinitmode(0
initgr();
Juego juego(10, 20, 24, 24
juego .m_base_x = 20
juego.m_base_y = 20); p>
juego .m_drop tiempo = fps/2;
juego m _ mov xtime = 10
BeginBatchDraw(); int k; (k = kbhit(1))!= -1;delay_fps(fps)) {
actualización del juego(k); /p>
}
EndBatchDraw();
Devuelve 0;
}
Este es el código fuente de Tetris en Lenguaje C, espero que se pueda utilizar.