enemytanksprite.cs

来自「基于.net FrameWork的windows游戏-坦克大战游戏.用到了多线程」· CS 代码 · 共 118 行

CS
118
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

public class EnemyTankSprite : SpriteDrawing
{
    int formWidth, formHeight;
    int enemyTankWidth, enemyTankHeight;
    int direction;
    int enemyTankX, enemyTankY;
    Image[] enemyTankImg;
    Form TankGame2008;
    Random rand;
    Thread newThread;
    static int speed = 2;

    public EnemyTankSprite(Image[] enemyTankImg, int enemyTankX, int enemyTankY, Form TankGame2008)
        : base(enemyTankImg,enemyTankX,enemyTankY,TankGame2008)
    {
        this.enemyTankX = enemyTankX;
        this.enemyTankY = enemyTankY;
        this.enemyTankImg = enemyTankImg;
        this.enemyTankWidth = 50;
        this.enemyTankHeight = 50;
        this.TankGame2008 = TankGame2008;
        formWidth = 640;
        formHeight = 480;
        setVisible(true);
        setActive(true);
        rand = new Random();
    }

    public void updateState(int SpriteDirection)
    {
        switch (SpriteDirection)
        {
            case 0:
                enemyTankX = this.getX() - speed;
                if (enemyTankX < 0)
                {
                    enemyTankX = 0;
                    this.direction = rand.Next(4);
                }
                break;
            case 1:
                enemyTankX = this.getX() + speed;
                if (enemyTankX > formWidth - enemyTankWidth)
                {
                    enemyTankWidth = formWidth - enemyTankWidth;
                    this.direction = rand.Next(4);
                }
                break;
            case 2:
                enemyTankY = this.getY() - speed;
                if (enemyTankY < 0)
                {
                    enemyTankX = 0;
                    this.direction = rand.Next(4);
                }
                break;
            case 3:
                enemyTankY = this.getY() + speed;
                if (enemyTankY > formHeight - enemyTankWidth)
                {
                    enemyTankX = formHeight - enemyTankHeight;
                    this.direction = rand.Next(4);
                }
                break;
        }
        this.setPos(enemyTankX, enemyTankY);
    }

    public void startRuning()
    {
        newThread = new Thread(run);
        newThread.Start();
    }

    public void stopRuning()
    {
        newThread = null;
    }

    public int getTankDirection()
    {
        return direction;
    }

    public void setTankDirection(int direction)
    {
        this.direction = direction;
    }

    public void run()
    {
        while (newThread != null)
        {
            TankGame2008.Invalidate();
            try
            {
                Thread.Sleep(10);
            }
            catch
            {

            }
            if (this.getActive() == true)
            {
                updateState(direction);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?