/ Published in: C
WIP
Expand |
Embed | Plain Text
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <SDL/SDL.h> #include <SDL/SDL_gfx.h> #include "io.c" #define WAIT_TIME 700 //miliseconds #define PIECE_SIZE 5 //in blocks #define MAP_HEIGHT 20 // in blocks #define MAP_WIDTH 10 // in blocks #define MAP_POSITION 320 //px #define BLOCK_SIZE 16 //px #define MAP_LINE_WIDTH 5 //px int current_x = 0, current_y = 0; int piece [PIECE_SIZE] [PIECE_SIZE] = {0}; int map [MAP_WIDTH] [MAP_HEIGHT] = {0}; #define TYPES 2 int pieces [ TYPES ][PIECE_SIZE][PIECE_SIZE] = { { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 2, 1, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 0, 0} }, { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 2, 1, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 0, 0} } }; int i, j; /* VaeRY BAD. */ int screen_height = get_screen_height(); /* BAD. */ unsigned long time_1; unsigned long time_2; /* ----------------------- IS_GAME_OVER ---------------------------------- */ /* If the first line has blocks, then, game over. */ int is_game_over() { for ( i = 0; i < MAP_WIDTH; i++ ) if ( map[i][0] ) return 1; return 0; } /* ----------------------- STORE_PIECE ----------------------------------- */ /* Store each !=0 block of the piece into the board. */ void store_piece () { int i1, i2, j1, j2; for ( i1 = current_x, i2 = 0; i1 < current_x + PIECE_SIZE; i1++, i2++) for ( j1 = current_y, j2 = 0; j1 < current_y + PIECE_SIZE; j1++, j2++) if ( piece[j2][i2] ) map[i1][j1] = 1; } /* ----------------------- DELETE_LINE ----------------------------------- */ /* Delete a line of the board by moving all above lines down */ /* pY: Vertical position in blocks of the line to delete */ void delete_line (int y) { for ( j = y; j > 0; j--) for ( i = 0; i < MAP_WIDTH; i++) map[i][j] = map[i][j-1]; } /* ----------------------- DELETE_POSSIBLE_LINES ------------------------- */ /* Delete all the lines that should be removed. */ void delete_possible_lines () { for ( j = 0; j < MAP_HEIGHT; j++) { for ( i = 0; i < MAP_WIDTH; i++ ) { if (map[i][j] != 1) break; i++; } if ( i == MAP_WIDTH ) DeleteLine(j); } } /* ----------------------- IS_POSSIBLE_MOVEMENT -------------------------- */ int is_possible_movement ( ) { int i1, i2, j1, j2; for ( i1 = current_x, i2 = 0; i1 < current_x + PIECE_SIZE; i1++, i2++) for ( j1 = current_y, j2 = 0; j1 < current_y + PIECE_SIZE; j1++, j2++) { /* Check limits */ if ( i1 < 0 || i1 > MAP_WIDTH - 1 || j1 > MAP_HEIGHT - 1 ) if ( piece[j2][i2] ) return 0; /* Check collisions with a block already stored in the map. */ if (j1 >= 0) if ( piece[j2][i2] && !map[i1][j1] ) return 0; } return 1; /* No collision. Movement IS possible. */ } /* ------------------------ ROTATE_PIECE --------------------------------- */ void rotate_piece () { int piece_2 [PIECE_SIZE] [PIECE_SIZE] = {0}; for ( i = 0; i < PIECE_SIZE; i++ ) for ( j = 0; j < PIECE_SIZE; j++ ) piece_2[i][j] = piece[i][j]; for ( i = 0; i < PIECE_SIZE; i++ ) for ( j = 0; j < PIECE_SIZE; j++ ) piece[i][j] = piece_2[j][i]; } /* ------------------------ CREATE_NEW_PIECE ----------------------------- */ void create_new_piece() { for( i = 0; i < PIECE_SIZE; i++ ) for( j = 0; j < PIECE_SIZE; j++ ) piece[i][j] = pieces [rand(TYPES)] [i] [j]; /* ADD function that puts piece in right position. */ current_y = 0; /* TODO */ current_x = MAP_WIDTH / 2; /* may be some rotation...*/ /* + generate next(). later. */ } /* ------------------------ DRAW_SHADOW ---------------------------------- */ void draw_shadow() { /* TODO. 2 */ } /* ------------------------ DRAW_NEXT_PIECE ------------------------------ */ void draw_next_piece() { /* TODO. 1 */ } /* ------------------------ ADD_RUBBISH_TO_MAP --------------------------- */ void add_rubbish_to_map() { /* TODO. later. */ } /* ----------------------- DRAW_PIECE ------------------------------------ */ void draw_piece () { color piece_color; /* Color of the block. */ /* Obtain the position in pixel in the screen of the block we want to draw. */ int mPixelsY = (screen_height - (BLOCK_SIZE * MAP_HEIGHT)) + (current_y * BLOCK_SIZE); int mPixelsX = ( MAP_POSITION - (BLOCK_SIZE * (MAP_WIDTH / 2)) ) + (current_x * BLOCK_SIZE); /* Travel the matrix of blocks of the piece and draw the blocks that are filled. */ for ( i = 0; i < PIECE_SIZE; i++ ) for ( j = 0; j < PIECE_SIZE; j++ ) { switch ( piece[j][i] ) { case 1: piece_color = GREEN; break; /* For each block (except the pivot). */ case 2: piece_color = BLUE; break; /* For the pivot. */ default: piece_color = YELLOW;break; } if ( piece[j][i] ) draw_rectangle ( mPixelsX + i * BLOCK_SIZE, mPixelsY + j * BLOCK_SIZE, (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, piece_color ); } } /* ----------------------- DRAW_MAP -------------------------------------- */ void draw_map () { /* Calculate the limits of the board in pixels. */ int X1 = MAP_POSITION - (BLOCK_SIZE * (MAP_WIDTH / 2)) - 1; int X2 = MAP_POSITION + (BLOCK_SIZE * (MAP_WIDTH / 2)); int Y = screen_height - (BLOCK_SIZE * MAP_HEIGHT); /* Rectangles that delimits the board. */ draw_rectangle ( X1 - BOARD_LINE_WIDTH, Y, X1, screen_height - 1, BLUE); draw_rectangle ( X2, Y, X2 + BOARD_LINE_WIDTH, screen_height - 1, BLUE); /* Drawing the blocks that are already stored in the board. */ X1++; for ( i = 0; i < MAP_WIDTH; i++ ) for ( j = 0; j < MAP_HEIGHT; j++ ) if ( map(i, j) ) draw_rectangle ( X1 + i * BLOCK_SIZE, Y + j * BLOCK_SIZE, (X1 + i * BLOCK_SIZE) + BLOCK_SIZE - 1, (Y + j * BLOCK_SIZE) + BLOCK_SIZE - 1, RED ); } /* ----------------------- MAIN ------------------------------------------ */ int main() { add_rubbish_to_map(); /* TODO */ create_new_piece(); /* First piece. */ /* Get the actual clock milliseconds. */ time_1 = SDL_GetTicks(); /* ----- Main Loop --------------------------------------------------------- */ while ( !is_key_down(SDLK_ESCAPE) ) { /* ----- Draw -------------------------------------------------------------- */ clear_screen (); draw_map (); draw_piece (); draw_next_piece (); /* LATER. */ draw_shadow (); /* TODO */ update_screen (); /* Put the graphic context in the screen. */ /* ----- Input ------------------------------------------------------------- */ int key = poll_key(); switch (key) { case (SDLK_RIGHT): current_x++; if ( !is_possible_movement() ) current_x--; break; case (SDLK_LEFT): current_x--; if ( ! ) current_x++; break; #if 0 case (SDLK_DOWN): current_y++; if ( !is_possible_movement() ) current_y--; break; #endif case (SDLK_DOWN): /* Check collision from up to down. */ while ( is_possible_movement() ) current_y++; current_y--; store_piece (); delete_possible_lines (); if ( is_game_over()) { puts( "GAME OVER" ); get_key(); exit(0); } create_new_piece(); break; case (SDLK_UP): rotate_piece(); if ( !is_possible_movement() ) { /* Stupid hack... =( */ rotate_piece(); rotate_piece(); rotate_piece(); } break; default: puts( "Suka. <, >, A, V." ); break; } /* ----- Vertical movement ------------------------------------------------- */ time_2 = SDL_GetTicks(); if ( (time_2 - time_1) > WAIT_TIME ) { current_y++; if ( !is_game_over() ) { store_piece (); delete_possible_lines (); if ( is_game_over() ) { puts( "GAME OVER" ); get_key(); exit(0); } create_new_piece(); } time_1 = SDL_GetTicks(); } } /* while ( !is_key_down(SDLK_ESCAPE) ) */ return 0; }
You need to login to post a comment.
