-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSneics.java
More file actions
285 lines (285 loc) · 6.95 KB
/
Copy pathSneics.java
File metadata and controls
285 lines (285 loc) · 6.95 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class Sneics extends JFrame implements Runnable,KeyListener,ActionListener{
Graphics2D bufer,fondo,gr;
Image ibufer,head,pasto,ifondo;
boolean izq,der,arr,aba,esc,unjugador,demo,enter,iniciado;
Button iniciar,red,salir,acerca;
int status,contador,cont=0,ex,ey;
String nombre;
SerpienteX serpientes[]=new SerpienteX[2],tu;
TextField texto;
Sneics(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setIgnoreRepaint(true);
setTitle("Sneics");
setUndecorated(true);
setLayout(null);
setSize(600,600);
setVisible(true);
//device.setFullScreenWindow(this);
//DisplayMode display=device.getDisplayMode();
//if(device.isDisplayChangeSupported())device.setDisplayMode(new DisplayMode(800,600,display.getBitDepth() ,DisplayMode.REFRESH_RATE_UNKNOWN));
ex=getWidth()/2-300;
ey=getHeight()/2-300;
status=0;
contador=0;
ibufer=createImage(600,600);
bufer=(Graphics2D)ibufer.getGraphics();
ifondo=createImage(getWidth(),getHeight());
fondo=(Graphics2D)ifondo.getGraphics();
gr=(Graphics2D)getGraphics();
head=obtImagen("head.png");
pasto=obtImagen("pasto.png");
iniciar=new Button("Un jugador");
red=new Button("Juego en red");
salir=new Button("Salir");
acerca=new Button("Acerca");
texto=new TextField("Nombre");
texto.setBounds(getWidth()/2-60,ey+245,120,30);
texto.setBackground(Color.RED);
texto.setForeground(Color.WHITE);
iniciar.setBounds(texto.getX(),texto.getY()+40,120,30);
red.setBounds(iniciar.getX(),iniciar.getY()+40,120,30);
red.setEnabled(false);
acerca.setBounds(iniciar.getX(),red.getY()+40,120,30);
salir.setBounds(iniciar.getX(),acerca.getY()+40,120,30);
iniciar.addActionListener(this);
red.addActionListener(this);
acerca.addActionListener(this);
salir.addActionListener(this);
agregar();
addKeyListener(this);
new Thread(this).start();
repintar();
}
private void repintar(){
iniciar.repaint();
red.repaint();
acerca.repaint();
salir.repaint();
texto.repaint();
iniciar.requestFocus();
red.requestFocus();
acerca.requestFocus();
salir.requestFocus();
texto.requestFocus();
}
private Image obtImagen(String img){
Toolkit tk=Toolkit.getDefaultToolkit();
return tk.getImage(getClass().getResource(img));
}
public void terminado(){
iniciado=false;
status=3;
}
private void demo(){
iniciar();
demo=true;
iniciado=true;
serpientes[0].setPc(true);
serpientes[1].setPc(true);
quitar();
requestFocus();
status=2;
}
private void iniciar(){
serpientes[0]=new SerpienteX(50,300,0,Color.BLUE,this,1);
serpientes[1]=new SerpienteX(550,300,2,Color.RED,this,2);
serpientes[0].setOtro(serpientes[1]);
serpientes[1].setOtro(serpientes[0]);
demo=false;
}
private void agregar(){
add(iniciar);
add(red);
add(acerca);
add(salir);
add(texto);
contador=0;
}
private void quitar(){
remove(iniciar);
remove(red);
remove(acerca);
remove(salir);
remove(texto);
}
public void pintar(){
for(int i=0;i<12;i++){
for(int j=0;j<12;j++){
bufer.drawImage(pasto,i*50,j*50,this);
}
}
if(status==0){
//MENU PRINCIPAL
dibujarInicio();
}
else if(status==1){
//ACERCA
dibujarInicio();
bufer.setColor(Color.BLACK);
bufer.setFont(new Font("",Font.PLAIN,12));
bufer.drawString("Diseno y programacion:",180,270);
bufer.drawString("Arturo Dominguez Garcia",180,290);
bufer.drawString("ArturoSoft todos los derechos reservados",180,340);
bufer.drawString("(Izquierdos tambien)",180,360);
}
else if(status==2){
//JUGANDO
serpientes[0].pintar(bufer);
serpientes[1].pintar(bufer);
}
else if(status==3){
//TERMINADO
bufer.setFont(new Font("",Font.BOLD,20));
serpientes[0].pintar(bufer);
serpientes[1].pintar(bufer);
bufer.setColor(new Color(0x80,0x80,0x80,180));
bufer.fillRect(130,250,360,170);
if(tu.muerto){
bufer.setColor(tu.enemigo.color);
bufer.drawString(tu.enemigo.nombre+" gana!",150,300);
}
else{
bufer.setColor(tu.color);
bufer.drawString(tu.nombre+" gana!",150,300);
}
bufer.drawString("Presiona ESC para salir...",150,350);
bufer.drawString("Presiona ENTER para reiniciar...",150,400);
}
fondo.setColor(Color.BLACK);
fondo.fillRect(0,0,getWidth(),getHeight());
fondo.drawImage(ibufer,ex,ey,this);
gr.drawImage(ifondo,0,0,this);
if(cont<10){
cont++;
repintar();
}
}
private void dibujarInicio(){
bufer.setColor(new Color(0x80,0x80,0x80,120));
bufer.fillRect(150,150,300,300);
bufer.drawImage(head,150,150,this);
}
public void actualizar(){
if(status==0){
contador++;
if(contador==500){
contador=0;
demo();
}
}
else if(status==2){
if(!demo){
if(izq){
if(tu.dir!=0)tu.dir=2;
}
else if(der){
if(tu.dir!=2)tu.dir=0;
}
else if(arr){
if(tu.dir!=1)tu.dir=3;
}
else if(aba){
if(tu.dir!=3)tu.dir=1;
}
}
if(esc){
agregar();
status=0;
}
if(iniciado){
serpientes[0].actualizar();
serpientes[1].actualizar();
}
if(serpientes[0].muerto||serpientes[1].muerto)terminado();
}
if(status==3){
if(esc||demo){
agregar();
status=0;
}
if(enter)
unjugador();
}
}
public void run(){
long tiempo,temp,max=40;
while(true){
tiempo=System.currentTimeMillis();
actualizar();
pintar();
temp=System.currentTimeMillis()-tiempo;
tiempo=max-temp;
try{
//System.out.println(tiempo);
if(tiempo>0)Thread.sleep(tiempo);
}catch(Exception e){}
}
}
public void keyReleased(KeyEvent evt){
int key=evt.getKeyCode();
if(key==KeyEvent.VK_LEFT)izq=false;
if(key==KeyEvent.VK_RIGHT)der=false;
if(key==KeyEvent.VK_UP)arr=false;
if(key==KeyEvent.VK_DOWN)aba=false;
if(key==KeyEvent.VK_ESCAPE)esc=false;
if(key==KeyEvent.VK_ENTER)enter=false;
}
public void keyPressed(KeyEvent evt){
if(status==2)iniciado=true;
int key=evt.getKeyCode();
if(key==KeyEvent.VK_LEFT)izq=true;
if(key==KeyEvent.VK_RIGHT)der=true;
if(key==KeyEvent.VK_UP)arr=true;
if(key==KeyEvent.VK_DOWN)aba=true;
if(key==KeyEvent.VK_ESCAPE)esc=true;
if(key==KeyEvent.VK_ENTER)enter=true;
}
public void keyTyped(KeyEvent evt){}
public void unjugador(){
nombre=texto.getText();
if(nombre.length()==0)nombre="Desconocido";
unjugador=true;
iniciar();
tu=serpientes[0];
tu.setPc(false);
tu.enemigo.setPc(true);
tu.nombre=nombre;
tu.enemigo.nombre="PC";
quitar();
requestFocus();
status=2;
}
public void actionPerformed(ActionEvent evt){
Object src=evt.getSource();
if(src==iniciar){
unjugador();
}
else if(src==red){
unjugador=false;
}
else if(src==acerca){
quitar();
add(salir);
status=1;
}
else if(src==salir){
if(status==0){
System.exit(0);
}
else if(status==1){
remove(salir);
agregar();
status=0;
}
}
}
public static void main(String arg[]){
new Sneics();
}
}