Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lt.mchackers.gametest.main;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import lt.mchackers.gametest.handlers.InputHandler;
- /**
- * Main class for the game
- */
- public class Main extends JFrame
- {
- private static final long serialVersionUID = -828018325337767157L;
- boolean isRunning = true;
- int fps = 30;
- int windowWidth = 320;
- int windowHeight = 320;
- int speed;
- BufferedImage backBuffer;
- Insets insets;
- InputHandler input;
- int x = 0;
- int y = 0;
- int xa = 0;
- int ya = 0;
- Coordinates coords = new Coordinates(0, 0);
- public static void main(String[] args)
- { Main game = new Main();
- game.run();
- System.exit(0);
- }
- /**
- * This method starts the game and runs it in a loop
- */
- public void run()
- {
- initialize();
- while(isRunning)
- {
- long time = System.currentTimeMillis();
- update();
- draw();
- // delay for each frame - time it took for one frame
- time = (1000 / fps) - (System.currentTimeMillis() - time);
- if (time > 0)
- {
- try
- {
- Thread.sleep(time);
- }
- catch(Exception e){}
- }
- }
- setVisible(false);
- }
- /**
- * This method will set up everything need for the game to run
- */
- void initialize()
- {
- setTitle("Game Tutorial");
- setSize(windowWidth, windowHeight);
- setResizable(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- insets = getInsets();
- setSize(insets.left + windowWidth + insets.right,
- insets.top + windowHeight + insets.bottom);
- backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
- input = new InputHandler(this);
- Graphics bbg = backBuffer.getGraphics();
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader("map"));
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- String line = null;
- try {
- BufferedImage gray = ImageIO.read(new File("gray.png"));
- BufferedImage black = ImageIO.read(new File("black.png"));
- while ((line = reader.readLine()) != null) {
- for(String s : line.split(""))
- {
- if (s.contains("*"))
- {
- bbg.drawImage(gray, xa-32, ya, null);
- }
- else if (s.contains("#"))
- {
- bbg.drawImage(black, xa-32, ya, null);
- }
- if (xa < 320)
- {
- xa += 32;
- }
- else
- {
- ya += 32;
- xa = 0;
- }
- System.out.println(xa);
- System.out.println(ya);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * This method will check for input, move things
- * around and check for win conditions, etc
- */
- void update()
- {
- if (input.isKeyDown(KeyEvent.VK_NUMPAD0))
- {
- speed -= 1;
- }
- if (input.isKeyDown(KeyEvent.VK_NUMPAD1))
- {
- speed += 1;
- }
- if (input.isKeyDown(KeyEvent.VK_RIGHT))
- {
- coords.setCoords(coords.getX() + 32, coords.getY());
- }
- if (input.isKeyDown(KeyEvent.VK_LEFT))
- {
- coords.setCoords(coords.getX() - 32, coords.getY());
- }
- if (input.isKeyDown(KeyEvent.VK_UP))
- {
- coords.setCoords(coords.getX(), coords.getY() - 32);
- }
- if (input.isKeyDown(KeyEvent.VK_DOWN))
- {
- coords.setCoords(coords.getX(), coords.getY() + 32);
- }
- //System.out.println(x);
- //System.out.println(y);
- //System.out.println(speed);
- if (coords.getY() < 0)
- {
- coords.setCoords(coords.getX(), 0);
- }
- if (coords.getX() < 0)
- {
- coords.setCoords(0, coords.getY());
- }
- if (coords.getX() > windowWidth - 32)
- {
- coords.setCoords(windowWidth - 32, coords.getY());
- }
- if (coords.getY() > windowHeight - 32)
- {
- coords.setCoords(coords.getX(), windowHeight - 32);
- // y = windowHeight - 32;
- }
- }
- /**
- * This method will draw everything
- */
- void draw()
- {
- Graphics g = getGraphics();
- //this.setBackground(Color.BLACK);
- //super.paintComponents(g);
- backBuffer.setRGB(x, y, Color.BLACK.getRGB());
- Graphics bbg = backBuffer.getGraphics();
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader("map"));
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- String line = null;
- try {
- BufferedImage gray = ImageIO.read(new File("gray.png"));
- BufferedImage black = ImageIO.read(new File("black.png"));
- while ((line = reader.readLine()) != null) {
- for(String s : line.split(""))
- {
- if (s.contains("*") && xa + 32!= coords.getX() && ya - 32 != coords.getY())
- {
- bbg.drawImage(gray, xa-32, ya, null);
- }
- else if (s.contains("#") && xa + 32 != coords.getX() && ya - 32 != coords.getY())
- {
- bbg.drawImage(black, xa-32, ya, null);
- }
- if (xa < 320)
- {
- xa += 32;
- }
- else
- {
- ya += 32;
- xa = 0;
- }
- //System.out.println(xa);
- //System.out.println(ya);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- bbg.setColor(Color.WHITE);
- xa = 0;
- ya = 0;
- System.out.println(coords.getX());
- bbg.setColor(Color.WHITE);
- bbg.fillRect(coords.getX(),coords.getY(), 32,32);
- System.out.println(coords.getY());
- //bbg.setColor(Color.BLACK);
- //bbg.drawOval(x, y, 20, 20);
- g.drawImage(backBuffer, insets.left, insets.top, this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment