1. 程式人生 > >坦克大戰0.2

坦克大戰0.2

fill class graphic 進行 image 分享 false init 背景色

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

namespace 坦克大戰0._2
{
    public partial class Form1 : Form
    {
        public Form1()
        {   
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          //this.Location = new Point(50, 50);//窗口的位置
          //this.DesktopLocation = new Point(50,50);
            this.Opacity = 1;//設置透明度
            this.Text = "坦克大戰0.2";//設置標題

            this.SetDesktopLocation(FormDimension.FormLoadX, FormDimension.FormLoadY);//窗口位置
            this.StartPosition = FormStartPosition.Manual;
            this.Size = new Size(FormDimension.FormX, FormDimension.FormY);

            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;//禁止手動調整大小
            this.MaximizeBox = false;//使最大化失效

            this.BackColor = Color.Orange; //設置背景色

            Button1 dd = new Button1();
            this.Controls.Add(dd);    

            dd.Click += dd_Click;     
        }

        static int GameState = 0;
        private void dd_Click(object sender, EventArgs e)
        {
            Button a = (Button)sender;

            if (GameState % 2 == 0)
            {
                GameState++;
                this.KeyPreview = true;//主窗體能否接受鍵盤事件
                a.Text = "遊戲進行中";   
            }
            else
            {
                GameState++;
                this.KeyPreview = false;
                a.Text = "暫停中";
            }
      
        }

        static int x = 50, y = 50;//坦克○坐標
        static int vx = x + 15, vy = y - 15;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Color c = this.BackColor;//獲取窗口背景色

            Pen a = new Pen(Color.Red);//new畫筆
            a.Width = 5;//設置畫筆寬度

          //e.Graphics.DrawEllipse(a, 20, 20, 50, 50);
            e.Graphics.FillEllipse(a.Brush, x,y,30,30);
            e.Graphics.DrawLine(a, x+15, y+15, vx, vy);

            this.BackColor = c;           
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.W || e.KeyCode == Keys.Up)
            {
                y -= 5;
                vx = x + 15;
                vy = y - 15;
                this.Refresh();
            }
            else if (e.KeyCode == Keys.S || e.KeyCode == Keys.Down)
            {
            
                y += 5;
                vx = x + 15;
                vy = y + 45;
                this.Refresh();
            }
            else if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left)
            {
                x -= 5;
                vx = x - 15;
                vy = y + 15;
                this.Refresh();
            }

            else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right)
            {
                x += 5;
                vx = x + 45;
                vy = y + 15;
                this.Refresh();
            }
        }
    }

    public class Button1 : Button
    { 
        public Button1 ()
        {
            this.Location = new Point((FormDimension.FormX / 2) - (this.Width / 2), 20);
            this.Text = "點擊開始";
            this.Height = 30;
            this.Width = 90;
            this.BackColor = Color.Gray;
        }     
    }
}  

  

技術分享

//添加暫停功能

坦克大戰0.2