プログラミング歴は3ヶ月ほどで、基礎的な段階です。
私が作っているプログラムは、黒点が1つ表示されおり、黒点がそれぞれキーボードのaを押すと左、sを押すと右、wを押すと上、zを押すと下にそれぞれ速度が5で動くというものです。
ここまでは正常に動きました。
ここからが問題です。
各キーボードを押したままdのキーボードを押した場合、移動方向は変わらずに黒点の速度が15に変化し、dを離すと速度は5に戻るようにしたいのです。しかし私の書いたコードでは同時にdを押すと黒点は止まってしまいます。
エラーは出ません。実行はできます。
&の使い方が間違っていると思い、自分なりに色々試してみたのですがうまくいきません。
どのようなプログラムを書けば各キーボードとdを同時に押したときに速度を15に変化させることができるでしょうか?
教えてくださるとうれしいです。よろしくお願いします。
環境はwindowsです。VsualStudio2017のC#、フォームアプリケーションを使用しています。
どこに間違いがあるのかわからないので、コードはほぼすべて載せています。
追加の質問ですが、ゲームプログラミングの基礎についてのおすすめの本やサイトがありましたら教えてください。お願いします。独学でやっているのですが、行き詰まり気味です。
追加の質問のみに答えてくださる方も歓迎です。
public partial class Form1 : Form
{
Vector character1Pos;
int character1Radius;
public Form1()
{
InitializeComponent();
this.character1Pos = new Vector(300, 300);
this.character1Radius = 3;
}
private void Draw(object sender, PaintEventArgs e)
{
SolidBrush Brush1 = new
SolidBrush(Color.Black);
float p1x = (float)this.character1Pos.X - character1Radius;
float p1y = (float)this.character1Pos.Y - character1Radius;
e.Graphics.FillEllipse(Brush1, p1x, p1y, this.character1Radius * 2, this.character1Radius * 2);
}
private void KeyPressed(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
{
this.character1Pos.X -= 5;
}
else if (e.KeyChar == 's')
{
this.character1Pos.X += 5;
}
else if (e.KeyChar == 'z')
{
this.character1Pos.Y += 5;
}
else if (e.KeyChar == 'w')
{
this.character1Pos.Y -= 5;
}
else if (e.KeyChar == 'a' & e.KeyChar == 'd')
{
this.character1Pos.X -= 15;
}
else if (e.KeyChar == 's' & e.KeyChar == 'd')
{
this.character1Pos.X += 15;
}
else if (e.KeyChar == 'z' & e.KeyChar == 'd')
{
this.character1Pos.Y += 15;
}
else if (e.KeyChar == 'w' & e.KeyChar == 'd')
{
this.character1Pos.Y -= 15;
}
Invalidate();
return;
}
}