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

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

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

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

Q&A

0回答

1406閲覧

ボール跳ね返りゲームでの

kazuma2525

総合スコア1

C#

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

0グッド

0クリップ

投稿2020/07/03 07:19

ボール跳ね返りゲームで下端に落とさないようにするゲームを作成したのですが、以下の機能を追加する場合どこにプログラムを追加したらいいですか?

ボールがクライアント領域の下端に達したときは画面の下端に "GAME OVER" と表示する。
ボールを跳ね返した回数を表示するラベルを追加する。
プレイヤーがバーの長さをテキストボックス・ボタン等で変更できるようにする。
ボールの移動速度を乱数で決めるようにする。
ボールの数を増やす。

c#

1class Program : System.Windows.Forms.Form 2 { 3 // ボール1個を扱うためのクラス 4 class Ball 5 { 6 private int CenterX, CenterY; // ボールの中心座標 7 private int Diameter; // ボールの直径 8 private int SpeedX, SpeedY; // ボールの移動速度 9 10 // コンストラクタ 11 public Ball(int centerX, int centerY, int diameter, int speedX, int speedY) 12 { 13 CenterX = centerX; 14 CenterY = centerY; 15 Diameter = diameter; 16 SpeedX = speedX; 17 SpeedY = speedY; 18 } 19 20 // クライアント領域のサイズ(clientSize)とバー(myBar)により跳ね返りを考慮してボールを移動 21 public void Move(System.Drawing.Size clientSize, Bar myBar) 22 { 23 // ボールを移動 24 CenterX += SpeedX; 25 CenterY += SpeedY; 26 27 // 左端・右端での跳ね返り 28 if (CenterX - Diameter / 2 <= 0 || CenterX + Diameter / 2 >= clientSize.Width) 29 SpeedX = -SpeedX; 30 31 // 上端での跳ね返り 32 if (CenterY - Diameter / 2 <= 0) 33 SpeedY = -SpeedY; 34 35 // バーによる跳ね返り 36 if (myBar.Hitting(CenterX, CenterY + Diameter / 2) && SpeedY > 0) 37 SpeedY = -SpeedY; 38 } 39 40 // ボールを描画 41 public void Draw(System.Windows.Forms.PaintEventArgs e) 42 { 43 e.Graphics.FillEllipse(System.Drawing.Brushes.Red, CenterX - Diameter / 2, CenterY - Diameter / 2, Diameter, Diameter); 44 } 45 } 46 47 // バー1本を扱うためのクラス 48 class Bar 49 { 50 private System.Drawing.Rectangle BarRect; // バーを表す長方形 51 52 // コンストラクタ 53 public Bar(System.Drawing.Rectangle barRect) 54 { 55 BarRect = barRect; 56 } 57 58 // バーを移動(centerX:中央のX座標) 59 public void Move(int centerX) 60 { 61 BarRect.X = centerX - BarRect.Width / 2; 62 } 63 64 // バーを描画 65 public void Draw(System.Windows.Forms.PaintEventArgs e) 66 { 67 e.Graphics.FillRectangle(System.Drawing.Brushes.Black, BarRect); 68 } 69 70 // 座標 (x, y) がバーの内部にあれば true を返す(ボールの跳ね返り判定に利用) 71 public bool Hitting(int x, int y) 72 { 73 return BarRect.Contains(x, y); 74 } 75 } 76 77 private Ball MyBall; // ゲームで使用されているボール 78 private Bar MyBar; // ゲームで使用されているバー 79 private System.Windows.Forms.Button ResetButton; // リセットボタン 80 81 // 画面を描画するときに呼び出されるメソッド 82 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 83 { 84 base.OnPaint(e); 85 86 MyBall.Draw(e); 87 MyBar.Draw(e); 88 } 89 90 // タイマにより定期的に呼び出されるメソッド 91 private void OnTick(object sender, System.EventArgs e) 92 { 93 // マウスカーソルの位置を取得してバーを移動 94 System.Drawing.Point mouse = PointToClient(System.Windows.Forms.Cursor.Position); 95 MyBar.Move(mouse.X); 96 97 // ボールを移動 98 MyBall.Move(ClientSize, MyBar); 99 100 // 画面を描画(OnPaint() が呼び出される) 101 Invalidate(); 102 } 103 104 // 新しいボールを作成してゲームで使用 105 private void CreateNewBall() 106 { 107 MyBall = new Ball(100, 100, 20, 7, 5); 108 } 109 110 // リセットボタンがクリックされたときに呼び出されるメソッド 111 private void ResetButton_Click(object sender, System.EventArgs e) 112 { 113 CreateNewBall(); 114 } 115 116 public Program() 117 { 118 // ウィンドウに関する設定 119 Text = "ボール跳ね返しゲーム"; 120 ClientSize = new System.Drawing.Size(400, 300); 121 FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 122 123 // ゲームで使用するボールとバーを生成 124 CreateNewBall(); 125 MyBar = new Bar(new System.Drawing.Rectangle(0, 250, 50, 10)); 126 127 // リセットボタンを配置 128 ResetButton = new System.Windows.Forms.Button(); 129 ResetButton.Text = "リセット"; 130 ResetButton.Location = new System.Drawing.Point(10, 10); 131 ResetButton.Size = new System.Drawing.Size(50, 20); 132 ResetButton.Click += new System.EventHandler(ResetButton_Click); 133 Controls.Add(ResetButton); 134 135 // タイマを設定(OnTick() が定期的に呼び出されるようにする) 136 System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); 137 timer.Interval = 33; // OnTick() の呼び出し間隔(ミリ秒単位) 138 timer.Tick += new System.EventHandler(OnTick); 139 timer.Start(); 140 } 141 142 static void Main(string[] args) 143 { 144 System.Windows.Forms.Application.Run(new Program()); 145 } 146 } 147}

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問