visual stdio2015で、ブロックを崩すゲームを様々なサイトを見て、ソースコードを手本にして、作っていました。ボールを動かすプログラムを作ったのですが、ボールが動きませんでした。自分でソースコードを見直したりしていましたが、ビルドしてもエラーが出ないため、よくわかりません。どうすればボールが動きますでしょうか。
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Windows.Forms; 9using System.Diagnostics; 10using System.Windows; 11 12namespace adbClient2 13{ 14 public partial class Form1 : Form 15 { 16 MyTcpClient client = new MyTcpClient("127.0.0.1", 7654); 17 Rectangle pad; 18 Vector ball; 19 Vector ballSpeed; 20 int ballRadius; 21 Rectangle block; 22 int ballSpeedX = 2, ballSpeedY = 2; 23 int count = 0; 24 Timer timer1; 25 26 public Form1() 27 { 28 InitializeComponent(); 29 client.Connected += new EventHandler(client_Connected); 30 client.Disconnected += new EventHandler(client_Disconnected); 31 client.DataRead += new EventHandler<DataReadEventArgs>(client_DataRead); 32 33 pictureBox1.BorderStyle = BorderStyle.FixedSingle; 34 pictureBox1.Paint += pictureBox1_Paint; 35 DoubleBuffered = true; 36 pad = new Rectangle(pictureBox1.Width / 2 - 25, pictureBox1.Bottom - 70, 50, 30); 37 block = new Rectangle(100,50,80,25); 38 //for(int x=0; x<= pictureBox1.Width; x += 60) 39 //{ 40 // for(int y = 0; y<= 150; y += 40) 41 //{ 42 // block.Add(new Rectangle(0+x, y, 50, 25)); 43 //} 44 //} 45 int[] psColor = { 0,0,255 }; 46 this.ball = new Vector(200,200); 47 this.ballRadius = 10; 48 this.ballSpeed = new Vector(-2, -2); 49 50 Timer timer1 = new Timer(); 51 timer1.Interval = 33; 52 timer1.Tick += new EventHandler(Update); 53 timer1.Start(); 54 } 55 56 57 58 59 double DotProduct(Vector a, Vector b) 60 { 61 return a.X * b.X + a.Y * b.Y; // 内積計算 62 } 63 64 bool LineVsCircle(Vector p1, Vector p2, Vector center, float radius) 65 { 66 Vector lineDir = (p2 - p1); // パドルの方向ベクトル 67 Vector n = new Vector(lineDir.Y, -lineDir.X); // パドルの法線 68 n.Normalize(); 69 70 Vector dir1 = center - p1; 71 Vector dir2 = center - p2; 72 73 double dist = Math.Abs(DotProduct(dir1, n)); 74 double a1 = DotProduct(dir1, lineDir); 75 double a2 = DotProduct(dir2, lineDir); 76 77 return (a1 * a2 < 0 && dist < radius) ? true : false; 78 } 79 80 int BlockVsCircle(Rectangle block, Vector ball) 81 { 82 if (LineVsCircle(new Vector(block.Left, block.Top), 83 new Vector(block.Right, block.Top), ball, ballRadius)) 84 return 1; 85 86 if (LineVsCircle(new Vector(block.Left, block.Bottom), 87 new Vector(block.Right, block.Bottom), ball, ballRadius)) 88 return 2; 89 90 if (LineVsCircle(new Vector(block.Right, block.Top), 91 new Vector(block.Right, block.Bottom), ball, ballRadius)) 92 return 3; 93 94 if (LineVsCircle(new Vector(block.Left, block.Top), 95 new Vector(block.Left, block.Bottom), ball, ballRadius)) 96 return 4; 97 98 return -1; 99 } 100 101 private void Update(object sender,EventArgs e) 102 { 103 //ボールの移動 104 ball += ballSpeed; 105 106 //左右の壁でのバウンド 107 if (ball.X + ballRadius > pictureBox1.Width || ball.X - ballRadius < 0) 108 { 109 ballSpeedX = -ballSpeedX; 110 } 111 112 //上の壁でバウンド 113 if (ball.Y - ballRadius < 0) 114 { 115 ballSpeedY = -ballSpeedY; 116 } 117 118 119 // ブロックとのあたり判定 120 int collision = BlockVsCircle(block, ball); 121 if (collision == 1 || collision == 2) 122 { 123 ballSpeed.Y *= -1; 124 } 125 else if (collision == 3 || collision == 4) 126 { 127 ballSpeed.X *= -1; 128 } 129 130 //再描画 131 Invalidate(); 132 133 } 134 135 void pictureBox1_Paint(object sender,PaintEventArgs e) 136 { 137 138 e.Graphics.FillRectangle(Brushes.Blue,pad); 139 140 if(count>2) 141 { 142 e.Graphics.FillRectangle(Brushes.Red, pad); 143 } 144 //e.Graphics.FillEllipse(Brushes.Purple, ball); 145 SolidBrush purpleBrush = new SolidBrush(Color.Purple); 146 SolidBrush brownBrush = new SolidBrush(Color.Brown); 147 float px = (float)this.ball.X - ballRadius; 148 float py = (float)this.ball.Y - ballRadius; 149 150 e.Graphics.FillEllipse(purpleBrush, px, py, this.ballRadius * 2, this.ballRadius * 2); 151 e.Graphics.FillRectangle(brownBrush, block); 152 153 //for (int i = 0; i < block.Count; i++) 154 //{ 155 // e.Graphics.FillRectangle(Brushes.Brown, block[i]); 156 //} 157 158 159 }
回答1件
あなたの回答
tips
プレビュー