c#编的部分植物大战僵尸代码

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Media; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms;

namespace 植物大战僵尸2 {

public partial class Form1 : Form {

bool[,] bo = new bool[9, 5]; public Form1() {

InitializeComponent();

//激活窗体的双缓冲技术,可以注释掉看看是什么效果 SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true);

SetStyle(ControlStyles.OptimizedDoubleBuffer, true); }

string str = null; int m = 0; int n = 0;

List listpic = new List(); List listpic1 = new List(); List listpic2 = new List(); List listpic3 = new List(); List listpic4 = new List(); List listJiangshi = new List(); List listJiangshi1 = new List(); List listJiangshi2 = new List(); List listJiangshi3 = new List(); List listJiangshi4 = new List();

private void pictureBoxWandou_MouseDown(object sender, MouseEventArgs e) {

if ((e.Button == System.Windows.Forms.MouseButtons.Left)) {

PictureBox pictureBox = sender as PictureBox; str = pictureBox.Tag.ToString();

pictureBox.DoDragDrop(Properties.Resources.豌豆射手, DragDropEffects.Copy | DragDropEffects.Move);

//形成拖拽效果,移动+拷贝的组合效果 }

}

SoundPlayer sp;

private void Form1_DragEnter(object sender, DragEventArgs e) {

e.Effect = DragDropEffects.Copy; }

bool booltimer = true;

private void Form1_DragDrop(object sender, DragEventArgs e) {

LocationTransport(e.X - this.Location.X, e.Y - this.Location.Y, out m, out n); //拖放完毕之后,自动生成新控件

sp = new SoundPlayer(Properties.Resources.拖放);//必须是带.wav扩展名的音频文件

sp.Play(); if (bo[m, n]) {

PictureBox pictureBox = sender as PictureBox; PictureBox btn = new PictureBox();

btn.Size = new System.Drawing.Size(70, 75);

btn.Location = this.PointToClient(new Point(m * 75 + 50 + this.Location.X, n * 70 + 95 + this.Location.Y));

//用这个方法计算出客户端容器界面的X,Y坐标。否则直接使用X,Y是屏幕坐标

switch (str) {

case \

btn.Image = Properties.Resources.向日葵; break; case \

btn.Image = Properties.Resources.豌豆射手; a = mWandou = m * 75 + 50 + this.Location.X; nWandou = n * 70 + 95 + this.Location.Y; wandou = new Label();

ClassWandou cwandou = new ClassWandou(mWandou, nWandou, a, wandou);

cwandou.wandou.Size = new System.Drawing.Size(30, 30); cwandou.wandou.Image = Properties.Resources.豌豆;

cwandou.wandou.BackColor = System.Drawing.Color.Transparent; this.Controls.Add(cwandou.wandou);

switch (n) {

case 0:

listpic.Add(cwandou); break; case 1:

listpic1.Add(cwandou); break; case 2:

音频文件

listpic2.Add(cwandou); break; case 3:

listpic3.Add(cwandou); break; case 4:

listpic4.Add(cwandou); break; }

if (booltimer) {

timerWandou.Enabled = true; timerWandou1.Enabled = true; timerWandou2.Enabled = true; timerWandou3.Enabled = true; timerWandou4.Enabled = true; timer1.Enabled = true; booltimer = false; } break; case \

btn.Image = Properties.Resources.草莓炸弹; break; }

btn.BackColor = System.Drawing.Color.Transparent;

btn.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; btn.BackColor = System.Drawing.Color.Transparent; this.Controls.Add(btn); bo[m, n] = false; } else {

sp = new SoundPlayer(Properties.Resources.重复);//必须是带.wav扩展名的 sp.Play();

} }

private void LocationTransport(double x, double y, out int m1, out int n1) {

m1 = (int)(x - 50) / 75; n1 = ((int)(y - 95) / 70);

if (m1 < 0 || n1 < 0 || m1 > 8 || n1 > 4) {

m1 = 0; n1 = 0; } }

private void Form1_Load(object sender, EventArgs e) {

for (int i = 0; i < 9; i++) {

for (int j = 0; j < 5; j++) {

bo[i, j] = true; } }

for(int l=0;l<10;l++){

int mun = this.Location.X + 700; int suiji = ran.Next(5);

int nun = suiji * 70 + 95 + this.Location.Y; int suijiLife = ran1.Next(10);

Jiangshi jiangshi = new Jiangshi(suijiLife+5, mun, nun, picturejiangshi); jiangshi.picJiangshi.Location=this.PointToClient(new Point(mun,nun)); switch (suiji) {

case 0:

listJiangshi.Add(jiangshi); break; case 1:

listJiangshi1.Add(jiangshi); break; case 2:

listJiangshi2.Add(jiangshi); break; case 3:

listJiangshi3.Add(jiangshi); break; case 4:

listJiangshi4.Add(jiangshi);

联系客服:779662525#qq.com(#替换为@)