延迟或循环播放列表?

问题描述 投票:0回答:2

我正在为我的大学作品开发这款游戏,基本上是太空侵略者,我有一个激光课,一个针对坏人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
c# winforms class delayed-execution
2个回答
1
投票
// 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
投票

尝试这样做可能会起作用

© www.soinside.com 2019 - 2024. All rights reserved.