具体的にはwhileループ内でキーを読み取り画面上の四角形(bar)のX座標を
変更したいと思っています。色々調べてみたのですが動いてくれませんので質問させていただきました。
どうかよろしくお願いいたします。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
public struct Bar
{
public int X;
public int Y;
public int WIDTH;
public int HEIGHT;
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap bmp;
Graphics Ball;//ボール
Graphics Bar;//バー
Stopwatch sw = new Stopwatch();
Bar bar = new Bar();
int Ballx=100, Bally=100;
public Form1()
{
InitializeComponent();
}
public void ini()
{
bar.X = 200;
bar.Y = 200;
bar.WIDTH = 100;
bar.HEIGHT= 50;
}
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = bmp;
Ball = Graphics.FromImage(bmp);
Bar = Graphics.FromImage(bmp);
ini();
}
private void button1_Click(object sender, EventArgs e)
{
main();
}
public void main()//メイン処理
{
sw.Start();
while (true)
{
Ball.Clear(Color.White);
Ball.FillEllipse(Brushes.Red,Ballx,Bally, 5, 5);
Bar.FillRectangle(Brushes.Aqua, bar.X, bar.Y, bar.WIDTH, bar.HEIGHT);
if (sw.ElapsedMilliseconds % 5 == 0)
{
Ballx++;
Bally++;
}
pictureBox1.Refresh();
}
}
public void keyPressed(ConsoleKeyInfo e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
bar.X--;
if (e.KeyData == Keys.Right)
bar.X++;
}
}
}