Zıplayan Toplar (Çarpışma Kuralına Göre)

DrogbA

Forum Üyesi
Katılım
27 Ara 2020
Mesajlar
3,440
Tepkime puanı
0
Puanları
36
C++ Builder 6 projesidir. Bu program da Timer nesnesi kullanılarak RadioButton ve Shape nesnelerinin hızlı bir hareketi sağlanmıştır. Eğer RadioButton ile Shape nesneleri çarpışma durumuna gelirlerse bulundukları istikamete göre zıt yönde hareket ederler. Böylece çarpışmada gerçeklik durumuna da değinilmiş olur. Çalışma zamanında formun boyutları pencerede kayma şeklinde olmayacak şekilde değiştirilirse çarpışma kuralı bağlı özellikler daha rahat gözlemlenebilir.

Kod:
//---------------------------------------------------------------------------
 
#include <vcl.h>
#pragma hdrstop
 
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
static int sx=5;
static int sy=5;
static int ax=5;
static int by=5;
int x,y,a,b;
x=RadioButton1->Left;
y=RadioButton1->Top;
a=Shape1->Left;
b=Shape1->Top;
if(y<=0)
sy=-sy;
if(b<=0)
by=-by;
if(y+RadioButton1->Height>=Form1->ClientRect.Bottom)
sy=-sy;
if(b+Shape1->Height>=Form1->ClientRect.Bottom)
by=-by;
if(x<=0)
sx=-sx;
if(a<=0)
ax=-ax;
if(x+RadioButton1->Width>=Form1->ClientRect.right)
sx=-sx;
if(a+Shape1->Width>=Form1->ClientRect.right)
ax=-ax;
 
if(RadioButton1->Left+RadioButton1->Width>Shape1->Left && RadioButton1->Left+RadioButton1->Width<Shape1->Left+Shape1->Width && RadioButton1->Top+RadioButton1->Height>Shape1->Top && RadioButton1->Top+RadioButton1->Height<Shape1->Top+Shape1->Height)
{ax=-ax;   sx=-sx; }
 
if( RadioButton1->Top+RadioButton1->Height>Shape1->Top&&RadioButton1->Top+RadioButton1->Height<Shape1->Top+Shape1->Height && RadioButton1->Left+RadioButton1->Width>Shape1->Left && RadioButton1->Left+RadioButton1->Width<Shape1->Left+Shape1->Width)
{by=-by;   sy=-sy; }
 
x+=sx;
y+=sy;
a+=ax;
b+=by;
RadioButton1->Left=x;
RadioButton1->Top=y;
Shape1->Left=a;
Shape1->Top=b;
}
//---------------------------------------------------------------------------
 

Nutella

Bayan Üye
Özel Üye
Katılım
2 Ocak 2021
Mesajlar
3,559
Tepkime puanı
0
Puanları
36
Cinsiyet
  1. Bayan
Takım
Galatasaray
Paylaşım için teşekkürler.
 
metal işleme
Üst