.net framework 创建一个毛坯游戏

**主要应用的是tiemr_tick控件执行界面的刷新

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Game
{
    public partial class Form1 : Form
    {
        Bitmap paper;
        Graphics g;
        Falcon falcon;
        const float speed = 6;
        List<GameObject> objs;
        

        public Form1()
        {
            InitializeComponent();
        }
        //Start()
        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化
            this.SetBounds(
                0,
                0,
                Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height);

            paper = new Bitmap(
                Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height,
                PixelFormat.Format32bppArgb);

            g = Graphics.FromImage(paper);
            falcon = new Falcon();
            objs = new List<GameObject>();
            objs.Add(falcon);
            ; timer1.Start();
            
        }

        //Update()
        private void timer1_Tick(object sender, EventArgs e)
        {
            g.Clear(Color.Gold);

            //更新方向
            ChangeDirection();

            //渲染
            foreach (var item in objs)
            {
                item.Draw(g);
            }
            box.Image = paper;
        }
        enum Direction
        {
            None, Up, Down, Left, Right,
            UpLeft, UpRight, DownLeft, DownRight
        };
        private Direction direct = Direction.None;

        private bool wDown = false;
        private bool aDown = false;
        private bool sDown = false;
        private bool dDown = false;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Application.Exit();
            //移动飞机
            if (e.KeyCode == Keys.W)
                wDown = true;
            if (e.KeyCode == Keys.A)
                aDown = true;
            if (e.KeyCode == Keys.S)
                sDown = true;
            if (e.KeyCode == Keys.D)
                dDown = true;



            //发射子弹
            if (e.KeyCode == Keys.J)
            {
                for (int i = 11; i < 12; i++)
                {
                    Bullet bullet = new Bullet();
                    bullet.X = falcon.X - 683 + i * 60;
                    bullet.Y = falcon.Y;
                    objs.Add(bullet);
                }
            }
            if (e.KeyCode == Keys.K)
            {
                for (int i = 11; i < 12; i++)
                {
                    Enemy enemy = new Enemy();
                    enemy.X = falcon.X - 646 + i * 60;
                    enemy.Y = falcon.Y + 50;
                    objs.Add(enemy);
                }
            }
            if (e.KeyCode == Keys.L)
            {
                for (int i = 11; i < 12; i++)
                {
                    Bullet1 bullet1 = new Bullet1();
                    bullet1.X = falcon.X - 683 + i * 60;
                    bullet1.Y = falcon.Y;
                    objs.Add(bullet1);
                }
            }
            if (e.KeyCode == Keys.L)
            {
                for (int i = 11; i < 12; i++)
                {
                    Bullet2 bullet2 = new Bullet2();
                    bullet2.X = falcon.X - 683 + i * 60;
                    bullet2.Y = falcon.Y;
                    objs.Add(bullet2);
                }
            }
            if (e.KeyCode == Keys.L)
            {
                for (int i = 11; i < 12; i++)
                {
                    Bullet3 bullet3 = new Bullet3();
                    bullet3.X = falcon.X - 683 + i * 60;
                    bullet3.Y = falcon.Y;
                    objs.Add(bullet3);
                }
            }

        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            //direct = Direction.None;
            if (e.KeyCode == Keys.W)
                wDown = false;
            if (e.KeyCode == Keys.A)
                aDown = false;
            if (e.KeyCode == Keys.S)
                sDown = false;
            if (e.KeyCode == Keys.D)
                dDown = false;
        }

        private void ChangeDirection()
        {
            bool UpLeft = wDown && aDown;
            bool UpRight = wDown && dDown;
            bool DownLeft = sDown && aDown;
            bool DownRight = sDown && dDown;
            if (UpLeft)
            {
                direct = Direction.UpLeft;
                falcon.X -= speed;
                falcon.Y -= speed;
            }
            else if (UpRight)
            {
                direct = Direction.UpRight;
                falcon.X += speed;
                falcon.Y -= speed;
            }
            else if (DownLeft)
            {
                direct = Direction.DownLeft;
                falcon.X -= speed;
                falcon.Y += speed;
            }
            else if (DownRight)
            {
                direct = Direction.DownRight;
                falcon.X += speed;
                falcon.Y += speed;
            }
            else if (wDown)
            {
                direct = Direction.Up;
                falcon.Y -= speed;
            }
            else if (aDown)
            {
                direct = Direction.Left;
                falcon.X -= speed;
            }
            else if (sDown)
            {
                direct = Direction.Down;
                falcon.Y += speed;
            }
            else if (dDown)
            {
                direct = Direction.Right;
                falcon.X += speed;
            }
            else
            {
                direct = Direction.None;
            }
        }

     
    }



    public abstract class GameObject
    {
        public float X = 800;
        public float Y = 400;
        public abstract void Draw(Graphics g);
    }

    public class Falcon : GameObject
    {
        Bitmap falcon;
        public Falcon()
        {
            falcon = new Bitmap("falcon.png");
        }
        public override void Draw(Graphics g)
        {
            g.DrawImage(falcon, X, Y, falcon.Width, falcon.Height);
        }
    }

    public class Bullet : GameObject
    {
        Bitmap bullet;
        public Bullet()
        {
            bullet = new Bitmap("bullet.png");
        }
        public override void Draw(Graphics g)
        {
            Y -= 30;
            g.DrawImage(bullet, X, Y, bullet.Width, bullet.Height);
        }
    }
    public class Enemy : GameObject
    {
        Bitmap enemy;
        public Enemy()
        {
            enemy = new Bitmap("big.png");
        }
        public override void Draw(Graphics g)
        {
            Y -= 5;
            g.DrawImage(enemy, X, Y, enemy.Width, enemy.Height);
        }
    }
    public class Bullet1 : GameObject
    {
        Bitmap bullet1;
        public Bullet1()
        {
            bullet1 = new Bitmap("bullet.png");
        }
        public override void Draw(Graphics g)
        {
            Y -= 20;
            X += 20;
            g.DrawImage(bullet1, X, Y, bullet1.Width, bullet1.Height);
        }
    }
    public class Bullet2 : GameObject
    {
        Bitmap bullet2;
        public Bullet2()
        {
            bullet2 = new Bitmap("bullet.png");
        }
        public override void Draw(Graphics g)
        {
            Y -= 20;
            X -= 20;
            g.DrawImage(bullet2, X, Y, bullet2.Width, bullet2.Height);
        }
    }
    public class Bullet3 : GameObject
    {
        Bitmap bullet3;
        public Bullet3()
        {
            bullet3 = new Bitmap("bullet.png");
        }
        public override void Draw(Graphics g)
        {
            Y -= 20;
            g.DrawImage(bullet3, X, Y, bullet3.Width, bullet3.Height);
        }
    }

}

在这里插入图片描述
能实现这样一种毛坯游戏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值