JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » Game  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 戰鬥方塊(多人遊戲)
skps2010030908





發文: 8
積分: 0
於 2017-02-23 12:01 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是一個十分簡單的電腦遊戲,可以二到四個人玩。

玩家要操控自己的方塊上下左右移動,並同時發射子彈來攻擊對手。

每隔一段時間地圖中會出現補給箱,打掉後會得到隨機的特殊效果。

先把對方打倒的那一方獲勝。



操作說明:

按數字鍵2,3,4來決定遊玩人數。



玩家操控:



1P  2P   3P  4P
上  w  方向鍵上  y  p
下  s  方向鍵下  h  ;
左  a  方向鍵左  g  l
右  d  方向鍵右  j  '
射擊  q  /   t  o


另外遊戲中有隱藏按鍵可以啟用外掛,就待各位自已找摟!


遊戲載點:https://www.dropbox.com/s/s62zm7lqan97i8k/戰鬥方塊.jar?dl=0
到樓下看原始碼

這個遊戲大概花了我一天半來製作,歡迎大家在底下留些意見。Big Smile


skps2010030908 edited on 2017-02-26 13:58
reply to postreply to post
作者 Re:戰鬥方塊(多人遊戲) [Re:skps2010030908]
skps2010030908





發文: 8
積分: 0
於 2017-02-26 13:57 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在下面把原始碼和註解貼出來:

class Main
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
package plane;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
 
import javax.swing.JFrame;
 
public class Main extends JFrame{
  private static final long serialVersionUID = 1;
  int x[];//玩家x座標
  int y[];//玩家y座標
  boolean s[];//發射與否
  int c[];//子彈冷卻時間
  boolean v[][];//移動與否
  int hp[];//血量
  int time[];//特殊效果剩餘時間
  int pt[];//特殊效果種類
  int pc;//特殊效果出現的剩餘時間
  ArrayList<Bullet> bullets=new ArrayList<Bullet>();//儲存子彈的陣列
  PowerUp power;//特殊效果
  int player;//玩家人數
  int hack=0;//外掛的時間控制
  
 
  public static void main(String[] args) {
    Main m=new Main();
    m.set();
    m.gameset(2);
    m.run();
  }
  
  public void set()//程式初始化
  {
    setSize(640, 640);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLocationRelativeTo(null);
    setFocusable(true);
    
    addKeyListener(new KeyListener() {
      public void keyTyped(KeyEvent e) {}
      
      public void keyPressed(KeyEvent e) {
        int k = e.getKeyCode();
        requestFocus();
        
        switch(k)
        {
          case KeyEvent.VK_UP: v[0][0]=true; break;
          case KeyEvent.VK_DOWN: v[0][1]=true; break;
          case KeyEvent.VK_LEFT: v[0][2]=true; break;
          case KeyEvent.VK_RIGHT: v[0][3]=true; break;
          case KeyEvent.VK_SLASH: s[0]=true; break;
          
          case KeyEvent.VK_W: v[1][0]=true; break;
          case KeyEvent.VK_S: v[1][1]=true; break;
          case KeyEvent.VK_A: v[1][2]=true; break;
          case KeyEvent.VK_D: v[1][3]=true; break;
          case KeyEvent.VK_Q: s[1]=true; break;
          
          case KeyEvent.VK_Y: v[2][0]=true; break;
          case KeyEvent.VK_H: v[2][1]=true; break;
          case KeyEvent.VK_G: v[2][2]=true; break;
          case KeyEvent.VK_J: v[2][3]=true; break;
          case KeyEvent.VK_T: s[2]=true; break;
          
          case KeyEvent.VK_P: v[3][0]=true; break;
          case KeyEvent.VK_SEMICOLON : v[3][1]=true; break;
          case KeyEvent.VK_L: v[3][2]=true; break;
          case 222: v[3][3]=true; break;
          case KeyEvent.VK_O: s[3]=true; break;
          
          case KeyEvent.VK_2: gameset(2); break;
          case KeyEvent.VK_3: gameset(3); break;
          case KeyEvent.VK_4: gameset(4); break;
          
          case KeyEvent.VK_SHIFT: if(hack<=0)hack=-20; break;
          case KeyEvent.VK_B: if(hack<=0)hack=-30; break;
          case KeyEvent.VK_X: if(hack==-20)hack=91; break;
          case KeyEvent.VK_M: if(hack==-30)hack=90; break;
        }
      }
 
      public void keyReleased(KeyEvent e) {
        int k = e.getKeyCode();
        requestFocus();
        
        switch(k)
        {
          case KeyEvent.VK_UP: v[0][0]=false; break;
          case KeyEvent.VK_DOWN: v[0][1]=false; break;
          case KeyEvent.VK_LEFT: v[0][2]=false; break;
          case KeyEvent.VK_RIGHT: v[0][3]=false; break;
          case KeyEvent.VK_SLASH: s[0]=false; break;
          
          case KeyEvent.VK_W: v[1][0]=false; break;
          case KeyEvent.VK_S: v[1][1]=false; break;
          case KeyEvent.VK_A: v[1][2]=false; break;
          case KeyEvent.VK_D: v[1][3]=false; break;
          case KeyEvent.VK_Q: s[1]=false; break;
          
          case KeyEvent.VK_Y: v[2][0]=false; break;
          case KeyEvent.VK_H: v[2][1]=false; break;
          case KeyEvent.VK_G: v[2][2]=false; break;
          case KeyEvent.VK_J: v[2][3]=false; break;
          case KeyEvent.VK_T: s[2]=false; break;
          
          case KeyEvent.VK_P: v[3][0]=false; break;
          case KeyEvent.VK_SEMICOLON : v[3][1]=false; break;
          case KeyEvent.VK_L: v[3][2]=false; break;
          case 222: v[3][3]=false; break;
          case KeyEvent.VK_O: s[3]=false; break;
        }
      }
    });
  }
  
  public void gameset(int t)//每場遊戲的初始化
  {
    player=t;
    x=new int[player];
    y=new int[player];
    hp=new int[player];
    for(int i=0;i<player;i++)
    {
      if(player<i+3&&i<2)x[i]=320;
      else if(i<2)x[i]=160;
      else x[i]=480;
      y[i]=(i%2==0?220:420);
      hp[i]=100;
    }
    bullets.clear();
    s=new boolean[player];
    c=new int[player];
    v=new boolean[player][4];
    time=new int[player];
    pt=new int[player];
    pc=200;
    powerup=new PowerUp();
  }
  
  public void run()//遊戲執行緒
  {
    while(true)
    {
      if(hack==-20)hack=0;
      if(pc==0)
      {
        powerup=new PowerUp();
        pc=-1;
      }
      else if(pc!=-1)pc--;
      
      for(int i=0;i<player;i++)
      {
        int d=5;
        if(pt[i]==3&&time[i]!=0)d=8;
        
        int a=x[i];
        if(v[i][2])a-=d;
        if(v[i][3])a+=d;
        int b=y[i];
        if(v[i][0])b-=d;
        if(v[i][1])b+=d;
        if(a>25&&a<615)x[i]=a;
        if(b>(i%2==0?25:345)&&b<(i%2==0?295:615))y[i]=b;
        
        if(c[i]<=0)
        {
          if(s[i]&&hp[i]>0)
          {
            bullets.add(new Bullet(x[i],y[i]+(i%2==0?25:-25),i));
            if(pt[i]==2&&time[i]!=0)
            {
              bullets.add(new Bullet(x[i]-40,y[i]+(i%2==0?25:-25),i));
              bullets.add(new Bullet(x[i]+40,y[i]+(i%2==0?25:-25),i));
            }
            c[i]=20;
          }
        }
        else c[i]--;
        
        if(time[i]!=0)
        {
          switch(pt[i])
          {
            case 0: hp[i]+=30; break;
            case 1: c[i]-=3; break;
          }
          
          time[i]--;
        }
      }
      
      for(Bullet bu:bullets)
      {
        if(bu.t%2==1)bu.y-=20; else bu.y+=20;
        
        for(int i=0;i<player;i++)
        {
          if(bu.t%2==i%2||hp[i]<=0)continue;
          if(Math.abs(bu.x-x[i])<25&&Math.abs(bu.y-y[i])<25)
          {
            if(pt[i]!=4||time[i]==0)hp[i]-=10;
            bu.dead=true;
          }
          
        }
        
        if(Math.abs(bu.x-powerup.x)<10&&Math.abs(bu.y-powerup.y)<10&&pc==-1)
        {
          bu.dead=true;
          pc=100+(int)(Math.random()*200);
          
          switch(powerup.type)
          {
            case 0: time[bu.t]=1; break;
            case 1: time[bu.t]=200; break;
            case 2: time[bu.t]=200; break;
            case 3: time[bu.t]=500; break;
            case 4: time[bu.t]=300; break;
          }
          
          pt[bu.t]=powerup.type;
        }
      }
      
      for(int i=0;i<bullets.size();i++)
      {
        if(bullets.get(i).y<-100||bullets.get(i).y>740||bullets.get(i).dead)
        {
          bullets.remove(i);
          i--;
        }
      }
      
      if(hack>0)hack-=2;
      if(hack==40)for(int i=0;i<=640;i+=10)bullets.add(new Bullet(i,0,0));
      if(hack==41)for(int i=0;i<=640;i+=10)bullets.add(new Bullet(i,640,1));
      
      repaint();
      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    
  }
  
  public void paint(Graphics paint)
  {
    Image offscreen = createImage(640, 640);
    Graphics2D g = (Graphics2D)offscreen.getGraphics();
    
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.magenta);
    for(int i=0;i<player;i++)if(pt[i]==4&&time[i]!=0)g.fillRect(x[i]-30, y[i]-30, 60, 60);
    for(int i=0;i<player;i++)
    {
      g.setColor(new Color[]{Color.green,Color.blue,Color.orange,Color.cyan}[i]);
      if(hp[i]>0)g.fillRect(x[i]-25, y[i]-25, 50, 50);
    }
    g.setColor(Color.red);
    for(Bullet b:bullets)g.fillOval(b.x-5, b.y-5, 10, 10);
    g.setColor(Color.white);
    g.setFont(new Font(null, Font.PLAIN, 15)); 
    for(int i=0;i<player;i++)if(hp[i]>0)g.drawString(hp[i]+"", x[i]-14, y[i]-33);
    g.setColor(Color.yellow);
    if(pc==-1)g.fillRect(powerup.x-10, powerup.y-10, 20, 20);
    g.setColor(Color.red);
    g.setFont(new Font(null, Font.PLAIN, 150)); 
    if(hack>60&&hack<90)g.drawString("外掛", 170, 380);
    
    paint.drawImage(offscreen, 0, 0, getWidth(), getHeight(),null);
  }
}


class Bullet
1
2
3
4
5
6
7
8
9
10
11
12
13
package plane;
 
public class Bullet {
  int x,y,t;
  boolean dead=false;
  
  public Bullet(int x,int y,int t)
  {
    this.x=x;
    this.y=y;
    this.t=t;//子彈屬於哪隊的
  }
}


class PowerUp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package plane;
 
public class PowerUp
{
  int x;
  int y;
  int type;
  
  public PowerUp()
  {
    this.x = (30 + (int)(Math.random() * 580.0D));
    this.y = (280 + (int)(Math.random() * 80.0D));
    this.type = ((int)(Math.random() * 5.0D));//特殊效果種類
  }
}


skps2010030908 edited on 2017-06-14 21:59
reply to postreply to post
作者 Re:戰鬥方塊(多人遊戲) [Re:skps2010030908]
weta00





發文: 1
積分: 0
於 2017-06-12 11:23 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問一下,PowerUp的class是否有錯

reply to postreply to post
作者 Re:戰鬥方塊(多人遊戲) [Re:weta00]
skps2010030908





發文: 8
積分: 0
於 2017-06-14 21:59 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不小心貼錯了
感謝提醒~
已修正


reply to postreply to post
» JWorld@TW »  Java 程式分享區 » Game

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8