Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package com.eusurto.main;
  2.  
  3.  
  4.  
  5. import javax.swing.*;
  6.  
  7. import com.eusurto.entities.Entity;
  8.  
  9. import java.awt.*;
  10. import java.awt.image.BufferStrategy;
  11. import java.awt.image.BufferedImage;
  12. import java.util.ArrayList;
  13.  
  14. public class Game extends Canvas implements Runnable{
  15.  
  16. private static final long serialVersionUID = 1L;
  17. public static JFrame frame;
  18. private Thread thread;
  19. private BufferedImage image;
  20. private final int WIDTH = 160;
  21. private final int HEIGHT = 120;
  22. private final int SCALE = 3;
  23.  
  24. private boolean isRunning = true;
  25.  
  26. public List<Entity> entities;
  27. public Spritesheet spritesheet;
  28.  
  29. public Game(){
  30.  
  31. setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  32. initFrame();
  33. //inicializando objetos
  34. image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  35. entities = new ArrayList<Entity>();
  36. spritesheet = new Spritesheet("/spritesheet");
  37. }
  38.  
  39. public void initFrame(){
  40. frame = new JFrame("Game #1");
  41. frame.add(this);
  42. frame.setResizable(false);
  43. frame.pack();
  44. frame.setLocationRelativeTo(null);
  45. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. frame.setVisible(true);
  47. }
  48.  
  49. public synchronized void start(){
  50. thread = new Thread(this);
  51. isRunning = true;
  52. thread.start();
  53. }
  54.  
  55. public synchronized void stop(){
  56. isRunning = false;
  57. try{
  58. thread.join();
  59. }catch (InterruptedException e){
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static void main(String[] args) {
  65. Game game = new Game();
  66. game.start();
  67. }
  68.  
  69. public void tick(){
  70. for(int i = 0; i < entities.size(); i++) {
  71. Entity e = entities.get(i);
  72. e.tick();
  73. }
  74.  
  75. public void render(){
  76. BufferStrategy bs = this.getBufferStrategy();
  77. if(bs == null){
  78. this.createBufferStrategy(3);
  79. return;
  80. }
  81.  
  82. Graphics g = image.getGraphics();
  83. g.setColor(new Color(0, 0, 0));
  84. g.fillRect(0, 0, WIDTH, HEIGHT);
  85. for(int i = 0; i < entities.size(); i++) {
  86. Entity e = entities.get(i);
  87. e.render();
  88. g.dispose();
  89. g = bs.getDrawGraphics();
  90. g.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
  91. bs.show();
  92. }
  93.  
  94. public void run() {
  95. long lastTime = System.nanoTime();
  96. double amountOfTicks = 60.0;
  97. double ns = 1000000000 / amountOfTicks;
  98. double delta = 0;
  99. int frames = 0;
  100. double timer = System.currentTimeMillis();
  101. while(isRunning){
  102. long now = System.nanoTime();
  103. delta += (now - lastTime) / ns;
  104. lastTime = now;
  105. if(delta >= 1){
  106. tick();
  107. render();
  108. frames++;
  109. delta--;
  110. }
  111. if(System.currentTimeMillis() - timer >= 1000){
  112. System.out.println("FPR: " + frames);
  113. frames = 0;
  114. timer += 1000;
  115. }
  116. }
  117. stop();
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement