• java俄罗斯方块游戏代码


    java俄罗斯方块游戏代码:

      1 package com;
      2 
      3 import java.awt.Color;
      4 import java.awt.Graphics;
      5 import java.awt.event.KeyEvent;
      6 import java.awt.event.KeyListener;
      7 import java.util.Random;
      8 
      9 import javax.swing.JFrame;
     10 import javax.swing.JPanel;
     11 
     12 public class Eluos extends JFrame{
     13     
     14     
     15     private Eluo_panel jPanel;
     16     
     17     private int this_width=500,this_height=500;
     18   public Eluos(){
     19     
     20       this.setSize(this_width, this_height);
     21       
     22       jPanel=new Eluo_panel();
     23       this.add(jPanel);
     24       
     25       
     26       this.setDefaultCloseOperation(EXIT_ON_CLOSE);
     27       this.setVisible(true);
     28       
     29       this.addKeyListener(new KeyListener() {
     30             
     31             @Override
     32             public void keyTyped(KeyEvent e) {
     33             }
     34             
     35             @Override
     36             public void keyReleased(KeyEvent e) {
     37                 System.out.println("type");
     38                 switch (e.getKeyCode()) {
     39                 case KeyEvent.VK_LEFT:
     40                     
     41                     Eluos.this.jPanel.moveOther(Eluo_panel.MOVE_RIGHT, Eluos.this.jPanel.curt_xingzhuang);
     42                     break;
     43                 case  KeyEvent.VK_RIGHT:
     44                     
     45                     Eluos.this.jPanel.moveOther(Eluo_panel.MOVE_LEFT, Eluos.this.jPanel.curt_xingzhuang);
     46                     break;
     47 
     48                 
     49                 
     50                 case KeyEvent.VK_UP:
     51                     System.out.println(Eluos.this.jPanel.curt_xingzhuang);
     52                 Eluos.this.jPanel.curt_xingzhuang=
     53                 Eluos.this.jPanel.bianXing(Eluos.this.jPanel.fangkuai.d, Eluos.this.jPanel.curt_xingzhuang);
     54                 
     55                     break;
     56                 }
     57                 
     58             }
     59             
     60             @Override
     61             public void keyPressed(KeyEvent e) {
     62                 
     63                 
     64                 
     65             }
     66         });
     67       
     68   }
     69   
     70   public static void main(String[] args) {
     71     
     72       new Eluos();
     73     
     74 }
     75     
     76 }
     77 
     78 class Eluo_panel extends JPanel implements Runnable{
     79 
     80      Fangkuai fangkuai;
     81     
     82      
     83     
     84      int huatu[][]=new int[20][20];
     85     long now_time=0;
     86     Random random=new Random();
     87     Color color=new Color(0);
     88   static final int MOVE_LEFT=1;
     89   static final int MOVE_RIGHT=2;
     90   
     91   boolean game_over=false;
     92    int curt_xingzhuang[][];
     93     public Eluo_panel(){
     94         
     95         fangkuai=createNewFangkui();
     96         
     97         Thread thread=new Thread(this);
     98         thread.start();
     99         
    100         
    101     }
    102     @Override
    103     public void paint(Graphics g) {
    104         super.paint(g);
    105         
    106         drawBack(g);
    107         drawFangkui(g,curt_xingzhuang);
    108         moveDown(curt_xingzhuang);
    109     }
    110     
    111     /**
    112      * 画背景
    113      * @param g
    114      */
    115     void drawBack(Graphics g){
    116         
    117         
    118         for (int i = 0; i < huatu.length; i++) {
    119             for (int j = 0; j < huatu[i].length; j++) {
    120                 if(huatu[i][j]!=0)
    121                     g.fillRect(j*20, i*20, Fangkuai.width-1,Fangkuai.height-1);
    122             }
    123         }
    124     }
    125     
    126     /**
    127      * 画一个方块
    128      * @param g
    129      * @param curt_xing
    130      */
    131      void drawFangkui(Graphics g,int curt_xing[][]){
    132     
    133             
    134          if(fangkuai==null)
    135          {
    136              fangkuai=createNewFangkui();
    137             
    138          }
    139          
    140         if(curt_xing!=null){
    141             int y=0;boolean b=false;
    142             for (int i = 0; i < curt_xing.length; i++) {
    143                 for (int j = 0; j < curt_xing[i].length; j++) {
    144                     if(curt_xing[i][j]!=0)
    145                     {
    146                         
    147                          g.setColor(fangkuai.getColor());
    148                         g.fillRect((fangkuai.run_x+j)*Fangkuai.width, (fangkuai.run_y+y)*Fangkuai.height,
    149                                 Fangkuai.width-1, Fangkuai.height-1);
    150                         b=true;
    151                         
    152                     }
    153                     
    154                 }
    155                 if(b)
    156                     y++;
    157                     
    158             
    159             }
    160         
    161         }
    162      }
    163     /**
    164      * 创建一个方块
    165      * @return
    166      */
    167     private Fangkuai createNewFangkui(){
    168         
    169         int index=0;
    170         Random random=new Random();
    171         Fangkuai fangkuai=new Fangkuai();
    172         Color color=new Color(random.nextInt(255),
    173                 random.nextInt(255),random.nextInt(255));
    174         
    175         index=random.nextInt(4);
    176         fangkuai.setColor(color);
    177         curt_xingzhuang=Fangkuai.xingzhuangs[index];
    178         
    179         return fangkuai;
    180     }
    181     
    182     /**
    183      * 判断是否能够向下移动
    184      * @param xingzhuang
    185      * @return
    186      */
    187     boolean isCan_down(int xingzhuang[][]){
    188         
    189         
    190         int y=0;boolean b=false;
    191         for (int i = 0; i < xingzhuang.length; i++) {
    192             for (int j = 0; j < xingzhuang[i].length; j++) {
    193                 if(xingzhuang[i][j]!=0)
    194                 {
    195                     b=true;
    196                     if(fangkuai.run_y+y>=19||huatu[fangkuai.run_y+y+1][fangkuai.run_x+j]!=0){
    197                         return false;
    198                     }
    199                     
    200                 }
    201                     
    202             }
    203             if(b)
    204                 y++;
    205         
    206         }
    207         
    208         return true;
    209     }
    210     /**
    211      * 变形
    212      */
    213     
    214     public int[][] bianXing(int d,int arr[][]){
    215         
    216         if(arr==null||arr[0]==null)
    217             return null;
    218         
    219         int arr2[][]=new int[arr.length][arr[0].length];
    220         
    221          
    222         switch (d) {
    223         case 1:
    224             
    225             
    226             for (int i = 0; i < arr.length; i++) {
    227                 for (int j = 0; j < arr[i].length; j++) {
    228                     arr2[j][arr[i].length-1-i]=arr[i][j];         
    229                 }
    230             }
    231             
    232             
    233             break;
    234 
    235         default:
    236             break;
    237         }
    238         
    239         for (int i = 0; i < arr2.length; i++) {
    240             
    241             for (int j = 0; j < arr2[i].length; j++) {
    242                 
    243                 if(arr2[i][j]!=0)
    244                 {
    245                 if(fangkuai.run_x+j>19||fangkuai.run_y+i>19||fangkuai.run_x+i<0
    246                     ||huatu[fangkuai.run_y+i][fangkuai.run_x+j]!=0)
    247                     return arr;
    248                 }
    249             }
    250         }
    251         
    252         return arr2;
    253         
    254     }
    255     /**
    256      * 向下移动
    257      * @param xingzhuang
    258      */
    259     private void moveDown(int xingzhuang[][]){
    260 
    261         if(isCan_down(xingzhuang))
    262             fangkuai.run_y++;
    263         
    264         else
    265         {
    266             
    267             /**
    268              * 如果不能向下移动就把当前方块的0和1 映射到整个面板上,重新创建一个方块
    269              */
    270             int y=0;boolean b=false;
    271             for (int i = 0; i < xingzhuang.length; i++) {
    272                 for (int j = 0; j < xingzhuang[i].length; j++) {
    273                     if(xingzhuang[i][j]!=0)
    274                     {
    275                         huatu[fangkuai.run_y+y][fangkuai.run_x+j]=1;
    276                         b=true;
    277                     }
    278                         
    279                 }
    280                 if(b)
    281                     y++;
    282             
    283             }
    284             
    285             
    286             xiaoChu();
    287             for (int i = 0; i < huatu[0].length; i++) {
    288                 if(huatu[0][i]!=0)
    289                     game_over=true;
    290             }
    291             
    292             fangkuai=createNewFangkui();
    293         }
    294             
    295     }
    296     public void xiaoChu(){
    297         
    298         boolean xiao=false;
    299         
    300         for (int i = huatu.length-1; i >=0; i--) {
    301             
    302             xiao=false;
    303             int j=0;
    304             for ( j = 0; j < huatu[i].length; j++) {
    305                 if(huatu[i][j]==0)
    306                     break;
    307             }
    308             
    309             if(j==huatu[i].length)
    310                 xiao=true;
    311             
    312             
    313             if(xiao){
    314                 
    315                 for ( j = i; j >0; j--) {
    316                     for (int j2 = 0; j2 < huatu[j].length; j2++) {
    317                         huatu[j][j2]=huatu[j-1][j2];
    318                     }
    319                 }
    320                 for ( j = 0; j <huatu[0].length; j++) {
    321                     huatu[0][j]=0;
    322                 }
    323                 
    324             }
    325             
    326         }
    327     }
    328 /**
    329  *  http://www.cnblogs.com/roucheng/
    330  * @param d
    331  * @param xingzhuang 
    332  */
    333      void moveOther(int d,int xingzhuang[][]){
    334         
    335         int dx=d==MOVE_LEFT?1:-1;
    336         if(is_CanMoveOther(d, xingzhuang)){
    337             fangkuai.run_x+=dx;
    338         }
    339     }
    340     private boolean is_CanMoveOther(int d,int xingzhuang[][]){
    341         
    342         int dx=d==MOVE_LEFT?1:-1;
    343         int y=0;boolean has=false;
    344         for (int i = 0; i < xingzhuang.length; i++) {
    345             has=false;
    346             for (int j = 0; j < xingzhuang[i].length; j++) {
    347                 
    348                 if(xingzhuang[i][j]!=0)
    349                 {
    350                     if(d==MOVE_LEFT&&fangkuai.run_x+j>=19||d==MOVE_RIGHT&&fangkuai.run_x+j<=0)
    351                         
    352                             return false;
    353                     has=true;
    354                    if(huatu[fangkuai.run_y+y][fangkuai.run_x+j+dx]!=0){
    355                        return false;
    356                    }                    
    357                 }
    358             }
    359             if(has)
    360                 y++;
    361         }
    362     
    363         return true;
    364     }
    365     
    366     
    367     
    368     
    369     @Override
    370     public void run() {
    371         
    372         
    373       while(!game_over)
    374       {
    375           
    376           
    377           this.repaint();
    378           try {
    379             Thread.sleep(300);
    380         } catch (InterruptedException e) {
    381             e.printStackTrace();
    382         }
    383       }
    384     }
    385     
    386 }
    387 class Fangkuai {
    388     
    389     
    390     
    391     private Color color;
    392     
    393      int run_x=10,run_y;
    394     
    395      int d=1;
    396     
    397      static final int width=20,height=20;
    398     
    399     public static final int  xingzhuangs[][][]={
    400         {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
    401             {0,0,0,0,1,1,1,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}},        
    402             
    403             {{0,0,1,0},{0,1,1,1},{0,0,0,0},{0,0,0,0}},//土形
    404             {{0,0,0,0},{1,0,0,0},{1,1,0,0},{0,1,0,0}},
    405             {{1,1,1,1},{1,0,0,0},{0,0,0,0},{0,0,0,0}},//T形
    406             {{1,1},{1,1}}
    407             
    408     };
    409 
    410     public Color getColor() {
    411         return color;
    412     }
    413 
    414     public void setColor(Color color) {
    415         this.color = color;
    416     }
    417 
    418     public int getRun_x() {
    419         return run_x;
    420     }
    421 
    422     public void setRun_x(int run_x) {
    423         this.run_x = run_x;
    424     }
    425 
    426     public int getRun_y() {
    427         return run_y;
    428     }
    429 
    430     public void setRun_y(int run_y) {
    431         this.run_y = run_y;
    432     }
    433 
    434     
    435     
    436     
    437 }
  • 相关阅读:
    Jqeury ajax 调用C#的后台程序
    电商设计1
    智能分类
    网络爬虫框架Webmagic
    Spring Cloud 微服务架构解决方案
    网站流量日志数据分析系统1
    Shell 编程
    Zookeeper
    网站流量日志数据自定义采集
    分库分表的面试题5
  • 原文地址:https://www.cnblogs.com/roucheng/p/3512223.html
Copyright © 2020-2023  润新知