java


Pause Button in Java Game


I've been working on my first "game" in Java, Pong, and I feel as if I'm ready to implement a pause button. With my current code, pressing escape (which is meant to make state = State.PAUSE, pausing the game) makes the screen flicker, and occasionally pause. The game has been running well until so far, so it'd be preferable if I could change my code as little as possible, if possible. My current code:
gameloop in main() running ~60 fps:
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
while (true) {
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
timer += now - lastTime;
lastTime = now;
if(delta >= 1){
//refreshes Keyboard
p.key.nUpdate();
//refreshes game
p.nUpdate();
//refreshes graphics
p.nPaint();
ticks++;
delta--;
}
if(timer >= 1000000000){
log();
ticks = 0;
timer = 0;
}
}
in Keyboard, I assign all needed values to booleans:
public class Keyboard extends KeyAdapter implements KeyListener {
private boolean[] keys;
public boolean escape;
public boolean r;
public Keyboard(){
keys = new boolean[256];
}
public void nUpdate(){
escape = keys[KeyEvent.VK_ESCAPE];
r = keys[KeyEvent.VK_R];
}
#Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
#Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
my game window, update methods:
public void nUpdate() {
if (key.r) newMatch();
if (key.escape) pause();
if (state == State.GAME) {...}
}
public void paint(Graphics g) {
switch (state) {
case GAME: ...
case PAUSE: ...
}
public void nPaint() {
repaint();
}
public void pause() {
if (state == State.GAME) state = State.PAUSE;
else if (state == State.PAUSE) state = State.GAME;
}
I'm wondering if there is a simpler or cleaner way to pause the game, or at least stop it from flickering during pause (or being paused/unpaused repeatedly). I also apologize in advance if I have too much/not enough/unclean code, absolutely any help is appreciated

Related Links

How to include two different versions of the same dependency?
How do I gradle run without any dependency checking?
Java Restlet framework: long going requests return after 60s
Null pointer in middle of nowhere
How to separate string in text file into different array (java)
How to Apply Insert key action for all opened Documents
Integrate Google input tools in java program
How to use NanoHTTPD to show log file
Hibernate session and concurrency
Intellij IDEA generate update method from another object instance. (update RealmObject)
Alternatives to JSON-object binding in Android application
Sound Effects causing Memory Leaks
NPE on jpa findAll with specification/predicate
NETWORKDAYS.INTL in Java
Passing random numbers into an instance of an object
Junit test that will fail if singleton getInstance() method is not synchronized

Categories

HOME
hive
wso2
cookies
pycharm
iot
cplex
amazon-cloudformation
applepay
dacpac
tomcat6
correlation
apache-cayenne
jplayer
hex-editors
serilog
solaris-10
flux
nas
ml
object-detection
jasonette
pass-by-reference
trading
java-7
javacv
cas
intel-pin
nhapi
opentype
gsoap
key-value-observing
oracle-fusion-middleware
usbserial
commit
android-nestedscrollview
overlap
plsql-psp
dosbox
retina-display
multilingual
withings
android-browser
vapor
reportbuilder
elasticsearch-plugin
wptoolkit
wso2carbon
fault
pango
qsslsocket
integrity
aurelia-validation
linode
hittest
django-scheduler
jquery-filter
savon
sts-springsourcetoolsuite
azure-virtual-network
database-optimization
adxstudio-portals
phishing
revolution-r
system.management
netmq
kendonumerictextbox
marching-cubes
jqgrid-formatter
security-testing
nsight
system32
codeigniter-url
rdtsc
picturefill
centos5
intentservice
uv-mapping
gridfs
listings
dotnetnuke-5
batterylevel
gwt-rpc
hungarian-algorithm
drools-planner
tridion-worldserver
netbeans-6.9
spring-portlet-mvc
pendrive
blackberry-playbook
oncheckedchanged
modelstate
virtual-functions
msdev
geneva-server
.net-1.0

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App