JAVA练习---做一个类似推箱子的小游戏

package com.review.test;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * 灰太狼抓喜洋洋游戏
 */
public class Game {

	public static void main(String[] args) {
		//主窗口
		wilfCatchSheep();
	}

	private static void wilfCatchSheep() {
		//设置窗口属性
		JFrame mainFrame = new JFrame("灰太狼抓喜洋洋");
		mainFrame.setSize(16*50+20, 12*50+40);		//设置窗口大小
		mainFrame.setLocationRelativeTo(null);		//设置窗口弹出位置(居中)
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置点击x窗口关闭
		//主面板(所有的组件都添加到这里,统一管理)
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(null);//把JPanel的默认布局管理器移除
		//定义二维数组,每个元素对应游戏界面的每个元素,1代表树,0代表空白
		int[][] pos = {
				{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,1,0,1,1,1,0,1,1,1,1,1,0,0,1},
				{1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,1,0,0,0,0,1,1,1,1,0,0,1},
				{1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,0,0,0,0,1,1,1,1,1,0,0,1},
				{1,0,1,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}
		};
		//遍历二维数组,有1的地方添加树
		for(int i= 0 ; i < 12 ; i ++){
			for(int j = 0 ; j < 16 ; j ++){
				if(pos[i][j]==1){
					JLabel tree = new JLabel(new ImageIcon("tree.png"));
					tree.setBounds(j*50,i*50, 50, 50);		
					mainPanel.add(tree);
				}
			}
		}
		//添加灰太狼
		JLabel wolff = new JLabel(new ImageIcon("wolff.png"));
		wolff.setBounds(150,150 , 50, 50);
		mainPanel.add(wolff);
		
		//添加喜洋洋
		JLabel sheep1 = new JLabel(new ImageIcon("sheep.png"));
		sheep1.setBounds(350, 150, 50, 50);
		mainPanel.add(sheep1);
		JLabel sheep2 = new JLabel(new ImageIcon("sheep.png"));
		sheep2.setBounds(350, 250, 50, 50);
		mainPanel.add(sheep2);
		JLabel sheep3 = new JLabel(new ImageIcon("sheep.png"));
		sheep3.setBounds(350, 350, 50, 50);
		mainPanel.add(sheep3);
		//添加三个笼子
		JLabel box1 = new JLabel(new ImageIcon("box.png"));
		box1.setBounds(700, 250, 50, 50);
		pos[5][14]=3;
		mainPanel.add(box1);
		JLabel box2 = new JLabel(new ImageIcon("box.png"));
		box2.setBounds(700, 300, 50, 50);
		pos[6][14]=3;
		mainPanel.add(box2);
		JLabel box3 = new JLabel(new ImageIcon("box.png"));
		box3.setBounds(700, 350, 50, 50);
		pos[7][14]=3;
		mainPanel.add(box3);
				
		//添加键盘监听
		mainFrame.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
			}
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_DOWN){
					wolff.setIcon(new ImageIcon("wolff.png"));
					if(pos[(wolff.getY()+50)/50][wolff.getX()/50]!=1){
						if((wolff.getX()==sheep1.getX())&&((wolff.getY()+50)==sheep1.getY())){
							if(!(pos[(sheep1.getY()+50)/50][sheep1.getX()/50]==1||
									(sheep1.getX()==sheep2.getX()&&(sheep1.getY()+50==sheep2.getY()))||
									(sheep1.getX()==sheep3.getX()&&(sheep1.getY()+50==sheep3.getY())))){
									sheep1.setLocation(sheep1.getX(), sheep1.getY()+50);
									wolff.setLocation(wolff.getX(), wolff.getY()+50);
							}
						}else if((wolff.getX()==sheep2.getX())&&((wolff.getY()+50)==sheep2.getY())){
							if(!(pos[(sheep2.getY()+50)/50][sheep2.getX()/50]==1||
									(sheep2.getX()==sheep1.getX()&&(sheep2.getY()+50==sheep1.getY()))||
									(sheep2.getX()==sheep3.getX()&&(sheep2.getY()+50==sheep3.getY())))){
									sheep2.setLocation(sheep2.getX(), sheep2.getY()+50);
									wolff.setLocation(wolff.getX(), wolff.getY()+50);
							}
						}else if((wolff.getX()==sheep3.getX())&&((wolff.getY()+50)==sheep3.getY())){
							if(!(pos[(sheep3.getY()+50)/50][sheep3.getX()/50]==1||
									(sheep3.getX()==sheep1.getX()&&(sheep3.getY()+50==sheep1.getY()))||
									(sheep3.getX()==sheep2.getX()&&(sheep3.getY()+50==sheep2.getY())))){
									sheep3.setLocation(sheep3.getX(), sheep3.getY()+50);
									wolff.setLocation(wolff.getX(), wolff.getY()+50);
							}
						}else{
							wolff.setLocation(wolff.getX(), wolff.getY()+50);
						}
					}
				}else if(e.getKeyCode()==KeyEvent.VK_UP){
					wolff.setIcon(new ImageIcon("wolfb.png"));
					if(!(pos[(wolff.getY()-50)/50][wolff.getX()/50]==1)){
						if((wolff.getX()==sheep1.getX())&&((wolff.getY()-50)==sheep1.getY())){
							if(!(pos[(sheep1.getY()-50)/50][sheep1.getX()/50]==1||
									(sheep1.getX()==sheep2.getX()&&(sheep1.getY()-50==sheep2.getY()))||
									(sheep1.getX()==sheep3.getX()&&(sheep1.getY()-50==sheep3.getY())))){
									sheep1.setLocation(sheep1.getX(), sheep1.getY()-50);
									wolff.setLocation(wolff.getX(), wolff.getY()-50);
							}
						}else if((wolff.getX()==sheep2.getX())&&((wolff.getY()-50)==sheep2.getY())){
							if(!(pos[(sheep2.getY()-50)/50][sheep2.getX()/50]==1||
									(sheep2.getX()==sheep1.getX()&&(sheep2.getY()-50==sheep1.getY()))||
									(sheep2.getX()==sheep3.getX()&&(sheep2.getY()-50==sheep3.getY())))){
									sheep2.setLocation(sheep2.getX(), sheep2.getY()-50);
									wolff.setLocation(wolff.getX(), wolff.getY()-50);
							}
						}else if((wolff.getX()==sheep3.getX())&&((wolff.getY()-50)==sheep3.getY())){
							if(!(pos[(sheep3.getY()-50)/50][sheep3.getX()/50]==1||
									(sheep3.getX()==sheep1.getX()&&(sheep3.getY()-50==sheep1.getY()))||
									(sheep3.getX()==sheep2.getX()&&(sheep3.getY()-50==sheep2.getY())))){
									sheep3.setLocation(sheep3.getX(), sheep3.getY()-50);
									wolff.setLocation(wolff.getX(), wolff.getY()-50);
							}
						}else{
							wolff.setLocation(wolff.getX(), wolff.getY()-50);
						}
					}
				}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
					wolff.setIcon(new ImageIcon("left.png"));
					if(!(pos[wolff.getY()/50][(wolff.getX()-50)/50]==1)){
						if((wolff.getY()==sheep1.getY())&&((wolff.getX()-50)==sheep1.getX())){
							if(!(pos[sheep1.getY()/50][(sheep1.getX()-50)/50]==1||
									(sheep1.getY()==sheep2.getY()&&(sheep1.getX()-50==sheep2.getX()))||
									(sheep1.getY()==sheep3.getY()&&(sheep1.getX()-50==sheep3.getX())))){
									sheep1.setLocation(sheep1.getX()-50, sheep1.getY());
									wolff.setLocation(wolff.getX()-50, wolff.getY());
							}
						}else if((wolff.getY()==sheep2.getY())&&((wolff.getX()-50)==sheep2.getX())){
							if(!(pos[sheep2.getY()/50][(sheep2.getX()-50)/50]==1||
									(sheep2.getY()==sheep1.getY()&&(sheep2.getX()-50==sheep1.getX()))||
									(sheep2.getY()==sheep3.getY()&&(sheep2.getX()-50==sheep3.getX())))){
									sheep2.setLocation(sheep2.getX()-50, sheep2.getY());
									wolff.setLocation(wolff.getX()-50, wolff.getY());
							}
						}else if((wolff.getY()==sheep3.getY())&&((wolff.getX()-50)==sheep3.getX())){
							if(!(pos[sheep3.getY()/50][(sheep3.getX()-50)/50]==1||
									(sheep3.getY()==sheep1.getY()&&(sheep3.getX()-50==sheep1.getX()))||
									(sheep3.getY()==sheep2.getY()&&(sheep3.getX()-50==sheep2.getX())))){
									sheep3.setLocation(sheep3.getX()-50, sheep3.getY());
									wolff.setLocation(wolff.getX()-50, wolff.getY());
							}
						}else{
							wolff.setLocation(wolff.getX()-50, wolff.getY());
						}
					}
				}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
					wolff.setIcon(new ImageIcon("right.png"));
					if(pos[wolff.getY()/50][(wolff.getX()+50)/50]!=1){
						if((wolff.getY()==sheep1.getY())&&((wolff.getX()+50)==sheep1.getX())){
							if(!(pos[sheep1.getY()/50][(sheep1.getX()+50)/50]==1||
									(sheep1.getY()==sheep2.getY()&&(sheep1.getX()+50==sheep2.getX()))||
									(sheep1.getY()==sheep3.getY()&&(sheep1.getX()+50==sheep3.getX())))){
									sheep1.setBounds(sheep1.getX()+50, sheep1.getY(), 50, 50);
									wolff.setBounds(wolff.getX()+50, wolff.getY(), 50, 50);
							}
						}else if((wolff.getY()==sheep2.getY())&&((wolff.getX()+50)==sheep2.getX())){
							if(!(pos[sheep2.getY()/50][(sheep2.getX()+50)/50]==1||
									(sheep2.getY()==sheep1.getY()&&(sheep2.getX()+50==sheep1.getX()))||
									(sheep2.getY()==sheep3.getY()&&(sheep2.getX()+50==sheep3.getX())))){
									sheep2.setBounds(sheep2.getX()+50, sheep2.getY(), 50, 50);
									wolff.setBounds(wolff.getX()+50, wolff.getY(), 50, 50);
							}
						}else if((wolff.getY()==sheep3.getY())&&((wolff.getX()+50)==sheep3.getX())){
							if(!(pos[sheep3.getY()/50][(sheep3.getX()+50)/50]==1||
									(sheep3.getY()==sheep1.getY()&&(sheep3.getX()+50==sheep1.getX()))||
									(sheep3.getY()==sheep2.getY()&&(sheep3.getX()+50==sheep2.getX())))){
									sheep3.setBounds(sheep3.getX()+50, sheep3.getY(), 50, 50);
									wolff.setBounds(wolff.getX()+50, wolff.getY(), 50, 50);
							}
						}else{
							wolff.setBounds(wolff.getX()+50, wolff.getY(), 50, 50);
						}
					}
				}
				
				if(pos[sheep1.getY()/50][sheep1.getX()/50]==3){
					sheep1.setIcon(new ImageIcon("40.png"));
				}else{
					sheep1.setIcon(new ImageIcon("sheep.png"));
				}
				if(pos[sheep2.getY()/50][sheep2.getX()/50]==3){
					sheep2.setIcon(new ImageIcon("40.png"));
				}else{
					sheep2.setIcon(new ImageIcon("sheep.png"));
				}
				if(pos[sheep3.getY()/50][sheep3.getX()/50]==3){
					sheep3.setIcon(new ImageIcon("40.png"));
				}else{
					sheep3.setIcon(new ImageIcon("sheep.png"));
				}
				
				if(pos[sheep1.getY()/50][sheep1.getX()/50]==3&&pos[sheep2.getY()/50][sheep2.getX()/50]==3&&pos[sheep3.getY()/50][sheep3.getX()/50]==3){
					JDialog jd = new JDialog();
					jd.setTitle("恭喜你!");
					jd.setSize(400, 400);
					jd.add(new JLabel(new ImageIcon("ok.jpg")));
					jd.setLocationRelativeTo(null);
					
					jd.setVisible(true);
				}
			}
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
			}
		});
		mainFrame.add(mainPanel);
		mainFrame.setVisible(true);
	}
}

学习因为家里发生一些事情中断了好久,现在做个小游戏,来复习一下以前的部分内容.....

其中一些图片就不能上传了,可以自己网上搜集一下。这个游戏的制作方法很多,肯定会有更简单的,我会慢慢调优的。

学习该继续了.....

猜你喜欢

转载自blog.csdn.net/XiaodunLP/article/details/82389588