Java编程小游戏:2048益智小游戏实现 偏基础附源码

给你Java学习路线:html-css-js-jq-javase-数据库-jsp-servlet-Struts2-hibernate-mybatis-spring4-springmvc-ssh-ssm

游戏名字大家应该不陌生吧,2048 益智游戏,其实实现方式很简单,和推箱子原理差不多,接下来就附上效果图和源码,大家可以参考,欢迎讨论!

效果图如下:

主窗口源码:

public MainFrame() {

this.setTitle("JAVA版2048");

this.setLayout(null);

initControlJp();

initViewJp();

this.setSize(450, 520);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

publicvoid initViewJp() {

viewJp = new JPanel() {

@Override

publicvoid paint(Graphics g) {

flash(g);

}小编推荐一个学Java的学习裙【 六五零,五五四,六零七 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

};

viewJp.setBounds(0, 50, 480, 480);

this.add(viewJp);

}

publicvoid initControlJp() {

JLabel jl=new JLabel("瑶哥版2048");

jl.setFont(new Font("微软雅黑", Font.PLAIN, 26));

jl.setBounds(160, 10, 300, 40);

controlJP.setBounds(0, 0, 480, 50);

controlJP.setLayout(null);

controlJP.add(jl);

this.add(controlJP);

}

/**

* 绘制Map中的内容

*

* @param g

*/

publicvoid flash(Graphics g) {

g.setColor(new Color(250, 248, 239));

g.fillRect(0, 0, 450, 450);

g.setFont(new Font("微软雅黑", Font.BOLD, 25));

for (int y = 0; y < map.length; y++) {

for (int x = 0; x < map.length; x++) {

if (map[y][x] == 0) {

g.setColor(Color.white);

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 2) {

g.setColor(new Color(220, 210, 199));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 4) {

g.setColor(new Color(225, 229, 189));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 8) {

g.setColor(new Color(225, 142, 109));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 16) {

g.setColor(new Color(254, 112, 84));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 32) {

g.setColor(new Color(244, 122, 94));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 64) {

g.setColor(new Color(238, 91, 61));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 128) {

g.setColor(new Color(238, 207, 107));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 256) {

g.setColor(new Color(238, 205, 88));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 512) {

g.setColor(new Color(239, 201, 68));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 1024) {

g.setColor(new Color(238, 198, 50));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

elseif (map[y][x] == 2048) {

g.setColor(new Color(238, 196, 30));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}else {

g.setColor(new Color(60, 220, 0));

g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)

+ 5, SIZE, SIZE, 10, 10);

}

g.setColor(Color.black);

if (map[y][x] != 0) {

g.drawString(map[y][x] + "", x * SIZE + (x * 5) + 25, y

* SIZE + (y * 5) + 65);

}小编推荐一个学Java的学习裙【 六五零,五五四,六零七 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

System.out.print(map[y][x] + " ");

}

System.out.println();

}

System.out.println("------------------------------------");

}

publicvoid addListener(KeyListener listener) {

this.addKeyListener(listener);

}

}

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

控制器源码比较多,小编就没放了,有兴趣的话可以小编可以提供各位!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。