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

Android Shared Preferences
Java Zip File System Provider: ReadOnly on remote drive [Windows]
How to write annotation-based servlet mapping in Spring?
Writing and Reading Integer/String Array Values to File (Android Application Development)
Spring framework upgrde from 3.2.2 -> 4.0.6, BeanCreationException … RequestMappingHandlerMapping
JAXB issue with missing namespace definition
Google App Engine Integer Array Parameter
Cannot add or update a child row on the sql
Casting from Object to Enum
no package identifier when getting value for resource number 0x00000000
Neo4j - OverlappingFileLockException
Java isn't getting the file in the source code when compiled
AWS EC2 Interminent UnknownHostException when trying to connect to RDS
what is hbase compaction file size
NoClassDefFoundError R$string on Android Studio
Is there any way to get a bean with prototype scope but not making a new bean every time you get it? [closed]

Categories

HOME
vim
testng
plone
angular-material
plot
sd-card
getelementsbytagname
spring-cloud-stream
jpeg
survey
ebean
ezpublish
networkx
mouse
in-app-purchase
python-unittest
tomcat6
fancybox-3
php-7.1
usergrid
hapi
carthage
jsprit
after-effects
highlight.js
facebook-page
apache-metamodel
predix
nas
css-animations
realex-payments-api
icloud-api
dxf
chromebook
intel-pin
rst2pdf
fopen
fifo
spring-mybatis
strncpy
log4js-node
wtx
atlassian-crucible
newline
r-raster
bluestacks
directx-10
bcd
calibre
jmonkeyengine
adobe-premiere
libraries
hilbert-curve
awt
g-code
reportingservices-2005
glew
thin
angular2-meteor
apple-news
pango
jquery-nestable
theming
typescript1.8
fancybox-2
slickedit
pillow
webdriverjs
measures
ctest
clang-static-analyzer
testng-dataprovider
fadeout
yaws
pagerank
ipconfig
freedesktop.org
kendonumerictextbox
comobject
computer-algebra-systems
graph-drawing
sankey-diagram
graph-api-explorer
facebook-graph-api-v2.4
dia
google-style-guide
xojo
android-radiobutton
article
contenttype
dealloc
stxxl
mysqltuner
ms-project-server-2010
google-cloud-save
ruby-datamapper
dbconnection
dataservice
dataadapter
padarn
plasma
type-equivalence
android-hardware
angularjs-controller
pvrtc
rdoc
propertyeditor
cufon
netdna-api
zpt
infobox
lpeg
nsviewanimation
photoshop-cs4
eqatec
pascal-fc
mediarss

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App