Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. Console.WriteLine("COLLISION OCCURS!");
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace Paddle_Test
  14. {
  15. public partial class Form1 : Form
  16. {
  17. Rectangle rec;
  18. Rectangle circle;
  19. int wLoc=0;
  20. int hLoc=0;
  21. int eWL;
  22. int eHL;
  23. int dx = 4;
  24. int dy = 4;
  25.  
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. wLoc=(this.Width) - 100;
  30. hLoc=(this.Height) - 100;
  31. eWL = 10;
  32. eHL = 10;
  33. rec = new Rectangle(wLoc,hLoc , 60, 10);
  34. }
  35.  
  36. private void Form1_Load(object sender, EventArgs e)
  37. {
  38. this.Refresh();
  39. }
  40.  
  41. private void Form1_Paint(object sender, PaintEventArgs e)
  42. {
  43. Graphics g = e.Graphics;
  44.  
  45. g.FillRectangle(new SolidBrush(Color.Blue), rec);
  46. g.FillEllipse(new SolidBrush(Color.Red), eWL, eHL, 40, 40);
  47.  
  48. }
  49.  
  50. private void Form1_KeyDown(object sender, KeyEventArgs e)
  51. {
  52. if (e.KeyCode==Keys.Left)
  53. {
  54. rec.X -= 30;
  55. this.Refresh();
  56. }
  57.  
  58. if (e.KeyCode==Keys.Right)
  59. {
  60. rec.X += 30;
  61. this.Refresh();
  62. }
  63. }
  64.  
  65. private void timer_Tick(object sender, EventArgs e)
  66. {
  67. eWL = eWL + dx;
  68. eHL = eHL + dy;
  69.  
  70. if (eWL >= (this.Width) - 100)
  71. {
  72. dx = dx * (-1);
  73. }
  74. else if (eHL >= (this.Height) - 100)
  75. {
  76. dy = dy * (-1);
  77. //timer.Enabled = false;
  78. //MessageBox.Show("Game Over!");
  79. }
  80. else if (eWL<=0)
  81. {
  82. dx = dx * (-1);
  83. }
  84. else if (eHL <= 0)
  85. {
  86. dy = dy * (-1);
  87. }
  88. else if (eWL == rec.X || eHL == rec.Y) //here I'm trying to detect the collision!
  89. {
  90. dx = dx * (-1);//invert the direction x-axis
  91. dy = dy * (-1);//invert the direction y-axis
  92.  
  93.  
  94. }
  95. this.Refresh();
  96. }
  97. }
  98. }
  99.  
  100. using System;
  101. using System.Collections.Generic;
  102. using System.ComponentModel;
  103. using System.Data;
  104. using System.Drawing;
  105. using System.Linq;
  106. using System.Text;
  107. using System.Threading.Tasks;
  108. using System.Windows.Forms;
  109.  
  110. namespace Paddle_Test
  111. {
  112. public partial class Form1 : Form
  113. {
  114. Rectangle rec;
  115. Rectangle circle;
  116. int wLoc=0;
  117. int hLoc=0;
  118. int eWL;
  119. int eHL;
  120. int dx = 2;
  121. int dy = 2;
  122.  
  123. public Form1()
  124. {
  125. InitializeComponent();
  126. wLoc=(this.Width) - 100;
  127. hLoc=(this.Height) - 100;
  128. eWL = 10;
  129. eHL = 10;
  130. rec = new Rectangle(wLoc,hLoc , 100, 10);
  131. circle = new Rectangle(eWL, eHL, 40, 40);
  132. }
  133.  
  134. private void Form1_Load(object sender, EventArgs e)
  135. {
  136. this.Refresh();
  137. }
  138.  
  139. private void Form1_Paint(object sender, PaintEventArgs e)
  140. {
  141. Graphics g = e.Graphics;
  142.  
  143. g.FillRectangle(new SolidBrush(Color.Blue), rec);
  144. g.FillEllipse(new SolidBrush(Color.Red), circle);
  145.  
  146. }
  147.  
  148. private void Form1_KeyDown(object sender, KeyEventArgs e)
  149. {
  150. if (e.KeyCode==Keys.Left)
  151. {
  152. rec.X -= 30;
  153. this.Refresh();
  154. }
  155.  
  156. if (e.KeyCode==Keys.Right)
  157. {
  158. rec.X += 30;
  159. this.Refresh();
  160. }
  161. }
  162.  
  163. int count=0;
  164. private void timer_Tick(object sender, EventArgs e)
  165. {
  166.  
  167. circle.X += dx;
  168. circle.Y += dy;
  169. count += 1;
  170.  
  171. if (circle.X >= (this.Width) - 100)
  172. {
  173. dx = dx * (-1);
  174. }
  175. else if (circle.Y >= (this.Height))
  176. {
  177. //dy = dy * (-1);
  178. timer.Enabled = false;
  179. MessageBox.Show("Game Over!");
  180. }
  181. else if (circle.X <= 0)
  182. {
  183. dx = dx * (-1);
  184. }
  185. else if (circle.Y <= 0)
  186. {
  187. dy = dy * (-1);
  188. }
  189.  
  190. else if (rec.IntersectsWith(circle))//detects collision!
  191. {
  192. //dx = dx * (-1);
  193. dy = dy * (-1);
  194.  
  195. Console.WriteLine("hello");
  196. }
  197.  
  198. this.Refresh();
  199. }
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement