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
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingApplication implements ActionListener
{
Image dot[]=new Image[400];
Image back;
JButton button = new JButton("DEMARRER");
JButton button1 = new JButton("ARRETER");
JButton button2 = new JButton("RECOMMENCER");
int x[]= new int[400];
int y[]= new int[400];
int rtemp=1;
int z;
int n;
int stop=1;
int game=1;
int score=0;
int add=1;
int count=0;
int level=100;
int compteur=0;
boolean go[]=new boolean[400];
boolean started=false;
boolean left=false;
boolean right=false;
boolean up=false;
boolean down=false;
JFrame frame1 = new JFrame("---Snake---");
frame1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
//éffectuer une tâche avec les booléens ci-dessus
}
});
//Specify the look and feel to use. Valid values:
//null (use the default), "Metal", "System", "Motif", "GTK+"
final static String LOOKANDFEEL = "Metal";
public Component createComponents()
{
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(this);
// label.setLabelFor(button);
button1.setMnemonic(KeyEvent.VK_I);
button1.addActionListener(this);
button1.setEnabled(false);
// label.setLabelFor(button1);
button2.setMnemonic(KeyEvent.VK_I);
button2.addActionListener(this);
button2.setEnabled(false);
// label.setLabelFor(button2);
/* An easy way to put space between a top-level container
* and its contents is to put the contents in a JPanel
* that has an "empty" border.
*/
JPanel pane = new JPanel(new GridLayout());
pane.add(button);
pane.add(button1);
pane.add(button2);
pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
10, //bottom
30) //right
);
pane.setSize(400,400);
return pane;
}
public void deplacement()
{
for(z=4 ;z <400 ; z++)
{
go[z]=false;
}
for(z=0 ; z<4 ; z++)
{
go[z]=true;
x[z]=91;
y[z]=91;
}
n=3;
game=2;
score=0;
locateRandom(4);
while(stop==1)
{
if (game==2)
{
if ((x[0]==x[n])&&(y[0]==y[n]))
{
go[n]=true;
locateRandom((n+1));
score+=add;
}
for(z = 399 ; z > 0 ; z--)
{
if (go[z])
{
x[z] = x[(z-1)];
y[z] = y[(z-1)];
if ((z>4)&&(x[0]==x[z])&&(y[0]==y[z]))
{
game=3;
}
}
}
if(left){ x[0]-=10; }
if(right){ x[0]+=10; }
if(up){ y[0]-=10; }
if(down){ y[0]+=10; }
}
if(y[0]>191){y[0]=191;game=3;}
if(y[0]<1){y[0]=1;game=3;}
if(x[0]>191){x[0]=191;game=3;}
if(x[0]<1){x[0]=1;game=3;}
if (game==3)
{
if (count <(1500/level))
{
count++;
}
else
{
count=0;
game=3;
//hei jframe refresh maachen
frame1.setContentPane(new Affiche(game,n,started,x,y,score));
frame1.pack();
frame1.setVisible(true);
stop=0;
}
}
//ici, il faut rafraichir la jframe avec les nouvelles donnees
frame1.setContentPane(new Affiche(game,n,started,x,y,score));
frame1.pack();
frame1.setVisible(true);
}
}
public void locateRandom(int turn)
{
rtemp=(int)(Math.random()*20);
x[turn]=((rtemp*10)+1) ;
rtemp=(int)(Math.random()*20);
y[turn]=((rtemp*10)+1);
n++;
}
public void actionPerformed(ActionEvent e)
{
// JFrame frame1 = new JFrame("---Snake---");
frame1.setSize(400,400);
if(e.getActionCommand().equals("DEMARRER"))
{
x[0]=91;
y[0]=91;
button.setEnabled(false);
button1.setEnabled(true);
frame1.setContentPane(new Affiche(2,0,true,x,y,0));
frame1.setLocationRelativeTo(frame1.getParent());
frame1.setBackground(Color.white);
//Display the window.
frame1.pack();
frame1.setVisible(true);
}
else
{
if(e.getActionCommand().equals("RECOMMENCER"))
{
x[0]=91;
y[0]=91;
frame1.setContentPane(new Affiche(2,0,true,x,y,0));
frame1.setLocationRelativeTo(frame1.getParent());
frame1.setBackground(Color.white);
//Display the window.
frame1.pack();
frame1.setVisible(true);
}
else
{
if(e.getActionCommand().equals("ARRETER"))
{
button.setEnabled(true);
frame1.setVisible(false);
}
}
}
}
private static void createAndShowGUI()
{
//Set the look and feel.
initLookAndFeel();
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("---Snake---");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
SwingApplication app = new SwingApplication();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.SOUTH);
frame.setLocationRelativeTo(frame.getParent());
frame.setBackground(Color.white);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
} |
Partager