java俄罗斯方块游戏代码建议收藏

java俄罗斯方块游戏代码:1packagecom;23importjava.awt.Color;4importjava.awt.Graphics;5importjava.a

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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 }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/120738.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 关于allow_url_fopen的设置与服务器的安全–不理解

    关于allow_url_fopen的设置与服务器的安全–不理解allow_url_fopen与安全以及PHPlibcurl  allow_url_fopen=ON常常会给服务器和管理员带来麻烦,但是经常性(至少我这样认为)的我们需要远程读取某个东西,如果设置allow_url_fopen=OFF将其关闭,我们就没有办法远程读取。  幸好我们有一个很好的PHP模块–curl。下面我就以一个例子说说我用curl远程读取的方法:  第一,a

  • sigaction函数解析

    sigaction函数解析sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作)。他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该使用这个接口)给信号signum设置新的信号处理函数act,同时保留该信号原有的信号处理函数oldactint sigaction(int signo,

  • Android开发CompoundButton抽象类控件类的使用UI之Radio、Check、Toggle[通俗易懂]

    Android开发CompoundButton抽象类控件类的使用UI之Radio、Check、Toggle[通俗易懂]本篇文章就讲解了一下CompoundButton抽象类下的三个实现控件类的使用,在Android4.0之后,又新加入了一个控Switch,对它的使用与之上介绍的三个控件类似,这里就不再详细讲解了。前言这篇文章讲解一下Android平台下,RadioButton、CheckBox以及ToggleButton三个控件的用法,因为这三个控件之中都存在一个选中或是没选中的状态,所以

  • django csdn_怎么使用cookie登录

    django csdn_怎么使用cookie登录前言cookie:在网站中,http请求是无状态的。也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户。cookie的出现就是为了解决这个问题,第一次登录

  • windows10添加开机启动项怎么设置_注册表添加开机启动项

    windows10添加开机启动项怎么设置_注册表添加开机启动项在日常生活中,偶尔要求其中的软件在开机时便能自动启动,比如MySQL一般被设置为自启动项。今天将为大家介绍window10中如何添加开机启动项。操作过程:1、按下win+R调出运行窗口,并输入“shell:startup”即可进入开机启动文件夹。2、开机启动文件夹如图所示,此时文件夹中内容为空。3、如果想要添加启动项,可以将软件快捷方式移入开机启动文件夹中,比如移入“福昕阅读器”。4、我们可以在任务管理器中查看是否成功添加开机启动项…

    2022年10月26日
  • 堆叠降噪自动编码器 Stacked Denoising Auto Encoder(SDAE)

    堆叠降噪自动编码器 Stacked Denoising Auto Encoder(SDAE)自动编码器(Auto-Encoder,AE)自编码器(autoencoder)是神经网络的一种,经过训练后能尝试将输入复制到输出。自编码器内部有一个隐藏层h,可以产生编码(code)表示输入。该网络可以看作由两部分组成:一个由函数h=f(x)表示的编码器和一个生成重构的解码器r=g(h)。我们不应该将自编码器设计成输入到输出完全相等。这通常需要向自编码器强加一些约束,使它只能近…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号