1. 程式人生 > >JAVA練習---做一個類似推箱子的小遊戲

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);
	}
}

學習因為家裡發生一些事情中斷了好久,現在做個小遊戲,來複習一下以前的部分內容.....

其中一些圖片就不能上傳了,可以自己網上搜集一下。這個遊戲的製作方法很多,肯定會有更簡單的,我會慢慢調優的。

學習該繼續了.....