zoukankan      html  css  js  c++  java
  • 俄罗斯方块

    俄罗斯方块

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Threading.Tasks;
      9 using System.Windows.Forms;
     10 
     11 namespace ShapeUI
     12 {
     13     public partial class forShape : Form
     14     {
     15         public forShape()
     16         {
     17             InitializeComponent();
     18         }
     19         private void forShape_Load(object sender, EventArgs e)
     20         {
     21             lblGrade.Text = "0";//显示等级
     22             lblScore.Text = "0";//显示分数
     23             Helper.Speed = 550;
     24             tirShape.Interval = Helper.Speed;
     25             lblSpeed.Text = Helper.Speed.ToString();//显示速度
     26         }
     27         private bool Stop(int key) //结束操作
     28         {
     29             bool b = true;
     30             foreach (Control c in gpbGame.Controls)
     31             {
     32                 if (c.Enabled)
     33                 {
     34                     int x = c.Location.X;
     35                     int y = c.Location.Y;
     36                     //判断是否撞墙
     37                     if (x >= gpbGame.Width - 20 && key == 68)
     38                     {
     39                         b = false;
     40                         break;
     41                     }
     42                     else if (x <= 0 && key == 65)
     43                     {
     44                         b = false;
     45                         break;
     46                     }
     47                     else if (y >= gpbGame.Height - 20 && key == 83)
     48                     {
     49                         b = false;
     50                         Enableds();//如果是下落操作碰到墙则固定形状
     51                         break;
     52                     }
     53                 }
     54             }
     55             return b;
     56         }
     57         public void Enableds()//固定
     58         {
     59             foreach (Control c in gpbGame.Controls)
     60             {
     61                 if (c.Enabled)
     62                 {
     63                     c.KeyDown -= buttons_KeyDown;//消除按钮事件
     64                     c.Enabled = false;
     65                     int Red = new Random().Next(1, 256);
     66                     int Green = new Random().Next(1, 256);
     67                     int Blue = new Random().Next(1, 256)>100?250:0;
     68                     //固定变换颜色
     69                     c.BackColor = Color.FromArgb(Red, Green, Blue);
     70                 }
     71                 //判断游戏结束
     72                 if (!c.Enabled && c.Location.Y <= 40)
     73                 {
     74                     tirShape.Stop();
     75                     MessageBox.Show("Game Over");//游戏结束,初始化游戏窗口
     76                     lblGrade.Text = "0";//初始化等级
     77                     lblScore.Text = "0";//初始化分数
     78                     Helper.Speed = 550;
     79                     tirShape.Interval = Helper.Speed;
     80                     lblSpeed.Text = Helper.Speed.ToString();//初始化速度
     81                     this.gpbGame.Controls.Clear();
     82                     return;
     83                 }
     84             }
     85             GetShape();//生成新的形状
     86         }
     87         private void GetShape() //生成形状
     88         {
     89             Helper.Score(gpbGame, lblScore);//加分判断
     90             lblGrade.Text = Helper.Grade.ToString();//刷新等级
     91             tirShape.Interval = Helper.Speed;
     92             lblSpeed.Text = Helper.Speed.ToString();//刷新速度
     93             IShapes s = null;
     94             if(checkAdd.Checked)
     95                 s = CreateShape.createShape(-1);//得分模式
     96             else
     97                 s = CreateShape.createShape(new Random().Next(1, 6));//获取形状
     98             foreach (Control c in s.Getbuttons())
     99             {
    100                 c.KeyDown += buttons_KeyDown;//添加事件
    101                 gpbGame.Controls.Add(c);
    102                 c.Focus();//获取焦点
    103             }
    104         }
    105         private void buttons_KeyDown(object sender, KeyEventArgs e)//键盘事件
    106         {
    107             MoveShape(e.KeyValue);//移动判断
    108         }
    109         private bool Impacts(int x, int y, int key) //向下碰撞到方块
    110         {
    111             bool b = true;
    112             foreach (Control c in gpbGame.Controls)
    113             {
    114                 if (c.Enabled)
    115                 {
    116                     //判断是否碰撞到方块
    117                     if (c.Location.X == x && c.Location.Y == y)
    118                     {
    119                         if (key == 83)//如果是向下降落的操作便固定
    120                             Enableds();
    121                         b = false;
    122                         break;
    123                     }
    124 
    125                 }
    126             }
    127             return b;
    128         }
    129         private void MoveShape(int key)//移动位置
    130         {
    131             if (Stop(key))//是否碰撞
    132             {
    133                 //预计下一步将要发生的事情
    134                 foreach (Control c in gpbGame.Controls)
    135                 {
    136                     if (!c.Enabled)
    137                     {
    138                         int x = c.Location.X;
    139                         int y = c.Location.Y;
    140                         if (key == 65)
    141                             x += 20;
    142                         else if (key == 68)
    143                             x -= 20;
    144                         else if (key == 87)
    145                         {
    146                             //ChangeShape();
    147                         }
    148                         else if (key == 83)
    149                         {
    150                             y -= 20;
    151                         }
    152                         if (!Impacts(x, y, key))//判断是否与方块碰撞,是则结束
    153                             return;
    154                     }
    155                 }
    156                 //如果预计通过则实施操作
    157                 foreach (Control c in gpbGame.Controls)
    158                 {
    159                     if (c.Enabled)
    160                     {
    161                         int x = c.Location.X;
    162                         int y = c.Location.Y;
    163                         if (key == 65)
    164                             x -= 20;
    165                         else if (key == 68)
    166                             x += 20;
    167                         else if (key == 87)
    168                         {
    169                             Helper.ChangeShape(gpbGame);//变换
    170                             return;//变换形状后结束方法,否则会导致循环变换
    171                         }
    172                         else if (key == 83)
    173                         {
    174                             y += 20;
    175                         }
    176                         c.Location = new Point(x, y);//移动操作
    177                     }
    178                 }
    179             }
    180         }
    181         private void btnBegin_Click(object sender, EventArgs e)//开始游戏
    182         {
    183             if (btnBegin.Text == "游戏开始")
    184             { 
    185                 btnBegin.Text = "重新开始";
    186             }
    187             else
    188             {
    189                 btnBegin.Text = "游戏开始";
    190             }
    191             lblGrade.Text = "0";//初始化等级
    192             lblScore.Text = "0";//初始化分数
    193             Helper.Speed = 550;
    194             tirShape.Interval = Helper.Speed;
    195             lblSpeed.Text = Helper.Speed.ToString();//初始化速度
    196             this.gpbGame.Controls.Clear();//初始化游戏窗口
    197             GetShape();//生成形状
    198             tirShape.Start();
    199         }
    200 
    201         private void btnEnd_Click(object sender, EventArgs e)//游戏结束
    202         {
    203             tirShape.Stop();
    204             Application.Exit();
    205         }
    206 
    207         private void tirShape_Tick(object sender, EventArgs e)//计时器
    208         {
    209             MoveShape(83);//默认自动下降操作
    210         }
    211 
    212         private void btnStop_Click(object sender, EventArgs e)//暂停游戏
    213         {
    214             if (tirShape.Enabled)
    215             {
    216                 tirShape.Stop();
    217                 forStop f = new forStop();
    218                 f.ShowDialog();//暂停中
    219                 tirShape.Start();//暂停结束
    220             }    
    221         }
    222 
    223         private void btnCue_Click(object sender, EventArgs e)//游戏操作提示
    224         {
    225             forCue f = new forCue();//提示
    226             f.ShowDialog();//显示提示
    227         }
    228     }
    229 }
    View Code
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Drawing;
      4 using System.Linq;
      5 using System.Text;
      6 using System.Threading.Tasks;
      7 using System.Windows.Forms;
      8 
      9 namespace ShapeUI
     10 {
     11     public static class Helper
     12     {
     13         public static int Speed { get; set; }//速度
     14         public static int Grade { get; set; }//等级
     15         public static List<button> Getbuttons(int[,] points) //组成形状
     16         {
     17             List<button> bs = new List<button>();
     18             for (int i = 0; i < 4; i++)
     19             {
     20                 button b = new button();
     21                 b.Location = new System.Drawing.Point(points[0, i], points[1, i]);//初始化坐标
     22                 if (i == 1)
     23                 {
     24                     b.Tag = "point";//定义原点
     25                 }
     26                 else
     27                     b.Tag = "";
     28                 bs.Add(b);
     29             }
     30             return bs;
     31         }
     32         public static void Score(GroupBox gpbGame, Label lblScore)//得分
     33         {
     34             int length = gpbGame.Width / 20;
     35             for (int i = 0; i < length; i++)//没行都判断是否满足加分条件
     36             {
     37                 AddScore(i,gpbGame,lblScore);//加分操作
     38             }
     39         }
     40         public static void AddScore(int rowPage, GroupBox gpbGame,Label lblScore)
     41         {
     42             int count = 0;
     43             int length = gpbGame.Width / 20;//列数
     44             List<button> list = new List<button>();
     45             foreach (Control c in gpbGame.Controls)
     46             {
     47                 if (c is button && !c.Enabled && c.Location.Y == gpbGame.Height - 20 * rowPage)//判断是否为指定行的区域
     48                 {
     49                     count++;
     50                     list.Add(c as button);//要删除的控件
     51                 }
     52                 if (count >= length)
     53                 {
     54                     int score = Convert.ToInt32(lblScore.Text);
     55                     lblScore.Text = (score += 1).ToString();//刷新分数
     56                     if(score == 5)//分数等级
     57                     {
     58                         Grade = 1;
     59                         Speed = 550;
     60                     }else if(score == 15)
     61                     {
     62                         Grade = 2;
     63                         Speed = 450;
     64                     }
     65                     else if (score == 35)
     66                     {
     67                         Grade = 3;
     68                         Speed = 400;
     69                     }
     70                     else if (score == 80)
     71                     {
     72                         Grade = 4;
     73                         Speed = 350;
     74                     }
     75                     else if (score == 200)
     76                     {
     77                         Grade = 5;
     78                         Speed = 300;
     79                     }
     80                     foreach (button b in list)
     81                     {
     82                         b.Dispose();//引用类型会同步所有的地址
     83                     }
     84 
     85                     Subtract(count, rowPage,gpbGame);//位置下降
     86                     foreach (Control b in gpbGame.Controls)
     87                     {
     88                         if (b is button && !b.Enabled && b.Location.Y == gpbGame.Height - 20 * rowPage)
     89                         {
     90                             count--;
     91                             if (count == 0)//在次满足等分条件递归调用加分
     92                                 Score(gpbGame,lblScore);
     93                         }
     94                     }
     95                     return;
     96                 }
     97             }
     98         }
     99         public static void Subtract(int count, int rowPage, GroupBox gpbGame)//减行
    100         {
    101             foreach (Control bb in gpbGame.Controls)
    102             {
    103                 if (bb is button && !bb.Enabled && bb.Location.Y == gpbGame.Height - 20 * rowPage)
    104                 {
    105                     count--;//判断整行是否有控件
    106                 }
    107             }
    108             foreach (Control b in gpbGame.Controls)
    109             {
    110                 //如果该行是空的则下降一行
    111                 if (count == 13 && !b.Enabled && b.Location.Y <= gpbGame.Height - 20 * rowPage)
    112                 {
    113                     int y = b.Location.Y;
    114                     b.Location = new Point(b.Location.X, y += 20);
    115                 }
    116             }
    117         }
    118         
    119         public static void ChangeShape(GroupBox gpbGame)//变换位置
    120         {
    121             int pointx = 0;
    122             int pointy = 0;
    123             //先取到原点坐标
    124             foreach (Control c in gpbGame.Controls)
    125             {
    126                 if (c.Enabled)//原点坐标为100,40
    127                 {
    128                     if (c.Tag.ToString() == "point")
    129                     {
    130                         pointx = c.Location.X;//得到原点坐标
    131                         pointy = c.Location.Y;
    132                         break;
    133                     }
    134                 }
    135             }
    136             
    137             bool b = true;
    138             foreach (Control c in gpbGame.Controls)
    139             {
    140                 if (c.Enabled)//原点坐标为第二个方块坐标
    141                 {
    142                     int x = c.Location.X - pointx;
    143                     int y = -c.Location.Y + pointy;//得到相对坐标
    144                     int X = y + pointx;
    145                     int Y = x + pointy;//相对于原点的坐标
    146                     //判断方块是否超出边界
    147                     if (X >= gpbGame.Width || X < 0 || Y > gpbGame.Height || Y < 0)
    148                     {
    149                         b = false;
    150                         c.BackColor = Color.Black;
    151                         break;
    152                     }
    153                     //判断方块是否碰到方块
    154                     foreach (Control cc in gpbGame.Controls)
    155                     {
    156                         if (!cc.Enabled)
    157                         {
    158                             if (X == cc.Location.X && Y == cc.Location.Y)
    159                             {
    160                                 b = false;
    161                                 break;
    162                             }
    163                         }
    164                     }
    165                 }
    166             }
    167             if (b)//判断操作可行
    168             {
    169                 foreach (Control c in gpbGame.Controls)
    170                 {
    171                     if (c.Enabled)
    172                     {
    173                         int x = c.Location.X - pointx;
    174                         int y = -c.Location.Y + pointy;
    175                         c.Location = new Point(y + pointx, x + pointy);                    }
    176                 }
    177             }
    178         }
    179         
    180     }
    181 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ShapeUI
     8 {
     9     public class CreateShape
    10     {
    11         public static IShapes createShape(int shape)
    12         {
    13             IShapes s = null;
    14             switch(shape)
    15             {
    16                 case 1:
    17                     s = new Shape1();
    18                     break;
    19                 case 2:
    20                     s = new Shape2();
    21                     break;
    22                 case 3:
    23                     s = new Shape3();
    24                     break;
    25                 case 4:
    26                     s = new Shape4();
    27                     break;
    28                 case 5:
    29                     s = new Shape5();
    30                     break;
    31             }
    32             return s;
    33         }
    34     }
    35 }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ShapeUI
    {
        public interface IShapes
        {
            List<button> Getbuttons();
        }
    }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ShapeUI
     8 {
     9     public class Shape1 : IShapes
    10     {
    11         //山字型
    12         private int[,] points = new int[,] { {80,100,120,100 }, {40,40,40,20 } };//初始化形状坐标
    13         public List<button> Getbuttons()//返回一个形状
    14         {
    15             return Helper.Getbuttons(points);
    16         }
    17     }
    18 }
    View Code

    其它几个形状也都是一样的

  • 相关阅读:
    loj6033.「雅礼集训 2017 Day2」棋盘游戏
    loj6032. 「雅礼集训 2017 Day2」水箱
    BZOJ 5217 [Lydsy2017省队十连测] 航海舰队
    P4173 残缺的字符串
    P3723 [AH2017/HNOI2017]礼物
    P3321 [SDOI2015]序列统计
    P4841 [集训队作业2013]城市规划
    MySQL基础
    MySQL查询
    HTTP响应码
  • 原文地址:https://www.cnblogs.com/LiuZhen/p/3770055.html
Copyright © 2011-2022 走看看