我正在用c#,Visual Studio写一个游戏。这个游戏是“太空入侵者”,正如你所知道的,我需要以某种方式做飞镖,所以我的老师告诉我用链条和类名Dart(GetX,GetY,SetX,SetY和绘画飞镖本身)来做这件事。当我在play游戏表单中编写我的代码时,一切都是正确的,但是视觉效果告诉我参数(g)无效,并且我已经尝试了这么多方法,但都不起作用。
如果我说得太多或做错了什么,我很抱歉,这是我第一次在这里问。The image is the Dart class
public partial class PlayGame : Form
{
Graphics g;
SpaBoard sb;
bool right;
int countMove = 0;
Lives life;
int score= 0;
int m = 0;
int s = 0;
Node<Dart> chain = null;
public PlayGame()
{
InitializeComponent();
}
private void PlayGame_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
sb.PaintSpaBoard(g);
if (chain == null)
timer3.Stop();
while (chain != null)
{
Node<Dart> p = chain;
while (p != null)
{
p.GetValue().PaintDart(g);
p = chain.GetNext();
}
}
private void PlayGame_MouseClick(object sender, MouseEventArgs e)
{
g = CreateGraphics();
}
private void PlayGame_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyData == Keys.Right)
{
x = x + 20;
if (x >= 1800)
x = 1800;
Point p = new Point(x, y);
pictureBox1.Location = p;
pictureBox1.Refresh();
}
if (e.KeyData == Keys.Left)
{
x = x - 20;
if (x <= 100)
x = 100;
Point p = new Point(x, y);
pictureBox1.Location = p;
pictureBox1.Refresh();
}
if (e.KeyData == Keys.Space)
{
x = x + 75;
Dart d = new Dart();
d.SetX(x);
d.SetY(y);
if (chain == null)
{
chain=new Node<Dart>(d);
}
else
{
chain.SetNext(new Node<Dart>(d));
}
Dart p = chain.GetValue();
p.PaintDart(g);
timer3.Start();
}
private void timer3_Tick(object sender, EventArgs e)
{
while (chain != null)
{
if (chain.GetValue().GetY() > 50)
{
chain.GetValue().SetY(chain.GetValue().GetY() - 10);
}
chain = chain.GetNext();
}
if (chain == null)
timer3.Stop();
}
//Class Dart
class Dart
{
private int x;
private int y;`
public Dart() {
this.x = 0;
this.y = 0;
}
public int GetX() { return this.x; }
public void SetX(int x) { this.x = x; }
public int GetY() { return this.y; }
public void SetY(int Y) { this.y = y; }
public void PaintDart(Graphics g) {
Image pic = Image.FromFile("Dart.png");
Point p = new Point(this.x, this.y);
g.DrawImage(pic, p);
}
}
发布于 2019-12-28 06:30:37
您可以从表单的OnPaint()
方法中获取Graphics
对象的引用g
。
下面是使用paint事件在窗体上绘制图像的示例代码:
public class Dart
{
private int x;
private int y;
private readonly Image image;
public int X
{
get { return x; }
set {x=value;}
}
public int Y
{
get { return y; }
set {y=value; }
}
public Image Image
{
get
{
return image;
}
}
public Dart()
{
this.X = 0;
this.Y = 0;
this.image = Image.FromFile("Dart.png");
}
public void PaintDart(Graphics g)
{
g.DrawImage(image, x, y);
}
}
和表单代码:
public partial class Form1 : Form
{
private Dart dart;
public Form1()
{
InitializeComponent();
this.dart = new Dart();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// center the origin to the form
e.Graphics.TranslateTransform(ClientSize.Width/2, ClientSize.Height/2);
dart.PaintDart(e.Graphics);
}
}
https://stackoverflow.com/questions/59492120
复制相似问题