推箱子小游戏(未优化)
这是最近看的黑马课程的推箱子小游戏的代码,这段代码是我自己写的,还没有优化,优化以后我会发另外一段代码的。希望大家喜欢。:keaipackage cn.it.lsz.ui;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class MainFrame extends Frame implements KeyListener {
public MainFrame() {
treeInit();
targetInit();
sheepInit();
wolfInit();
backgroundInit();
setMainFrameUI();
this.addKeyListener(this);
}
int wx;
int wy;
JLabel [][]sheeps = new JLabel;
int [][]datas = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
private void treeInit() {
Icon ic = new ImageIcon("tree.png");
for(int i=0;i<datas.length;i++) {
for(int j=0;j<datas.length;j++) {
if(datas == 1) {
JLabel lab_tree = new JLabel(ic);
lab_tree.setBounds(12+50*j, 38+50*i, 50, 50);
this.add(lab_tree);
}
}
}
}
private void targetInit() {
Icon i = new ImageIcon("target.png");
JLabel lab_target1 = new JLabel(i);
lab_target1.setBounds(712, 238, 50, 50);
this.add(lab_target1);
JLabel lab_target2 = new JLabel(i);
lab_target2.setBounds(712, 288, 50, 50);
this.add(lab_target2);
JLabel lab_target3 = new JLabel(i);
lab_target3.setBounds(712, 338, 50, 50);
this.add(lab_target3);
}
private void sheepInit() {
Icon i = new ImageIcon("sheep-no.png");
JLabel lab_sheep1 = new JLabel(i);
lab_sheep1.setBounds(12+50*6, 38+50*4, 50, 50);
this.add(lab_sheep1);
datas = 4;
sheeps = lab_sheep1;
JLabel lab_sheep2 = new JLabel(i);
lab_sheep2.setBounds(12+50*6, 38+50*6, 50, 50);
this.add(lab_sheep2);
datas = 4;
sheeps = lab_sheep2;
JLabel lab_sheep3 = new JLabel(i);
lab_sheep3.setBounds(12+50*6, 38+50*8, 50, 50);
this.add(lab_sheep3);
datas = 4;
sheeps = lab_sheep3;
}
private void wolfInit() {
wx = 4;
wy = 6;
Icon i = new ImageIcon("wolf-zm.png");
lab_wolf = new JLabel(i);
lab_wolf.setBounds(12+50*wx, 38+wy*50, 50, 50);
this.add(lab_wolf);
}
JLabel lab_wolf;
private void backgroundInit() {
Icon i = new ImageIcon("bg.png");
JLabel lab_bg = new JLabel(i);
lab_bg.setBounds(12, 38, 800, 600);
this.add(lab_bg);
}
private void setMainFrameUI() {
this.setLayout(null);
this.setTitle("推箱子小游戏");
this.setLocation(110, 100);
this.setSize(826, 650);
this.setVisible(true);
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == 37) {
if(datas == 1) {
return ;
}
if(datas == 4) {
if(datas!=1 && datas!=4) {
datas = 0;
datas = 4;
sheeps.setLocation(12+50*wx-100, 38+50*wy);
sheeps = sheeps;
sheeps = null;
wx = wx-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i = new ImageIcon("wolf-zb.png");
lab_wolf.setIcon(i);
}
}
if(datas == 0) {
wx = wx-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i = new ImageIcon("wolf-zb.png");
lab_wolf.setIcon(i);
}
}
if(key == 38) {
if(datas == 1) {
return ;
}
if(datas == 4) {
if(datas!=1 &&datas!=4) {
datas = 0;
datas = 4;
sheeps.setLocation(12+50*wx, 38+50*wy-100);
sheeps = sheeps;
sheeps = null;
wy = wy-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i = new ImageIcon("wolf-sm.png");
lab_wolf.setIcon(i);
}
}
if(datas == 0) {
wy = wy-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i = new ImageIcon("wolf-sm.png");
lab_wolf.setIcon(i);
}
}
if(key == 39) {
if(datas == 1) {
return ;
}
if(datas == 4) {
if(datas!=1&&datas!=4) {
datas = 0;
datas = 4;
sheeps.setLocation(12+50*wx+100, 38+50*wy);
sheeps = sheeps;
sheeps = null;
wx = wx+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i = new ImageIcon("wolf-ym.png");
lab_wolf.setIcon(i);
}
}
if(datas == 0) {
wx = wx+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i = new ImageIcon("wolf-ym.png");
lab_wolf.setIcon(i);
}
}
if(key == 40) {
if(datas == 1) {
return ;
}
if(datas == 4) {
if(datas!=1 && datas!=4) {
datas = 0;
datas = 4;
sheeps.setLocation(12+50*wx, 38+50*wy+100);
sheeps = sheeps;
sheeps = null;
wy = wy+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i = new ImageIcon("wolf-zm.png");
lab_wolf.setIcon(i);
}
}
if(datas == 0) {
wy = wy+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i = new ImageIcon("wolf-zm.png");
lab_wolf.setIcon(i);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
import cn.it.lsz.ui.MainFrame;
public class App {
public static void main(String[] args) {
new MainFrame();
}
}
加油~~~~~~~ 纯粹学习,挺有意思的 给力啊楼主,我说学习编程呢,现在还没有开始学。 支持楼主! 谢谢大家的支持:keai 感谢楼主分享 学习了,,,
页:
[1]
2