俄罗斯方块小游戏
简单的实现俄罗斯方块,只有一个主代码,很好理解的,有暂停/继续、重新开始、结束游戏的简单功能。这里就不多说实现的原理了,可以在网上进行相关的查询。这里就直接给出了源代码,这个也是我参考网上的,自己进行了相关的更改,增加了一些功能。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.imageio.*; import javax.swing.*; import javax.swing.Timer;
public class MyGame extends JFrame {
public MyGame(){ GameBody gamebody= new GameBody(); gamebody.setBounds( 5 , 10 , 500 , 600 ); // gamebody.setOpaque( false ); gamebody.setLayout( null ); addKeyListener(gamebody); add(gamebody);
int w=Toolkit.getDefaultToolkit().getScreenSize().width; int h=Toolkit.getDefaultToolkit().getScreenSize().height; JLabel image= new JLabel( new ImageIcon( "13.jpg" )); image.setBounds( 0 , 0 , 500 , 600 ); getLayeredPane().setLayout( null ); getLayeredPane().add(image, new Integer(Integer.MIN_VALUE)); ((JPanel)getContentPane()).setOpaque( false );
final JButton login= new JButton( new ImageIcon( "login4.PNG" )); login.setContentAreaFilled( false ); login.setMargin( new Insets( 0 , 0 , 0 , 0 )); login.setBorderPainted( false ); login.setBounds( 340 , 320 , 120 , 26 ); gamebody.add(login);
login.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { //登录的按钮 if (e.getSource()==login){ requestFocus( true ); //获得焦点,不用失去焦点 gamebody.resetMap(); //重置地图 gamebody.drawWall(); //冲重新绘制边界墙体 gamebody.createshape(); //重新产生新的地图 gamebody.setStart( false ); //唤醒定时下落的线程 gamebody.score= 0 ; //将分数置为零 repaint(); } } });
final JButton pauselogin= new JButton( new ImageIcon( "login6.PNG" )); pauselogin.setContentAreaFilled( false ); pauselogin.setMargin( new Insets( 0 , 0 , 0 , 0 )); pauselogin.setBorderPainted( false ); pauselogin.setBounds( 340 , 370 , 120 , 26 ); gamebody.add(pauselogin);
pauselogin.addMouseListener( new MouseListener(){ //暂停的按钮 /* public void actionPerformed(ActionEvent e) { //用事件监听,没用实现再次按暂停键后继续游戏 //while(true){ if (e.getSource()==pauselogin) { gamebody.setStart(true); requestFocus(true);
}if(e.getSource()==login){ new GameBody.setStart(false); requestFocus(true); } //} }*/
//鼠标点击事件,可以分别判断不同的事件,做出不同的反应 public void mouseClicked(MouseEvent e){ if (e.getButton()==e.BUTTON1 ){ //单击左键暂停 gamebody.setStart( true ); //将自动下落线程关闭 //requestFocus(true); //同时整个JFrame失去焦点,无法操作,但可以点击按钮 } else if (e.getButton()==e.BUTTON3 ){ //右击暂停,继续游戏 gamebody.setStart( false ); //唤醒自动下落线程 requestFocus( true ); } if (e.getClickCount()== 2 ){ //左键双击,也可以继续游戏 gamebody.setStart( false ); requestFocus( true ); } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){}
});
final JButton overlogin= new JButton( new ImageIcon( "login5.PNG" )); //退出的按钮 overlogin.setMargin( new Insets( 0 , 0 , 0 , 0 )); overlogin.setContentAreaFilled( false ); overlogin.setBounds( 340 , 420 , 120 , 26 ); overlogin.setBorderPainted( false ); add(overlogin);
overlogin.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit( 1 ); } });
setTitle( "俄罗斯方块 V.1.0" ); setResizable( false ); setFocusable( true ); setBounds((w- 500 )/ 2 ,(h- 600 )/ 2 , 500 , 600 ); setLayout( null );
setVisible( true ); setDefaultCloseOperation( 3 ); } public static void main(String[] args) { new MyGame(); }
//创建需要定义的局部变量和游戏类 class GameBody extends JPanel implements KeyListener{ private int shapeType=- 1 ; //定义方块的类型 定义的为7中 private int shapeState=- 1 ; //定义方块为何种状态,每种都有四种状态 private int nextshapeType=- 1 ; //定义下一块产生的类型 private int nextshapeState=- 1 ; //定义下一块的方块的状态 private final int CELL= 25 ; //定义方格的大小 private int score= 0 ; //定义显示的成绩 private int left; //定义初始图形与两边的墙的距离 private int top; //定义初始图形与上下墙的距离 private int i= 0 ; //表示列 private int j= 0 ; //表示行 public int flag= 0 ; public volatile boolean start= false ; //暂停的判断条件,为轻量锁,保持同步的 //public boolean start=false; //Timer t; Random randomcolor= new Random(); Random random= new Random();
//定义地图的大小,创建二位的数组 int [][] map= new int [ 13 ][ 23 ];
//初始化地图 public void resetMap(){ for (i= 0 ;i< 12 ;i++){ for (j= 0 ;j< 22 ;j++){ //遍历的范围不能小 map[i][j]= 0 ; } } }
//画围墙的方法 public void drawWall(){ for (j= 0 ;j< 22 ;j++) //0到21行 { map[ 0 ][j]= 2 ; map[ 11 ][j]= 2 ; //第0行和第11行为墙 } for (i= 0 ;i< 12 ;i++){ //0到11列 map[i][ 21 ]= 2 ; //第21行划墙 } }
//定义随机的图形种类 private final int [][][] shapes= new int [][][]{ // i { { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 }, { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 } }, // s { { 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 } }, // z { { 1 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 1 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0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 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 } } };
//产生新图形的方法 public void createshape(){ if (shapeType==- 1 &&shapeState==- 1 ){ shapeType = random.nextInt(shapes.length); shapeState = random.nextInt(shapes[ 0 ].length); } else { shapeType=nextshapeType; shapeState=nextshapeState; } nextshapeType = random.nextInt(shapes.length); nextshapeState = random.nextInt(shapes[ 0 ].length); //shapeType=(int)(Math.random()*1000)%7; //在7中类型中随机选取 //shapeState=(int)(Math.random()*1000)%4; //在四种状态中随机选取 left= 4 ; top= 0 ; //图形产生的初始位置为(4,0)\ if (gameOver(left,top)== 1 ){ //判断游戏 resetMap(); drawWall(); score= 0 ; JOptionPane.showMessageDialog( null , "GAME OVER" ); } }
//遍历[4][4]数组产生的方块并判断状态 public int judgeState( int left, int top, int shapeType, int shapeState){ for ( int a= 0 ;a< 4 ;a++){ for ( int b= 0 ;b< 4 ;b++){ if (((shapes[shapeType][shapeState][a* 4 +b]== 1 && //遍历数组中为1的个数,即判断是否有图形 map[left+b+ 1 ][top+a]== 1 ))|| //判断地图中是否还有障碍物 ((shapes[shapeType][shapeState][a* 4 +b]== 1 && //遍历数组中为1的个数,即判断是否有图形 map[left+b+ 1 ][top+a]== 2 ))){ //判断是否撞墙 return 0 ; //表明无法不能正常运行 } } } return 1 ; }
//创建键盘事件监听 public void keyPressed(KeyEvent e){ //if (start){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: leftMove(); //调用左移的方法 repaint(); break ; case KeyEvent.VK_RIGHT: rightMove(); //调用右移的方法 repaint(); break ; case KeyEvent.VK_DOWN: downMove(); //调用左移的方法 repaint(); break ; case KeyEvent.VK_UP: turnShape(); //调用变形的方法 repaint(); break ; } // } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { }
//创建左移的方法 public void leftMove(){ if (judgeState(left- 1 ,top,shapeType,shapeState)== 1 ){ left-= 1 ; } } //创建右移的方法 public void rightMove(){ if (judgeState(left+ 1 ,top,shapeType,shapeState)== 1 ){ left+= 1 ; };
} //创建下移的方法 public void downMove(){ if (judgeState(left,top+ 1 ,shapeType,shapeState)== 1 ){ //判断有图形 top+= 1 ; deleteLine(); //判断下移后是否有满行 } if (judgeState(left,top+ 1 ,shapeType,shapeState)== 0 ){ //判断没有图形
addshape(left,top,shapeType,shapeState); createshape(); deleteLine(); } }
//创建旋转变形的方法 public void turnShape(){ int tempshape=shapeState; shapeState=(shapeState+ 1 )% 4 ; //在四中的状态中选取 if (judgeState(left,top,shapeType,shapeState)== 1 ){
} if (judgeState(left,top,shapeType,shapeState)== 0 ){ shapeState=tempshape; //没有图形,不能进行旋转,还原原来状态 } repaint(); }
//绘图方法 public void paintComponent(Graphics g){ super .paintComponent(g); int t=randomcolor.nextInt( 5 ); int count=randomcolor.nextInt( 5 ); Color[] color= new Color[]{Color.pink,Color.green,Color.red,Color.yellow,Color.blue}; /* Image image = null; try { image = ImageIO.read(new File("1.jpg")); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); g.drawImage(image, 0, 0,this); }*/
//绘制围墙 for (j= 0 ;j< 22 ;j++){ for (i= 0 ;i< 12 ;i++){ if (map[i][j]== 2 ){ //判断是否为墙并绘制 g.setColor(Color.blue); g.fill3DRect(i*CELL,j*CELL,CELL,CELL, true ); } if (map[i][j]== 0 ){ //判断是否为墙并绘制 g.setColor(Color.red); g.drawRoundRect(i*CELL,j*CELL,CELL,CELL, 6 , 6 );} } }
//绘制正在下落的图形 for ( int k= 0 ;k< 16 ;k++){ if (shapes[shapeType][shapeState][k]== 1 ){ g.setColor(Color.red); g.fill3DRect((left+k% 4 + 1 )*CELL,(top+k/ 4 )*CELL,CELL,CELL, true ); //left\top为左上角的坐标 } }
//绘制落下的图形 for (j= 0 ;j< 22 ;j++){ for (i= 0 ;i< 12 ;i++){ if (map[i][j]== 1 ){ g.setColor(Color.green); g.fill3DRect(i*CELL,j*CELL,CELL,CELL, true ); } } }
//显示右边预览图形 for ( int i = 0 ; i < 4 ; i++) { for ( int j = 0 ; j < 4 ; j++){ if (shapes[nextshapeType][nextshapeState][i* 4 +j] == 1 ) { g.setColor(Color.red); g.fill3DRect( 375 +(j*(CELL- 10 )), 190 +(i*(CELL- 10 )), CELL- 10 , CELL- 10 , true ); } } } //添加右边预览图形方格 for ( int i = 0 ; i < 5 ; i++) { for ( int j = 0 ; j < 5 ; j++){ g.setColor(Color.blue); g.drawRoundRect( 360 +(j*(CELL- 10 )), 175 +(i*(CELL- 10 )),CELL- 10 , CELL- 10 , 3 , 3 ); } } g.setFont( new Font( "楷书" ,Font.BOLD, 20 )); g.setColor(Color.BLUE); g.drawString( "游戏分数:" , 310 , 70 ); g.setColor(Color.RED); g.drawString(score+ " " , 420 , 70 ); g.setColor(Color.BLUE); g.drawString( " 分" , 450 , 70 );
}
//创建添加新图形到地图的方法、 public void addshape( int left, int top, int shapeType, int shapeState){ int temp= 0 ; for ( int a= 0 ;a< 4 ;a++){ for ( int b= 0 ;b< 4 ;b++){ //对存储方块队的[4][4]数组遍历 if (map[left+b+ 1 ][top+a]== 0 ){ //表明[4][4]数组没有方块
map[left+b+ 1 ][top+a]=shapes[shapeType][shapeState][temp]; } temp++; } } }
//创建消行的方法,即将满行以上的部分整体下移 public void deleteLine(){ int tempscore= 0 ; //定义满行的列个数满足1 for ( int a= 0 ;a< 22 ;a++){ //对地图进行遍历 for ( int b= 0 ;b< 12 ;b++){ if (map[b][a]== 1 ){ //表示找到满行 tempscore++; // 记录一行有多少个1 if (tempscore== 10 ){ score+= 10 ; for ( int k=a;k> 0 ;k--){ //从满行开始回历 for ( int c= 1 ;c< 12 ;c++){ map[c][k]=map[c][k- 1 ]; //将图形整体下移一行 } } } } } tempscore= 0 ; } }
//判断游戏结束,1、判断新块的状态是否不存在,即judgeState()==0 //2、判断初始产生的位置是否一直为1; public int gameOver( int left, int top){ if (judgeState(left,top,shapeType,shapeState)== 0 ){ return 1 ; } return 0 ; }
//创建构造方法 public GameBody(){ resetMap(); drawWall(); createshape(); //Timer timer=new Timer(1000,new TimeListener()); Thread timer= new Thread( new TimeListener()); timer.start(); }
public void setStart( boolean start){ //改变start值的方法 this .start=start; }
//创建定时下落的监听器 class TimeListener implements Runnable{ public void run(){ while ( true ){ if (!start){ try { repaint(); if (judgeState(left,top+ 1 ,shapeType,shapeState)== 1 ){ top+= 1 ; deleteLine();} if (judgeState(left,top+ 1 ,shapeType,shapeState)== 0 ){ if (flag== 1 ){ addshape(left,top,shapeType,shapeState); deleteLine(); createshape(); flag= 0 ; } flag= 1 ; } Thread.sleep( 800 ); } catch (Exception e){ e.getMessage(); } } } } } } } //外层 |
下面是游戏演示的一些界面效果图:
这里说一下,我是把图片和源代码直接放在了一个文件夹下,对于初学者很好理解,这也是很好的练手项目。图片什么的大家可以在网上直接找一个,然后重命名一下就可了。
以上就是Java游戏开发之俄罗斯方块的实现的详细内容,更多关于Java俄罗斯方块的资料请关注其它相关文章!
原文链接:https://blog.csdn.net/Code__rookie/article/details/101780313
查看更多关于Java游戏开发之俄罗斯方块的实现的详细内容...