java坦克大战实例讲解(二)
一:绘制图片和播放音乐
我们玩游戏最重要的是什么,就是图片和音乐,那么如何展示它们呢?我们首先需要导入它们,大家可以在这个链接处下载素材:
https://download.****.net/download/itxiaoangzai/10719767
我们导入我们需要的包,里面有图片包和音乐包。
1.绘制图片
我们创建一个新的图片类,并在类中写入下面的代码:
package com.itxiaoangzai.tankegame.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class DrawUtils {
private static Map<String, Texture> map = new LinkedHashMap<>();
private DrawUtils() {
}
/**
* 获得图片的大小
*
* @param imagePath
* 图片路径
* @return 返回长度为2的数组,0为宽,1为高
* @throws IOException
* 图片不存在时的异常
*/
public static int[] getSize(String imagePath) throws IOException {
String key = getKey(imagePath);
Texture texture = map.get(key);
if (texture == null) {
String format = getFormat(imagePath);
texture = TextureLoader.getTexture(format, new FileInputStream(new File(imagePath)));
map.put(key, texture);
}
int width = (int) (texture.getImageWidth() + 0.5f);
int height = (int) (texture.getImageHeight() + 0.5f);
return new int[] { width, height };
}
/**
* 绘制图片
*
* @param imagePath
* 图片路径
* @param x
* 图片绘制时的x坐标
* @param y
* 图片绘制时的y坐标
* @throws IOException
* 图片不存在时的异常
*/
public static void draw(String imagePath, int x, int y) throws IOException {
String key = getKey(imagePath);
Texture texture = map.get(key);
if (texture == null) {
String format = getFormat(imagePath);
texture = TextureLoader.getTexture(format, new FileInputStream(new File(imagePath)));
map.put(key, texture);
}
int width = texture.getImageWidth();
int height = texture.getImageHeight();
width = texture.getTextureWidth();
height = texture.getTextureHeight();
Color.white.bind();
texture.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex2f(x + width, y);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex2f(x + width, y + height);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex2f(x, y + height);
}
GL11.glEnd();
}
private static String getKey(String imagePath) {
return imagePath;
}
private static String getFormat(String imagePath) {
if (imagePath == null) {
return null;
}
int index = imagePath.lastIndexOf(".");
if (index == -1) {
return null;
}
return imagePath.substring(index + 1);
}
}
然后我们在MyWindows类中重写方法:
/**
* 窗体加载时执行,只执行一次
*/
protected void onCreate(){
}
/**
* 鼠标事件
*/
protected void onMouseEvent(int key, int x, int y){
}
/**
* 键盘事件
*/
protected void onKeyEvent(int key){
}
/**
* 不断刷新
*/
protected void onDisplayUpdate(){
try{
DrawUtils.draw("shiCai/img/tkk.jpg", 0, 0);
}catch (IOException e){
e.printStackTrace();
}
}
这时有个疑问,我们是在每次刷新一次时写入图片,还是在不断刷新时写入图片呢?其实是这个窗体是按照毫秒显示组件的(可以理解为与游戏有关的东西),所以我们要重写不断刷新类。
我们看一下效果:
二:播放音乐
播放音乐和显示图片是一样的,只要选对路径即可。但要注意的是,播放音乐是播放一次,需要写到显示一次的类中:
protected void onCreate(){
try{
SoundUtils.play("shiCai/mysound/start.wav");
}catch(IOException e){
e.printStackTrace();
}
}
我们可以增加鼠标事件,来控制,这个音乐播放:
protected void onMouseEvent(int key, int x, int y){
if(key == 0){
try {
SoundUtils.play("shiCai/mysound/start.wav");
} catch (IOException e) {
e.printStackTrace();
}
}
else if(key == 1){
SoundUtils.stop("shiCai/mysound/start.wav");
}
}
接下来就要向大家介绍如何绘制砖墙了,欢迎大家提意见和评论。