Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lt.mchackers.anothercargame.main;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import javax.swing.JPanel;
- import lt.mchackers.anothercargame.textures.Black;
- import lt.mchackers.anothercargame.textures.Gray;
- public class GamePanel extends JPanel implements Runnable
- {
- private static final long serialVersionUID = 6892533030374996243L;
- public static final int WIDTH = 320;
- public static final int HEIGHT = 320;
- int xa = 0;
- int x;
- int ya = 0;
- int y;
- private Thread animator;
- private Thread timeThread;
- BufferedImage backBuffer;
- private volatile boolean running = false;
- private volatile boolean isGameOver = false;
- private volatile boolean isUserPaused = false;
- private volatile boolean isWindowPaused = false;
- public static Map<Coordinates, String> hm = new LinkedHashMap<Coordinates, String>();
- public BufferedImage gray;
- public BufferedImage black;
- private Graphics dbg;
- private Image dbImage = null;
- public int timeSpentInSeconds;
- private static final int NO_DELAYS_PER_YIELD = 16;
- private static final int MAX_FRAME_SKIPS = 5;
- private static final Color backgroundColor = Color.BLACK;
- private static long fps = 30;
- private static long period = 1000000L * (long) 1000.0 / fps;
- private static volatile boolean isPainted = false;
- public GamePanel()
- {
- setBackground(backgroundColor);
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
- setFocusable(true);
- requestFocus();
- readyForPause();
- addKeyListener(new KeyAdapter()
- {
- public void keyPressed(KeyEvent e)
- {
- try{
- System.out.println(hm.keySet());
- int keyCode = e.getKeyCode();
- if ((keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A)
- && isPainted && hm.get(new Coordinates(x - 32, y)).equalsIgnoreCase("gray"))
- {
- x -= 32;
- }
- if ((keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D)
- && isPainted && hm.get(new Coordinates(x + 32, y)).equalsIgnoreCase("gray"))
- {
- x += 32;
- }
- if ((keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W)
- && isPainted && hm.get(new Coordinates(x, y - 32)).equalsIgnoreCase("gray"))
- {
- y -= 32;
- }
- if ((keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S)
- && isPainted && hm.get(new Coordinates(x, y + 32)).equalsIgnoreCase("gray"))
- {
- y += 32;
- }
- if (x > WIDTH - 32 && isPainted)
- {
- x = WIDTH - 32;
- }
- if (y > HEIGHT - 32 && isPainted)
- {
- y = HEIGHT - 32;
- }
- if (y < 0 && isPainted)
- {
- y = 0;
- }
- if (x < 0 && isPainted)
- {
- x = 0;
- }
- isPainted = false;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- });
- }
- public void addNotify()
- {
- super.addNotify();
- startGame();
- }
- void startGame()
- {
- if (animator == null || !running)
- {
- animator = new Thread(this);
- animator.start();
- timeThread = new Thread(new TimeThread());
- timeThread.start();
- }
- }
- void stopGame()
- {
- running = false;
- }
- private void readyForPause()
- {
- addKeyListener(new KeyAdapter()
- {
- public void keyPressed(KeyEvent e)
- {
- int keyCode = e.getKeyCode();
- if ((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q)
- || (keyCode == KeyEvent.VK_END) || (keyCode == KeyEvent.VK_P)
- || ((keyCode == KeyEvent.VK_C) && e.isControlDown()))
- {
- if (!isUserPaused)
- setUserPaused(true);
- else
- setUserPaused(false);
- }
- }
- });
- }
- public void run()
- {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader("map"));
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
- for(String s : line.split(""))
- {
- if (s.contains("*"))
- {
- hm.put(new Coordinates(xa-32, ya), "gray");
- }
- else if (s.contains("#"))
- {
- hm.put(new Coordinates(xa-32, ya), "black");
- }
- if (xa < 320)
- {
- xa += 32;
- }
- else
- {
- ya += 32;
- xa = 0;
- }
- //System.out.println(xa);
- //System.out.println(ya);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- xa = 0;
- ya = 0;
- long beforeTime, afterTime, timeDiff, sleepTime;
- long overSleepTime = 0L;
- int noDelays = 0;
- long excess = 0L;
- beforeTime = System.nanoTime();
- running = true;
- for (Map.Entry<Coordinates, String> entry : hm.entrySet()) {
- System.out.println(entry.getValue().equalsIgnoreCase("gray"));
- if (entry.getValue().equalsIgnoreCase("gray"))
- {
- createImage(WIDTH, HEIGHT).getGraphics().setColor(Color.WHITE);
- createImage(WIDTH, HEIGHT).getGraphics().drawRect(entry.getKey().getX(), entry.getKey().getY(), 32, 32);
- x = entry.getKey().getX();
- y = entry.getKey().getY();
- break;
- }
- }
- System.out.println(x);
- System.out.println(y);
- while (running)
- {
- requestFocus();
- gameUpdate();
- gameRender();
- paintScreen();
- afterTime = System.nanoTime();
- timeDiff = afterTime - beforeTime;
- sleepTime = (period - timeDiff) - overSleepTime;
- if (sleepTime > 0)
- {
- try
- {
- Thread.sleep(sleepTime / 1000000L);
- }
- catch (InterruptedException e)
- {
- }
- overSleepTime = (System.nanoTime() - afterTime - sleepTime);
- }
- else
- {
- excess -= sleepTime;
- overSleepTime = 0L;
- if (++noDelays >= NO_DELAYS_PER_YIELD)
- {
- Thread.yield();
- noDelays = 0;
- }
- }
- beforeTime = System.nanoTime();
- int skips = 0;
- while ((excess > period) && (skips < MAX_FRAME_SKIPS))
- {
- excess -= period;
- gameUpdate();
- skips++;
- }
- isPainted = true;
- }
- System.exit(0);
- }
- private void gameUpdate()
- {
- if (!isUserPaused && !isWindowPaused && !isGameOver)
- {
- }
- }
- private void gameRender()
- {
- if (dbImage == null)
- {
- dbImage = createImage(WIDTH, HEIGHT);
- if (dbImage == null)
- {
- System.out.println("Image is null.");
- return;
- }
- else
- dbg = dbImage.getGraphics();
- }
- dbg.setColor(backgroundColor);
- dbg.fillRect(0, 0, WIDTH, HEIGHT);
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader("map"));
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
- for(String s : line.split(""))
- {
- if (s.contains("*"))
- {
- dbg.drawImage(Gray.getGray(), xa-32, ya, null);
- }
- else if (s.contains("#"))
- {
- dbg.drawImage(Black.getBlack(), 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();
- }
- xa = 0;
- ya = 0;
- dbg.setColor(Color.WHITE);
- dbg.fillRect(x,y, 32,32);
- if (isGameOver)
- gameOverMessage(dbg);
- }
- private void gameOverMessage(Graphics g)
- {
- g.setColor(new Color(33, 33, 33));
- g.setFont(new Font("Comic Sans MS", Font.BOLD, 45));
- g.drawString("Game Over!", 265, 250);
- g.setColor(new Color(0, 64, 0));
- g.setFont(new Font("Comic Sans MS", Font.BOLD, 40));
- }
- private void paintScreen()
- {
- Graphics g;
- try
- {
- g = this.getGraphics();
- if ((g != null) && (dbImage != null))
- g.drawImage(dbImage, 0, 0, null);
- Toolkit.getDefaultToolkit().sync();
- g.dispose();
- }
- catch (Exception e)
- {
- System.out.println("Graphics context error : " + e);
- }
- }
- public void setWindowPaused(boolean isPaused)
- {
- isWindowPaused = isPaused;
- }
- public void setUserPaused(boolean isPaused)
- {
- isUserPaused = isPaused;
- }
- public class TimeThread implements Runnable
- {
- public void run()
- {
- while (!isUserPaused && !isWindowPaused && !isGameOver)
- {
- timeSpentInSeconds += 1;
- try
- {
- Thread.sleep(1000);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement