質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

解決済

1回答

2984閲覧

ブロック崩しゲームを作成していたが、ボールが動かないため、その原因を解決したい。

wing

総合スコア20

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2020/07/29 13:02

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 }

デバックするとこうなります。
左の色の変更とその下の3つのテキストボックスは関係ありません。
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2020/07/29 13:23

//再描画 pictureBox1.Invalidate(); とすればとりあえず動くようにはなるんじゃないですかね。
wing

2020/07/29 13:47

動くようになりました。ありがとうございました。
TN8001

2020/07/29 14:07

Daregadaさんが回答してくださったので、ベストアンサーにして解決済みにしてください。 動いたのであれば気が付いたと思いますが、ballSpeedX ballSpeedYはいらないですね。 あとメンバー変数のほうのTimer timer1が使われていません。
guest

回答1

0

ベストアンサー

参照したサイト(おそらくは、【Visual C#でゲームを作る】ブロック崩し編 その1 - おもちゃラボ)の記述とは違って、ピクチャーボックスのPaintイベントでpictureBox1_Paintメソッドを呼び出していますから、UpdateメソッドでInvalidateするのはForm1ではなくpictureBox1でなければいけません。

C#

1pictureBox1.Invalidate();

として、後は跳ね返りの処理などを修正していってください。

投稿2020/07/29 13:56

Daregada

総合スコア11990

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Daregada

2020/07/29 13:58

って修正依頼で解決してるじゃん。失礼しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問