什么是二维数组?二维遍历?Java二维数组制作图片迷宫 使用如鹏游戏引擎制作窗口界面 附带压缩包下载,解压后双击start.bat启动

什么是二维数组?

数组当中放的还是数组

int [][] arr=new int[3][2];

有3个小箱子,每个箱子2个格子。

看结果?

int [][] arr=new int[3][2];

System.out.println(arr);

System.out.println(arr[0]);

System.out.println(arr[0][0]);

System.out.println(arr.length]);

System.out.println(arr[1].length);

再看结果?

int [][] arr=new int[3][];

System.out.println(arr);

System.out.println(arr[0]);//什么结果?还是哈希值吗?

System.out.println(arr[0][0]);//这个是什么结果?

二维数组的遍历?

 

程序源码如下:

package com.swift;

import java.awt.Point;
import java.awt.event.KeyEvent;

import com.rupeng.game.GameCore;

public class GameOfShuzu implements Runnable{

    public static void main(String[] args) {
        GameCore.start(new GameOfShuzu());
    }

    @Override
    public void run() {
        GameCore.setGameSize(760, 570);
        GameCore.setGameTitle("图片迷宫游戏—二维数组制作");
        GameCore.loadBgView("bg.png");
        //二维数组赋值15行15列,每张图片宽度50*36
        int[][] arr = {{3,0,0,0,1,1,0,0,2,2,3,0,0,0,1},
                        {0,0,0,0,4,1,0,0,0,2,0,0,0,0,1},
                        {0,3,0,0,0,0,0,0,0,0,0,3,3,0,1},
                        {0,3,0,0,0,0,0,0,0,2,0,0,3,0,0},
                        {0,0,0,0,0,1,0,0,0,2,0,0,0,0,1},
                        {4,4,0,0,1,1,0,0,0,0,4,4,0,0,1},
                        {0,0,0,0,0,4,4,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,2,0,0,1},
                        {0,0,0,0,1,0,0,0,0,2,2,0,0,0,1},
                        {0,4,4,0,1,0,0,0,0,0,4,4,0,0,1},
                        {0,0,0,0,0,4,4,0,0,3,0,0,0,0,0},
                        {1,1,2,0,0,0,0,1,0,0,0,4,0,0,0},
                        {0,4,0,0,0,1,0,0,0,0,0,2,0,0,1},
                        {0,0,3,0,1,0,0,0,2,2,0,0,0,2,0}};
        //二维数组遍历查看每个元素的值
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {
                System.out.print(i+"行"+j+"列 "+arr[i][j]+" | ");
            }
            System.out.println();
        }
        //计数器
        int num=0;
        //根据元素的值在游戏窗口中生成图片墙
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {
                if(arr[i][j]==1) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "1.png");
                    GameCore.setImagePosition(num, j*50, i*36);
                    num++;
                }
                if(arr[i][j]==2) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "2.png");
                    GameCore.setImagePosition(num, j*50, i*36);
                    num++;
                }
                if(arr[i][j]==3) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "3.png");
                    GameCore.setImagePosition(num, j*50, i*36);
                    num++;
                }
                if(arr[i][j]==4) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "4.png");
                    GameCore.setImagePosition(num, j*50, i*36);
                    num++;
                }
            }
            System.out.println();
        }
        
        // 女精灵
                int spriteGirl = 0;
                GameCore.createSprite(spriteGirl, "butterfly");
                GameCore.playSpriteAnimate(spriteGirl, "fly", true);
                GameCore.setSpritePosition(spriteGirl, 100, 20);
                
                for (;;) {

                    int codeNum = GameCore.getPressedKeyCode();
                    Point position = GameCore.getSpritePosition(spriteGirl);

                    if (codeNum == KeyEvent.VK_UP || codeNum == KeyEvent.VK_W) {
                        if (position.y > 30) {
                            System.out.println("up");
                            GameCore.setSpritePosition(spriteGirl, position.x, --position.y);
                            GameCore.pause(11);
                        }
                    }

                    if (codeNum == KeyEvent.VK_DOWN || codeNum == KeyEvent.VK_S) {
                        if (position.y < 550) {
                            System.out.println("down");
                            GameCore.setSpritePosition(spriteGirl, position.x, ++position.y);
                            GameCore.pause(11);
                        }
                    }
                    if (codeNum == KeyEvent.VK_LEFT || codeNum == KeyEvent.VK_A) {
                        if (position.x > 0) {
                            System.out.println("left");
                            GameCore.setSpriteFlipX(spriteGirl, true);
                            GameCore.setSpritePosition(spriteGirl, --position.x, position.y);
                            GameCore.pause(2);
                        }
                    }
                    if (codeNum == KeyEvent.VK_RIGHT || codeNum == KeyEvent.VK_D) {
                        if (position.x < 730) {
                            System.out.println("right");
                            GameCore.setSpriteFlipX(spriteGirl, false);
                            GameCore.setSpritePosition(spriteGirl, ++position.x, position.y);
                            GameCore.pause(1);
                        }
                    }
                }
        
    }

}

 

画面效果如图:

 

压缩包及相关素材下载地址:

https://pan.baidu.com/s/1hrC3TtU

 

posted @ 2017-11-01 19:34  Advancing-Swift  阅读(733)  评论(0编辑  收藏  举报