C#坦克大战

版权声明: https://blog.csdn.net/eds124/article/details/82314587
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int y = 900, x = 900;
        public Form1()
        {
            InitializeComponent();
            this.BackColor = Color.Black;//设置背景是黑色
            this.WindowState = FormWindowState.Maximized;//设置窗口最大化
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawRectangle(Pens.Red, x, y, 100, 100);
            g.DrawRectangle(Pens.Red, x + 25, y + 25, 50, 50);
            g.DrawRectangle(Pens.Red, x + 45, y, 10, 25);
            g.DrawRectangle(Pens.Red, x + 45, 0, 10, y);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.E == e.KeyCode && y > 0)
            {
                y -= 100;
            }
            else if (Keys.D == e.KeyCode && y < 900)
            {
                y += 100;
            }
            else if (Keys.S == e.KeyCode && x > 0)
            {
                x -= 100;
            }
            else if (Keys.F == e.KeyCode && x < 1800)
            {
                x += 100;
            }

            this.Refresh();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/eds124/article/details/82314587