Yeah here's my main setup of repainting and updating:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| public void run() { long lastLoopTime = System.nanoTime(); final int TARGET_FPS = 100; final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
while (inRealGame) { updateParticles(); if (inGame) { long now = System.nanoTime(); long updateLength = now - lastLoopTime; lastLoopTime = now; delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength; fps++;
gamePhysic(delta);
if (lastFpsTime >= 1000000000) { realFPS = fps; lastFpsTime = 0; fps = 0; }
try { Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000); } catch (Exception e) { e.printStackTrace(); } } repaint(); } }
public void updateParticles() { for (int i = 0; i <= particles.size() - 1; i++) { Particle part = particles.get(i); if (part != null) { if (part.update()) { particles.remove(i); } if (part != null) {
if (part.getLoc().x <= 0) { part.getLoc().x = 0; part.getVel().x *= -.9; } if (part.getLoc().x >= 700) { part.getLoc().x = 700; part.getVel().x *= -.9; } if (part.getLoc().y <= 0) { part.getLoc().y = 0; part.getVel().y *= -.9; } if (part.getLoc().y >= 550) { part.getLoc().y = 550; part.getVel().y *= -.9; } } } } for (int i = 0; i <= overParticles.size() - 1; i++) { Particle part = overParticles.get(i); if (part != null) { if (part.update()) { overParticles.remove(i); } if (part != null) {
if (part.getLoc().x <= 0) { part.getLoc().x = 0; part.getVel().x *= -.9; } if (part.getLoc().x >= 700) { part.getLoc().x = 700; part.getVel().x *= -.9; } if (part.getLoc().y <= 0) { part.getLoc().y = 0; part.getVel().y *= -.9; } if (part.getLoc().y >= 550) { part.getLoc().y = 550; part.getVel().y *= -.9; } } } } }
public void gamePhysic(double delta) { if (!isFirst) { room.physic(); mobSpawner(); for (int i = 0; i < currLevel.mobs.length; i++) { if (currLevel.mobs[i].inGame) { currLevel.mobs[i].physic(); } } if (currLevel.levelOver) { getNextLevel(currLevel); } } } |
and for drawing things, I use the generic g.drawImage/fillOval(for some particles) and such. So you think I should have a variable that by default is one and when I maximize it set it to 3, 4, 5, 6 or whatever works and say g.drawImage(img, x, y, width * Screen.screenSizeMultiplier, height * Screen.screenSizeMultiplier, null);?