Java项目:16款java游戏(java+swing)

功能简介:

16款Java小游戏

 

 

 

游戏控制界面:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Game.java

package org.game.engine;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.*;

public abstract class Game
    implements KeyListener, MouseListener, MouseMotionListener
{

    public Game()
    {
        delay = 5;
        width = 400;
        height = 300;
        background = Color.black;
        title = "My Game";
    }

    public String getTitle()
    {
        return title;
    }

    public long getDelay()
    {
        return (long)delay;
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public void resize(int i, int j)
    {
    }

    public Color getBackground()
    {
        return background;
    }

    public abstract void init();

    public abstract void update();

    public abstract void draw(Graphics2D graphics2d);

    public boolean isOver()
    {
        return over;
    }

    public void mouseDragged(MouseEvent mouseevent)
    {
    }

    public void mouseMoved(MouseEvent mouseevent)
    {
    }

    public void mouseClicked(MouseEvent mouseevent)
    {
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
    }

    public void mouseExited(MouseEvent mouseevent)
    {
    }

    public void mousePressed(MouseEvent mouseevent)
    {
    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    }

    public void keyPressed(KeyEvent keyevent)
    {
    }

    public void keyReleased(KeyEvent keyevent)
    {
    }

    public void keyTyped(KeyEvent keyevent)
    {
    }

    protected boolean over;
    protected int delay;
    protected int width;
    protected int height;
    protected Color background;
    protected String title;
}

开始游戏界面:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   GameStart.java

package org.game.engine;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

// Referenced classes of package org.game.engine:
//            Game, GameLoop, GameCanvas

public class GameStart
{

    public GameStart()
    {
    }

    public static void start(final Game game, final GameLoop loop)
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                JFrame frame = new JFrame(game.getTitle());
                System.setProperty("sun.java2d.translaccel", "true");
                System.setProperty("sun.java2d.ddforcevram", "true");
                frame.setSize(game.getWidth(), game.getHeight());
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(3);
                GameCanvas canvas = new GameCanvas(game);
                frame.add(canvas);
                frame.setVisible(true);
                canvas.requestFocus();
                loop.setGame(game);
                loop.setCanvas(canvas);
                loop.start();
            }

        
        }
);
    }
}

mini地图:

/**
 * Minimap handler, representing the map textures.
 */
public class Minimap {

    private final Map map;
    private final StrategyCursor cursor;
    private final StrategyCamera camera;
    private final EntryHandler entrys;
    private int mapViewH, mapViewV;
    private boolean minimapLocked, canMinimap;
    private Player player;

    public Minimap(Map map, StrategyCursor cursor, StrategyCamera camera, EntryHandler entrys) {
        this.map = map;
        this.cursor = cursor;
        this.camera = camera;
        this.entrys = entrys;

    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public void setView(int h, int v) {
        this.mapViewH = h;
        this.mapViewV = v;
    }

    public void update(int x, int y) {
        if (this.cursor.getClick() == 0) {
            this.minimapLocked = false;
            this.canMinimap = true;
        }
        int cx = this.cursor.getScreenX();
        int cy = this.cursor.getScreenY();
        int maxX = x + this.map.getWidthInTiles();
        int maxY = y + this.map.getHeightInTiles();
        if (!this.minimapLocked) {
            if (cx >= x && cx <= maxX && cy >= y && cy <= maxY) {
                if (this.cursor.getClick() > 0) {
                    this.minimapLocked = true;
                }
            } else {
                if (this.cursor.getClick() > 0) {
                    this.canMinimap = false;
                }
            }
        }
        if (this.minimapLocked && this.canMinimap) {
            //this.cursor.fixBetween(x, y, maxX, maxY);
            int mx = (this.cursor.getScreenX() - this.mapViewH / 2 - x) * this.map.getTileWidth();
            int my = (this.cursor.getScreenY() - this.mapViewV / 2 - y) * this.map.getTileHeight();
            this.camera.place(mx, my);
        }
    }

    public void render(Graphics2D g, int x, int y) {
        this.map.renderMiniMap(g, x, y);
        for (AbstractEntry<Tile, ModelSkill, Attributes> entry : this.entrys.list()) {
            if (entry.isAlive() && entry.isVisible() && entry.isActive()) {
                if (entry.getOwnerID() == this.player.id) {
                    g.setColor(Color.GREEN);
                } else if (entry.getOwnerID() == 0) {
                    g.setColor(Color.LIGHT_GRAY);
                } else {
                    g.setColor(Color.RED);
                }
                if (this.map.fogOfWar.isVisited(entry.getYInTile(), entry.getXInTile())
                        && this.map.fogOfWar.isFogged(entry.getYInTile(), entry.getXInTile())) {
                    g.fillRect(entry.getXInTile() + x, entry.getYInTile() + y, entry.getWidthInTile(), entry.getHeightInTile());
                }
            }
        }
        g.setColor(Color.GREEN);
        int sx = (this.camera.getOffsetX() + this.camera.getX()) / this.map.getTileWidth();
        int sy = (this.camera.getOffsetY() + this.camera.getY()) / this.map.getTileHeight();
        g.drawRect(x + sx - 1, y + sy - 1, this.mapViewH + 1, this.mapViewV + 1);
    }
}

Guess you like

Origin blog.csdn.net/m0_59687645/article/details/121727694