hello, I have following maze game which needs to have extra levels, i have a bas
ID: 653959 • Letter: H
Question
hello, I have following maze game which needs to have extra levels, i have a basic one level but need help making level 2 with little bit added difficulty? it needs, implementing a functional button which i can click to go to next level when win at maze 1, so you can go to maze 2. please see code attached below...
___________________________________________
package maze;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Map m;
private User u;
private BufferedImage image;
private boolean win = false;
private String Message = "";
private Font font = new Font("Sans-Serif", Font.ITALIC, 48);
public Board(){
m = new Map();
u = new User();
addKeyListener(new Al());
setFocusable(true);
timer = new Timer(30,this);
timer.start();
}
public void actionPerformed(ActionEvent e){
if(m.getMap(u.getTileX(), u.getTileY()). equals("C")){
Message = "Got the cheese..:)";
try {
image = ImageIO.read(new File("C://Project//Jerry got it.jpg"));
} catch (IOException ex) {
// handle exception...
}
win = true;
}
repaint();
}
public void paint(Graphics g){
super.paint(g);
if(!win){
for(int y = 0; y < 16; y++){
for(int x = 0; x < 16; x++){
if(m.getMap(x, y).equals("C")){
g.drawImage(m.getCheese(), x * 32, y * 32, null);
}
if(m.getMap(x, y).equals("G")){
g.drawImage(m.getGround(), x * 32, y * 32, null);
}
if(m.getMap(x, y).equals("W")){
g.drawImage(m.getWall(), x * 32, y * 32, null);
}
}
}
g.drawImage(u.getUser(), u.getTileX() * 32, u.getTileY() * 32, null);
}
if(win){
g.setColor(Color.GREEN);
g.setFont(font);
g.drawString(Message, 100, 150);
g.drawImage(image, 200, 161, null);
}
}
public class Al extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP){
if(!m.getMap(u.getTileX(), u.getTileY() - 1). equals("W")){
u.move(0, -1);
}
}
if(keycode == KeyEvent.VK_DOWN){
if(!m.getMap(u.getTileX(), u.getTileY() + 1). equals("W")){
u.move(0, 1);
}
}
if(keycode == KeyEvent.VK_LEFT){
if(!m.getMap(u.getTileX() - 1, u.getTileY()). equals("W")){
u.move(-1, 0);
}
}
if(keycode == KeyEvent.VK_RIGHT){
if(!m.getMap(u.getTileX() + 1, u.getTileY()). equals("W")){
u.move(1, 0);
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
}
__________________________________________________
package maze;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;
public class Maze {
public static void main(String[] args) throws Exception {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("C://Sound1.wav"));
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
Thread.sleep(100); // looping as long as this thread is alive
new Maze();
}
public Maze(){
JFrame f = new JFrame();
f.setTitle("A maze-ing");
f.add(new Board());
f.setSize(528, 550);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
__________________________________________
package maze;
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;
public class Map {
private Scanner m;
private String Map[] = new String[16];
private Image Ground,
Cheese,
Wall;
public Map(){
ImageIcon img = new ImageIcon("C://Project//Ground.png");
Ground = img.getImage();
img = new ImageIcon("C://Project//Wall.png");
Wall = img.getImage();
img = new ImageIcon("C://Project//Cheese.png");
Cheese = img.getImage();
openFile();
readFile();
closeFile();
}
public Image getGround(){
return Ground;
}
public Image getWall(){
return Wall;
}
public Image getCheese(){
return Cheese;
}
public String getMap(int x, int y){
String index = Map[y].substring(x, x+1);
return index;
}
public void openFile(){
try{
m = new Scanner(new File("C://Project//Map.txt"));
}catch(Exception e){
System.out.println("Error while loading the map");
}
}
public void readFile(){
while(m.hasNext()){
for(int i = 0; i < 16; i++){
Map[i] = m.next();
}
}
}
public void closeFile(){
m.close();
}
}
___________________________________
package maze;
import java.awt.Image;
import javax.swing.ImageIcon;
public class User {
private int tileX, tileY;
private Image user;
public User(){
ImageIcon img = new ImageIcon("C://Project//Jerry.png");
user = img.getImage();
tileX = 1;
tileY = 1;
}
public Image getUser(){
return user;
}
public int getTileX(){
return tileX;
}
public int getTileY(){
return tileY;
}
public void move(int dx, int dy){
tileX += dx;
tileY += dy;
}
}
Explanation / Answer
Answer
I tried my best
if(m.getMap(x, y).equals("C")){
g.drawImage(m.getCheese(), x * 32, y * 32, null);
}
if(m.getMap(x, y).equals("G")){
g.drawImage(m.getGround(), x * 32, y * 32, null);
}
if(m.getMap(x, y).equals("W")){
g.drawImage(m.getWall(), x * 32, y * 32, null);
}
change the above lines of code to
if(m.getMap(x, y).equals("C")){
g.drawImage(m.getCheese(), x * 64, y * 64, null);
}
if(m.getMap(x, y).equals("G")){
g.drawImage(m.getGround(), x * 64, y * 64, null);
}
if(m.getMap(x, y).equals("W")){
g.drawImage(m.getWall(), x * 64, y * 64, null);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.