有延迟或循环列表的功能吗?

时间:2019-04-07 11:44:40

标签: c# winforms class delayed-execution

因此我正在为我的大学作品开发这款游戏,基本上是太空入侵者,我有一个针对激光的班级和一个针对坏人atm的班级,它只能设置为激活5个坏人从屏幕顶部掉落,我想做的是要么循环列表,然后说5秒钟后重复该列表,另外5个坏人出现在屏幕顶部,要么制作一个包含100个坏人的列表,但在5个延迟之后再延迟5个,以此类推

namespace SpaceInvaders

{

public partial class Form1 : Form
{

    private List<Invader> invaders = new List<Invader>();
    private List<Laser> lasers = new List<Laser>();

    int invaderNumber = 0;
    int score = 0;



    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.Equals(Keys.W))
        {
            if (SpaceFighter.Top > 0)
            {
                SpaceFighter.Top = SpaceFighter.Top - 30;

            }
        }
        if (e.KeyCode.Equals(Keys.A))
        {
            if (SpaceFighter.Left > 0)
            {
                SpaceFighter.Left = SpaceFighter.Left - 10;

            }
        }
        if (e.KeyCode.Equals(Keys.D))
        {
            if (SpaceFighter.Right < this.Width)
            {
                SpaceFighter.Left = SpaceFighter.Left + 10;

            }
        }
        if (e.KeyCode.Equals(Keys.S))
        {
            if (SpaceFighter.Bottom < this.Height - 10)
            {
                SpaceFighter.Top = SpaceFighter.Top + 10;

            }
        }
        if (e.KeyCode.Equals(Keys.Space))
        {
            this.lasers.Add(new Laser(this, SpaceFighter));
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

            if (invaderNumber > 4)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
            }
            else
            {
                invaders.Add(new Invader(this));
                invaderNumber++;
            }



    }

    private void timer2_Tick(object sender, EventArgs e)
    {

        invaders.RemoveAll(ship => ship.isDisposed);
        foreach(Invader ship in invaders)
        {
            ship.MoveInvader(this);
            if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds))
            {
                timer2.Enabled = false;
                MessageBox.Show("You Lose!");
                return;

            }
        }

        lasers.RemoveAll(laser => laser.isDisposed);
        foreach (Laser laser in lasers)
        {
            laser.MoveLaser(this);
            foreach (Invader ship in invaders)
            {
                if (laser.laser.Bounds.IntersectsWith(ship.ship.Bounds))
                {
                    laser.isDisposed = true;
                    laser.laser.Dispose();
                    ship.isDisposed = true;
                    ship.ship.Dispose();
                    score = score + 2;
                    lblScore.Text = score.ToString();
                }
            }

        }

form
https://pastebin.com/P9enYPN5

laser class
https://pastebin.com/nvZ1VU8C

bad guys class
https://pastebin.com/eByLZ1Q8

2 个答案:

答案 0 :(得分:0)

// function for delaying     
System.Threading.Thread.Sleep(1000 * 5);

// loop with delay
while (true/* condition */)
{
    // some more code
    //..

    // delay
    System.Threading.Thread.Sleep(1000 * 5);
}

答案 1 :(得分:0)

尝试这样做可能会起作用

// initialize 
int delayNoOfBadGuy = 0;

// before releasing every badguy increment with 1
delayNoOfBadGuy++;

if (delayNoOfBadGuy >= 5)
{
    // set to zero
    delayNoOfBadGuy = 0;

    // add delay
    System.Threading.Thread.Sleep(1000 * 5);
}

// rest of code