我已经在自己的游戏上工作了几个星期,并且一直被碰撞所困。游戏地图是平铺的(32*32 瓷砖),我不确定我应该从哪里开始玩家 - 瓷砖碰撞。我尝试过使用矩形,但每次它都以空白屏幕或很多错误结束。
我的主要课程:
package net.Exploration.alk;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JFrame;
public class Explore_Main extends Applet implements Runnable{
private static final long serialVersionUID = 1L;
public final int TARGET_FPS = 60;
public final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
public static long lastFpsTime = 0;
public static int fps = 0;
public static int renderFps = 0;
private static JFrame frame;
public static final int res = 1;
public static double offsetY = 121, offsetX = 2;
public static int dir = 0;
public static boolean moving = false;
public static boolean run = false;
private Image screen;
public static Player player;
public Level level;
public static Dimension screenSize = new Dimension(700, 560);
public static Dimension pixel = new Dimension(screenSize.width, screenSize.height);
public static Dimension Size;
public static String name = "Exploration Alpha 0.2";
public Explore_Main(){
setPreferredSize(screenSize);
addKeyListener(new Input());
}
public static void main(String[] args){
Explore_Main explore = new Explore_Main();
frame = new JFrame();
frame.add(explore);
frame.pack();
Size = new Dimension(frame.getWidth(), frame.getHeight());
frame.setTitle(name);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
explore.start();
}
public void start(){
requestFocus();
//define classes
level = new Level(1);
player = new Player("billy bob");
new Tile();
run = true;
new Thread(this).start();
}
public void stop(){
run = false;
}
public void tick(double delta){
frame.pack();
player.tick(delta);
level.tick(delta);
}
public void render(){
Graphics g = screen.getGraphics();
g.setColor(Color.black);
g.drawRect(0, 0, 800, 800);
level.render(g, (int)offsetX, (int)offsetY, (pixel.width / Tile.size) + 2, (pixel.height / Tile.size) + 2);
player.render(g);
g.setColor(Color.red);
g.drawString("oX: " + (int)offsetX + " oY: " + (int)offsetY, 600, 515);
g.drawString("FPS: " + renderFps, 600, 530);
g = this.getGraphics();
g.drawImage(screen, 0, 0, screenSize.width, screenSize.height, 0, 0, pixel.width, pixel.height, null);
g.dispose();
}
public void run() {
screen = createVolatileImage(pixel.width, pixel.height);
long lastLoopTime = System.nanoTime();
while(run){
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / (double)OPTIMAL_TIME;
lastFpsTime += updateLength;
fps++;
if(lastFpsTime >= 1000000000){
renderFps = fps;
fps = 0;
lastFpsTime = 0;
}
tick(delta);
render();
try{
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
}catch(Exception e){}
}
}
}
等级类:
package net.Exploration.alk;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
public class Level
{
public int width = 100;
public int height =100;
public Background[][] backgroundvariable = new Background[width][height];
public Solid[][] solid = new Solid[width][height];
public Item[][]item = new Item[width][height];
//Level = Map
//Dpath is the first part of a levels name
public final String Dpath = "Resources/World/Level_";
public String path = Dpath;
private boolean[][] blocked;
public TiledMap map = null;
public Level(int id)
{
path = Dpath + Integer.toString(id) + ".tmx";
System.out.println(path);
try{
map = new TiledMap(path,false);
}catch(SlickException e)
{
System.out.println("Error Loading map!(Also might want to fix):");
}
for(int x = 0; x < backgroundvariable.length; x++)
{
for(int y = 0; y < backgroundvariable[0].length; y ++)
{
backgroundvariable[x][y] = new Background(new Rectangle(x * Tile.size, y * Tile.size,Tile.size,Tile.size),Tile.blank);
solid[x][y] = new Solid(new Rectangle(x * Tile.size, y * Tile.size,Tile.size,Tile.size),Tile.blank);
item[x][y] = new Item(new Rectangle(x * Tile.size, y * Tile.size,Tile.size,Tile.size),Tile.blank);
}
}
loadWorld();
}
public void loadWorld()
{
//declare layers
int Background = map.getLayerIndex("background");
int Solid = map.getLayerIndex("collision");
int Item = map.getLayerIndex("items");
for(int x = 0; x < backgroundvariable.length; x++)
{
for(int y = 0; y < backgroundvariable[0].length; y ++)
{
//terrain or background
if(map.getTileId(x, y, Background) == 1){
backgroundvariable[x][y].id = Tile.grass;
}
if(map.getTileId(x, y, Background) == 2){
backgroundvariable[x][y].id = Tile.stone;
}
if(map.getTileId(x, y, Background) == 3){
backgroundvariable[x][y].id = Tile.sky;
}
if(map.getTileId(x, y, Background) == 4){
backgroundvariable[x][y].id = Tile.dirt;
}
//solids
if(map.getTileId(x, y, Solid) == 1){
solid[x][y].id = Tile.grass;
//items
{
}
}
}}
}
public void tick(double delta)
{
}
public void render(Graphics g,int CamX,int CamY,int RenX,int RenY)
{
for(int x = (CamX / Tile.size); x < (CamX / Tile.size) + RenX; x++){
for(int y = (CamY / Tile.size); y < (CamY /Tile.size) + RenY; y++){
if(x >= 0 && y >= 0 && x < width && y < height){
backgroundvariable[x][y].render(g);;
}
}
}
}
}
玩家等级:
package net.Exploration.alk;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
public class Player extends Rectangle{
public double moveSpeed = 1.8;
public static int[][] pImgRight = {{0, 0}, {1, 0}, {0, 0}};
public static int[][] pImgLeft = {{2, 0}, {3, 0}, {2, 0}};
public static boolean isMoving = false;
public static boolean up = false;
public static boolean down = false;
public static boolean left = false;
public static boolean right = false;
private Rectangle PlayerRect;
public int aniFrame = 0;
public int aniTime = 10;
public int aniDelta = 0;
public Player(String name){
width = 32;
height = 32;
setBounds((Explore_Main.pixel.width / 2) - (width / 2), (Explore_Main.pixel.height / 2) - (height / 2), width, height);
}
public void tick(double delta){
aniDelta ++;
if(aniDelta >= aniTime){
aniFrame ++;
aniDelta = 0;
if(aniFrame > 2){
aniFrame = 0;
}
}
if(up){
Explore_Main.offsetY -= moveSpeed * delta;
}
if(down){
Explore_Main.offsetY += moveSpeed * delta;
}
if(left){
Explore_Main.offsetX -= moveSpeed * delta;
}
if(right){
Explore_Main.offsetX += moveSpeed * delta;
}
}
public void render(Graphics g){
if(down){
g.drawImage(Tile.Characters, this.x, this.y, x + width, y + height, pImgRight[aniFrame][0] * Tile.size, pImgRight[aniFrame][1] * Tile.size, pImgRight[aniFrame][0] * Tile.size + Tile.size, pImgRight[aniFrame][1] * Tile.size + Tile.size, null);
}else if(up){
g.drawImage(Tile.Characters, this.x, this.y, x + width, y + height, pImgRight[aniFrame][0] * Tile.size, pImgRight[aniFrame][1] * Tile.size, pImgRight[aniFrame][0] * Tile.size + Tile.size, pImgRight[aniFrame][1] * Tile.size + Tile.size, null);
}else if(left){
g.drawImage(Tile.Characters, this.x, this.y, x + width, y + height, pImgLeft[aniFrame][0] * Tile.size, pImgLeft[aniFrame][1] * Tile.size, pImgLeft[aniFrame][0] * Tile.size + Tile.size, pImgLeft[aniFrame][1] * Tile.size + Tile.size, null);
}else if(right){
g.drawImage(Tile.Characters, this.x, this.y, x + width, y + height, pImgRight[aniFrame][0] * Tile.size, pImgRight[aniFrame][1] * Tile.size, pImgRight[aniFrame][0] * Tile.size + Tile.size, pImgRight[aniFrame][1] * Tile.size + Tile.size, null);
}else{
g.drawImage(Tile.Characters, this.x, this.y, x + width, y + height, pImgRight[0][0] * Tile.size, pImgRight[0][1] * Tile.size, pImgRight[0][0] * Tile.size + Tile.size, pImgRight[0][1] * Tile.size + Tile.size, null);
}
}
}