Advertisement
bochkareffsasha

PR6 FINAL

Dec 7th, 2022
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <grxkeys.h>
  4. #include <grx20.h>
  5.  
  6. struct TPoint{
  7.  int x,y;
  8.  int vx,vy;
  9.  int color;
  10.  TPoint(int ix,int iy,int icolor);
  11.  virtual ~TPoint();
  12.  virtual void Draw(int icolor);
  13.  void Show();
  14.  virtual void Hide();
  15.  void MoveXY(int ix, int iy);
  16. };
  17.  
  18. struct TBox: public TPoint{
  19.  int w,h;
  20.  TBox(int ix,int iy,int icolor,int iw,int ih);
  21.  virtual ~TBox();
  22.  virtual void Draw(int icolor);
  23. };
  24.  
  25. struct TCircle: public TPoint{
  26.  int r;
  27.  TCircle(int ix,int iy,int icolor,int ir);
  28.  virtual ~TCircle();
  29.  virtual void Draw(int icolor);
  30. };
  31.  
  32. struct TM: public TPoint{
  33.  int size;
  34.  TM(int ix,int iy,int icolor,int isize);
  35.  virtual ~TM();
  36.  virtual void Hide();
  37.  virtual void Draw(int icolor);
  38. };
  39.  
  40. TPoint::TPoint(int ix,int iy,int icolor)
  41. {
  42.  vx=rand()%9-3;
  43.  vy=rand()%9-3;
  44.  x=ix; y=iy;
  45.  color=icolor;
  46. }
  47.  
  48. TPoint::~TPoint()
  49. {
  50.  Hide();
  51. }
  52.  
  53. void TPoint::Draw(int icolor)
  54. {
  55.  GrFilledCircle(x,y,3,icolor);
  56. }
  57.  
  58. void TPoint::Show()
  59. {
  60.  Draw(color);
  61. }
  62.  
  63. void TPoint::Hide()
  64. {
  65.  Draw(0);
  66. }
  67.  
  68. void TPoint::MoveXY(int ix,int iy)
  69. {
  70.  Hide();
  71.  x=ix; y=iy;
  72.  if(x+3>GrMaxX())
  73.     vx=-vx;
  74.  
  75.  if(x-3<0)
  76.     vx=-vx;
  77.  if(y+3>GrMaxY())
  78.     vy=-vy;
  79.  if(y-3<0)
  80.     vy=-vy;
  81.  Show();
  82. }
  83.  
  84. TBox::TBox(int ix,int iy,int icolor,int iw,int ih):
  85.  TPoint(ix,iy,icolor)
  86. {
  87.  w=iw; h=ih;
  88. }
  89.  
  90. TBox::~TBox()
  91. {
  92.  Hide();
  93. }
  94.  
  95. void TBox::Draw(int icolor)
  96. {
  97.  GrFilledBox(x-w/2,y-h/2,x+(w-w/2),y+(h-h/2),icolor);
  98. }
  99.  
  100. TCircle::TCircle(int ix,int iy,int icolor,int ir):
  101.  TPoint(ix,iy,icolor)
  102. {
  103.  r=ir;
  104. }
  105.  
  106. TCircle::~TCircle()
  107. {
  108.  Hide();
  109. }
  110.  
  111. void TCircle::Draw(int icolor)
  112. {
  113.  GrFilledCircle(x,y,r,icolor);;
  114. }
  115.  
  116. TM::TM(int ix,int iy,int icolor,int isize):
  117.  TPoint(ix,iy,icolor)
  118. {
  119.  size=isize;
  120. }
  121. void TM::Hide()
  122. {
  123. GrLine(x,y,x+0.5*size*4,y-size*4,0);
  124. GrLine(x+0.5*size*4,y-size*4,x+size*4,y,0);
  125. GrLine(x+1,y,x+0.5*size*4+1,y-size*4,0);
  126. GrLine(x+1+0.5*size*4,y-size*4,x+size*4+1,y,0);
  127. GrLine(x+0.3*size*4,y-0.5*size*4,x+0.8*size*4,y-0.5*size*4,0);
  128.  
  129. GrLine(x+size*5,y-size*4,x+size*5,y,0);
  130. GrLine(x+size*5+1,y-size*4,x+size*5+1,y,0);
  131. GrLine(x+size*8,y-size*4,x+size*8,y,0);
  132. GrLine(x+size*8+1,y-size*4,x+size*8+1,y,0);
  133. GrLine(x+size*5,y-0.5*size*4,x+size*8,y-0.5*size*4,0);
  134. GrLine(x+size*5,y-0.5*size*4+1,x+size*8,y-0.5*size*4+1,0);
  135. }
  136.  
  137. TM::~TM()
  138. {
  139.  Hide();
  140. }
  141.  
  142. void TM::Draw(int icolor)
  143. {
  144.  
  145. GrLine(x,y,x+0.5*size*4,y-size*4,color);
  146. GrLine(x+0.5*size*4,y-size*4,x+size*4,y,color);
  147. GrLine(x+1,y,x+0.5*size*4+1,y-size*4,color);
  148. GrLine(x+1+0.5*size*4,y-size*4,x+size*4+1,y,color);
  149. GrLine(x+0.3*size*4,y-0.5*size*4,x+0.8*size*4,y-0.5*size*4,color);
  150.  
  151. GrLine(x+size*5,y-size*4,x+size*5,y,color);
  152. GrLine(x+size*5+1,y-size*4,x+size*5+1,y,color);
  153. GrLine(x+size*8,y-size*4,x+size*8,y,color);
  154. GrLine(x+size*8+1,y-size*4,x+size*8+1,y,color);
  155. GrLine(x+size*5,y-0.5*size*4,x+size*8,y-0.5*size*4,color);
  156. GrLine(x+size*5,y-0.5*size*4+1,x+size*8,y-0.5*size*4+1,color);
  157.  
  158. }
  159.  
  160. #define N 70
  161. main()
  162. {
  163.  GrSetMode(GR_default_graphics);
  164.  TPoint *obj[N];
  165.  srand(time(0));
  166.  int i;
  167.  for(i=1;i<N;i++){
  168.     int flag = rand()%3;
  169.     switch(flag){
  170.         case 0: obj[i]=new TPoint(GrMaxX()/2,GrMaxY()/2,rand()%15+1); break;
  171.         case 1: obj[i]=new TBox(GrMaxX()/2,GrMaxY()/2, rand()%15+1, rand()%20+10, rand()%20+10); break;
  172.         case 2: obj[i]=new TCircle(GrMaxX()/2,GrMaxY()/2,rand()%15+1, rand()%20+10);
  173.     }
  174.     obj[0]=new TM(GrMaxX()/2,GrMaxY()/2, rand()%15+1, rand()%20+10);
  175.     obj[i]->Show();
  176.  }
  177.  GrKeyType key;
  178.  do{
  179.     while (!GrKeyPressed()){
  180.         GrSleep(20);
  181.         for(i=0;i<N;i++){
  182.             obj[i]->MoveXY(obj[i]->x+obj[i]->vx,obj[i]->y+obj[i]->vy);
  183.             if(rand()%50==30){
  184.                 obj[i]->vx=rand()%7-3;
  185.                 obj[i]->vy=rand()%7-3;
  186.             }
  187.         }
  188.     }
  189.  key=GrKeyRead();
  190.  }while(key!=GrKey_Escape);
  191.  
  192.  for(i=0;i<N;i++){
  193.     delete obj[i];
  194.  }
  195.  
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement