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; } } }
//添加暂停功能
时间: 2024-10-19 17:34:42