Skip to content

Instantly share code, notes, and snippets.

@thiagodeschamps
Created December 22, 2018 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagodeschamps/cd78a9e238982ddfbc75062cf84bbcea to your computer and use it in GitHub Desktop.
Save thiagodeschamps/cd78a9e238982ddfbc75062cf84bbcea to your computer and use it in GitHub Desktop.
Uso de polimorfismo e interface grafica, recebendo comandos do teclados para mover os objetos
/*
https://github.com/jakleg
INTRUCOES:
CONTROLES:
- CIRCULO: W, A, S, D;
- QUADRADO: F, T, G, H;
- ELIPSE: J, I, K, L;
- TODOS AO MESMO TEMPO: SETAS: CIMA, BAIXO, ESQUERDA, DIREITA;
FUNCIONAMENTO:
PARA A EXECUCAO DESSE PROGRAMA, SAO NECESSARIAS AS BIBLIOTECAS:
- gaphics.h
- windows.h
- conio.h
ANTES DE COMPILAR, VERIFIQUE SE ESTAO INSTALADAS NO COMPUTADOR;
*/
#include<graphics.h>
#include<windows.h>
#include<conio.h>
#define speed 10 //"velocidade" dos objetos
// classe principal
class Figura {
public:
Figura(){}
virtual ~Figura(){}
virtual void desenha() = 0;
virtual void setPosicao() = 0;
protected:
int x, y; // COORDENADAS X E Y NA TELA
};
class Circulo: public Figura {
public:
Circulo(int r = 30, int _x = 150, int _y = 250) {
raio = r;
x = _x;
y = _y;
}
void desenha() {
setcolor(RED);
circle(x, y, raio);
}
void setPosicao() {
if(GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(0x41)) {
x -= speed;
}
else if(GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState(0x44)) {
x += speed;
} else if(GetAsyncKeyState(VK_UP)|| GetAsyncKeyState(0x57)) {
y -= speed;
} else if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(0x53)) {
y += speed;
}
}
protected:
int raio; // RAIO DO CRICULO
};
class Retangulo: public Figura {
public:
Retangulo(int _x = 200,int _y = 200) {
x = _x;
y = _y;
}
void desenha() {
setcolor(BLUE);
rectangle(x,y,x + 100, y+100);
}
void setPosicao() {
if(GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(0x46)) {
x -= speed;
}
else if(GetAsyncKeyState(VK_RIGHT)|| GetAsyncKeyState(0x48)) {
x += speed;
} else if(GetAsyncKeyState(VK_UP)|| GetAsyncKeyState(0x54)) {
y -= speed;
} else if(GetAsyncKeyState(VK_DOWN)|| GetAsyncKeyState(0x47)) {
y += speed;
}
}
};
class Elipse: public Figura {
public:
Elipse(int _a = 370,int _b = 250, int _c = 0, int _d = 360, int _e = 30,
int _f = 100) {
x = _a;
y = _b;
c = _c;
d = _d;
e = _e;
f = _f;
}
void desenha() {
setcolor(GREEN);
ellipse(x, y, c, d, e, f);
}
void setPosicao() {
if(GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(0x4A)) {
x -= speed;
}
else if(GetAsyncKeyState(VK_RIGHT)|| GetAsyncKeyState(0x4C)) {
x += speed;
} else if(GetAsyncKeyState(VK_UP)|| GetAsyncKeyState(0x49)) {
y -= speed;
} else if(GetAsyncKeyState(VK_DOWN)|| GetAsyncKeyState(0x4B)) {
y += speed;
}
}
protected:
int c;// ENDANGLE
int d;// STANGLE
int e;// RAIO DO EIXO X
int f;// RAIO DO EIXO Y
};
int main() {
int page = 0;
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
Figura *r = new Retangulo;
Figura *t = new Elipse;
// EXEMPLO DE COMO INSERIR UMA NOVA FIGURA
// NOME_DA_CLASSE PONTEIRO = new NOME_DA_CLASSE(ATRIBUTOS)
Figura *c = new Circulo(40, 150, 250); // (RAIO, X, Y)
while(1) {
//DESENHANDO OS 3 OBJETOS
r->desenha();
c->desenha();
t->desenha();
setactivepage(page);
setvisualpage(1 - page);
cleardevice();
//ATUALIZANDO A POSICAO DELES
r->setPosicao();
c->setPosicao();
t->setPosicao();
page = 1 - page;
delay(5);
}
getch();
closegraph();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment