Hello Guest

Problem!

  • 2 Replies
  • 4294 Views
Problem!
« on: July 08, 2011, 03:34:14 »
I'm having 2 really annoying problems today. Firstly I'm trying to access a txt file, that is in a folder within the game package. If I do levels/levelone.txt it says the system cannot find the specified path but if I use the absolute path it has no worries. Code below.
Code: [Select]
package game;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

@SuppressWarnings("deprecation")
public class Game extends BasicGameState {

float startPx = 560;
float startPy = 64;
float startEx = 100;
float startEy = 340;

int accTime;
int score;
int value = 1;
int health = 200;
int sh, sw, sx, sy, hh, hw, hx, hy;

Input input;
Player player;
Enemy enemy;
Paused paused;
Image wallI, playerI, goalI, enemyI, invisibleWallI, healthI;
TrueTypeFont font;
String scoreString, healthString;

// list of blocks
ArrayList<Block> blocks = new ArrayList<Block>();
ArrayList<InvisibleBlock> invisblocks = new ArrayList<InvisibleBlock>();
ArrayList<Goal> goals = new ArrayList<Goal>();
ArrayList<Goal> removeGoals = new ArrayList<Goal>(); // list of goals to remove each frame

public int getID() {
return Main.GAME_STATE;
}

public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
wallI = new Image("game/images/w.png");
goalI = new Image("game/images/g.png");
playerI = new Image("game/images/p.png");
enemyI = new Image("game/images/e.png");
healthI = new Image("game/images/h.png");
invisibleWallI = new Image("game/images/iw.png");

player = new Player(this, startPx, startPy, playerI);
enemy = new Enemy(this, startEx, startEy, enemyI);

font = new TrueTypeFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 14), true);

File levelOne = new File("game/levels/levelone.txt");
try
{
load(levelOne);
}
catch (Exception e)
{
e.printStackTrace();
}

input = gc.getInput();
}

public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException {
g.setBackground(Color.black);

// draw blocks
for (int b = 0; b < blocks.size(); b++) {
Block block = blocks.get(b);
block.draw(g);
}

for (int go = 0; go < goals.size(); go++) {
Goal goal = goals.get(go);
goal.draw(g);
}

for (int ib = 0; ib < invisblocks.size(); ib++) {
InvisibleBlock invisBlock = invisblocks.get(ib);
invisBlock.draw(g);
}

player.draw(g);
enemy.draw(g);

scoreString = "Score: " + score;
sh = font.getHeight(scoreString);
sw = font.getWidth(scoreString);
sx = 20;
sy = 544 - 16 - sh / 2;

g.setColor(Color.white);
g.setFont(font);
g.drawString(scoreString, sx, sy);

//healthbar
if (health < 0) {
health = 0;
}
g.drawRect(420, 544 - 16 - 17 / 2, 200, 17);
g.setColor(Color.red);
g.fillRect(420, 544 - 16 - 17 / 2, 200, 17);
g.setColor(Color.green);
g.fillRect(420, 544 - 16 - 17 / 2, health, 17);
}

public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
checkDeath(sb);

player.leftPressed = input.isKeyDown(Input.KEY_LEFT);
player.rightPressed = input.isKeyDown(Input.KEY_RIGHT);
player.jumpPressed = input.isKeyDown(Input.KEY_SPACE);

if (input.isKeyPressed(Input.KEY_P)) {
sb.enterState(Main.PAUSED_STATE);
}

player.update(delta);
enemy.update(delta);

goals.removeAll(removeGoals); // remove all goals in remove list
removeGoals.clear(); // clear the remove list
}

public void load(File file) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        for (int y = 0; y < 16; y++) {
            String line = reader.readLine();
            for (int x = 0; x < 20; x++) {
             char c = line.charAt(x);
             if (c == '#')  {
             blocks.add(new Block(wallI, x * 32, y * 32));
             }
             if (c == '@') {
             goals.add(new Goal(this, goalI, x * 32, y * 32));
             }
             if (c == '$') {
             invisblocks.add(new InvisibleBlock(invisibleWallI, x * 32, y * 32));
             }
            }
        }
        reader.close();
}

public boolean isPlayerCollisionWithBlock(Player player) {
for (int i = 0; i < blocks.size(); i++) {
Block block = blocks.get(i);

if (player.rectangle.intersects(block.rectangle)) {
return true; // collision
}
}

return false;
}

public boolean isPlayerCollisionWithEnemy(Player player) {
if (player.rectangle.intersects(enemy.rectangle)) {
return true;
}

return false;
}

public boolean isEnemyCollisionWithBlock(Enemy enemy) {
for (int i = 0; i < blocks.size(); i++) {
Block block = blocks.get(i);

if (enemy.rectangle.intersects(block.rectangle)) {
return true; // collision
}
}

return false;
}

public boolean isEnemyCollisionWithInvisBlock(Enemy enemy) {
for (int i = 0; i < invisblocks.size(); i++) {
InvisibleBlock invisBlock = invisblocks.get(i);

if (enemy.rectangle.intersects(invisBlock.rectangle)) {
return true;
}
}
return false;
}

public boolean isCollisionWithGoal(Player player) {
for (int i = 0; i < goals.size(); i++) {
Goal goal = goals.get(i);

if (player.rectangle.intersects(goal.rectangle)) {
// collision detected now alert player and/or goal object
goal.collidedWith(player);
return true;
}
}

return false;
}

public void removeGoal(Goal goal) {
removeGoals.add(goal);
addScore();
}

public void addScore() {
value += 1;
score += value;
}

public void checkDeath(StateBasedGame sb) {
if (health == 0) {
sb.enterState(Main.GAMEOVER_STATE);
}
}
}


I am stumped for ideas, so if anyone can help I would really appreciate that.

Thanks,
Shannon
« Last Edit: July 08, 2011, 03:41:49 by Shazer2 »

Re: Problem!
« Reply #1 on: July 08, 2011, 12:37:27 »
You probably have a different working directory then you think you have. When running in Eclipse (or probably any other IDE), the working directory of a running application is the location on disk where the code was compiled. You need to change your start-up configuration in the IDE to make the working directory the root of all your data.

e.g. If your images are located at "C:/new/folder/on/disk/game/images/" then you would have to put your working directory to "C:/new/folder/on/disk/"

P.S. Your post says 2 problems, but I only see one. ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: Problem!
« Reply #2 on: July 08, 2011, 22:51:19 »
Clearly, the second problem is that he's stumped for ideas  ;D


... I apologize.