JavaFX横幅类游戏开发 教训 游戏贴图
上一节课,我们即将完成战旗Demo有了一个大概的了解。教训这,我们将学习绘制游戏地图。
由于JavaFX 2.2中添加了Canvas相关的功能,我们就能够使用Canvas来实现游戏绘制了。
游戏地图绘制主要用到GraphicsContext.drawImage方法。
drawImage(Image image,double sx,double sy,double sw,double sh,double dx,double dy,double dw,double dh);
当中image 表示源图片。
sx,sy,sw,sh表示相对于源图片的x,y坐标和截取的宽度和高度。
dx,dy,dw,dy表示绘制到画布上的x, y坐标和绘制的宽度和高度。
单元图片例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2luZ2ZvdXJldmVy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
地图绘制就是将单元格进行拼接。
通常使用一个二维数组来表示地图数据例如以下:
int[][] mapIndex = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 18, 18, 18, 18, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 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 }, };
以下来看看我们的游戏地图类:
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image; public class GameMap {
private int[][] mapIndex = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 18, 18, 18, 18, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 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 }, }; private int tileWidth;
private int tileHeight;
private int cols;
private Image image; public GameMap(int tileWidth,int tileHeight, Image map){
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.image = map;
cols = (int) (map.getWidth() / tileWidth);
} public void drawMap(GraphicsContext gc) {
int mapWidth = mapIndex[0].length;
int mapHeight = mapIndex.length;
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
int px = mapIndex[y][x] % cols;
int py = mapIndex[y][x] / cols;
gc.drawImage(image, px * tileWidth, py * tileHeight, tileWidth, tileHeight, x * tileWidth, y
* tileHeight, tileWidth, tileHeight);
}
}
} public int[][] getMapIndex() {
return mapIndex;
} public void setMapIndex(int[][] mapIndex) {
this.mapIndex = mapIndex;
}
}
在实际游戏开发中。游戏地图数据通常存储在文件里。从文件读取,因为我这仅仅是个Demo,写进来方便大家直观的了解。
首先,我们通过地图贴图的宽度和单元格的宽度来计算地图贴图单元格的列数,然后在绘制的时候,就能够通过地图索引和单元格列数,计算当前绘制的贴图的行和列。通过drawImage绘制出来。
接下来,创建我们的Canvas类:
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image; public class MainCanvas extends Canvas { // 游戏地图
private GameMap gameMap;
private GraphicsContext gContext;
private Image map;
private int tileWidth = 32;
private int tileHeight = 32; private boolean isRunning = true;
private long sleep = 100;
// 主线程
private Thread thread = new Thread(new Runnable() { @Override
public void run() {
while (isRunning) {
Platform.runLater(new Runnable() { @Override
public void run() {
draw();
update();
}
});
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
public MainCanvas(double width, double height) {
super(width, height);
map = new Image(getClass().getResourceAsStream("map0.png"));
gContext = getGraphicsContext2D(); // 初始化游戏地图
gameMap = new GameMap(tileWidth, tileHeight, map); thread.start();
} public void draw() {
gameMap.drawMap(gContext);
} public void update() { }
}
MainCanvas类比較简单,创建一个线程,用于运行draw和update方法。
然后加载地图贴图,初始化GameMap,并完毕绘制工作。
最后,在Main类中,将我们的Canvas增加到布局中。
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane; public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root,640,480);
MainCanvas mainCanvas = new MainCanvas(640, 480);
root.getChildren().add(mainCanvas);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
launch(args);
}
}
以下看看执行效果:
这样,游戏地图就绘制成功了。有兴趣的朋友也能够自行改动地图索引。来绘制不同的地图。
当然在实际开发中,我们还是会用地图编辑器来编辑的。
这一节课就到此结束了,下一节再见。
本文章为个人原创。版权全部,转载请注明出处:http://blog.csdn.net/ml3947。另外我的个人博客:http://www.wjfxgame.com.
版权声明:本文博客原创文章,博客,未经同意,不得转载。
JavaFX横幅类游戏开发 教训 游戏贴图的更多相关文章
- [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计
声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...
- java游戏开发杂谈 - 游戏物体
现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...
- java游戏开发杂谈 - 游戏编程浅析
每个游戏,你所看到的它的一切,都是计算机画出来的! 地图是画出来,人物是画出来的,树木建筑是画出来的,菜单按钮是画出来的,滚动的文字.闪烁的图标.云雾烟火,都是画出来的. 游戏编程,所要做的,就是控制 ...
- Unity3D游戏开发之游戏读/存档功能在Unity3D中的实现
喜欢我的博客请记住我的名字:秦元培,我的博客地址是:http://qinyuanpei.com 转载请注明出处,本文作者:秦元培, 本文出处:http://blog.csdn.net/qinyuanp ...
- 【Unity3d游戏开发】游戏中的贝塞尔曲线以及其在Unity中的实现
RT,马三最近在参与一款足球游戏的开发,其中涉及到足球的各种运动轨迹和路径,比如射门的轨迹,高吊球,香蕉球的轨迹.最早的版本中马三是使用物理引擎加力的方式实现的足球各种运动,后来的版本中使用了根据物理 ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(2)-游戏框架搭建
让我们抛开理论开始code吧. 入口类CanyonBunnyMain的代码: package com.packtpub.libgdx.canyonbunny; import com.badlogic. ...
- 三、微信小游戏开发 --- 小游戏API调用Platform
微信小游戏API Platform主要是Egret用于来调用平台的SDK的. 在Egret中使用接口定义Platform. Egret项目中默认的platform值是DebugPlatform. 发布 ...
- csdn vip文章:Unity游戏开发-小游戏-非常简单的游戏-unity-
原文https://blog.csdn.net/qq_20484877/article/details/81841190 1*创建物体 Create菜单下 3D Object菜单下Cube 1.1设置 ...
- Android游戏开发研究帧动画实现
1.动画的原则框架 帧的动画帧的动画顾名思义,画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在同样区域高速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,事实上只 ...
随机推荐
- LINUX专题之操作系统字符集
原创作品,出自 "深蓝的blog" 博客,欢迎转载.转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog: http://blog.csdn.net/huangyanl ...
- storm-编程入门
一 编程接口 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbm ...
- 使用WIX打包客户端程序
原文:使用WIX打包客户端程序 用WPF为客户做了个小工具,打包的时候发现VS2012居然没有安装项目了,搜了下才知道现在推荐使用WIX来打包了http://wix.sourceforge.net/, ...
- SonarQube升级
1.阅读SonarQube更新日志: http://docs.codehaus.org/display/SONAR/Upgrading#Upgrading-ReleaseUpgradeNotes 2. ...
- Linux lamp环境编译安装
1.安装准备: 1)httpd-2.0.52.tar.gz 2)mysql-4.1.12.tar.gz 3)libxml2-2.6.27.tar 4)freetype-2.1.10.tar 5)gd- ...
- 逆向 Framework.jar
Ref:http://www.addictivetips.com/mobile/what-is-odex-and-deodex-in-android-complete-guide/ Ref:http: ...
- nginx学习12 ngx_cycle_t 和 ngx_init_cycle
在nginx在启动过程,ngx_init_cycle这个函数最初始工作.变量的初始化存储在ngx_cycle_t这个结构体中,为了深入了解这个函数都做了那些初始化工作,就化时间研究了一下.并写下来以便 ...
- java文件创建、删除、读取、写入操作大全
一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{ System.out.println("请输入您的 ...
- OpenStack_Swift源代码分析——ObjectReplicator源代码分析(2)
1.Replicator运行代码具体分析 上篇问中介绍了启动Replicator的详细过程,以下解说Replicator的运行代码的详细实现,首先看replicate方法: def replicate ...
- java 通过sftp服务器上传下载删除文件
最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...