I’m learning Java, now I’ve just been trying to write such a simple 2D game for the sake of interest: control the red circle within the window with the mouse, dodging black triangles flying endlessly on top. Stuck at the stage of adding triangles: fps drops to 5-3, when the triangles are about 20-25 (there is a screen). Since there are an infinite number of triangles, I stuff them, their coordinates in four ArrayList (it took exactly 4), add 1 every second. Question: why lags and how to increase the amount of memory allocated for the program so that the triangles with normal fps reach at least the bottom edge? What can be done for optimization in this case?

Before the appearance of triangles (97-100fps):

After (10 fps):

This is what a piece of code responsible for fps looks like:

 public void run() { gc.setActionTime(true); long last1 = System.currentTimeMillis(),last2 = System.currentTimeMillis(),lastaction = System.currentTimeMillis(); while(r){ long delta1 = System.currentTimeMillis()-last1; long delta2 = System.currentTimeMillis()-last2; long deltaaction = System.currentTimeMillis()-lastaction; if(delta1>=1000D/100D){ last1 = System.currentTimeMillis(); gc.render(); gc.tick(); fps++; } if(delta2>=1000){ f.setTitle(fps+"fps"); fps=0; last2=System.currentTimeMillis(); } if(deltaaction>=1000){ lastaction = System.currentTimeMillis(); gc.setActionTime(true); } } } 

This is what the main code looks like (not completely):

 public class GameContent { Canvas c; JOptionPane j; int W, H; int ticks = 0; ArrayList < Obstacle > o = new ArrayList < Obstacle > (1); ArrayList < Integer > o_x = new ArrayList < Integer > (1); ArrayList < Integer > o_y = new ArrayList < Integer > (1); ArrayList < Integer > yOffsets = new ArrayList < Integer > (1); Random r; Graphics2D g; boolean clicked = false, actiontime = false, started = false; Player p; int x, y, x_o; public GameContent(Canvas c) { this.c = c; x = c.getWidth() / 2 - 25; y = c.getHeight() - 70; W = c.getWidth(); H = c.getHeight(); p = new Player(); r = new Random(); c.addMouseMotionListener(new MouseMotionListener() { int x1 = x; int y1 = y; public void mouseDragged(MouseEvent e) { if (e.getSource() == e.getComponent()) if (!clicked) { clicked = true; started = true; } { x = e.getX() - 10; y = e.getY() - 10; } } public void mouseMoved(MouseEvent e) { } }); } public synchronized void render() { BufferStrategy bs = c.getBufferStrategy(); if (bs == null) { c.createBufferStrategy(3); return; } g = (Graphics2D) bs.getDrawGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, W, H); g.drawImage(p.getImage(), x, y, null); if (clicked) { for (int i = 0; i < o.size(); i++) { g.drawImage(o.get(i).getImage(), o_x.get(i), o_y.get(i) + yOffsets.get(i), null); } } if (actiontime) { actiontime = false; o.add(new Obstacle()); o_x.add(r.nextInt(c.getWidth())); o_y.add(10); yOffsets.add(0); } g.dispose(); bs.show(); } 
  • Olezhka, unfortunately, all regular psychics of the Stack Overflow in Russian are now on vacation. To help you mere mortals, add your code to the question. - fori1ton
  • Added what is significant - Olezhka
  • Show the implementation of the Obstacle class - perhaps, it uses Image instead of BufferedImage or some calculations are performed in the getImage method. A couple more tips: 1) you do not need to store the coordinates in a separate array, you can put them in instances of the Obstacle class 2) in the run () method you do not need to constantly call System.getCurrentMillis (), it’s better to save time in a variable, then it’s enough to get time once before the cycle and once inside the cycle. - user2228947

0