Red de conocimiento informático - Material del sitio web - Código fuente del juego FPS en línea

Código fuente del juego FPS en línea

#incluye "graphics.h"

# incluye lttime.h gt

# incluye ltstdio.h gt

# incluye ltstring.h gt

const int g _ ancho = 400

const int g _ alto = 520

/*Tabla utilizada para registrar 7 formas y 4 variaciones*/

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},

}, {

{ 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},

}, }, {{

{0, 0, 0, 0}, {0, 7, 7, 0}, {0, 7, 7, 0},

}, },

};

memcpy(g_trs_map, trs_map, tamaño

of(Trs_map));

}

Juego de clase{

Público:

/*Tabla de estado*/

Enum {

ST_START, /*El juego se reinicia*/

ST_NEXT, /*Prepara el siguiente cuadro*/

ST_NORMAL, /*Jugador fase de control*/

ST_OVER /*El juego termina, F2 comienza de nuevo*/

};

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 Keys[8] = {VK_F2, VK_LEFT, VK_RIGHT, VK_DOWN, VK_UP, VK_NUMPAD0, VK _ SPACE};

memcpy(m_Keys, Claves, tamaño de (Claves));

memset(m_KeyState, 0, tamaño de (m _ estado de clave)); gamepool _ w = w

m _ gamepool _ h = h

m _ base _ w = bw

m _ base _ h = bh

aleatorio();

m _ CTL _ t =-1;

m_pcb = nueva imagen

for(int I = 0 ; ilt10; i) {

drawtile(bw * i, 0, bw, bh, 5, mapa de colores[I]); , 0, 0, bw*10, BH);

m _ state = ST _ START

}

/*Procesamiento de transición de estado*/

int reparto() {

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));

for(int y = 1; y lt= m _ grupo de juegos _ h; y) {

para (int x = 1; x lt= m _ juego de billar _ w; x)

m _ juego de billar[y][x]= 0;

}

m_disable_down = 0;

m _ CTL _ t = -1

nRet = 1

m _ estado = ST _ NEXT

} si no (m_state == ST_NEXT) {

m _ CTL _ x = (m _ juego de billar _ w-4)/2 1

m _ CTL _ y = 1

m _ CTL _ t; = 0;

m_CTL_s = m_siguiente 1_s;

m_CTL_dy = 0;

m_siguiente 1_ s = m_siguiente 2 _ s

>m _ next 2 _ s = random(7) 1

m _ curtime = m _ droptime

m _ curxtime = 0

nRet = 1;

if ( isCrash() ) {

m _ gris _ y = m _ juego de billar _ h * 2; = 0;

m_estado = 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 ;

p>

Otros

m _ curxtime-;

}

/*Procesamiento de claves*/

for (i = 1, j = 1; ilt=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; p>}

}

m _ CTL _ dx = float(double( m _ curxtime)/m _ mov xtime); // Procesamiento de suavizado de dirección X

for (i = 4, j = 1; i lt=5; i, j-=2) {

for(int t; m _ key flag[I] gt; 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(es bloqueo ())m_CTL_t = t;

}

}

if(m_disable_close= = 0 amperios 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_t =-1;

if ( m_KeyState[3])

m_disable_down = 1;

m_state = ST _ NEXT

}De lo contrario {

m _ curtime = m _ tiempo de entrega

}

}

if (m_state == ST_NORMAL) {

m _ CTL _ dy = float(double(m _ curtime)/m _ drop time //Procesamiento del 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 _ gris _ y gt; gt1][x])

m _ juego de billar [m_gris_y gt;x]= 8;

m_gris_y-

m_sobre_ST;

if(m _ key flag[0] gt; 0)

m _ state = ST _ START

}

memset(m_KeyFlag, 0, sizeof( m _ key flag));

Return 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

| p>

}

}

Devuelve falso

}

fusión nula ( ) {

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_juego pool_w; >}

if(x lt; = m_gamepool_w) {

If (cy!= y) {

for(x = 1; x lt= m _ juego de billar _ w; x)

m _ juego de billar[cy][x]= m _ juego de billar[y][x];

}

-cy;

}

}

for(y = cy; y gt= 1 ;- y) {

for(x = 1; x lt= m _ juego de billar _ w; x)

m _ juego de billar[y][x]= 0;

}

}

/*Función principal de actualización lógica*/

actualización vacía(int k) {

Y (k amp amp(k = getch( 1)) ) {

for(int I = 0; i lt8; i) {

if ((k amp0x ffff )=(m _ Keys[I] amp; 0xFFFF )) {

If (k ampKEYMSG_DOWN) {

m _ key flag[I];

m _ estado clave[I]= 1;

p>

} else if(k amp; KEYMSG_UP) {

m _ indicador clave[I]= 0; /p>

m _ estado clave[I]= 0;

si (i == 3)

m_disable_drop = 0;

}

}

}

k = kb hit(1);

}

mientras(acuerdo() );

}

void drawedge(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));

lineto(x w, y h);

lineto(x, y h); p>

}

void drawtile(int x, int y, int w, int h, int d, int color) {

w -, h-;

setfillstyle(color);

barra(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};

setfillstyle(0x 010101);

bar(x, y, x w -, y h-);

for(int I = 0; ilt7; i) {

- x, - y, w = 2, h = 2;

p>

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(por (y-dy)* m_base_h 1000.5)-1000,

m_base_w, m_base_h, profundo,

m_colormap[ c] );

}

}

}

}

void drawfps() {

Cadena estática[100];

Static int n_time = 0, fps = 0;

establecer color(0x ffffff);

setbkmode(transparente );

Sprintf(str, "Frame rate.2f fps", obtener fps());

outtextxy(0, 0, str);

}

/*Función principal de actualización gráfica*/

void render() {

int x, y, c, bx, by, deep = 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 _ juego de billar _ h * m _ base _ h

manuar(m_base_x, m_base_y, 4*m_base_w, 4 *m_base_h) ;

manuar(m_base_x, m_base_y 5*m_base_h, 4*m_base_w, 4 *m_base_h);

/*Extrae el grupo de juego principal*/

bx = m _ base _ x 4 * m _ base _ w

by = m _ base _ y-1 * m _; base _ h;

for(y = m _ juego billar _ h; y gt= 1; - y) {

for(x = 1; x lt= m _ juego grupo_w; >m_base_w, m_base_h, m_pcb,

c * m_base_w,

}

}

/*); Bloque de control de dibujo* /

if(m_CTL_t gt;=0) {

bx = m_base_x (m_CTL_x4)* m_base_w

by = m_base_y (m_CTL_y-; 1)* m_base_h;

draw44(bx, by, g_trs_map[m_ctl_s][m_ctl_t] , m_ctl_dx, m_CTL_dy);

}

/*Dibuja uno pieza de ajedrez y dos piezas de ajedrez*/

bx = m _ base _ x;

by = m_base_y;

draw44(bx, by, 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);

set 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;

}

int estático getchangcolor( int Color, float t) {

int r = GetRValue(Color), g = GetGValue(Color), b = getb valor(Color);

r = dealbit( r, t);

g = acuerdobit(g, t);

b = acuerdobit(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_gamepool_h;

int m_gamepool[30][30]; //Comienza con 1, 0 para la detección de colisiones 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

);

juego .m_drop tiempo = fps/2;

juego .m _ mov xtime = 10

BeginBatchDraw(); ; (k = kbhit(1))!= -1; delay_fps(fps)) {

actualización del juego(k); >

}

EndBatchDraw();

Devuelve 0;

}

Este es el código fuente de Tetris en lenguaje C. , espero que se pueda utilizar.