本次项目为《推箱子》,我们应用Java编程,完成一个界面简洁流畅、游戏方式简单,玩起来易于上手的桌面游戏。该推箱子项目运用的主要技术即是Java编程中的一些窗口类库、二维数组。通过面向对象、数组等技术为我们展现了一个震撼人心的游戏大战。
本项目为入门级,只设置一关,没有使用IO流。
package Txz;
import javax.swing.JFrame;
public class Txz_JFrame extends JFrame {
public Txz_JFrame() {
this.setTitle("推箱子");
this.setSize(450, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Txz_Jpanel txz=new Txz_Jpanel();
this.add(txz);
this.setVisible(true);
this.addKeyListener(txz);
}
public static void main(String[] args) {
new Txz_JFrame();
}
}
package Txz;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Txz_Jpanel extends JPanel implements Runnable,KeyListener,MouseListener,MouseMotionListener {
Thread t=null;
int tx=0;
int ty=0;
int time=250;
boolean end=true;
boolean start=true;
int x=10,y=25;
int height=40;
int weight=40;
int endtime;
int humanx=3,humany=4;
int count=15;
boolean ck=true;
int[][] maps=
{
{3,3,3,3,3,3,3,3},
{3,0,0,0,0,0,0,3},
{3,0,0,0,0,2,0,3},//3表示墙
{3,0,0,0,3,0,3,3},//2表示箱子
{3,0,0,1,0,0,0,3},//1表示人
{3,0,3,0,2,0,4,3},//0表示空地
{3,0,0,0,0,0,4,3},//4表示终点
{3,3,3,3,3,3,3,3},
};
static Image timage;
public Txz_Jpanel() {
t=new Thread(this);
addMouseListener(this);
addMouseMotionListener(this);
t.start();
}
static {
try {
timage=ImageIO.read(new File("image/award/123316.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setFont(new Font("微软雅黑",Font.BOLD,20));
g.setColor(Color.black);
g.drawString("上:W", 360, 100);
g.drawString("下:S", 360, 130);
g.drawString("左:A", 360, 160);
g.drawString("右:D", 360, 190);
g.setFont(new Font("微软雅黑",Font.BOLD,150));
g.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
g.drawLine(350, 1, 350, 352);
g.drawImage(timage,-100,-40,null);
if(ck==false) {
for(int i = 0 ; i < maps.length ; i++)
{
for(int j = 0 ; j < maps[i].length ; j++) {
//绘制墙壁
if(maps[i][j]==3)
{
g.drawImage(new ImageIcon("image/award/obz1.png").getImage(),x+j*40,y+i*40,weight, height, null);
}
else if(maps[i][j]==0)
{
int eh=(int)(Math.random()*2);
//绘制空地
g.drawImage(new ImageIcon("image/award/ob6.png").getImage(),x+j*40,y+i*40,weight,height, null);
}
else if(maps[i][j]==1)
{
//绘制水手
g.drawImage(new ImageIcon("image/award/2.png").getImage(),x+j*40,y+i*40,weight,height, null);
}
else if(maps[i][j]==2)
{
//绘制箱子
g.drawImage(new ImageIcon("image/award/ob5.png").getImage(),x+j*40,y+i*40,weight,height, null);
}
else if(maps[i][j]==4)
{
//绘制终点
g.drawImage(new ImageIcon("image/award/white.png").getImage(),x+j*40,y+i*40,weight,height, null);
}
if(humany<=0&&isrun==true) {
g.setFont(new Font("微软雅黑",Font.BOLD,60));
g.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
g.drawString("胜利", 150-i, 200-i);
}
if(isrun==false) {
g.setFont(new Font("微软雅黑",Font.BOLD,60));
g.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
g.drawString("失败", 150-i, 200-i);
}
if(start==true) {
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
g.drawString(""+count, 360-i, 60-i);
}
}
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if(ck&&e.getX()>=164&&e.getX()<=300&&e.getY()>=64&&e.getY()<=101) {
setCursor(new Cursor(Cursor.HAND_CURSOR));
}else {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(ck&&e.getX()>=164&&e.getX()<=300&&e.getY()>=64&&e.getY()<=101) {
ck=false;
try {
timage=ImageIO.read(new File("image/award/15616513.PNG"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
char ch=e.getKeyChar();//获取按键
if(ch=='w'||ch=='W') {
if(maps[humany-1][humanx]==0)//如果这是空地
{
maps[humany-1][humanx]=1;//1表示人 空地变成人
maps[humany][humanx]=0;//0表示空地 人下面的变成空地
humany--;
}
else if(maps[humany-1][humanx]==2&&maps[humany-2][humanx]==0)//如果这是箱子且箱子上面是空地
{
maps[humany-2][humanx]=2;//空地变成箱子
maps[humany-1][humanx]=1;//箱子变成人
maps[humany][humanx]=0;//人走过的变成空地
humany--;
}
repaint();
}
else if(ch=='a'||ch=='A'){
//向左走
if(maps[humany][humanx-1]==0)//如果这是空地
{
maps[humany][humanx-1]=1;//空地变人
maps[humany][humanx]=0;//人走过的变成地
humanx--;
}
else if(maps[humany][humanx-1]==2&&maps[humany][humanx-2]==0)
{
maps[humany][humanx-2]=2;
maps[humany][humanx-1]=1;
maps[humany][humanx]=0;
humanx--;
}
repaint();
}
else if(ch=='s'||ch=='S'){
//向下走动
if(maps[humany+1][humanx]==0)
{
maps[humany+1][humanx]=1;
maps[humany][humanx]=0;
humany++;
}
else if(maps[humany+1][humanx]==2&&maps[humany+2][humanx]==0)
{
maps[humany+2][humanx]=2;
maps[humany+1][humanx]=1;
maps[humany][humanx]=0;
humany++;
}
repaint();
}
else if(ch=='d'||ch=='D'){
//向右走
if(maps[humany][humanx+1]==0)
{
maps[humany][humanx+1]=1;
maps[humany][humanx]=0;
humanx++;
}
else if(maps[humany][humanx+1]==2&&maps[humany][humanx+2]==0)
{
maps[humany][humanx+2]=2;
maps[humany][humanx+1]=1;
maps[humany][humanx]=0;//走过的
humanx++;
}else if(ch=='d'||ch=='D') {
if(maps[humany][humanx+1]==4) {
maps[humany][humanx+1]=1;
maps[humany][humanx]=0;
humanx++;
}else if(maps[humany][humanx+1]==2&&maps[humany][humanx+2]==4) {
maps[humany][humanx+2]=2;
maps[humany][humanx+1]=1;
maps[humany][humanx]=0;
humanx++;
}
}
}
if(maps[6][6]==2&&maps[5][6]==2||isrun==false) {//箱子退=推到目的地
humany=-1;
t.stop();
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
boolean isrun=true;
public void run() {
// TODO Auto-generated method stub
while(isrun) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}if(ck==false) {
count--;
}
if(count==0) {
isrun=false;
}
repaint();
}
}
}