Добавил:
bagiwow
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <grx20.h>
#include <grxkeys.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int m = 20;
struct TPoint {
int x, y, color, vx, vy;
TPoint(int ix, int iy, int ivx, int ivy, int icolor);
virtual ~TPoint();
virtual void Draw(int icolor);
void Show();
void Hide();
void MoveXY(int ix, int iy);
};
struct TBox:public TPoint {
int w, h;
TBox(int ix, int iy, int ivx, int ivy, int icolor, int iw, int ih);
virtual ~TBox();
virtual void Draw(int icolor);
};
struct TCircle:public TPoint {
int r;
TCircle(int ix, int iy, int ivx, int ivy, int icolor, int r);
virtual ~TCircle();
virtual void Draw(int icolor);
};
TPoint::TPoint(int ix, int iy, int ivx, int ivy, int icolor) {
x = ix; y = iy; vx = ivx; vy = ivy; color = icolor;
}
TPoint::~TPoint() {
Hide();
}
void TPoint::Draw(int icolor) {
GrFilledCircle(x, y, 1, icolor);
}
void TPoint::Show() {
Draw(color);
}
void TPoint::Hide() {
Draw(GrAllocColor(0, 0, 0));
}
void TPoint::MoveXY(int ix, int iy) {
Hide();
if (ix > GrMaxX()) x = ix - GrMaxX() - 1;
else if (ix < 0) x = GrMaxX() + ix + 1;
else if (iy > GrMaxY()) y = iy - GrMaxY() - 1;
else if (iy < 0) y = GrMaxY() + iy + 1;
else {x = ix; y = iy;}
Show();
}
TBox::TBox(int ix, int iy, int ivx, int ivy, int icolor, int iw, int ih):TPoint(ix, iy, ivx, ivy, icolor) {
w = iw; h = ih;
}
TBox::~TBox() {
Hide();
}
void TBox::Draw(int icolor) {
GrBox(x - w / 2, y - h / 2, x + (w - w / 2), y + (h - h / 2), icolor);
}
TCircle::TCircle(int ix, int iy, int ivx, int ivy, int icolor, int ir):TPoint(ix, iy, ivx, ivy, icolor) {
r = ir;
}
TCircle::~TCircle() {
Hide();
}
void TCircle::Draw(int icolor) {
GrCircle(x, y, r, icolor);
}
struct Center {
int x, y;
};
void P(int px, int py, GrColor pc)
{
GrLine(px+100,py+75,px+100,py+60,pc);
GrLine(px+60,py+100,px+75,py+100,pc);
GrLine(px+125,py+100,px+140,py+100,pc);
GrLine(px+100,py+125,px+100,py+140,pc);
GrCircle(px+100,py+100,25,pc);
}
int main() {
int state = 0;
int px=100,py=100;
TPoint * obj[m];
Center center;
srand(time(NULL));
GrSetMode(GR_width_height_color_graphics, 900, 500, GrAllocColor(0, 0, 0));
int k = 0, n = 0, finish;
GrKeyType key;
GrColor color=15;
do {
while (!GrKeyPressed()) {
GrSleep(10);
P(px,py,color);
if(state == 0) {
for (int i = 0; i < n; i++) {
obj[i] -> vx = (center.x - obj[i] -> x) / 10 + rand() % 7 - 3;
obj[i] -> vy = (center.y - obj[i] -> y) / 10 + rand() % 7 - 3;
}
finish = 1;
if(finish == 1) {
state = 0;
for(int i = 0; i < n; i++) {
obj[i] -> vx = rand() % 11 - 5;
obj[i] -> vy = rand() % 11 - 5;
}
}
}
for(int i = 0; i < n; i++) {
obj[i] -> MoveXY(obj[i] -> x + obj[i] -> vx, obj[i] -> y + obj[i] -> vy);
}
if (state == 0) {
state = 1;
center.x = GrMaxX() / 2 + rand() % (GrMaxX() / 4) - rand() % (GrMaxX() / 4);
center.y = GrMaxY() / 2 + rand() % (GrMaxY() / 4) - rand() % (GrMaxY() / 4);
}
for(int i=0;i<n;i++)
{
if((sqrt((obj[i]->x-px-99)*(obj[i]->x-px-99)+(obj[i]->y-py-99)*(obj[i]->y-py-99))<=25)&&n>0)
{
//delete obj[i];
//--n;
obj[i]->color=0;
}
}
}
key = GrKeyRead();
switch(key) {
case GrKey_Insert:
if (n < m) {
k = rand() % 3;
switch (k) {
case 0:
obj[n] = new TPoint(GrMaxX() / 2, GrMaxY() / 2, rand() % 11 - 5, rand() % 11 - 5, GrAllocColor(rand() % 256, rand() % 256, rand() % 256));
break;
case 1:
obj[n] = new TBox(GrMaxX() / 2, GrMaxY() / 2, rand() % 11 - 5, rand() % 11 - 5, GrAllocColor(rand() % 256, rand() % 256, rand() % 256), rand() % 20, rand() % 20);
break;
case 2:
obj[n] = new TCircle(GrMaxX() / 2, GrMaxY() / 2, rand() % 11 - 5, rand() % 11 - 5, GrAllocColor(rand() % 256, rand() % 256, rand() % 256), rand() % 20);
break;
}
obj[n] -> Show();
n++;
}
break;
case GrKey_Delete:
if (n > 0) {
delete obj[n - 1];
n--;
}
break;
case GrKey_Up:
P(px,py,0);
py-=10;
break;
case GrKey_Down:
P(px,py,0);
py+=10;
break;
case GrKey_Left:
P(px,py,0);
px-=10;
break;
case GrKey_Right:
P(px,py,0);
px+=10;
break;
}
} while(key != GrKey_Escape);
for(int i = 0; i < n; i++)
{
delete obj[i];
}
return 0;
}