-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTank.java
More file actions
261 lines (238 loc) · 6.8 KB
/
Tank.java
File metadata and controls
261 lines (238 loc) · 6.8 KB
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
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
/*
* Tank.java
*
* Created on 24 ãÇÑÓ, 2008, 11:36 Õ
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Mohamed Talaat Saad
*/
public class Tank {
private Image[] tankImg;
private BufferedImage ImageBuff;
private Bomb bomb[]=new Bomb[1000];
private int curBomb=0;
private int tankID;
private int posiX=-1,posiY=-1;
private int direction=1;
private float velocityX=0.03125f,velocityY=0.03125f;
private int width=559,height=473;
public int getDirection()
{
return direction;
}
/** Creates a new instance of Tank */
public Tank()
{
while(posiX<70|posiY<50|posiY>height-43|posiX>width-43)
{
posiX=(int)(Math.random()*width);
posiY=(int)(Math.random()*height);
}
loadImage(4);
}
public Tank(int x,int y,int dir,int id)
{
posiX=x;
posiY=y;
tankID=id;
direction=dir;
loadImage(0);
}
public void loadImage(int a)
{
tankImg=new Image[4];
for(int i=a;i<tankImg.length+a;i++)
{
tankImg[i-a]=new ImageIcon("Images/"+i+".png").getImage();
}
ImageBuff=new BufferedImage(tankImg[direction-1].getWidth(null),tankImg[direction-1].getHeight(null),BufferedImage.TYPE_INT_RGB);
ImageBuff.createGraphics().drawImage(tankImg[direction-1],0,0,null);
}
public BufferedImage getBuffImage()
{
return ImageBuff;
}
public int getXposition()
{
return posiX;
}
public int getYposition()
{
return posiY;
}
public void setXpoistion(int x)
{
posiX=x;
}
public void setYposition(int y)
{
posiY=y;
}
public void moveLeft()
{
if(direction==1|direction==3)
{
ImageBuff=new BufferedImage(tankImg[3].getWidth(null),tankImg[3].getHeight(null),BufferedImage.TYPE_INT_RGB);
ImageBuff.createGraphics().drawImage(tankImg[3],0,0,null);
direction=4;
}
else
{
int temp;
temp=(int)(posiX-velocityX*posiX);
if(checkCollision(temp,posiY)==false&&temp<70)
{
posiX=70;
}
else if(checkCollision(temp,posiY)==false)
{
posiX=temp;
}
}
}
public void moveRight()
{
if(direction==1|direction==3)
{
ImageBuff=new BufferedImage(tankImg[1].getWidth(null),tankImg[1].getHeight(null),BufferedImage.TYPE_INT_RGB);
ImageBuff.createGraphics().drawImage(tankImg[1],0,0,null);
direction=2;
}
else
{
int temp;
temp=(int)(posiX+velocityX*posiX);
if(checkCollision(temp,posiY)==false&&temp>width-43+20)
{
posiX=width-43+20;
}
else if(checkCollision(temp,posiY)==false)
{
posiX=temp;
}
}
}
public void moveForward()
{
if(direction==2|direction==4)
{
ImageBuff=new BufferedImage(tankImg[0].getWidth(null),tankImg[0].getHeight(null),BufferedImage.TYPE_INT_RGB);
ImageBuff.createGraphics().drawImage(tankImg[0],0,0,null);
direction=1;
}
else
{
int temp;
temp=(int)(posiY-velocityY*posiY);
if(checkCollision(posiX,temp)==false&&temp<50)
{
posiY=50;
}
else if(checkCollision(posiX,temp)==false)
{
posiY=temp;
}
}
}
public void moveBackward()
{
if(direction==2|direction==4)
{
ImageBuff=new BufferedImage(tankImg[2].getWidth(null),tankImg[2].getHeight(null),BufferedImage.TYPE_INT_RGB);
ImageBuff.createGraphics().drawImage(tankImg[2],0,0,null);
direction=3;
}
else
{
int temp;
temp=(int)(posiY+velocityY*posiY);
if(checkCollision(posiX,temp)==false&&temp>height-43+50)
{
posiY=height-43+50;
}
else if(checkCollision(posiX,temp)==false)
{
posiY=temp;
}
}
}
public void shot()
{
bomb[curBomb]=new Bomb(this.getXposition(),this.getYposition(),direction);
bomb[curBomb].startBombThread(true);
curBomb++;
}
public Bomb[] getBomb()
{
return bomb;
}
public void setTankID(int id)
{
tankID=id;
}
public int getTankID()
{
return tankID;
}
public void setDirection(int dir)
{
ImageBuff=new BufferedImage(tankImg[dir-1].getWidth(null),tankImg[dir-1].getHeight(null),BufferedImage.TYPE_INT_RGB);
ImageBuff.createGraphics().drawImage(tankImg[dir-1],0,0,null);
direction=dir;
}
public void Shot()
{
bomb[curBomb]=new Bomb(this.getXposition(),this.getYposition(),direction);
bomb[curBomb].startBombThread(false);
curBomb++;
}
public boolean checkCollision(int xP,int yP)
{
ArrayList<Tank>clientTanks=GameBoardPanel.getClients();
int x,y;
for(int i=1;i<clientTanks.size();i++) {
if(clientTanks.get(i)!=null)
{
x=clientTanks.get(i).getXposition();
y=clientTanks.get(i).getYposition();
if(direction==1)
{
if(((yP<=y+43)&&yP>=y)&&((xP<=x+43&&xP>=x)||(xP+43>=x&&xP+43<=x+43)))
{
return true;
}
}
else if(direction==2)
{
if(((xP+43>=x)&&xP+43<=x+43)&&((yP<=y+43&yP>=y)||(yP+43>=y&&yP+43<=y+43)))
{
return true;
}
}
else if(direction==3)
{
if(((yP+43>=y)&&yP+43<=y+43)&&((xP<=x+43&&xP>=x)||(xP+43>=x&&xP+43<=x+43)))
{
return true;
}
}
else if(direction==4)
{
if(((xP<=x+43)&&xP>=x)&&((yP<=y+43&&yP>=y)||(yP+43>=y&&yP+43<=y+43)))
{
return true;
}
}
}
}
return false;
}
}