Advertisement
shywolf91

Tetris updated 5-19 (all shapes)

May 19th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.70 KB | None | 0 0
  1. //Dark GDK - The Game Creators - www.thegamecreators.com
  2.  
  3. //the wizard has created a very simple project that uses Dark GDK
  4. //it contains the basic code for a GDK application
  5.  
  6. //whenever using Dark GDK you must ensure you include the header file
  7. #include "DarkGDK.h"
  8.  
  9.  
  10. //Global
  11. int Block_Size=20;
  12. int grid[20][10];
  13. //colors
  14. const DWORD Red = dbRGB(255,0,0);
  15. const DWORD Green = dbRGB(0,255,0);
  16. const DWORD Blue = dbRGB(0,0,255);
  17. const DWORD Magenta = dbRGB(255,0,255);
  18. const DWORD Black = dbRGB(0,0,0);
  19. const DWORD White = dbRGB(255,255,255);
  20. const DWORD Yellow = dbRGB(255,255,0);
  21. const DWORD Cyan = dbRGB(0,255,255);
  22. const DWORD Orange = dbRGB(255,165,0);
  23. const DWORD Grey = dbRGB(177,177,177);
  24. //end colors
  25. //end global
  26.  
  27. //Classes
  28.  
  29. class Block {
  30. private:
  31.     DWORD color;
  32.     //int blocks[4];
  33. public:
  34.     int x,y;
  35.     Block();
  36.     Block(int, int, DWORD);
  37.     void draw();
  38.     void move(int, int);
  39.     void clear();
  40.     //bool col();
  41.  
  42. };
  43. Block::Block(){
  44. x = 0;
  45. y =0;
  46. color = White;
  47. }
  48. //Methods (Block)
  49.  
  50. Block::Block(int X, int Y, DWORD COLOR)
  51. {
  52.     x=X;
  53.     y=Y;
  54.     color=COLOR;
  55. }
  56.  
  57. void Block::draw()
  58. {
  59.     int x1, y1, x2, y2;
  60.     x1=x*Block_Size;
  61.     y1=y*Block_Size;
  62.     x2=x1+Block_Size;
  63.     y2=y1+Block_Size;
  64.  
  65. //  dbInk(color, Black);
  66.     dbBox(x1,y1,x2,y2);
  67. }
  68.  
  69. void Block::clear()
  70. {
  71.  
  72.     dbInk(Black, Black);
  73.     //dbBox(x1,y1,x2,y2);
  74.     draw();
  75. }
  76.  
  77. void Block::move(int dx, int dy)
  78. {
  79.     x=x+dx;
  80.     y=y+dy;
  81.     dbInk(color, Black);
  82.     draw();
  83. }
  84.  
  85. //End Method (Block)
  86.  
  87. class Shape {
  88.  
  89. private:
  90.     Block blocks[4];
  91.     //int pos[8];
  92. public:
  93.     int pos[8];
  94.     DWORD color;
  95.     Shape();
  96.     void make_ishape();
  97.     void make_oshape();
  98.     void make_jshape();
  99.     void make_lshape();
  100.     void make_tshape();
  101.     void make_zshape();
  102.     void make_sshape();
  103.     void move_shape(int,int);
  104.     void draw_shape();
  105.     bool col(int,int);
  106.  
  107.    
  108.  
  109.    
  110. };
  111. //Methods (Shape)
  112. Shape::Shape() {
  113. blocks[0] = Block(0,0, White);
  114. blocks[1] = Block(0,0, White);
  115. blocks[2] = Block(0,0, White);
  116. blocks[3] = Block(0,0, White);
  117. }
  118. bool Shape::col(int dx, int dy)
  119. {
  120.     for (int i=0; i<=4; i++)
  121.     {
  122.         if (blocks[i].y + dy > 19 || blocks[i].x + dx < 0 || blocks[i].x +dx > 9)
  123.         {
  124.             return false;
  125.         }
  126.        
  127.     }
  128.     return true;
  129. }
  130.  
  131.  
  132. void Shape::make_ishape()
  133. {
  134.     blocks[0] = Block(pos[0],pos[1],Blue);
  135.     blocks[1] = Block(pos[2],pos[3],Blue);
  136.     blocks[2] = Block(pos[4],pos[5],Blue);
  137.     blocks[3] = Block(pos[6],pos[7],Blue);
  138.    
  139. }
  140. void Shape::make_oshape()
  141. {
  142.     blocks[0] = Block(pos[0],pos[1],Green);
  143.     blocks[1] = Block(pos[2],pos[3],Green);
  144.     blocks[2] = Block(pos[4],pos[5],Green);
  145.     blocks[3] = Block(pos[6],pos[7],Green);
  146.    
  147. }
  148. void Shape::make_jshape()
  149. {
  150.     blocks[0] = Block(pos[0],pos[1],Yellow);
  151.     blocks[1] = Block(pos[2],pos[3],Yellow);
  152.     blocks[2] = Block(pos[4],pos[5],Yellow);
  153.     blocks[3] = Block(pos[6],pos[7],Yellow);
  154.    
  155. }
  156. void Shape::make_lshape()
  157. {
  158.     blocks[0] = Block(pos[0],pos[1],Orange);
  159.     blocks[1] = Block(pos[2],pos[3],Orange);
  160.     blocks[2] = Block(pos[4],pos[5],Orange);
  161.     blocks[3] = Block(pos[6],pos[7],Orange);
  162.    
  163. }
  164. void Shape::make_tshape()
  165. {
  166.     blocks[0] = Block(pos[0],pos[1],Cyan);
  167.     blocks[1] = Block(pos[2],pos[3],Cyan);
  168.     blocks[2] = Block(pos[4],pos[5],Cyan);
  169.     blocks[3] = Block(pos[6],pos[7],Cyan);
  170.    
  171. }
  172. void Shape::make_zshape()
  173. {
  174.     blocks[0] = Block(pos[0],pos[1],Red);
  175.     blocks[1] = Block(pos[2],pos[3],Red);
  176.     blocks[2] = Block(pos[4],pos[5],Red);
  177.     blocks[3] = Block(pos[6],pos[7],Red);
  178.    
  179. }
  180. void Shape::make_sshape()
  181. {
  182.     blocks[0] = Block(pos[0],pos[1],Magenta);
  183.     blocks[1] = Block(pos[2],pos[3],Magenta);
  184.     blocks[2] = Block(pos[4],pos[5],Magenta);
  185.     blocks[3] = Block(pos[6],pos[7],Magenta);
  186.    
  187. }
  188. void Shape::draw_shape()
  189. {
  190.     for (int i=0; i<4; i++)
  191.     {
  192.         blocks[i].draw();
  193.     }
  194. }
  195.  
  196. void Shape::move_shape(int dx, int dy)
  197. {
  198.     for (int i=0; i<4; i++)
  199.     {
  200.         blocks[i].clear();
  201.     }
  202.     for (int i=0; i<4; i++)
  203.     {
  204.         blocks[i].move(dx,dy);
  205.     }
  206. }
  207.  
  208.  
  209. class I_Shape: public Shape{
  210. public:
  211.     I_Shape(int,int);
  212.    
  213. private:
  214.     DWORD color;
  215.     //int pos[8];
  216. };
  217.  
  218. //Methods (I_Shape)
  219.  
  220.  
  221. I_Shape::I_Shape(int x, int y):Shape()
  222. {
  223.  
  224.     color=Blue;
  225.     pos[0]=x-1;
  226.     pos[1]=y;
  227.     pos[2]=x;
  228.     pos[3]=y;
  229.     pos[4]=x+1;
  230.     pos[5]=y;
  231.     pos[6]=x+2;
  232.     pos[7]=y;
  233.     make_ishape(); //makes I_Shape
  234. }
  235.  
  236.  
  237. class O_Shape: public Shape{
  238. public:
  239.     O_Shape(int,int);
  240. private:
  241.     DWORD color;
  242. };
  243. O_Shape::O_Shape(int x, int y):Shape()
  244. {
  245.     color=Red;
  246.     pos[0]=x;
  247.     pos[1]=y;
  248.     pos[2]=x;
  249.     pos[3]=y-1;
  250.     pos[4]=x+1;
  251.     pos[5]=y-1;
  252.     pos[6]=x+1;
  253.     pos[7]=y;
  254.     make_oshape(); //makes O_Shape
  255. }
  256.  
  257. class J_Shape: public Shape{
  258. public:
  259.     J_Shape(int,int);
  260. private:
  261.     DWORD color;
  262. };
  263. J_Shape::J_Shape(int x, int y):Shape()
  264. {
  265.     color=Yellow;
  266.     pos[0]=x-1;
  267.     pos[1]=y;
  268.     pos[2]=x;
  269.     pos[3]=y;
  270.     pos[4]=x+1;
  271.     pos[5]=y;
  272.     pos[6]=x+1;
  273.     pos[7]=y-1;
  274.     make_jshape(); //makes I_Shape
  275. }
  276.  
  277. class L_Shape: public Shape{
  278. public:
  279.     L_Shape(int,int);
  280. private:
  281.     DWORD color;
  282. };
  283. L_Shape::L_Shape(int x, int y):Shape()
  284. {
  285.     color=Orange;
  286.     pos[0]=x+1;
  287.     pos[1]=y;
  288.     pos[2]=x;
  289.     pos[3]=y;
  290.     pos[4]=x-1;
  291.     pos[5]=y;
  292.     pos[6]=x-1;
  293.     pos[7]=y-1;
  294.     make_lshape(); //makes L_Shape
  295. }
  296.  
  297. class T_Shape: public Shape{
  298. public:
  299.     T_Shape(int,int);
  300. private:
  301.     DWORD color;
  302. };
  303. T_Shape::T_Shape(int x, int y):Shape()
  304. {
  305.     color=Cyan;
  306.     pos[0]=x-1;
  307.     pos[1]=y;
  308.     pos[2]=x;
  309.     pos[3]=y;
  310.     pos[4]=x;
  311.     pos[5]=y-1;
  312.     pos[6]=x+1;
  313.     pos[7]=y;
  314.     make_tshape(); //makes T_Shape
  315. }
  316.  
  317. class Z_Shape: public Shape{
  318. public:
  319.     Z_Shape(int,int);
  320. private:
  321.     DWORD color;
  322. };
  323. Z_Shape::Z_Shape(int x, int y):Shape()
  324. {
  325.     color=Red;
  326.     pos[0]=x-1;
  327.     pos[1]=y;
  328.     pos[2]=x;
  329.     pos[3]=y;
  330.     pos[4]=x;
  331.     pos[5]=y+1;
  332.     pos[6]=x+1;
  333.     pos[7]=y+1;
  334.     make_zshape(); //makes Z_Shape
  335. }
  336.  
  337. /*/class S_Shape: public Shape{
  338. public:
  339.     S_Shape(int,int);
  340. private:
  341.     DWORD color;
  342. };
  343. S_Shape::S_Shape(int x, int y):Shape()
  344. {
  345.     color=Magenta;
  346.     pos[0]=x-1;
  347.     pos[1]=y;
  348.     pos[2]=x;
  349.     pos[3]=y;
  350.     pos[4]=x;
  351.     pos[5]=y+1;
  352.     pos[6]=x-1;
  353.     pos[7]=y+1;
  354.     make_sshape(); //makes S_Shape
  355. }/*/
  356.  
  357. // the main entry point for the application is this function
  358. //(MAIN)
  359. void DarkGDK ( void )
  360. {
  361.     dbInk(White,Black);
  362.     dbBox(0,0,200,400);
  363.    
  364.     //I_Shape First(3,1);
  365.     //O_Shape First(3,1);
  366.     //J_Shape First(3,1);
  367.     //L_Shape First(3,1);
  368.     //T_Shape First(3,1);
  369.     Z_Shape First(3,1);
  370.    
  371.     // turn on sync rate and set maximum rate to 1 fps
  372.     dbSyncOn   ( );
  373.     dbSyncRate (2);
  374.     First.draw_shape();
  375.     //First.move_shape(5,4);
  376.    
  377.  
  378.     // our main loop
  379.     while ( LoopGDK ( ) )
  380.     {
  381.        
  382.        
  383.             if (dbLeftKey())
  384.             {
  385.                 if (First.col(-1,0))
  386.                 {  
  387.                 First.move_shape(-1,0);
  388.                 }
  389.             }
  390.             if (dbRightKey())
  391.             {
  392.                 if (First.col(1,0))
  393.                 {
  394.                 First.move_shape(1,0);
  395.                 }
  396.             }  
  397.             if (dbDownKey())
  398.             {
  399.                 if (First.col(0,1))
  400.                 {
  401.                 First.move_shape(0,1);     
  402.                 }
  403.             }
  404.         // update the screen
  405.         dbSync ( );
  406.         dbWaitKey();
  407.     }
  408.    
  409.    
  410.     // return back to windows
  411.     return;
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement