gameFrame.addKeyListener(new KeyListener() {//添加键盘监听
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
});
的快捷键为在小括号里输入 new K即可
今天是核污水排海的321天
也是小日子的第七轮排海中
自行体会,今天已经是一个完整的关卡了
package com.meilingluo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TXZ_0 {//每一个格子大小默认为48*48
public static void main(String[] args) {
//JFrame 窗体
//ImageIcon 图片
//JLable 标签
//JPanel 面板
//布局管理器
JFrame gameFrame = new JFrame("推箱子版本0--梅零落");
gameFrame.setSize(22 * 48 + 12, 12 * 48 + 35);//12和35是标题和边框占的距离
gameFrame.setLocationRelativeTo(null);//让窗体居中
gameFrame.setDefaultCloseOperation(3);//关闭结束进程
JPanel panel = new JPanel();//面板
panel.setBackground(Color.PINK);//染色
panel.setLayout(null);//自定义布局
gameFrame.setContentPane(panel);//窗体和面板关联
ImageIcon boxImg = new ImageIcon("imgs/box3.png");
JLabel box = new JLabel(boxImg);
panel.add(box);
box.setBounds(3 * 48, 3 * 48, 48, 48);
ImageIcon goalImg = new ImageIcon("imgs/goal3.png");
JLabel goal = new JLabel(goalImg);
panel.add(goal);
goal.setBounds(7 * 48, 8 * 48, 48, 48);
ImageIcon workerImg = new ImageIcon("imgs/workerRight4.png");
JLabel worker = new JLabel(workerImg);
panel.add(worker);
worker.setBounds(10 * 48, 1 * 48, 48, 48);
ImageIcon wallImg = new ImageIcon("imgs/wall3.png");
JLabel[] walls = new JLabel[22 * 2 + 10 * 2 + 8 + 10];
for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(wallImg);
}
int index = 0;//记录使用到数组中的那块瓷砖
//铺上下墙
for (int i = 0; i < 22; i++) {
panel.add(walls[index]);
walls[index++].setBounds(i * 48, 0, 48, 48);
panel.add(walls[index]);
walls[index++].setBounds(i * 48, 11 * 48, 48, 48);
}
//铺左右墙
for (int i = 1; i <= 10; i++) {
panel.add(walls[index]);
walls[index++].setBounds(0, i * 48, 48, 48);
panel.add(walls[index]);
walls[index++].setBounds(21 * 48, i * 48, 48, 48);
}
for (int i = 0; i < 8; i++) {
panel.add(walls[index]);
walls[index++].setBounds((5 + i) * 48, 2 * 48, 48, 48);
}
for (int i = 0; i < 5; i++) {
panel.add(walls[index]);
walls[index++].setBounds((9 + i) * 48, (8 - i) * 48, 48, 48);
panel.add(walls[index]);
walls[index++].setBounds((2 + i) * 48, (4 + i) * 48, 48, 48);
}
System.out.println(index);
gameFrame.addKeyListener(new KeyListener() {//添加键盘监听
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
//柔柔移动
int keyCode = e.getKeyCode();
int x = 0;
int y = 0;
String imgPath = "workerLeft4.png";
if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
System.out.println("左");
x-=48;
} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
System.out.println("上");
y-=48;
imgPath = "workerUp4.png";
} else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
System.out.println("右");
x+=48;
imgPath = "workerRight4.png";
} else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
System.out.println("下");
y+=48;
imgPath = "workerDown4.png";
}
ImageIcon img = new ImageIcon("imgs/"+ imgPath);
worker.setIcon(img);
worker.setBounds(worker.getBounds().x+x,worker.getBounds().y+y,48,48);
//柔柔不能撞墙
for(int i=0;i<walls.length;i++){
//如果位置重合
if(worker.getBounds().contains(walls[i].getBounds())){
System.out.println("重合了");
worker.setBounds(worker.getBounds().x-x,worker.getBounds().y-y,48,48);
break;
}
}
//柔柔推箱子
if(worker.getBounds().contains(box.getBounds())){
box.setBounds(box.getBounds().x+x,box.getBounds().y+y,48,48);
}
//防止箱子撞墙
for (int i = 0;i<walls.length;i++){
if(box.getBounds().contains(walls[i].getBounds())){
box.setBounds(box.getBounds().x-x,box.getBounds().y-y,48,48);
worker.setBounds(worker.getBounds().x-x,worker.getBounds().y-y,48,48);
break;
}
}
//输赢判断
if(box.getBounds().contains(goal.getBounds())){
JOptionPane.showMessageDialog(null,"赢了");
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
gameFrame.setVisible(true);//可视,写最后一行
}
}