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

Salı, 28 Nisan 2009 22:29 NEO
Yazdır

AÇIKLAMA

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.

Programın Tamamını Aşağıdaki Linkten İndirebilirsiniz

Program Kodu:

  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13.         : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17.  
  18. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  19. {
  20. static int sx=5;
  21. static int sy=5;
  22. static int ax=5;
  23. static int by=5;
  24. int x,y,a,b;
  25. x=RadioButton1->Left;
  26. y=RadioButton1->Top;
  27. a=Shape1->Left;
  28. b=Shape1->Top;
  29. if(y<=0)
  30. sy=-sy;
  31. if(b<=0)
  32. by=-by;
  33. if(y+RadioButton1->Height>=Form1->ClientRect.Bottom)
  34. sy=-sy;
  35. if(b+Shape1->Height>=Form1->ClientRect.Bottom)
  36. by=-by;
  37. if(x<=0)
  38. sx=-sx;
  39. if(a<=0)
  40. ax=-ax;
  41. if(x+RadioButton1->Width>=Form1->ClientRect.right)
  42. sx=-sx;
  43. if(a+Shape1->Width>=Form1->ClientRect.right)
  44. ax=-ax;
  45.  
  46. 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)
  47. {ax=-ax;   sx=-sx; }
  48.  
  49. 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)
  50. {by=-by;   sy=-sy; }
  51.  
  52. x+=sx;
  53. y+=sy;
  54. a+=ax;
  55. b+=by;
  56. RadioButton1->Left=x;
  57. RadioButton1->Top=y;
  58. Shape1->Left=a;
  59. Shape1->Top=b;
  60. }
  61. //---------------------------------------------------------------------------
  62.  

Yorumlar
Yeni Ekle Ara
+/-
Yorum yaz
Adınız:
E-posta:
 
Web Sayfas1:
Başlık:
UBB Kodu:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Lütfen resimdeki güvenlik kodunu giriniz.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Son Güncelleme ( Çarşamba, 13 Mayıs 2009 18:00 )