吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4819|回复: 19
收起左侧

[Java 转载] 推箱子小游戏(未优化)

[复制链接]
yy852 发表于 2017-1-6 18:04
这是最近看的黑马课程的推箱子小游戏的代码,这段代码是我自己写的,还没有优化,优化以后我会发另外一段代码的。希望大家喜欢。 TuiXiangZi.rar (67.5 KB, 下载次数: 68)


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| yy852 发表于 2017-1-6 18:09
[Java] 纯文本查看 复制代码
package 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[12][16];
	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[i].length;j++) {
				if(datas[i][j] == 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][6] = 4;
		sheeps[4][6] = lab_sheep1;
		
		JLabel lab_sheep2 = new JLabel(i);
		lab_sheep2.setBounds(12+50*6, 38+50*6, 50, 50);
		this.add(lab_sheep2);
		datas[6][6] = 4;
		sheeps[6][6] = lab_sheep2;
		
		JLabel lab_sheep3 = new JLabel(i);
		lab_sheep3.setBounds(12+50*6, 38+50*8, 50, 50);
		this.add(lab_sheep3);
		datas[8][6] = 4;
		sheeps[8][6] = 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[wy][wx-1] == 1) {
				return ;
			}
			
			if(datas[wy][wx-1] == 4) {
				if(datas[wy][wx-2]!=1 && datas[wy][wx-2]!=4) {
					datas[wy][wx-1] = 0;
					datas[wy][wx-2] = 4;
					sheeps[wy][wx-1].setLocation(12+50*wx-100, 38+50*wy);
					sheeps[wy][wx-2] = sheeps[wy][wx-1];
					sheeps[wy][wx-1] = 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[wy][wx-1] == 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[wy-1][wx] == 1) {
				return ;
			}
			
			if(datas[wy-1][wx] == 4) {
				if(datas[wy-2][wx]!=1 &&datas[wy-2][wx]!=4) {
					datas[wy-1][wx] = 0;
					datas[wy-2][wx] = 4;
					sheeps[wy-1][wx].setLocation(12+50*wx, 38+50*wy-100);
					sheeps[wy-2][wx] = sheeps[wy-1][wx];
					sheeps[wy-1][wx] = 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[wy-1][wx] == 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[wy][wx+1] == 1) {
				return ;
			}
			
			if(datas[wy][wx+1] == 4) {
				if(datas[wy][wx+2]!=1&&datas[wy][wx+2]!=4) {
					datas[wy][wx+1] = 0;
					datas[wy][wx+2] = 4;
					sheeps[wy][wx+1].setLocation(12+50*wx+100, 38+50*wy);
					sheeps[wy][wx+2] = sheeps[wy][wx+1];
					sheeps[wy][wx+1] = 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[wy][wx+1] == 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[wy+1][wx] == 1) {
				return ;
			}
			
			if(datas[wy+1][wx] == 4) {
				if(datas[wy+2][wx]!=1 && datas[wy+2][wx]!=4) {
					datas[wy+1][wx] = 0;
					datas[wy+2][wx] = 4;
					sheeps[wy+1][wx].setLocation(12+50*wx, 38+50*wy+100);
					sheeps[wy+2][wx] = sheeps[wy+1][wx];
					sheeps[wy+1][wx] = 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[wy+1][wx] == 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) {
		
	}

}
 楼主| yy852 发表于 2017-1-6 18:07
[Java] 纯文本查看 复制代码
import cn.it.lsz.ui.MainFrame;






public class App {
	public static void main(String[] args) {
		new MainFrame();
	}
}
小渊点点 发表于 2017-1-6 18:18
tkss1111 发表于 2017-1-6 18:36
纯粹学习,挺有意思的
flyz007 发表于 2017-1-6 18:56
给力啊楼主,我说学习编程呢,现在还没有开始学。
gunxsword 发表于 2017-1-6 19:08
支持楼主!
 楼主| yy852 发表于 2017-1-6 19:27
谢谢大家的支持
lulu639521 发表于 2017-1-6 19:48 来自手机
感谢楼主分享
zdf1985 发表于 2017-1-6 20:04 来自手机
学习了,,,
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 07:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表