【问题标题】:C# picturebox and timerC# 图片框和计时器
【发布时间】:2014-05-27 19:27:48
【问题描述】:

我正在尝试使用图片框和计时器创建汽车游戏

这是我的代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    PictureBox car = new PictureBox();
    Timer t = new Timer();

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        //Right arrow key
        if (e.KeyCode.Equals(Keys.Right) && carPlayer.Location.X < 300)
        {
            int x = carPlayer.Location.X + 5;
            int y = carPlayer.Location.Y;
            int width = carPlayer.Size.Width;
            int height = carPlayer.Size.Height;
            carPlayer.SetBounds(x, y, width, height);
        }

        //Left arrow key
        if (e.KeyCode.Equals(Keys.Left) && carPlayer.Location.X > 35)
        {
            int x = carPlayer.Location.X - 5;
            int y = carPlayer.Location.Y;
            int width = carPlayer.Size.Width;
            int height = carPlayer.Size.Height;
            carPlayer.SetBounds(x, y, width, height);
        }

        if (e.KeyCode.Equals(Keys.Space))
        {
            spawnCar();
        }
    }


    void spawnCar()
    {
        string[] cars = { "data/car_red.png", "data/car_blue.png", "data/car_green.png", "data/car_grey.png" };
        Random rand = new Random();
        car.SizeMode = PictureBoxSizeMode.StretchImage;
        car.Image = Image.FromFile(cars[rand.Next(0, 4)]);
        car.Visible = true;

        if (rand.Next(0,2) == 0)
        {
            car.SetBounds(100, 10, 50, 85);
        }
        else
        {
            car.SetBounds(250, 10, 50, 85);
        }

        this.Controls.Add(car);
        car.BringToFront();

        t.Interval = 1;
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }

    private void t_Tick(object sender, EventArgs e)
    {
        if (car.Bounds.IntersectsWith(carPlayer.Bounds))
        {
            t.Stop();
            car.Image = Image.FromFile("data/car_wreck.png");
            carPlayer.Image = Image.FromFile("data/player_wreck.png");
        }
        if (car.Bounds.Y > 340)
        {
            t.Stop();
            this.Controls.Remove(car);
        }
        else
        {
            car.Top++;
        }
    }
}

http://i.stack.imgur.com/NhCgY.png

现在,当我在汽车出现在顶部并缓慢向下移动并在到达底部时消失时按空格,但是当我多次按空格时,汽车的速度会越来越快。 任何人都请帮我让汽车在每次创建时都以相同的速度移动。

谢谢

【问题讨论】:

  • 我不建议使用 PictureBox 来制作游戏。如果您绝对必须使用 WinForms,那么至少使用 Panel 和 OnPaint 事件。另外,请阅读双缓冲
  • 我知道这不是制作游戏的最佳方式,它只是为了好玩。

标签: c# timer picturebox


【解决方案1】:

问题是您在每次生成汽车时都注册了一个新的Tick 事件处理程序,您只想这样做一次。但是,没有一种简单的方法可以检查是否已分配处理程序,因此我建议使用全局标志...

//at class level
bool eventSet = false;

//in spawn method
t.Interval = 1;
if(!eventSet)//check if no handler assigned yet
{
    t.Tick += new EventHandler(t_Tick);
    eventSet = true;
}
t.Start();

或者,您可以尝试在分配之前删除处理程序...

//in spawn method
t.Interval = 1;
t.Tick -= new EventHandler(t_Tick);//remove previous one if it exists
t.Tick += new EventHandler(t_Tick);
t.Start();

【讨论】:

  • 它不工作我得到这个错误,它不会编译。错误1 事件'System.Windows.Forms.Timer.Tick'只能出现在+=或-=左侧
  • @user3678586:我添加了一个可能更简单的替代方案,假设它有效
猜你喜欢
  • 1970-01-01
  • 2019-04-19
  • 2018-01-09
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 2014-09-09
  • 1970-01-01
相关资源
最近更新 更多