Reputation: 7
I have a Pong game with an FPS counter but I have a few problems. I want the FPS counter to NOT be limited at 60 FPS, I want the FPS counter to go as high as it can.
The FPS is currently working but the problem is that the game is running REALLY fast, I want the game to always run at a speed that is playable. I tried adding a timer for 5 ms but that caused the FPS counter to not work properly.
I want the FPS counter to NOT be limited at 60 FPS but at the same time use some sort of timer. Here is my code:
package Game;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Main extends JPanel implements KeyListener, ActionListener{
// Lite variabler
JFrame frame;
int windowWidth, windowHeight;
Point bollXY;
int screenWidth, screenHeight;
Image bildPaddel;
Image bildBollen;
int paddelY;
int paddel2Y;
boolean paddelUp, paddelDown;
Random rand;
int score, score2;
boolean bollUp, bollRight, changeDirection;
int fruktDistansRand;
long time;
int fps, newfps;
int fpsTimesCounter;
// Konstruktor
public Main(){
// Definera variabler
frame = new JFrame();
bildPaddel = new ImageIcon("src/images/Player.png").getImage();
bildBollen = new ImageIcon("src/images/Pong.png").getImage();
bollXY = new Point(673, 352);
paddelY = 312;
paddel2Y = 312;
paddelUp = false;
rand = new Random();
score = 0;
score2 = 0;
bollUp = false;
bollRight = false;
changeDirection = false;
fruktDistansRand = 0;
time = System.currentTimeMillis();
fps = 0;
newfps = 0;
fpsTimesCounter = 0;
// Lyssnare
frame.addKeyListener(this);
// Bygga fönstret
frame.add(this);
// Obligatoriska egenskaper
frame.setTitle("Pong");
frame.setSize(1366, 724); // Eftersom spelet inte ska vara helt i fullskärm sätts fönstret på 1366x724 så att aktivitetsfältet syns
frame.setVisible(true);
frame.setLocationRelativeTo(null); // Centrerar fönstret
frame.setResizable(false);
this.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bollUp = true;
bollRight = true;
changeDirection = false;
bollXY = new Point();
frame.add(this);
}
// Metoder
public static void main(String[] args) {
new Main();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(bildBollen, bollXY.x, bollXY.y, null);
g.drawImage(bildPaddel, 50, paddelY, null);
g.drawImage(bildPaddel, 1300, paddel2Y, null);
g.setFont(new Font("Arial", Font.BOLD, 100));
g.setColor(Color.WHITE);
g.drawString(score2 + "", 1002, 100);
g.drawString(score + "", 314, 100);
if(System.currentTimeMillis() - time >= 1000){
time = System.currentTimeMillis();
fps = newfps;
newfps = 0;
}
else{
newfps++;
}
g.setFont(new Font("Arial", Font.BOLD, 25));
g.drawString(fps + "", 5, 22);
update();
}
public void AI(){
fruktDistansRand = rand.nextInt(200) + 950;
if(bollXY.x > fruktDistansRand && bollXY.x < 1380 && bollRight && paddel2Y > 0 && paddel2Y < 596){
if(paddel2Y + 50 < bollXY.y){
paddel2Y = paddel2Y + 3;
}
else{
paddel2Y = paddel2Y - 3;
}
if(paddel2Y <= 0){
paddel2Y = paddel2Y + 3;
}
if(paddel2Y >= 596){
paddel2Y = paddel2Y - 3;
}
}
}
public void ifUp(){
if(bollUp){
if(changeDirection){
if(bollXY.y < 0){
bollUp = false;
}
else{
bollXY.y = bollXY.y - 3;
}
}
else{
if(bollXY.y < 0){
bollUp = false;
}
else{
bollXY.y = bollXY.y - 3;
}
}
}
else{
if(changeDirection){
if(bollXY.y > 675){
bollUp = true;
}
else{
bollXY.y = bollXY.y + 3;
}
}
else{
if(bollXY.y > 675){
bollUp = true;
}
else{
bollXY.y = bollXY.y + 3;
}
}
}
}
public void update(){
if(paddelUp){
if(paddelY > 0){
paddelY = paddelY - 3;
}
}
if(paddelDown){
if(paddelY < 596){
paddelY = paddelY + 3;
}
}
if(bollRight){
if(bollXY.x > 1290 && bollXY.x < 1300 && bollXY.y < paddel2Y + 100 && bollXY.y > paddel2Y-20){
if(!bollUp && bollXY.y < paddel2Y){
changeDirection = true;
bollUp = true;
}
else if(bollUp && bollXY.y > paddel2Y + 80){
changeDirection = true;
bollUp = false;
}
bollRight = false;
}
else if(bollXY.x > 1600){
score++;
bollXY.y = rand.nextInt(690);
bollXY.x = 678;
}
else
bollXY.x = bollXY.x + 3;
ifUp();
}
else{
if(bollXY.x > 50 && bollXY.x < 60 &&bollXY.y < paddelY + 100 && bollXY.y > paddelY-20){
if(!bollUp && bollXY.y < paddelY){
changeDirection = true;
bollUp = true;
}
else if(bollUp && bollXY.y > paddelY + 80){
changeDirection = true;
bollUp = false;
}
bollRight = true;
}
else if(bollXY.x < -244){
score2++;
bollXY.x = 678;
bollXY.y = rand.nextInt(596);
}
else
bollXY.x = bollXY.x - 3;
ifUp();
}
AI();
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
update();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP){
paddelUp = true;
}
if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN){
paddelDown = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP){
paddelUp = false;
}
if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN){
paddelDown = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
}
Upvotes: 0
Views: 463
Reputation: 34
Your game is running really fast, because you use a static increment in each cpu cycle. If you run this program on an slow cpu, your game will run slower. If you run it with a fast cpu your game will run faster.
cpu 1: 100 cycles per second: your ball will move 300 units per second.
cpu 2: 2000 cycles per second: your ball will move 6000 units per second.
When you capped your game at 60 fps, you avoided that issue, because the update will only happen 60 times per second and not 100 or 2000 times.
Another option is to involve time. This means you measure the time since the last frame and use it as an multiplicator. The faster your cpu runs the lower your time between frames will be. Your ball will move acording to you time. If your your cpu is slower the time between your frames increases and the ball will move further.
float delta = // time between your frames
pulic void update(float delta){
...
bollXY.y -= delta * 3;
Alternatively a great article for game loops: https://gafferongames.com/post/fix_your_timestep/
Upvotes: 1