我无法弄清楚为什么我的图像不会重新绘制

时间:2013-10-08 00:01:57

标签: c# windows winforms paint picturebox

我的图像绝对是移动的,但无论我做什么,我都无法得到它,所以图像重新粉刷! GameManger描绘了cat,而form1将paint args和object传递给了gamemanager。

Form1中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CatAndMouse
{
    public partial class Form1 : Form
    {
        GameManager myGM = new GameManager();
        int dir = 0;
        public Form1()
        {
            InitializeComponent();
            newGame();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.myGM != null)
                this.myGM.paint(e.Graphics);
            //e.Graphics.DrawImage(imgMouse.Images[0], pointXMouse, pointYMouse);
            //e.Graphics.DrawImage(imgCat.Images[0], 50, 100);
            //e.Graphics.DrawImage(imgCheese.Images[0], 75, 100);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                dir = 0;
            }
            if (e.KeyCode == Keys.Right)
            {
                dir = 1;
            }
            if (e.KeyCode == Keys.Down)
            {
                dir = 2;
            }
            if (e.KeyCode == Keys.Left)
            {
                dir = 3;
            }
        }
        public void newGame()
        {
            timer1.Start();
            myGM.newGame(imgCat, imgMouse, imgCheese, this);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }
        public int getDir()
        {
            return dir;
        }
    }
}

游戏管理:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CatAndMouse
{
    class GameManager
    {
        Form1 myForm;
        Cat ca1 = new Cat();
        Mouse m = new Mouse();
        Cheese ch = new Cheese();
        int amount = 5;
        int catdir = 0;
        Timer time = new Timer();
        public ImageList imgCat = new ImageList();
        public ImageList imgMouse = new ImageList();
        public ImageList imgCheese = new ImageList();

        public void newGame(ImageList cat, ImageList mouse, ImageList cheese, Form1 form)
        {
            imgCat = cat;
            imgMouse = mouse;
            imgCheese = cheese;
            myForm = form;
            time.Start();
        }

        public void move()
        {
            ca1.Move(amount, catdir);
            m.Move(amount, catdir);
        }

        public void paint(Graphics g)
        {
            g.DrawImage(imgCat.Images[0], ca1.getLocation());
        }

        private void time_Tick(object sender, EventArgs e)
        {
            move();
            getDir();
            myForm.Refresh();
        }
        public void getDir()
        {
            catdir = myForm.getDir();
        }
    }
}

猫:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CatAndMouse
{
    class Cat: Moveable
    {
        Random myCLoc = new Random();
        private Moveable myCatMove;
        public Point p = new Point(100, 100);
        int dir = 0;

        public void Move(int n, int d)
        {
            dir = d;
            if (dir == 0)
            {
                p.Y = p.Y - n;
            }
            if (dir == 1)
            {
                p.X = p.X + n;
            }
            if (dir == 2)
            {
                p.Y = p.Y + n;
            }
            if (dir == 3)
            {
                p.X = p.X - n;
            }
        }
        public void changeDirection()
        {
        }

        public Point getLocation()
        {
            return p;
        }

        public void paint(PaintEventArgs e)
        {

        }
    }
}

无论我做什么,都不会重复!

3 个答案:

答案 0 :(得分:1)

如果启用了计时器并且触发了PictureBox.Paint事件,请仔细检查(不要害怕使用调试器)。如果不能尝试不使用PictureBox.Paint事件和Refresh方法,请直接在Timer.Tick处理程序中调用myGM.paint

答案 1 :(得分:1)

这在PictureBoxes和其他Controls

中一直对我有用
PictureBox.Refresh();
PictureBox.Update();

OR

PictureBox.Update();
PictureBox.Refresh();

虽然我不记得哪个

答案 2 :(得分:0)

我必须将time.Tick +=new EventHandler(time_Tick);添加到GameManager构造函数中,显然这是计时器如何连接time_tick方法。

相关问题