我只是编程游戏,但有些东西不能用于主要代码(其中keylistener是(和JFrame))
package two.game.first.windows;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map.Entry;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import two.game.first.windows.OpenLoad;
import two.game.first.help.Images;
import two.game.first.help.Images.dir;
import two.game.first.help.Data;
public class Game{
public static JFrame f = new JFrame();
private static int width = OpenLoad.width;
private static int height = OpenLoad.height;
private static String map=null, save=null;
private static String p = OpenLoad.path;
public static boolean paused = false;
public static HashMap<String, String> blocks = new HashMap<String, String>();
private static File mapf;
//X IS ALWAYS FIRST IN THE FILES!!!
public Game(File map) throws IOException{
this.map = map.getName();
this.mapf = map;
start();
}
public Game(File map, File save) throws IOException{
this.map = map.getName();
this.save = save.getName();
this.mapf = map;
start();
}
private static void start(){
f.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP){
Images.reDraw(blocks, dir.UP);
System.out.println("up key");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
Images.reDraw(blocks, dir.RIGHT);
System.out.println("right key");
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
Images.reDraw(blocks, dir.DOWN);
System.out.println("down key");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
Images.reDraw(blocks, dir.LEFT);
System.out.println("left key");
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
pause();
}
if (e.getKeyCode() == KeyEvent.VK_E){
try {
Data.save(mapf, blocks);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP){
Images.reDraw(blocks, dir.UP);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
Images.reDraw(blocks, dir.RIGHT);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
Images.reDraw(blocks, dir.DOWN);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
Images.reDraw(blocks, dir.LEFT);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
});
f.setSize(width, height);
if (save == null){
f.setTitle("RofsGame - "+map);
}else{
f.setTitle("RofsGame - "+map+" - "+save);
}
f.setVisible(true);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.getContentPane().setLayout(null);
File f = new File("apple.jpg");
Images.draw(f, 10, 10, blocks);
OpenLoad.close();
Menu.close();
try {
Data.get(blocks, mapf);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void pause(){
if (paused){
paused = false;
}else{
paused = true;
}
}
public static void close(){
f.dispose();
}
}
这是另一个类(图像(和移动图像))
package two.game.first.help;
import java.io.File;
import java.util.HashMap;
import java.util.Map.Entry;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import two.game.first.windows.Game;
public class Images {
public static void draw(File file, int x, int y, HashMap<String, String> saves){
ImageIcon img = new ImageIcon(file.getPath());
String name = file.getName();
JLabel imgs = new JLabel(img);
imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
imgs.setLocation(x, y);
imgs.setVisible(true);
Game.f.add(imgs);
imgs.setVisible(true);
saves.put(x+"_"+y, name);
}
public static void reDraw(HashMap<String, String> saves, int x, int y){
Game.f.getContentPane().removeAll();
for (Entry<String, String> e : saves.entrySet()){
String img = e.getValue();
String[] help = e.getKey().split("_");
int ox = Integer.parseInt(help[0]), oy = Integer.parseInt(help[1]);
File f = new File(img);
if (!f.exists()){
return;
}
ImageIcon imgi = new ImageIcon(img);
JLabel l = new JLabel(imgi);
l.setBounds(x, y, imgi.getIconWidth(), imgi.getIconHeight());
l.setLocation(x, y);
l.setVisible(true);
Game.f.add(l);
saves.remove(ox+"_"+oy);
saves.put(x+"_"+y, f.getName());
}
}
public static void reDraw(HashMap<String, String> saves, dir d){
if (saves.isEmpty() || saves == null){
return;
}
HashMap<String, String> asd = new HashMap<String, String>();
Game.f.getContentPane().removeAll();
for (Entry<String, String> e : saves.entrySet()){
if (saves.isEmpty()){
return;
}
String img = e.getValue();
File f = new File(img);
if (!f.exists()){
return;
}
String[] help = e.getKey().split("_");
int ox = Integer.parseInt(help[0]), oy = Integer.parseInt(help[1]);
int x = 0, y = 0;
ImageIcon i = new ImageIcon(img);
JLabel l = new JLabel(i);
switch(d){
case LEFT:
x = ox+200;
l.setBounds(x, oy, i.getIconWidth(), i.getIconHeight());
l.setLocation(x, oy);
l.setVisible(true);
Game.f.add(l);
l.setVisible(true);
asd.put(x+"_"+oy, f.getName());
case RIGHT:
x = ox-200;
l.setBounds(x, oy, i.getIconWidth(), i.getIconHeight());
l.setLocation(x, oy);
l.setVisible(true);
Game.f.add(l);
l.setVisible(true);
asd.put(x+"_"+oy, f.getName());
case UP:
y = oy+200;
l.setBounds(ox, y, i.getIconWidth(), i.getIconHeight());
l.setLocation(ox, y);
l.setVisible(true);
Game.f.add(l);
l.setVisible(true);
asd.put(ox+"_"+y, f.getName());
case DOWN:
y = oy-200;
l.setBounds(ox, y, i.getIconWidth(), i.getIconHeight());
l.setLocation(ox, y);
l.setVisible(true);
Game.f.add(l);
l.setVisible(true);
asd.put(ox+"_"+y, f.getName());
}
}
saves.clear();
saves = asd;
}
public static enum dir{
LEFT,RIGHT,UP,DOWN;
}
}
顺便说一下,我试过在keylistener上放一个try {} catch(){}循环,但我没有收到错误!