首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >参数无效-g图形

参数无效-g图形
EN

Stack Overflow用户
提问于 2019-12-27 02:30:00
回答 1查看 76关注 0票数 0

我正在用c#,Visual Studio写一个游戏。这个游戏是“太空入侵者”,正如你所知道的,我需要以某种方式做飞镖,所以我的老师告诉我用链条和类名Dart(GetX,GetY,SetX,SetY和绘画飞镖本身)来做这件事。当我在play游戏表单中编写我的代码时,一切都是正确的,但是视觉效果告诉我参数(g)无效,并且我已经尝试了这么多方法,但都不起作用。

如果我说得太多或做错了什么,我很抱歉,这是我第一次在这里问。The image is the Dart class

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
 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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-12-28 06:30:37

您可以从表单的OnPaint()方法中获取Graphics对象的引用g

下面是使用paint事件在窗体上绘制图像的示例代码:

代码语言:javascript
运行
复制
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);
    }
}

和表单代码:

代码语言:javascript
运行
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59492120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档