0

我有简单的蛇游戏。有蛇和一种随机食物。但我想要另一种食物,但这种食物会受到时间限制。例如:当蛇达到20点时,它会产生奖励限时食物。该食物将显示 5 秒钟(例如)。我该怎么做?

public partial class Form1 : Form
    {
        Random randFood = new Random();

        private had _model;
        Graphics paper;
        had snakes = new had();
        kocky food;
        bool left = false;
        bool right = false;
        bool up = false;
        bool down = false;
        int score = 0;
        kocky1 food1;
        public Form1()
        {
            InitializeComponent();
            food = new kocky(randFood);
            food1 = new kocky1(randFood);  //time limited food//
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            paper = e.Graphics;
            food.drawFood(paper);
            food1.drawFood(paper);         
            snakes.drawSnake(paper);    
        }
//---------------------------program----------------------------------------------------//

 private void timer1_Tick(object sender, EventArgs e)
        {
            snakeScoreLabel.Text = Convert.ToString(score);
            if (down) { snakes.moveDown(); }
            if (up) { snakes.moveUp(); }
            if (right) { snakes.moveRight(); }
            if (left) { snakes.moveLeft(); }
            this.Invalidate();
            collision();
            for (int i = 0; i < snakes.SnakeRec.Length; i++)
            {
                if (snakes.SnakeRec[i].IntersectsWith(food.foodRec))
                {
                    WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
                    wplayer.URL = "C:\\Users\\Boris\\Desktop\\C# had\\SNOTPLOP.wav";
                    wplayer.controls.play(); 
                    timer1.Enabled = true;
                    score += 1;
                    ProgressBar1.Increment(1);
                    snakes.growSnake();
                    food.foodlocation(randFood);
                    food1.foodlocation(randFood);
                }
            if (snakes.SnakeRec[i].IntersectsWith(food1.foodRec))  //time limited food//
                {
                    WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
                    wplayer.URL = "C:\\Users\\Boris\\Desktop\\C# had\\SNOTPLOP.wav";
                    wplayer.controls.play();
                    timer1.Enabled = true;
                    score += 1;
                    ProgressBar1.Increment(1);
                    snakes.growSnake();
                    food.foodlocation(randFood);
                    food1.foodlocation(randFood);
                }
4

2 回答 2

1

您的限时食物可能有一个 int 持续时间,该持续时间会被计时器减少,并在 0 时自行删除。

于 2013-10-23T20:48:38.420 回答
0

首先是您想要制作的特殊食物,称为“Power up”。你可以创建这个的方式。就像你拿着零食块阵列一样多,你拿着长度为 1 的食物阵列

每种食物都有自己的状态:正常,特殊。

创建食物时设置时间戳或其他创建时间指示(如滴答声),然后当您达到生命秒数时,您可以杀死食物实例并将其从屏幕上删除

于 2013-10-23T20:54:29.393 回答