服务器之家

服务器之家 > 正文

java编写的简单移动方块小游戏代码

时间:2020-03-12 11:41     来源/作者:octobershiner

本文实例讲述了java编写的简单移动方块小游戏代码。分享给大家供大家参考,具体如下:

运行效果截图如下:

java编写的简单移动方块小游戏代码

第一次用java编写图形化的界面,还是有些青涩。。以后继续努力!!具体代码如下:

?
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
//Little Box Game by AlexYui
//Game.java By 1093710210@ HIT
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;
public class Game{
 public static void main(String[] agrs){
  MyFrame gameframe = new MyFrame();
  gameframe.setTitle("Java Box Game By AlexYui");
  gameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  gameframe.setVisible(true);
 }
}
//框架
class MyFrame extends JFrame {
 private int flag = 0;
 protected double x[] = {0,0,270,0,270};
 protected double y[] = {0,0,0,270,270};
 protected int winner[]={0,0,0,0,0};
 public static final int DEFAULT_WIDTH = 317;
 public static final int DEFAULT_HEIGHT = 339;
 public MyFrame(){
 MyPanel a = new MyPanel();
 addKeyListener(new MyKeyListener());
 this.add(a);
 setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
 }
class MyPanel extends JPanel{
 public MyPanel(){}
 public void paint(Graphics g){
 super.paint(g);
 Graphics2D g2 = (Graphics2D)g;
 Rectangle2D rect1 = new Rectangle2D.Double(x[1],y[1],30f,30f);
 Rectangle2D rect2 = new Rectangle2D.Double(x[2],y[2],30f,30f);
 Rectangle2D rect3 = new Rectangle2D.Double(x[3],y[3],30f,30f);
 Rectangle2D rect4 = new Rectangle2D.Double(x[4],y[4],30f,30f);
 //画中间的四个方框
 g2.setPaint(Color.RED);
 g2.drawRect(120,120 ,30,30);
 g2.drawRect(120,150 ,30,30);
 g2.drawRect(150,120 ,30,30);
 g2.drawRect(150,150 ,30,30);
 //填充四个角上的方块
 g2.setPaint(Color.BLUE);
 if(winner[1] == 0)g2.fill(rect1);
 if(winner[2] == 0)g2.fill(rect2);
 if(winner[3] == 0)g2.fill(rect3);
 if(winner[4] == 0)g2.fill(rect4);
 g2.setPaint(Color.RED);
 //将已经选择的方块涂成红色
 if(flag == 1) g2.fill(rect1); 
   if(flag == 2) g2.fill(rect2);
   if(flag == 3) g2.fill(rect3);
   if(flag == 4)   g2.fill(rect4);
   g2.setPaint(Color.BLUE);
 if(winner[1] == 1)g2.fill(rect1);
 if(winner[2] == 1)g2.fill(rect2);
 if(winner[3] == 1)g2.fill(rect3);
 if(winner[4] == 1)g2.fill(rect4);
  g2.setPaint(Color.RED);
   if(winner[1] == 1 && winner[2] == 1 && winner[3] == 1 && winner[4] == 1)
 g2.drawString("Succes! You Won!",100,75);
 //保持边框的颜色
 g2.drawRect(120,120 ,30,30);
 g2.drawRect(120,150 ,30,30);
 g2.drawRect(150,120 ,30,30);
 g2.drawRect(150,150 ,30,30);
   repaint();
 }
}
 class MyKeyListener implements KeyListener{
   //overide
   public void keyPressed(KeyEvent event){
  int keyCode = event.getKeyCode();
     if(keyCode == KeyEvent.VK_1){flag = 1;}
     if(keyCode == KeyEvent.VK_2){flag = 2;}
     if(keyCode == KeyEvent.VK_3){flag = 3;}
     if(keyCode == KeyEvent.VK_4){flag = 4;}
     if(keyCode == KeyEvent.VK_UP){move(0,-30,flag);win();}
     if(keyCode == KeyEvent.VK_DOWN){move(0,30,flag);win();}
     if(keyCode == KeyEvent.VK_LEFT){move(-30,0,flag);win();}
     if(keyCode == KeyEvent.VK_RIGHT){move(30,0,flag);win();}
     }
   public void keyReleased(KeyEvent event){}
   public void keyTyped(KeyEvent event){}
   }
   //用于判别基本规则,移动方块
   void move(int mx,int my,int mflag){
     int i;
     boolean teller = false;
     x[mflag] +=mx;
     y[mflag] +=my;
     if(! ((x[flag] >= 0 && x[flag]<=270)&&(y[flag] >= 0 && y[flag]<=270))){
     x[mflag] -=mx;
     y[mflag] -=my;
       }
     for(i = 1;i<5;i++){
       if(mflag != i)
       if(x[mflag] == x[i] && y[mflag] == y[i])
       teller = true;
       }
     if(teller == true){
    x[mflag] -= mx;
     y[mflag] -= my;
       }
     }
     //用于判别已经进入符合条件区域的方块
     void win(){
       int j1,j2;
       for(j1=1;j1<5;j1++)
       if((x[j1]<=150&&x[j1]>=120)&&(y[j1]<=150&&y[j1]>=120))
         winner[j1]=1;
       for(j2=1;j2<5;j2++)
       if(!((x[j2]<=150&&x[j2]>=120)&&(y[j2]<=150&&y[j2]>=120)))
         winner[j2]=0;
     }
}

希望本文所述对大家Java程序设计有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部