Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <unistd.h>
- #include <time.h>
- #include "primlib.h"
- #include "pieces.h"
- /** width of the playable area, in basic square units (not pixels), it has to be a positive integer multiplication of 5 (5,10,15...) */
- #define BOARDWIDTH 10
- /** height of the playable area, in basic square units (not pixels), it has to be a positive integer multiplication of 12 (12,24,36,...) */
- #define BOARDHEIGHT 24
- /*####################################################################################################################################################*/
- int board[BOARDHEIGHT+2][BOARDWIDTH+2];
- int UNIT = 480/BOARDHEIGHT;
- struct useful
- {
- int flag;
- int rotation;
- int type;
- int rot_next;
- int type_next;
- } useful;
- void initialdraw();
- void borderdraw();
- void movement();
- void update_board();
- int checkrow();
- void shiftall(int);
- int spawnbrick();
- int random_num (float);
- /*####################################################################################################################################################*/
- int main(int argc, char *argv[])
- {
- int i,j;
- useful.flag = 0;
- useful.rot_next = random_num(4);
- useful.type_next = random_num(7);
- for(i=0;i<BOARDHEIGHT;i++)/* setting the main board array to zero*/
- {
- for(j=1;j<=BOARDWIDTH;j++)
- {
- board[i][j]=0;
- }
- }
- for(i=BOARDHEIGHT;i<BOARDHEIGHT+2;i++)/* setting the rows outside to non-zero values, so that the move_down function can refer to them as filled and stop when the ground is reached*/
- {
- for(j=0;j<BOARDWIDTH + 2;j++)
- {
- board[i][j]=3;
- }
- }
- for (i = 0; i < BOARDHEIGHT; i++)/* setting the invisible columns to the left and right to 3 */
- {
- board [i][0] = 3;
- board [i][BOARDWIDTH + 1] = 3;
- }
- if (initGraph())
- exit(3);
- for(;;)
- {
- if(isKeyDown(SDLK_ESCAPE) || useful.flag == 1)
- {
- break;
- }
- if (spawnbrick())
- initialdraw();
- else break;
- movement();
- for (i = 1; i <= BOARDHEIGHT; i++)
- {
- j = checkrow();
- if (j) shiftall(j - 1);
- }
- printf ("\n");
- }
- sleep(1);
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
- textout (screenWidth()/2, screenHeight()/2, "GAME OVER!", RED);
- updateScreen();
- getkey();
- return 0;
- }
- /*####################################################################################################################################################*/
- /** function drawing the borders */
- void borderdraw()
- {
- int boardshift = (screenWidth()/UNIT-BOARDWIDTH)/2 +BOARDWIDTH % 2;
- filledRect(boardshift*UNIT-5,0,boardshift*UNIT,screenHeight()-1,YELLOW);
- filledRect(screenWidth()-(boardshift*UNIT),0,screenWidth()-(boardshift*UNIT-5),screenHeight()-1,YELLOW);
- }
- /*####################################################################################################################################################*/
- /** function spawning the brick */
- void initialdraw()
- {
- int i,j;
- srand(time(NULL));
- useful.type = useful.type_next;
- useful.rotation = useful.rot_next;
- useful.type_next = random_num(7);
- useful.rot_next =random_num(4);
- borderdraw();
- filledRect(0,0,screenWidth()-1,screenHeight()-1,BLACK);
- if(((useful.type==2 || useful.type==4 || useful.type==5 || useful.type==6) && (useful.rotation==1)) || (useful.type==1 && useful.rotation%2==0) || (useful.type==3 && useful.rotation==3))/** some of pieces would be displayed lower than they should be - failsafe mechanism.. */
- {
- for (i=0;i<3;i++)
- {
- for(j=BOARDWIDTH/2;j<BOARDWIDTH/2+4;j++)
- {
- board[i][j] = pieces[useful.type][useful.rotation][i+1][j-(BOARDWIDTH/2)];
- }
- }
- }
- else
- {
- for (i=0;i<4;i++)
- {
- for(j=BOARDWIDTH/2;j<BOARDWIDTH/2+4;j++)
- {
- board[i][j]+=pieces[useful.type][useful.rotation][i][j-(BOARDWIDTH/2)];
- }
- }
- }
- draw_next();
- update_board();
- for(i=0;i<BOARDHEIGHT + 2;i++)
- {
- for(j=0;j<=BOARDWIDTH+1;j++)
- {
- printf("%d ", board[i][j]);
- }
- printf("\n");
- }
- }
- /*####################################################################################################################################################*/
- /** function moving block down by 1 square */
- int move_down()
- {
- int temp,i,j,possible;
- for (i=BOARDHEIGHT;i>=0;i--)
- {
- for(j=BOARDWIDTH;j>=1;j--)
- {
- if((board[i][j]==1 || board[i][j]==2) && board[i+1][j]==3)
- {
- return;
- }
- }
- for(j=1;j<=BOARDWIDTH;j++)
- {
- if((board[i][j]==1 || board[i][j]==2) && board[i+1][j]==3)
- {
- return;
- }
- }
- }
- for (i = BOARDHEIGHT; i >= 0; i--)
- {
- for(j=BOARDWIDTH;j>0;j--)
- {
- if((board[i][j]==1 || board[i][j]==2))
- {
- temp = board[i][j];
- board[i][j]=0;
- board[i+1][j]=temp;
- }
- }
- }
- update_board();
- return ;
- }
- /*####################################################################################################################################################*/
- void move_left()
- {
- int temp,i,j, possible = 1;
- for (i = 0; i < BOARDHEIGHT; i++)
- {
- for (j = 0; j < BOARDWIDTH + 1; j++)
- {
- if (board[i][j] == 3 && (board [i][j + 1] == 1 || board[i][j + 1] == 2))
- {
- return;
- }
- }
- }
- for (i=BOARDHEIGHT;i>=0;i--)
- {
- for(j=1;j<=BOARDWIDTH;j++)
- {
- if((board[i][j]==1 || board[i][j]==2))
- {
- temp = board[i][j];
- board[i][j]=0;
- board[i][j-1]=temp;
- }
- }
- }
- update_board();
- }
- /*####################################################################################################################################################*/
- void move_right()
- {
- int temp,i,j, possible = 1;
- for (i = 0; i < BOARDHEIGHT; i++)
- {
- for (j = BOARDWIDTH + 1; j > 0; j--)
- {
- if (board[i][j] == 3 && (board [i][j - 1] == 1 || board[i][j - 1] == 2))
- {
- possible = 0;
- break;
- }
- }
- }
- for (i=0;i<BOARDHEIGHT;i++)
- {
- for(j=BOARDWIDTH-1;j>=0;j--)
- {
- if((board[i][j]==1 || board[i][j]==2) && possible)
- {
- temp = board[i][j];
- board[i][j]=0;
- board[i][j+1]=temp;
- }
- }
- }
- update_board();
- }
- /*
- void rotate(int vert_position, int hori_position, int type, int rotation)
- {
- int i,j;
- for(i=0;i<BOARDHEIGHT;i++)
- {
- for(j=0;j<BOARDWIDTH;j++)
- {
- if(board[i][j]==1 || board[i][j]==2)
- {
- board[i][j]=0;
- }
- }
- }
- for(i=vert_position;i<vert_position+4;i++)
- {
- for(j=hori_position-2;j<hori_position+2;j++)
- {
- if(rotation<3)
- {
- board[i][j] = pieces [type][rotation+1][i-vert_position][j-hori_position+2];
- }
- else if(rotation==3)
- {
- board[i][j] = pieces [type][0][i-vert_position][j-hori_position+2];
- }
- }
- }
- update_board();
- }
- */
- /*####################################################################################################################################################*/
- /** function performing the movement of a block */
- void movement()
- {
- int i, j, k, l, p, key;
- int vert_position=0;
- key = '0';
- while(vert_position<BOARDHEIGHT)
- {
- for(j=0;j<70000000;j++)
- {
- key = pollkey();
- if(isKeyDown(SDLK_ESCAPE))
- {
- useful.flag = 1;
- return ;
- }
- if(key == SDLK_RIGHT)
- {
- move_right();
- for(p=j;p<70000000;p++)
- {}
- move_down();
- break;
- }
- else if(key == SDLK_LEFT)
- {
- move_left();
- for(p=j;p<70000000;p++)
- {}
- move_down();
- break;
- }
- else if (key == SDLK_DOWN)
- {
- for (p = 0; p < BOARDHEIGHT; p++) move_down();
- break;
- }
- else
- {
- for(p=j;p<70000000;p++)
- {}
- move_down();
- break;
- }
- }
- for(i=BOARDHEIGHT;i>0;i--)
- {
- for(j=BOARDWIDTH + 1;j > 0;j--)
- {
- if((board[i][j]==1 || board[i][j]==2) && board[i+1][j]==3)
- {
- for(k=0;k<BOARDHEIGHT;k++)
- {
- for(l=0;l<BOARDWIDTH + 2;l++)
- {
- if(board[k][l]==1 || board[k][l]==2)
- {
- board[k][l]=3;
- }
- }
- }
- return ;
- }
- }
- }
- vert_position++;
- update_board();
- }
- }
- /*####################################################################################################################################################*/
- /** Function goes through all elements of the main (board) array, checking their values, and drawing squares on the corresponding coordinates. If the array element is equal 0, nothing happens, if 1 - a red square is drawn, if 2 - a yellow (pivot) square is drawn, and if 3 = magenta square is drawn. The magenta squares are those, which have already fallen and are no longer moving. */
- void update_board()
- {
- int i,j,x1,y1,x2,y2;
- int boardshift = (screenWidth()/UNIT-BOARDWIDTH)/2 +BOARDWIDTH % 2;/* boardshift - a variable, which determines the horizontal position of the board */
- filledRect(0,0,screenWidth()-1,screenHeight()-1,BLACK);
- borderdraw();/* drawing the borders */
- for(i=0;i<BOARDHEIGHT;i++)
- {
- for(j=1;j<=BOARDWIDTH;j++)
- {
- /* setting the coordinates for the square, they are dependent on the position in array - the visible board is a reflection of this array */
- x1 = ((j-1)*UNIT)+boardshift*UNIT+2;
- y1 = ((i)*UNIT)+2;
- x2 = ((j)*UNIT)+boardshift*UNIT-2;
- y2 = ((i+1)*UNIT)-2;
- /* setting the appropriate colours */
- if(board[i][j]==1)/* normal piece element */
- {
- filledRect(x1,y1,x2,y2, RED);
- }
- if(board[i][j]==2)/* pivot piece element */
- {
- filledRect(x1,y1,x2,y2,YELLOW);
- }
- if(board[i][j]==3)/* elements which have already fallen (unmovable) */
- {
- filledRect(x1,y1,x2,y2,WHITE);
- }
- if(board[i][j]==0)/* the remaining, zero fields, are filled with black (helpful in removing the lines) */
- {
- filledRect(x1,y1,x2,y2,BLACK);
- }
- }
- }
- updateScreen();
- }
- int checkrow(void)
- {
- int i, j, counter = 0;
- for (i = 0; i < BOARDHEIGHT; i ++)
- {
- for (j = 1; j <= BOARDWIDTH; j ++)
- {
- if (board[i][j]) counter++;
- if (counter == BOARDWIDTH) return i + 1;
- }
- counter = 0;
- }
- return 0;
- }
- void shiftall(int row)
- {
- int i, j;
- for (i = row; i >= 0; i--)
- {
- for (j = 1; j <= BOARDWIDTH; j ++)
- {
- if (i == 0)
- {
- board [i][j] = 0;
- continue;
- }
- board[i][j] = board[i - 1][j];
- }
- }
- update_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement