Time remaining:
Help with java tile game

Programming
Tutor: None Selected Time limit: 0 Hours

Hi i'm writing a tile game(with no external engine) and I'm having problems with changing one tile to another. I have tried everything I can think of..here's the code (p.s  i am new to java so you will have to guide me)


Board Class


package boards;

import graphics.levelselect;

import graphics.menubar.MouseHandler;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import javax.swing.Timer;

import sound.code.SoundManager;

import sound.code.sound;

import Core.code.PlayerClass;

import Maps.Map1;

public class Board extends JPanel implements ActionListener{

//Double buffering

    Image dbImage;

    Graphics dbg;

Rectangle levelselect = new Rectangle(150, 100, 100, 25);

private boolean cherry = false;

    boolean mainmenu = true;

private boolean win = false;

private String Message = "";

private String fab2 = "Health";

private String fab = "Score";

private Timer timer;

private PlayerClass p;

private Map1 map;

private int score = 0;

   

    private int health = 2;

public Board() {

map = new Map1();

p = new PlayerClass();

addKeyListener(new Al());

setFocusable(true);

timer = new Timer(25, this);

timer.start();

        addMouseListener(new MouseHandler());

        addMouseMotionListener(new MouseHandler());

}

public void actionPerformed(ActionEvent e) {

SoundManager s = new SoundManager(){

@Override

public void initSounds(){

sounds.add(new sound("cherry", sound.getURL("cherry.wav")));

        }

};

if(health == 0){

Message = "You have failed to beat the maze!";

win = true;

}

if(map.getMap(p.gettileX(), p.gettileY()).equals("f")){

Message = "Congrats You Have Beaten The Maze";

win = true;

}

if(map.getMap(p.gettileX(), p.gettileY()).equals("c")){

score++;

s.playSound("cherry");

   s.stopSound("cherry");

}

repaint();

}

public void paint(Graphics g) {

super.paint(g);

if (!win) {

for (int y = 0; y < 14; y++) {

for (int x = 0; x < 14; x++) {

if (map.getMap(x, y).equals("b")) {

g.drawImage(map.getBack(), x * 32, y * 32, null);

}

if (map.getMap(x, y).equals("m")) {

g.drawImage(map.getMud(), x * 32, y * 32, null);

}

if (map.getMap(x, y).equals("f")) {

g.drawImage(map.getFinish(), x * 32, y * 32, null);

}

if (map.getMap(x, y).equals("c")) {

g.drawImage(map.getCherry(), x * 32, y * 32, null);

}

}

}

g.drawImage(p.getPlayer(), p.gettileX() * 32, p.gettileY() * 32, null);

String s = fab + Integer.toString(score);

Font font = new Font("Serif", Font.BOLD, 32);

g.setFont(font);

g.setColor(Color.BLACK);

g.drawString(s, 20 + 1, 20 - 1);

g.setColor(new Color(  0, 255,   0));

g.drawString(s, 20, 20);

String f = fab2 + Integer.toString(health);

Font font1 = new Font("Serif", Font.BOLD, 32);

g.setFont(font1);

g.setColor(Color.BLACK);

g.drawString(f, 180 + 1, 20 - 1);

g.setColor(new Color(  0, 255,   0));

g.drawString(f, 180, 20);

}

Font cat = new Font("Serif", Font.BOLD, 32);

        g.setFont(cat);

        g.setColor(Color.BLACK);

       

        if (win) {

g.setColor(Color.BLUE); 

g.setFont(cat);

g.drawString(Message, 32, 32);

dbImage = createImage(getWidth(), getHeight());

       dbg = dbImage.getGraphics();

       draw(dbg);

       g.drawImage(dbImage, 0, 0, this);

}

}

private void draw(Graphics g) {

g.drawString(Message, 32, 32);

g.fillRect(levelselect.x, levelselect.y, levelselect.width, levelselect.height);

g.setFont(new Font("Arial", Font.BOLD, 12));

        g.setColor(Color.GRAY);

        g.drawString("Back to Level Select", levelselect.x+20, levelselect.y+17);

        g.setColor(Color.CYAN);

}

public class Al extends KeyAdapter{

public void keyPressed(KeyEvent e){

int keycode = e.getKeyCode();

if (keycode == KeyEvent.VK_W) {

if (!map.getMap(p.gettileX(), p.gettileY() - 1).equals("m")) {

p.Move(0, -1);

}

}

if (keycode == KeyEvent.VK_A) {

if (!map.getMap(p.gettileX() - 1, p.gettileY()).equals("m")) {

p.Move(-1, 0);

}

}

if (keycode == KeyEvent.VK_S) {

if (!map.getMap(p.gettileX(), p.gettileY() + 1).equals("m")) {

p.Move(0, 1);

}

}

if (keycode == KeyEvent.VK_D) {

if (!map.getMap(p.gettileX() + 1, p.gettileY()).equals("m")) {

p.Move(1, 0);

}

}

if (keycode == KeyEvent.VK_UP) {

if (!map.getMap(p.gettileX(), p.gettileY() - 1).equals("m")) {

p.Move(0, -1);

}

}

if (keycode == KeyEvent.VK_LEFT) {

if (!map.getMap(p.gettileX() - 1, p.gettileY()).equals("m")) {

p.Move(-1, 0);

}

}

if (keycode == KeyEvent.VK_DOWN) {

if (!map.getMap(p.gettileX(), p.gettileY() + 1).equals("m")) {

p.Move(0, 1);

}

}

if (keycode == KeyEvent.VK_RIGHT) {

if (!map.getMap(p.gettileX() + 1, p.gettileY()).equals("m")) {

p.Move(1, 0);

}

}

}

public void keyRelased(KeyEvent e){

}

   public void keyTyped(KeyEvent e){

   

   

   }

}

public class MouseHandler extends MouseAdapter {

        @Override

        public void mouseMoved(MouseEvent e){

           

        }

@Override

    public void mousePressed(MouseEvent e){

        int mx = e.getX();

        int my = e.getY();

        if(mx > levelselect.x && mx < levelselect.x+levelselect.width && mx > levelselect.x && my < levelselect.y+levelselect.height && my > levelselect.y){

        levelselect f = new levelselect();

            setVisible(false);

       

        }

       

    }

}

}

package boards;

import graphics.levelselect;

import graphics.menubar.MouseHandler;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import javax.swing.Timer;

import sound.code.SoundManager;

import sound.code.sound;

import Core.code.PlayerClass;

import Maps.Map1;

public class Board extends JPanel implements ActionListener{

//Double buffering

    Image dbImage;

    Graphics dbg;

Rectangle levelselect = new Rectangle(150, 100, 100, 25);

private boolean cherry = false;

    boolean mainmenu = true;

private boolean win = false;

private String Message = "";

private String fab2 = "Health";

private String fab = "Score";

private Timer timer;

private PlayerClass p;

private Map1 map;

private int score = 0;

   

    private int health = 2;

public Board() {

map = new Map1();

p = new PlayerClass();

addKeyListener(new Al());

setFocusable(true);

timer = new Timer(25, this);

timer.start();

        addMouseListener(new MouseHandler());

        addMouseMotionListener(new MouseHandler());

}

public void actionPerformed(ActionEvent e) {

SoundManager s = new SoundManager(){

@Override

public void initSounds(){

sounds.add(new sound("cherry", sound.getURL("cherry.wav")));

        }

};

if(health == 0){

Message = "You have failed to beat the maze!";

win = true;

}

if(map.getMap(p.gettileX(), p.gettileY()).equals("f")){

Message = "Congrats You Have Beaten The Maze";

win = true;

}

if(map.getMap(p.gettileX(), p.gettileY()).equals("c")){

score++;

s.playSound("cherry");

   s.stopSound("cherry");

}

repaint();

}

public void paint(Graphics g) {

super.paint(g);

if (!win) {

for (int y = 0; y < 14; y++) {

for (int x = 0; x < 14; x++) {

if (map.getMap(x, y).equals("b")) {

g.drawImage(map.getBack(), x * 32, y * 32, null);

}

if (map.getMap(x, y).equals("m")) {

g.drawImage(map.getMud(), x * 32, y * 32, null);

}

if (map.getMap(x, y).equals("f")) {

g.drawImage(map.getFinish(), x * 32, y * 32, null);

}

if (map.getMap(x, y).equals("c")) {

g.drawImage(map.getCherry(), x * 32, y * 32, null);

}

}

}

g.drawImage(p.getPlayer(), p.gettileX() * 32, p.gettileY() * 32, null);

String s = fab + Integer.toString(score);

Font font = new Font("Serif", Font.BOLD, 32);

g.setFont(font);

g.setColor(Color.BLACK);

g.drawString(s, 20 + 1, 20 - 1);

g.setColor(new Color(  0, 255,   0));

g.drawString(s, 20, 20);

String f = fab2 + Integer.toString(health);

Font font1 = new Font("Serif", Font.BOLD, 32);

g.setFont(font1);

g.setColor(Color.BLACK);

g.drawString(f, 180 + 1, 20 - 1);

g.setColor(new Color(  0, 255,   0));

g.drawString(f, 180, 20);

}

Font cat = new Font("Serif", Font.BOLD, 32);

        g.setFont(cat);

        g.setColor(Color.BLACK);

       

        if (win) {

g.setColor(Color.BLUE); 

g.setFont(cat);

g.drawString(Message, 32, 32);

dbImage = createImage(getWidth(), getHeight());

       dbg = dbImage.getGraphics();

       draw(dbg);

       g.drawImage(dbImage, 0, 0, this);

}

}

private void draw(Graphics g) {

g.drawString(Message, 32, 32);

g.fillRect(levelselect.x, levelselect.y, levelselect.width, levelselect.height);

g.setFont(new Font("Arial", Font.BOLD, 12));

        g.setColor(Color.GRAY);

        g.drawString("Back to Level Select", levelselect.x+20, levelselect.y+17);

        g.setColor(Color.CYAN);

}

public class Al extends KeyAdapter{

public void keyPressed(KeyEvent e){

int keycode = e.getKeyCode();

if (keycode == KeyEvent.VK_W) {

if (!map.getMap(p.gettileX(), p.gettileY() - 1).equals("m")) {

p.Move(0, -1);

}

}

if (keycode == KeyEvent.VK_A) {

if (!map.getMap(p.gettileX() - 1, p.gettileY()).equals("m")) {

p.Move(-1, 0);

}

}

if (keycode == KeyEvent.VK_S) {

if (!map.getMap(p.gettileX(), p.gettileY() + 1).equals("m")) {

p.Move(0, 1);

}

}

if (keycode == KeyEvent.VK_D) {

if (!map.getMap(p.gettileX() + 1, p.gettileY()).equals("m")) {

p.Move(1, 0);

}

}

if (keycode == KeyEvent.VK_UP) {

if (!map.getMap(p.gettileX(), p.gettileY() - 1).equals("m")) {

p.Move(0, -1);

}

}

if (keycode == KeyEvent.VK_LEFT) {

if (!map.getMap(p.gettileX() - 1, p.gettileY()).equals("m")) {

p.Move(-1, 0);

}

}

if (keycode == KeyEvent.VK_DOWN) {

if (!map.getMap(p.gettileX(), p.gettileY() + 1).equals("m")) {

p.Move(0, 1);

}

}

if (keycode == KeyEvent.VK_RIGHT) {

if (!map.getMap(p.gettileX() + 1, p.gettileY()).equals("m")) {

p.Move(1, 0);

}

}

}

public void keyRelased(KeyEvent e){

}

   public void keyTyped(KeyEvent e){

   

   

   }

}

public class MouseHandler extends MouseAdapter {

        @Override

        public void mouseMoved(MouseEvent e){

           

        }

@Override

    public void mousePressed(MouseEvent e){

        int mx = e.getX();

        int my = e.getY();

        if(mx > levelselect.x && mx < levelselect.x+levelselect.width && mx > levelselect.x && my < levelselect.y+levelselect.height && my > levelselect.y){

        levelselect f = new levelselect();

            setVisible(false);

       

        }

       

    }

}

}

Map Class

package Maps;

import java.awt.*;

import java.util.*;

import javax.swing.*;

public class Map1 {

private Scanner map;

private String Tiles[] = new String[14];

    public Image  back,

                  finish,

                  cherry,

                  mud;

   

    public Map1(){

    //images

   

    ImageIcon img = new ImageIcon(getClass().getResource("/Textures/map1.png"));

    back = img.getImage();

    img = new ImageIcon(getClass().getResource("/Textures/mud.png"));

    mud = img.getImage();

    img = new ImageIcon(getClass().getResource("/Textures/flag.png"));

    finish = img.getImage();

    img = new ImageIcon(getClass().getResource("/Textures/cherry.png"));

    cherry = img.getImage();

   

   

   

         // files

openFile();

readfile();

closeFile();

}

    public Image getBack(){

    return back;

    }

   

    public Image getMud(){

    return mud;

    }

   

    public Image getFinish(){

    return finish;

    }

    public Image getCherry(){

    return cherry;

    }

   

   

   

   

    public String getMap(int x, int y){

String index = Tiles[y].substring(x, x + 1);

return index;

}

public void openFile() {

try{

map = new Scanner(getClass().getClassLoader().getResourceAsStream("Text/1.txt"));

}catch(Exception e){

  System.out.print("Error Loading Map");

}

}

public void readfile() {

while(map.hasNext()){

for(int i = 0; i < 14; i++){

Tiles[i] = map.next();

}

}

}

public void closeFile() {

map.close();

}

}


Nov 30th, -0001

hi 


So there is some concern, did your java code did not run or some other issue for this tile game.

Sep 6th, 2014

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Nov 30th, -0001
...
Nov 30th, -0001
Dec 5th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer