Ahora les presento una sencilla aplicación en C++ que simula el ataque a un muro. Para esta aplicación se usaron gráficos basicos como rectángulos y círculos ademas de la Escena Fractal que da un efecto de MOVIMIENTO al avión. El avión se mueve conforme nosotros movamos el cursor y al dar clic lanza un proyectil que borra el objeto que aparece al final.
La imagen muestra la interfaz del juego.
La técnica es muy simple: Destruye la barrera con los proyectiles y recibe puntos.
Componentes Usados
Los componentes que usamos son muy simples, la parte interesante es el siguiente código que se muestra :
CÓDIGO C++
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int x;
int y;
int xx;//Variables para mover las figuras
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)//Aqui se crea el avion
{
Canvas->Brush->Color = clRed;
Canvas->Pen->Color=clBlue;
Canvas->Pen->Style= psDashDotDot;
Canvas->Rectangle(X-20, Y-30, X, Y+30);
Canvas->MoveTo(X, Y-10);
Canvas->LineTo(X+100, Y);
Canvas->MoveTo(X, Y+10);
Canvas->LineTo(X+100, Y);
Canvas->MoveTo(X, Y-10);
Canvas->LineTo(X, Y+10);//Se dibuja el triangulo del avion
Canvas->Rectangle(X-40, Y-20, X-20, Y-2);//parte trasera
Canvas->Rectangle(X-40, Y+20, X-20, Y+2);//del avion
//-----------------------------------------
Sleep(10);
//Codigo que borra el avion para evitar que deje rastro
Canvas->Brush->Color = clSkyBlue;
Canvas->Pen->Color=clSkyBlue;
Canvas->Pen->Style = psDashDotDot;
Canvas->Rectangle(X-20, Y-30, X, Y+30);
Canvas->MoveTo(X, Y-10);
Canvas->LineTo(X+100, Y);
Canvas->MoveTo(X, Y+10);
Canvas->LineTo(X+100, Y);
Canvas->MoveTo(X, Y-10);
Canvas->LineTo(X, Y+10);
Canvas->Rectangle(X-40, Y-20, X-20, Y-2);
Canvas->Rectangle(X-40, Y+20, X-20, Y+2);
xx=X+100;
x=X;
y=Y;
//--------------------------
}
//---------------------------------------------------------------------------
int maxlevel=6;
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
//Diseño Escena Fractal
PaintBox1->Canvas->Pen->Color = clSkyBlue;
PaintBox1->Canvas->Rectangle(0,0,PaintBox1->ClientWidth, PaintBox1->ClientHeight);
//Cielo
PaintBox1->Canvas->Brush->Color = clSkyBlue;
fractal(100, 100, maxlevel, 0.5, 50.0); //Montañas
draw_fractal();
PaintBox1->Canvas->FloodFill(1, 1, PaintBox1->Canvas->Pixels[0][1], fsBorder);
//monta;as
fractal(170, 150, maxlevel, 0.9, 30.0); //Oceano
draw_fractal();
PaintBox1->Canvas->Brush->Color = clGreen;
PaintBox1->Canvas->FloodFill(1, 150, PaintBox1->Canvas->Pixels[0][150], fsBorder);
//Oceano
PaintBox1->Canvas->Brush->Color = clBlue;
PaintBox1->Canvas->FloodFill(1, 220, PaintBox1->Canvas->Pixels[0][220], fsBorder);
}
//---------------------------------------------------------------------------
//Escena Fractal
void TForm1::fractal(int y1, int y2, int maxlevel, double h, double scale)
{
int first, last;
double ratio, std;
first = 0;
last = (int)pow(2.0,(double)maxlevel);
frctl[first] = y1;
frctl[last] = y2;
ratio = 1.0 / pow(2.0, h);
std = scale * ratio;
subdivide(first, last, std, ratio);
}
void TForm1::subdivide(int p1, int p2, double std, double ratio)
{
int midpnt;
double stdmid;
midpnt = (p1 + p2) / 2;
if(midpnt != p1 && midpnt != p2)
{
frctl[midpnt] = (frctl[p1] + frctl[p2]) / 2 + (double)(random(16) - 8) / 8.0 * std;
stdmid = std * ratio;
subdivide(p1, midpnt, stdmid, ratio);
subdivide(midpnt, p2, stdmid, ratio);
}
}
//--------------------------------------------------------------------------
//Dibujo del fractal
void TForm1::draw_fractal()
{
int i, x, xinc, l;
l = (int)pow(2.0, (double)maxlevel);
xinc = this->ClientWidth / l * 3 / 2;
PaintBox1->Canvas->MoveTo(0,100);
//linea_DDA(0, 100, 0, 0);
for(i = 0, x = 0; i < l; i++, x+=xinc)
{
PaintBox1->Canvas->LineTo(x, (int)frctl[i]);
// linea_DDA(0, 100, x, (int)frctl[i]);
frctl2[i] = x;
}
}
//-------------------------------------------------------------------------
void TForm1::redraw_fractal(int y1, int y2)
{
int i, x, l;
l = (int)pow(2.0, (double)maxlevel);
frctl[0] = y1;
frctl[l] = y2;
PaintBox1->Canvas->MoveTo(0,100);
for(i = 0; i < l; i++)
{
PaintBox1->Canvas->LineTo(frctl2[i], (int)frctl[i]);
}
}
//Inicializamos variables
void __fastcall TForm1::FormCreate(TObject *Sender)
{
maxsize = 1000;
maxlevel = 6;
dia = true;
Timer3->Enabled=false;
}
//---------------------------------------------------------------------------
//Redibujado del avion
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
//Ayuda a poner el avion estatico cada vez que soltamos
//en raton o el cursor sale del area de la forma
Canvas->Brush->Color = clRed;
Canvas->Pen->Color=clBlue;
Canvas->Pen->Style= psDashDotDot;
Canvas->Rectangle(x-20, y-30, x, y+30);
Canvas->MoveTo(x, y-10);
Canvas->LineTo(x+100, y);
Canvas->MoveTo(x, y+10);
Canvas->LineTo(x+100, y);
Canvas->MoveTo(x, y-10);
Canvas->LineTo(x, y+10);
Canvas->Rectangle(x-40, y-20, x-20, y-2);
Canvas->Rectangle(x-40, y+20, x-20, y+2);
Sleep(10);
Canvas->Brush->Color = clSkyBlue;
Canvas->Pen->Color=clSkyBlue;
Canvas->Pen->Style= psDashDotDot;
Canvas->Rectangle(x-20, y-30, x, y+30);
Canvas->MoveTo(x, y-10);
Canvas->LineTo(x+100, y);
Canvas->MoveTo(x, y+10);
Canvas->LineTo(x+100, y);
Canvas->MoveTo(x, y-10);
Canvas->LineTo(x, y+10);
Canvas->Rectangle(x-40, y-20, x-20, y-2);
Canvas->Rectangle(x-40, y+20, x-20, y+2);
}
//---------------------------------------------------------------------------
int i;
int k;
int val;
int puntaje=0;
//Dibujo de los proyectiles y Validacion de puntos
void __fastcall TForm1::Timer3Timer(TObject *Sender)
{
Canvas->Brush->Color = clBlack;
Canvas->Ellipse( xx+i-10, y+10, xx+i, y);//circulo que simula
//un proyectil
Sleep(20);
Canvas->Brush->Color = clSkyBlue;//Borra el proyectil para
//evitar dejar rastro
Canvas->Ellipse( xx+i-10, y+10, xx+i, y);
val=xx+i;
if(val<(ClientWidth-20))
{
i+=20;
}
if(val>=(ClientWidth-50))
{
i++;
puntaje+=1;
Panel1->Caption=puntaje;
if(val == ClientWidth)
Timer3->Enabled=false;//se detiene al salir de la forma
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
i=10;
Timer3->Enabled=true;//Activa el Timer3 al dar clic en la //forma
}
//---------------------------------------------------------------------------
bool enc=true;
void __fastcall TForm1::Timer4Timer(TObject *Sender)
{
//Dibujado del muro
Canvas->Brush->Color = clMaroon;
Canvas->Pen->Color=clBlack;
TColor color;
int x1;
int y1;
int x2;
int y2;
if(enc == true){
x1=this->ClientWidth-10;
y1=this->ClientHeight-0;
x2=this->ClientWidth-0;
y2=this->ClientHeight-440;//
enc=false;
}
Canvas->Rectangle(x1, y1, x2, y2);
Timer4->Enabled=false;
}
//---------------------------------------------------------------------------
Este es todo el post, próximamente subire mas programas. Dejen comentarios!
No hay comentarios:
Publicar un comentario