JAVA游戏设计核心类(初学者适用)
来源:互联网 发布:iphone故障检测软件 编辑:程序博客网 时间:2024/06/03 11:52
你想利用JAVA开发游戏?却不知道从何开始?
一个游戏通常以下元素组成:
- 框架:游戏窗口
- 帆布:渲染表面
- GameLoop:渲染和更新方法
- 渲染方法:绘图
- 更新的方法:你的游戏这里的逻辑
- 鼠标和键盘输入:KeyListener,鼠标监听器和MouseMotionListener添加到画布
下面是一个模板,你只需要添加你的代码在渲染(Graphics2D的G)和更新(INT deltaTime)方法。
You want to do a game and don't know where to start?
A game is usually compose of the following elements :
- Frame : The window of the game
- Canvas : The rendering surface
- GameLoop : Call the rendering and update methods
- Rendering method : All your drawing here
- Update method : All your game logic here
- Mouse and Key input : Add KeyListener, MouseListener and MouseMotionListener to the canvas
Here is a template that I use for my games. To make your own game, you just have to add your code in the render(Graphics2D g) and update(int deltaTime) methods.
import java.awt.Canvas;import java.awt.Dimension;import java.awt.Graphics2D;import java.awt.event.MouseAdapter;import java.awt.image.BufferStrategy; import javax.swing.JFrame;import javax.swing.JPanel; public class Game implements Runnable{ final int WIDTH = 1000; final int HEIGHT = 700; JFrame frame; Canvas canvas; BufferStrategy bufferStrategy; public Game(){ frame = new JFrame("Basic Game"); JPanel panel = (JPanel) frame.getContentPane(); panel.setPreferredSize(new Dimension(WIDTH, HEIGHT)); panel.setLayout(null); canvas = new Canvas(); canvas.setBounds(0, 0, WIDTH, HEIGHT); canvas.setIgnoreRepaint(true); panel.add(canvas); canvas.addMouseListener(new MouseControl()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setResizable(false); frame.setVisible(true); canvas.createBufferStrategy(2); bufferStrategy = canvas.getBufferStrategy(); canvas.requestFocus(); } private class MouseControl extends MouseAdapter{ } long desiredFPS = 60; long desiredDeltaLoop = (1000*1000*1000)/desiredFPS; boolean running = true; public void run(){ long beginLoopTime; long endLoopTime; long currentUpdateTime = System.nanoTime(); long lastUpdateTime; long deltaLoop; while(running){ beginLoopTime = System.nanoTime(); render(); lastUpdateTime = currentUpdateTime; currentUpdateTime = System.nanoTime(); update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000))); endLoopTime = System.nanoTime(); deltaLoop = endLoopTime - beginLoopTime; if(deltaLoop > desiredDeltaLoop){ //Do nothing. We are already late. }else{ try{ Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000)); }catch(InterruptedException e){ //Do nothing } } } } private void render() { Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); g.clearRect(0, 0, WIDTH, HEIGHT); render(g); g.dispose(); bufferStrategy.show(); } //TESTING private double x = 0; /** * Rewrite this method for your game */ protected void update(int deltaTime){ x += deltaTime * 0.2; while(x > 500){ x -= 500; } } /** * Rewrite this method for your game */ protected void render(Graphics2D g){ g.fillRect((int)x, 0, 200, 200); } public static void main(String [] args){ Game ex = new Game(); new Thread(ex).start(); } }
- JAVA游戏设计核心类(初学者适用)
- 【java初学者】导出jar包,隐藏核心实现类代码
- JDK环境变量的配置(java初学者适用)
- php基础知识 适用初学者
- 初学者:java中的四个核心概念
- 必备 | Java初学者4大核心基础知识!
- 十全干货:核心游戏系统架构设计
- 【游戏核心算法编程内幕】---设计模式
- 十全干货:核心游戏系统架构设计
- 以核心图表解析游戏核心机制设计原理
- 通过游戏学java(适合初学者)
- 驱动程序开发流程 (初学者适用)
- JAVA开发核心设计模式
- 消除类游戏核心算法
- QQ侧边栏功能简单实现,对初学者很适用,无序任何java代码即可实现
- Java在Linux部署项目时,常用Linux命令。适用初学者!
- 扫雷游戏设计-Java
- Java 核心类库中的一些设计模式
- SOAP简介
- struts2关于action拦截器使用方法 现记录如下
- C++指针问题与学习
- 三层架构之间的关系
- 中心湖
- JAVA游戏设计核心类(初学者适用)
- 推荐人工智能编程的必读书籍
- 6.087 Practical Programming in C, lec9
- c++中构造函数初始化的方法以及主要区别
- DataReader和DataSet的异同
- 全排列算法——JohnsonTrotter之粗略实现(1)
- Windows下配置Ruby On Rails
- .bash_profile和.bashrc的什么区别及启动过程
- 一个基础的for循环面试题