2019独角兽企业重金招聘Python工程师标准>>>
之前用过Android,Python的pygame,c#编程语言开发过游戏,一般来说,Java开发的游戏通常是AVG(英文adwenture game的简称)冒险游戏。
- package org.loon.simple.avg;
- import java.awt.Graphics;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- /**
- * Copyright 2008 - 2009
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- *
- * @project loonframework
- * @author chenpeng
- * @email:ceponline@yahoo.com.cn
- * @version 0.1
- */
- public interface IControl extends MouseListener, MouseMotionListener,
- KeyListener {
- public abstract void draw(final Graphics g);
- public abstract IControl invoke();
- }
- package org.loon.simple.avg;
- import java.awt.Graphics;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- /**
- * Copyright 2008 - 2009
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- *
- * @project loonframework
- * @author chenpeng
- * @email:ceponline@yahoo.com.cn
- * @version 0.1
- */
- public interface IAVG extends MouseListener, MouseMotionListener,
- KeyListener {
- public abstract void draw(final Graphics g);
- public abstract IControl getControl();
- public abstract void setControl(final IControl control);
- }
- package org.loon.simple.avg;
- import java.awt.Canvas;
- import java.awt.Graphics;
- /**
- * Copyright 2008 - 2009
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- *
- * @project loonframework
- * @author chenpeng
- * @email:ceponline@yahoo.com.cn
- * @version 0.1
- */
- public class AVGCanvas extends Canvas {
- /**
- *
- */
- private static final long serialVersionUID = 1982278682597393958L;
- private boolean start;
- private IAVG avg;
- public AVGCanvas(IAVG handler) {
- this.avg = handler;
- this.start = false;
- this.addKeyListener(handler);
- this.addMouseListener(handler);
- this.addMouseMotionListener(handler);
- }
- public void update(Graphics g) {
- paint(g);
- }
- public void paint(Graphics g) {
- if (this.start) {
- this.avg.draw(g);
- }
- }
- public void startPaint() {
- this.start = true;
- }
- public void endPaint() {
- this.start = false;
- }
- }
- package org.loon.simple.avg;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- /**
- * Copyright 2008 - 2009
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- *
- * @project loonframework
- * @author chenpeng
- * @email:ceponline@yahoo.com.cn
- * @version 0.1
- */
- public class AVGFrame extends Frame implements Runnable {
- /**
- *
- */
- private static final long serialVersionUID = 198284399945549558L;
- private IAVG avg;
- private AVGCanvas canvas;
- private boolean fps;
- private String titleName;
- private Thread mainLoop;
- public AVGFrame(String titleName, int width, int height) {
- this(new AVG(), titleName, width, height);
- }
- public AVGFrame(IAVG avg, String titleName, int width, int height) {
- super(titleName);
- Lib.WIDTH = width;
- Lib.HEIGHT = height;
- this.avg = avg;
- this.titleName = titleName;
- this.addKeyListener(avg);
- this.setPreferredSize(new Dimension(width + 5, height + 25));
- this.initCanvas(Lib.WIDTH, Lib.HEIGHT);
- this.pack();
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- this.setResizable(false);
- this.setLocationRelativeTo(null);
- this.setVisible(true);
- }
- public void run() {
- gameLoop();
- }
- /**
- * 开始循环窗体图像
- *
- */
- private synchronized void gameLoop() {
- canvas.startPaint();
- long second = 0L;
- int moveCount = 0;
- // 循环绘制
- for (;;) {
- long start = System.currentTimeMillis();
- this.paintScreen();
- long end = System.currentTimeMillis();
- long time = end - start;
- long sleepTime = 20L - time;
- if (sleepTime < 0L)
- sleepTime = 0L;
- try {
- Thread.sleep(sleepTime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (this.fps) {
- moveCount++;
- second += System.currentTimeMillis() - start;
- if (second >= 1000L) {
- this.setTitle(new StringBuilder(titleName).append(" FPS:")
- .append(moveCount).toString());
- moveCount = 0;
- second = 0L;
- }
- }
- }
- }
- /**
- * 启动游戏循环
- *
- */
- public void mainLoop() {
- this.mainLoop = new Thread(this);
- this.mainLoop.start();
- }
- /**
- * 初始化背景帆布
- *
- * @param width
- * @param height
- */
- private void initCanvas(final int width, final int height) {
- canvas = new AVGCanvas(avg);
- canvas.setBackground(Color.black);
- canvas.setPreferredSize(new Dimension(width, height));
- this.add(canvas);
- }
- public IAVG getAVG() {
- return this.avg;
- }
- protected void processWindowEvent(WindowEvent e) {
- super.processWindowEvent(e);
- }
- public synchronized void paintScreen() {
- canvas.repaint();
- }
- public boolean isShowFPS() {
- return fps;
- }
- public void setShowFPS(boolean fps) {
- this.fps = fps;
- }
- public Thread getMainLoop() {
- return mainLoop;
- }
- public String getTitleName() {
- return titleName;
- }
- }
例子 图下: