Advertisement
Guest User

Untitled

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