Hey Everyone
So I am basically creating a simple 2D game which will consist of a maze/level. I need to print out multiple images without having to print out the same line hundreds of times within different 2D coordinates. The only problem is I have no idea what else i need in my program to do this. I want to print an image wherever I have a '#' in my maze class. Something like this:
private String level = "####################################\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"####################################\n";
So Wherever there is a blank space, nothing is printed. '\n' starts a new line of the maze and the '#' prints out my cobblestone image.
I will give you the code to my current executable program:
package outrun; import java.awt.Image; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; public class Person { private String person = "/images/person.png"; private int dx; private int dy; private int x; private int y; private Image image; public Person() { ImageIcon i = new ImageIcon(this.getClass().getResource(person)); image = i.getImage(); x = 10; y = 10; } public void move() { x += dx; y += dy; } public int getX() { return x; } public int getY() { return y; } public Image getImage() { return image; } public void KeyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT) { dx = 1; } if (key == KeyEvent.VK_LEFT) { dx = -1; } if (key == KeyEvent.VK_UP) { dy = -1; } if (key == KeyEvent.VK_DOWN) { dy = 1; } } public void KeyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT) { dx = 0; } if (key == KeyEvent.VK_LEFT) { dx = 0; } if (key == KeyEvent.VK_UP) { dy = 0; } if (key == KeyEvent.VK_DOWN) { dy = 0; } } }
That is the person class.
package outrun; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JPanel; import javax.swing.Timer; public class Board extends JPanel implements ActionListener { private String maze = "####################################\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"# #\n" +"####################################\n"; private Timer timer; private Person person; public Board() { addKeyListener(new TAdapter()); setFocusable(true); setBackground(Color.BLACK); setDoubleBuffered(true); setSize(600, 400); person = new Person(); timer = new Timer(5, this); timer.start(); } public void paint(Graphics g) { super.paint(g); Graphics2D m = (Graphics2D)g; m.drawImage(person.getImage(), person.getX(), person.getY(), this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void actionPerformed(ActionEvent e) { person.move(); repaint(); } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { person.KeyReleased(e); } public void keyPressed(KeyEvent e) { person.KeyPressed(e); } } }
That is the Board Class.
package outrun; import javax.swing.JFrame; public class Outrun extends JFrame { public Outrun() { add(new Board()); setTitle("Outrun"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(600, 400); setResizable(false); setVisible(true); setLocationRelativeTo(null); } public static void main(String[] args) { new Outrun(); } }
That is the Outrun Class (Outrun being the name of the game).
This is the Cobblestone Image I will be using:
Cobblestone.png
Any help given would be appreciated. Thanks!