如何使用箭头键移动形状?
How to make a shape move with the arrow keys?
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form
{
int vx = 5;
int vy = 5;
int bx = 0;
int by = 50;
int px = 93;
public Form1()
{
InitializeComponent();
timer1.Interval = 100;
timer1.Start();
}
public class Ball
{
public int X;
public int Y;
public int W;
public int H;
public Ball(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
public class Paddle
{
public int X;
public int Y;
public int W;
public int H;
public Paddle(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
public class Brick
{
public int X;
public int Y;
public int W;
public int H;
public Brick(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
int[] brickxs = { 0, 51, 102, 153, 204, 255, 306, 357, 408, 459, 510, 561, 612, 663, 714, 765 };
int bc = 0;
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Ball b = new Ball(55, 55, 25, 25);
Paddle p = new Paddle(93, 377, 130, 30);
Brick br = new Brick(20, 20, 51, 20);
br.X = 0;
while (bc < 16)
{
br.X = brickxs[bc];
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(br.X, 0, 49, 20));
myBrush.Dispose();
formGraphics.Dispose();
bc = bc + 1;
}
Rectangle ball = new Rectangle(bx, by, b.W, b.H);
Rectangle paddle = new Rectangle(px, p.Y, p.W, p.H);
//Rectangle brick = new Rectangle(br.X, br.Y, br.W, br.H);
e.Graphics.FillEllipse(blueBrush, ball);
e.Graphics.FillRectangle(blueBrush, paddle);
//e.Graphics.FillRectangle(blueBrush, brick);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Right)
{
px += 5;
}
}
private void MoveTimer_Tick(object sender, EventArgs e)
{
Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
bx = bx + vx;
by = by + vy;
if (px <= 0)
{
px = 0;
}
if (px >= 771)
{
px = 771;
}
WallCollision();
floorandCeilingCollision();
Invalidate();
}
public void WallCollision()
{
if (bx >= 771)
{
vx = -5;
}
if (bx <= 0)
{
vx += 5;
}
}
public void floorandCeilingCollision()
{
if (by >= 420)
{
vy = -5;
}
if (by <= 0)
{
vy = 5;
}
}
}
我正在制作游戏,需要一些帮助。
在我的代码中,游戏的每个部分都有 类:球、球拍和积木。数组定位砖块。
我想用箭头键左右移动桨(它只是一个矩形)。我尝试使用按键按下的方法,但没有用。
您能否提出任何解决方案或指出我遗漏的任何内容?
就我个人而言,我使用 e.KeyCode 而不是 e.KeyData,先试试这个。
确保您的表单具有焦点,而不是图片框或您在游戏中可能拥有的其他东西。因为您尝试为窗体调用 KeyDown 事件,而不是为窗体中的控件调用。
我从未使用过 Paint 事件,你确定它被调用了吗?可能是您的游戏记录了移动但从未向您显示更改。我通常有一个单独的绘图方法,每次有变化我都会调用它,你也应该试试这个。
如果没有任何效果,请尝试调试。在您的 KeyDown 方法中设置一个断点以查看它是否被调用。如果是,请在 Paint 方法中设置它。这个肯定会在运行时被调用一次,但是如果你在那个时候单击“继续”并尝试移动你的对象。如果其他时间没有调用它,那么这里就是你的答案:)
请告诉我你在尝试这些事情后的发现,如果你遇到困难或根本不知道还能做什么,请问我下一步该怎么做:)
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form
{
int vx = 5;
int vy = 5;
int bx = 0;
int by = 50;
int px = 93;
public Form1()
{
InitializeComponent();
timer1.Interval = 100;
timer1.Start();
}
public class Ball
{
public int X;
public int Y;
public int W;
public int H;
public Ball(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
public class Paddle
{
public int X;
public int Y;
public int W;
public int H;
public Paddle(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
public class Brick
{
public int X;
public int Y;
public int W;
public int H;
public Brick(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
int[] brickxs = { 0, 51, 102, 153, 204, 255, 306, 357, 408, 459, 510, 561, 612, 663, 714, 765 };
int bc = 0;
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Ball b = new Ball(55, 55, 25, 25);
Paddle p = new Paddle(93, 377, 130, 30);
Brick br = new Brick(20, 20, 51, 20);
br.X = 0;
while (bc < 16)
{
br.X = brickxs[bc];
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(br.X, 0, 49, 20));
myBrush.Dispose();
formGraphics.Dispose();
bc = bc + 1;
}
Rectangle ball = new Rectangle(bx, by, b.W, b.H);
Rectangle paddle = new Rectangle(px, p.Y, p.W, p.H);
//Rectangle brick = new Rectangle(br.X, br.Y, br.W, br.H);
e.Graphics.FillEllipse(blueBrush, ball);
e.Graphics.FillRectangle(blueBrush, paddle);
//e.Graphics.FillRectangle(blueBrush, brick);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Right)
{
px += 5;
}
}
private void MoveTimer_Tick(object sender, EventArgs e)
{
Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
bx = bx + vx;
by = by + vy;
if (px <= 0)
{
px = 0;
}
if (px >= 771)
{
px = 771;
}
WallCollision();
floorandCeilingCollision();
Invalidate();
}
public void WallCollision()
{
if (bx >= 771)
{
vx = -5;
}
if (bx <= 0)
{
vx += 5;
}
}
public void floorandCeilingCollision()
{
if (by >= 420)
{
vy = -5;
}
if (by <= 0)
{
vy = 5;
}
}
}
我正在制作游戏,需要一些帮助。
在我的代码中,游戏的每个部分都有 类:球、球拍和积木。数组定位砖块。
我想用箭头键左右移动桨(它只是一个矩形)。我尝试使用按键按下的方法,但没有用。
您能否提出任何解决方案或指出我遗漏的任何内容?
就我个人而言,我使用 e.KeyCode 而不是 e.KeyData,先试试这个。
确保您的表单具有焦点,而不是图片框或您在游戏中可能拥有的其他东西。因为您尝试为窗体调用 KeyDown 事件,而不是为窗体中的控件调用。
我从未使用过 Paint 事件,你确定它被调用了吗?可能是您的游戏记录了移动但从未向您显示更改。我通常有一个单独的绘图方法,每次有变化我都会调用它,你也应该试试这个。
如果没有任何效果,请尝试调试。在您的 KeyDown 方法中设置一个断点以查看它是否被调用。如果是,请在 Paint 方法中设置它。这个肯定会在运行时被调用一次,但是如果你在那个时候单击“继续”并尝试移动你的对象。如果其他时间没有调用它,那么这里就是你的答案:)
请告诉我你在尝试这些事情后的发现,如果你遇到困难或根本不知道还能做什么,请问我下一步该怎么做:)