quarta-feira, 16 de outubro de 2019

C++ builder - detectando colisão entre dois quadrados

Uma dos recursos mais importantes em jogos, 
é um bloco detector de colisão.
Detectar colisão é fundamental para que outras funções
ou comandos possam ser executados.
Neste exemplo estamos detectando colisão das bordas da janela, 
e entre dois quadrados, móvel, acompanhem no vídeo.



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool a = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
        : TForm ( Owner ), currentx ( 0 ), currenty ( 0 ), dragging ( false ) {
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 190, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 190, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 210, "sa_sp10@hotmail.com" );
    Canvas -> Font -> Name = "Garamond";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender )
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clBlue;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    Canvas -> Font -> Size = 16;
    Canvas -> Font -> Name = "SF Theramin Gothic Shaded Oblique";
    Canvas -> Font -> Color = clRed;
    Canvas -> Font -> Style = TFontStyles ( ) << fsBold  ;
    Canvas -> TextOut ( 20, 12, "DETECTANDO COLISÃO ENTRE DOIS QUADRADOS" );
    Informe ( Sender );
}
//---------------------------------------------------------------------------
 


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Vcl.Graphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TShape *Shape1;
    TShape *Shape2;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TLabel *Label4;
    TLabel *Label5;
    void __fastcall Shape1MouseMove ( TObject *Sender, TShiftState Shift, int X, int Y );
    void __fastcall Shape1MouseDown ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Shape1MouseUp ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Shape2MouseMove ( TObject *Sender, TShiftState Shift, int X, int Y );
    void __fastcall Shape2MouseDown ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Shape2MouseUp ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Detect_Colisao ( TObject *Sender );
    void __fastcall OnPaint ( TObject *Sender );
    void __fastcall Informe ( TObject *Sender );
private:    // User declarations
        int currentx;
        int currenty;
        bool dragging;
public:        // User declarations
        __fastcall TForm1 ( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
 


object Form1: TForm1
  Left = 455
  Top = 118
  Caption = 'DETECTANDO COLIS'#195'O ENTRE DOIS QUADRADOS'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Shape1: TShape
    Left = 160
    Top = 72
    Width = 65
    Height = 65
    Brush.Color = clBlue
    Pen.Width = 4
    OnMouseDown = Shape1MouseDown
    OnMouseMove = Shape1MouseMove
    OnMouseUp = Shape1MouseUp
  end
  object Shape2: TShape
    Left = 408
    Top = 72
    Width = 65
    Height = 65
    Brush.Color = clRed
    Pen.Width = 4
    OnMouseDown = Shape2MouseDown
    OnMouseMove = Shape2MouseMove
    OnMouseUp = Shape2MouseUp
  end
  object Label1: TLabel
    Left = 504
    Top = 60
    Width = 7
    Height = 24
    Color = clBlue
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentColor = False
    ParentFont = False
  end
  object Label2: TLabel
    Left = 504
    Top = 90
    Width = 7
    Height = 24
    Color = clRed
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentColor = False
    ParentFont = False
  end
  object Label3: TLabel
    Left = 190
    Top = 260
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clGreen
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 40
    Top = 60
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clYellow
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label5: TLabel
    Left = 40
    Top = 90
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.