Java-如何停止无限时间
我是Java新手,正在Android Studio上制作俄罗斯方块游戏。我试着做一个“暂停/开始”按钮,但它不起作用。当我按下“暂停”按钮时,音乐和游戏停止(它正常工作)。但当我再次按下“开始”时,音乐会继续播放,但游戏不会继续 我在Java-如何停止无限时间,java,android,while-loop,Java,Android,While Loop,我是Java新手,正在Android Studio上制作俄罗斯方块游戏。我试着做一个“暂停/开始”按钮,但它不起作用。当我按下“暂停”按钮时,音乐和游戏停止(它正常工作)。但当我再次按下“开始”时,音乐会继续播放,但游戏不会继续 我在while中创建了一个函数“pause”,其中包含sleep(),函数“restart”中的布尔值变为false(这将设置while的条件) 这是我的密码: private void pause() { ps=true; while(ps) {
while
中创建了一个函数“pause”,其中包含sleep()
,函数“restart”中的布尔值变为false(这将设置while的条件)
这是我的密码:
private void pause() {
ps=true;
while(ps) {
try {
sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void restart() {
ps=false;
}
这是我的main活动的一部分
:
public void pauseButton (View view) {
if (ps == false) {
ps=true;
GameState.getInstance().setUserAction(UserAction.PAUSE);
mediaPlayer.pause();
} else {
ps=false;
GameState.getInstance().setUserAction(UserAction.START);
mediaPlayer.start();
}
}
下面是“Useraction.Pause/Start”的作用
如何使功能重新启动工作?我需要在该功能中停止“while”或可能停止睡眠,但我无法。。谢谢你的帮助
public class GameThread extends Thread{
private Shape shape;
private Set<Block> remainingBlockSet = new HashSet<>();
private boolean isRunning = true;
private long time;
private char[] shapeTypes={'O', 'I', 'J', 'L', 'Z', 'S','T'};
private boolean ps = true;
GameThread(){
//La première pièce du jeu apparait
spawnNewShape();
//Initialisation du temps
this.time = System.currentTimeMillis();
}
//----------------------------------------------------------------------------------------------
// METHODES DE GESTION DE LA THREAD
//----------------------------------------------------------------------------------------------
/**
*Boucle événementielle
*/
@Override
public void run() {
while (isRunning) {
//On bouge la pièce en fonction des demande utilisateur (bouton préssé)
processUserAction();
//La pièce tombe
processMoveDown();
//On enlève les lignes remplie
removeCompletedRows();
//On effectue le rendu graphique
RenderManager.getInstance().render(shape, remainingBlockSet);
}
}
/**
* Arret de la boucle événementielle
*/
void stopRunning(){
isRunning = false;
}
//----------------------------------------------------------------------------------------------
// METHODES DE GESTION DU JEU
//----------------------------------------------------------------------------------------------
/**
* Traitement des actions utilisateur
*/
private void processUserAction() {
//On recupère la dernière action
UserAction action = GameState.getInstance().getUserAction();
switch (action) {
case LEFT:
moveLeft();
break;
case RIGHT:
moveRight();
break;
case ROTATE:
rotate();
break;
case FALL:
fall();
break;
case PAUSE:
pause();
break;
case RESUME:
restart();
break;
}
//On définit la dernière action à nulle
GameState.getInstance().setUserAction(UserAction.NONE);
}
// met en pause le jeu
public void pause() {
ps = true;
while(ps) {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// permet de relancer le jeu
public void restart() {
ps=false;
}
private void fall(){
while(shape.canMoveDown(remainingBlockSet)){
shape.moveDown();
}
}
private void rotate(){
shape.rotate(remainingBlockSet);
}
/**
* Décale la pièce d'une case vers la gauche
*/
private void moveLeft(){
shape.moveLeft(remainingBlockSet);
}
/**
* Décale la pièce d'une case vers la droite
*/
private void moveRight(){
shape.moveRight(remainingBlockSet);
}
/**
* Décale la pièce d'une case vers le bas
*/
private void processMoveDown(){
//Si le laps de temps n'est pas écoulé on ne fait rien
long now = System.currentTimeMillis();
if(now-time < GameConstants.TIME_LAPS){
return;
}
//Sinon la pièce descend
if(shape.canMoveDown(remainingBlockSet)){
shape.moveDown();
}
else{
processShapeCollision();
}
//On redéfinit le temps comme le temps actuel
time = System.currentTimeMillis();
}
private void processShapeCollision(){
remainingBlockSet.addAll(shape.getBlockList());
spawnNewShape();
}
/**
* A partir d'un caractère choisis aléatoirement, cette methode
permet
de générer la pièce qui
* lui est associé.
*/
private void spawnNewShape(){
int x = (GameConstants.COLUMNS/2)-2;
int y = 0;
char shapeType = chooseRandomShape();
switch (shapeType){
case 'O':
this.shape = new OShape(x,y);
break;
case 'T':
this.shape = new TShape(x,y);
break;
case 'S':
this.shape = new SShape(x,y);
break;
case 'I':
this.shape = new IShape(x,y);
break;
case 'L':
this.shape = new LShape(x,y);
break;
case 'Z':
this.shape = new ZShape(x,y);
break;
case 'J':
this.shape = new JShape(x,y);
break;
default:
//imposible
break;
}
}
/**
* Cette methode génère aléatoirement un entier entre 0 et 7. Ce dernier permet ensuite de
* séléctioner le caractère de la pièce à faire appraitre.
* @return un caractère correspondant à la prochaine pièce à faire apparaitre.
*/
private char chooseRandomShape(){
int x = (int) (Math.random() * shapeTypes.length);
return shapeTypes[x];
}
private void removeCompletedRows() {
for(int y = GameConstants.ROWS-1; y>0; y--){
List<Block> line = getBlockLine(y);
if (isLineCompleted(line)){
//Log.i(TAG,"la ligne"+y+"est pleine :"+line);
removeLine(line);
moveDownAboveBlocks(y);
}
}
}
private List<Block> getBlockLine(int y) {
List<Block> line = new ArrayList<>();
for(Block block : remainingBlockSet){
if(y == block.getY()){
line.add(block);
}
}
return line;
}
private boolean isLineCompleted(List<Block> line) {
for(int x = 0; x< GameConstants.COLUMNS; x++){
boolean found = false;
for(Block block : line){
if (block.getX() == x){
found = true;
break;
}
}
if(!found) {
return false;
}
}
return true;
}
private void removeLine(List<Block> line){
remainingBlockSet.removeAll(line);
}
private void moveDownAboveBlocks(int y){
for(Block block : remainingBlockSet){
if (block.getY()<y){
block.moveDown();
}
}
}
公共类GameThread扩展线程{
私人造型;
私有集remainingBlockSet=new HashSet();
私有布尔值isRunning=true;
私人时间长;
private char[]shapeTypes={'O','I','J','L','Z','S','T'};
私有布尔值ps=true;
游戏线程(){
//拉普莱米耶尔皮耶斯杜丘公寓
新形状();
//临时初始化
this.time=System.currentTimeMillis();
}
//----------------------------------------------------------------------------------------------
//线消化法
//----------------------------------------------------------------------------------------------
/**
*布克莱维内门蒂尔酒店
*/
@凌驾
公开募捐{
同时(正在运行){
//关于利用需求基金(bouton préssé)
processUserAction();
//拉皮耶斯汤贝酒店
processMoveDown();
//论《木纹年鉴》
removeCompletedRows();
//论人都图形的效果
RenderManager.getInstance().render(形状,remainingBlockSet);
}
}
/**
*阿雷特·德拉布克莱维内门蒂尔酒店
*/
void stopRunning()的名称{
isRunning=false;
}
//----------------------------------------------------------------------------------------------
//消化法
//----------------------------------------------------------------------------------------------
/**
*利用行动的背叛
*/
私有void processUserAction(){
//论德尔尼埃行动
UserAction action=GameState.getInstance().getUserAction();
开关(动作){
案例左:
左移();
打破
案例权利:
moveRight();
打破
案例轮换:
旋转();
打破
个案数字:
fall();
打破
案例暂停:
暂停();
打破
案件简历:
重启();
打破
}
//关于无效行为的有限性
GameState.getInstance().setUserAction(UserAction.NONE);
}
//我和勒丘见过面
公共空间暂停(){
ps=真;
while(ps){
试一试{
睡眠(5000);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
//勒琼酒店
公共无效重新启动(){
ps=假;
}
私人空降{
while(shape.canMoveDown(remainingBlockSet)){
shape.moveDown();
}
}
私有空间旋转(){
形状。旋转(保留块集);
}
/**
*这是一个很好的例子
*/
私有void moveLeft(){
shape.moveLeft(剩余块集);
}
/**
*这是一个很好的例子
*/
私权{
shape.moveRight(保留块集);
}
/**
*这是一个很好的例子
*/
私有void processMoveDown(){
//这是一个新的信仰
long now=System.currentTimeMillis();
如果(现在时间0;y--){
列表行=getBlockLine(y);
如果(isLineCompleted(行)){
//日志i(标签“la ligne”+y+“est pleine:”+行);
拆线;
向下移动块(y);
}
}
}
私有列表getBlockLine(整数y){
列表行=新的ArrayList();
for(块:remainingBlockSet){
如果(y==block.getY()){
行。添加(块);
}
}
回流线;
}
私有布尔是
public class GameThread extends Thread{
private Shape shape;
private Set<Block> remainingBlockSet = new HashSet<>();
private boolean isRunning = true;
private long time;
private char[] shapeTypes={'O', 'I', 'J', 'L', 'Z', 'S','T'};
private boolean ps = true;
GameThread(){
//La première pièce du jeu apparait
spawnNewShape();
//Initialisation du temps
this.time = System.currentTimeMillis();
}
//----------------------------------------------------------------------------------------------
// METHODES DE GESTION DE LA THREAD
//----------------------------------------------------------------------------------------------
/**
*Boucle événementielle
*/
@Override
public void run() {
while (isRunning) {
//On bouge la pièce en fonction des demande utilisateur (bouton préssé)
processUserAction();
//La pièce tombe
processMoveDown();
//On enlève les lignes remplie
removeCompletedRows();
//On effectue le rendu graphique
RenderManager.getInstance().render(shape, remainingBlockSet);
}
}
/**
* Arret de la boucle événementielle
*/
void stopRunning(){
isRunning = false;
}
//----------------------------------------------------------------------------------------------
// METHODES DE GESTION DU JEU
//----------------------------------------------------------------------------------------------
/**
* Traitement des actions utilisateur
*/
private void processUserAction() {
//On recupère la dernière action
UserAction action = GameState.getInstance().getUserAction();
switch (action) {
case LEFT:
moveLeft();
break;
case RIGHT:
moveRight();
break;
case ROTATE:
rotate();
break;
case FALL:
fall();
break;
case PAUSE:
pause();
break;
case RESUME:
restart();
break;
}
//On définit la dernière action à nulle
GameState.getInstance().setUserAction(UserAction.NONE);
}
// met en pause le jeu
public void pause() {
ps = true;
while(ps) {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// permet de relancer le jeu
public void restart() {
ps=false;
}
private void fall(){
while(shape.canMoveDown(remainingBlockSet)){
shape.moveDown();
}
}
private void rotate(){
shape.rotate(remainingBlockSet);
}
/**
* Décale la pièce d'une case vers la gauche
*/
private void moveLeft(){
shape.moveLeft(remainingBlockSet);
}
/**
* Décale la pièce d'une case vers la droite
*/
private void moveRight(){
shape.moveRight(remainingBlockSet);
}
/**
* Décale la pièce d'une case vers le bas
*/
private void processMoveDown(){
//Si le laps de temps n'est pas écoulé on ne fait rien
long now = System.currentTimeMillis();
if(now-time < GameConstants.TIME_LAPS){
return;
}
//Sinon la pièce descend
if(shape.canMoveDown(remainingBlockSet)){
shape.moveDown();
}
else{
processShapeCollision();
}
//On redéfinit le temps comme le temps actuel
time = System.currentTimeMillis();
}
private void processShapeCollision(){
remainingBlockSet.addAll(shape.getBlockList());
spawnNewShape();
}
/**
* A partir d'un caractère choisis aléatoirement, cette methode
permet
de générer la pièce qui
* lui est associé.
*/
private void spawnNewShape(){
int x = (GameConstants.COLUMNS/2)-2;
int y = 0;
char shapeType = chooseRandomShape();
switch (shapeType){
case 'O':
this.shape = new OShape(x,y);
break;
case 'T':
this.shape = new TShape(x,y);
break;
case 'S':
this.shape = new SShape(x,y);
break;
case 'I':
this.shape = new IShape(x,y);
break;
case 'L':
this.shape = new LShape(x,y);
break;
case 'Z':
this.shape = new ZShape(x,y);
break;
case 'J':
this.shape = new JShape(x,y);
break;
default:
//imposible
break;
}
}
/**
* Cette methode génère aléatoirement un entier entre 0 et 7. Ce dernier permet ensuite de
* séléctioner le caractère de la pièce à faire appraitre.
* @return un caractère correspondant à la prochaine pièce à faire apparaitre.
*/
private char chooseRandomShape(){
int x = (int) (Math.random() * shapeTypes.length);
return shapeTypes[x];
}
private void removeCompletedRows() {
for(int y = GameConstants.ROWS-1; y>0; y--){
List<Block> line = getBlockLine(y);
if (isLineCompleted(line)){
//Log.i(TAG,"la ligne"+y+"est pleine :"+line);
removeLine(line);
moveDownAboveBlocks(y);
}
}
}
private List<Block> getBlockLine(int y) {
List<Block> line = new ArrayList<>();
for(Block block : remainingBlockSet){
if(y == block.getY()){
line.add(block);
}
}
return line;
}
private boolean isLineCompleted(List<Block> line) {
for(int x = 0; x< GameConstants.COLUMNS; x++){
boolean found = false;
for(Block block : line){
if (block.getX() == x){
found = true;
break;
}
}
if(!found) {
return false;
}
}
return true;
}
private void removeLine(List<Block> line){
remainingBlockSet.removeAll(line);
}
private void moveDownAboveBlocks(int y){
for(Block block : remainingBlockSet){
if (block.getY()<y){
block.moveDown();
}
}
}