Java游戏开发 - 标题屏幕问题

时间:2014-12-25 20:13:57

标签: java swing

我想知道是否有人可以告诉我这里我做错了什么。我在java编写基本游戏,我想在开头添加标题屏幕。基本上,如果玩家点击“1”,我希望游戏开始。但是,当我尝试更改图形时,屏幕是空白的,但游戏的键仍然有效,游戏肯定会自行更新。它只是没有显示任何东西。这是文件。非常感谢任何帮助!

FarthornWindow.java:

package com.Farthorn.game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class FarthornWindow extends Canvas
{
private static final long serialVersionUID = 2297047193030106579L;
private JFrame frame;
private TitleScreen ts;

public FarthornWindow(int width, int  height)
{
    ts = new TitleScreen(this);
    frame = new JFrame("Farthorn - Kos");
    frame.setSize(width, height);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(ts);
    frame.setVisible(true); 
    ts.start();
}

public JFrame getFrame()
{
    return frame;
}

}

TitleScreen.java:

package com.Farthorn.game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class TitleScreen extends Canvas implements Runnable
{
FarthornWindow window;
private boolean running = false;
private Thread thread;
private BufferStrategy bs;

public TitleScreen(FarthornWindow window)
{
    this.window = window;
    this.addKeyListener(new MenuInput(this));
}

public synchronized void start()
{
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop()
{
    try
    {
        thread.join();
        running = false;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public void run() 
{
    this.requestFocus();
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while (running)
    {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while (delta >= 1)
        {
            tick();
            delta--;
        }
        if (running)
        {
            render();
        }
        frames++;

        if(System.currentTimeMillis() - timer > 1000)
        {
            timer += 1000;
            System.out.println("FPS1 " + frames);
            frames = 0;
        }
    }
    stop();
}

private void tick()
{       

}

private void render()
{
    bs = this.getBufferStrategy();
    if (bs == null)
    {
        this.createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.red);
    g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);

    g.dispose();
    bs.show();
}

public FarthornWindow getWindow()
{
    return window;
}

public void setRunning(boolean r)
{
    running = r;
}


}

Game.java:

package com.Farthorn.game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;

import javax.swing.ImageIcon;

public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = -4431078110090264106L;
public final static int WIDTH = 640;
public final static int HEIGHT = WIDTH / 12 * 9;

private Thread thread;
private boolean running = false;
private CharacterHandler cHandler;
private ObjectHandler oHandler;
public int change = 0;

private static String mapBackgroundTop = "/Users/myName/Desktop/FarthornPics/background_top.png";
private static String mapBackgroundBottom = "/Users/myName/Desktop/FarthornPics/background_bottom.png";
private static String plant1 = "/Users/myName/Desktop/FarthornPics/PlantEnemy1.png";
private static String animal1 = "/Users/myName/Desktop/FarthornPics/DumbEnemy1Still.png";

private Image backgroundTop;
private Image backgroundBottom;

private int baseheight = HEIGHT/2+64;

public Game()
{
    ImageIcon i = new ImageIcon(mapBackgroundTop);
    backgroundTop = i.getImage();
    i = new ImageIcon(mapBackgroundBottom);
    backgroundBottom = i.getImage();

    cHandler = new CharacterHandler();
    oHandler = new ObjectHandler();

    Player p1 = new Player(ID.Player, WIDTH/2-32, HEIGHT/2, oHandler, cHandler);
    cHandler.addCharacter(p1);
    /*
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+180, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+195, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+210, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+225, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+240, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+255, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+270, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+285, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+300, HEIGHT/2+104, p1, plant1));
    cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+315, HEIGHT/2+104, p1, plant1));
    */

    cHandler.addCharacter(new DumbEnemy(ID.DumbEnemy, (int)2*WIDTH, HEIGHT/2+104, p1, animal1));

    oHandler.addObject(new HUD(ID.HUD, 10, 10, cHandler));
    oHandler.addObject(new Platform(ID.Platform, -100, Game.HEIGHT/2+124, 2400, 1, (Player) cHandler.characters.get(0)));
    oHandler.addObject(new Platform(ID.Platform, 2*WIDTH, baseheight+20, 100, 1, (Player) cHandler.characters.get(0)));

    this.addKeyListener(new KeyInput((Player) cHandler.characters.get(0)));
    this.addMouseListener(new MouseInput(((Player) cHandler.characters.get(0))));

    // FarthornWindow window = new FarthornWindow(WIDTH, HEIGHT, this);
}

public synchronized void start()
{
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop()
{
    try
    {
        thread.join();
        running = false;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public void run() 
{
    this.requestFocus();

    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while (running)
    {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while (delta >= 1)
        {
            tick();
            delta--;
        }
        if (running)
        {
            render();
        }
        frames++;

        if(System.currentTimeMillis() - timer > 1000)
        {
            timer += 1000;
            System.out.println("FPS " + frames);
            frames = 0;
        }
    }
    stop();
}

private void tick()
{       
    int deltaX = 0;
    int afterX = 0;

    deltaX = cHandler.characters.get(0).getX();

    oHandler.tick();
    cHandler.tick();

    deltaX = cHandler.characters.get(0).getX();
    if (deltaX != afterX)
        change = (afterX-deltaX);
    else
        change = 0;
}

private void render()
{
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null)
    {
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(backgroundTop, 0+change, 0, null);
    g.drawImage(backgroundBottom, 0+change, 364, null);

    g.setColor(Color.blue);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    oHandler.render(g);
    cHandler.render(g);

    g.dispose();
    bs.show();
}

public int getChange()
{
    return change;
}


public static int clamp(int var, int min, int max)
{
    if (var >= max)
    {
        return var = max;
    }
    else if (var <= min){
        return var = min;
    }
    else
        return var;
}


public static boolean isSolid(Player player, ObjectHandler oHandler)
{
    for (int i = 0; i < oHandler.objects.size(); i++)
    {
        GameObject temp = oHandler.objects.get(i);
        if (temp.getId() == ID.Platform)
        {
            if (((Platform) temp).getRect().intersects(player.getFeet()))
            {
                player.setVelY(0);
                return true;
            }
        }
    }
    return false;
}

}

MenuInput.java:

package com.Farthorn.game;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MenuInput extends KeyAdapter
{
private TitleScreen ts;
public MenuInput(TitleScreen ts)
{
    this.ts = ts;
}

public void keyPressed(KeyEvent e)
{
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_1)
    {
        ts.setRunning(false);
        ts.getWindow().getFrame().remove(ts);
        Game game = new Game();
        ts.getWindow().getFrame().add(game);
        game.start();
    }
}
}

KeyInput.java:

package com.Farthorn.game;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter
{
private boolean[] pressed = new boolean[4];
private Player player;
public KeyInput(Player player)
{
    this.player = player;
    for (int i = 0; i < pressed.length; i++)
        pressed[i] = false;
}

public class jumpThread implements Runnable
{
    long jumpingTime = 200;
    public void run() {
        try
        {
            player.setVelY(-5);
            Thread.sleep(jumpingTime);
            jumpingTime = 150;
            player.setVelY(-3);
            Thread.sleep(jumpingTime);
            jumpingTime = 50;
            player.setVelY(-2);
            Thread.sleep(jumpingTime);
            player.setLanding(true);
            Thread.sleep(500);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            new Thread(this).start();
        }
    }
}

public class walkThread implements Runnable
{
    long walkingTime = 200;
    public void run() 
    {
        try {
            while (player.getWalking() == true)
            {
                    player.setCharImage("/Users/myName/Desktop/FarthornPics/charWalking1.png");
                Thread.sleep(walkingTime);
                    player.setCharImage("/Users/myName/Desktop/FarthornPics/charWalking2.png");
                Thread.sleep(walkingTime);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

public void keyPressed(KeyEvent e)
{   
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_ESCAPE){
        System.exit(0);
    }
    if (player.getID() == ID.Player)
    {
        if (key == KeyEvent.VK_A)
        {
            player.setWalking(true);
            new Thread(new walkThread()).start();
            player.setVelX(-5);
            pressed[0] = true;
        }
        if (key == KeyEvent.VK_D)
        {
            new Thread(new walkThread()).start();
            player.setVelX(5);
            pressed[1] = true;
            player.setWalking(true);
        }
        if (key == KeyEvent.VK_W)
        {
            new Thread(new jumpThread()).start();
            pressed[2] = true;
        }

    }
}

public void keyReleased(KeyEvent e)
{
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_A)
    {
        pressed[0] = false;
    }
    if (key == KeyEvent.VK_D)
    {
        pressed[1] = false;
    }

    if (!pressed[0] && !pressed[1])
    {
        player.setWalking(false);
        player.setVelX(0);
    }
}
}

RunMethod.java:

package com.Farthorn.game;

public class RunMethod {

public static void main(String[] args) 
{
    new FarthornWindow(Game.WIDTH, Game.HEIGHT);
}

}

0 个答案:

没有答案