У меня есть программа тетрис на Android.
часть программы:
вот основная деятельность:
package com.tetris.classictetris;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ParcelGamesField pgf = new ParcelGamesField(10,20);
final Button buttonLeft = findViewById(R.id.button_left);
buttonLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pgf.moveLeft();
}
});
final Button buttonStartPause = findViewById(R.id.button_start_pause);
buttonStartPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(String.valueOf(GameSurface.class));
startActivity(intent);
}
});
final Button buttonReset = findViewById(R.id.button_reset);
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
вот ParcelGamesField:
package com.tetris.classictetris;
public class ParcelGamesField implements GamesField {
int a,b;
int[] gamesField;
int sh;
int state;
public ParcelGamesField(int a, int b) {
this.a = a;
this.b = b;
gamesField = new int[a*b];
}
class Field extends View {
private Rect mRectField;
private Rect mRectSquare;
private Paint mPaint;
private Paint mPaintErase;
public Field(Context context, AttributeSet attrs) {
super(context, attrs);
mRectField = new Rect();
mRectSquare = new Rect();
mPaint = new Paint();
mPaintErase = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x80000000);
mPaintErase.setStyle(Paint.Style.STROKE);
mPaintErase.setColor(0x80FFFFFF);
}
@Override
protected void onDraw(Canvas canvas) {
Rect rf = mRectField;
Rect rs = mRectSquare;
Paint paint = mPaint;
Paint paintErase = mPaintErase;
canvas.drawRect(0,0,501,1001,paint);
for (int j=0; j < b; j++){
for (int i=0; i < a; i++){
if ( gamesField[i+a*j] == 1 ) {
canvas.drawRect(25+i*50,25+j*50,75+i*50,75+j*50,paint);
}
}
}
super.onDraw(canvas);
}
}
@Override
public void disappearFullSolidRows() {
//...........
}
@Override
public boolean setShape(int generatedShape) {
sh = generatedShape;
switch (generatedShape) {
//.......
}
public boolean naturalFallingDown(){
//.....
}
public void moveLeft(){
//.....
}
код детали. Я хочу перейти из ParcelGamesField в GameSurface.
(сейчас я только копирую.)
как заставить его работать вместе: mainactivity, activity_main.xml (с кнопкой в игре), игровой интерфейс, gamethread и parcelgamesfield или без (удалить) ParcelGamesField?
вот GameSurface:
package com.tetris.classictetris;
public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {
private Rect mRectField;
private Rect mRectSquare;
private Paint mPaint;
private Paint mPaintErase;
int a,b;
int[] gamesField;
int sh;
int state;
private GameThread gameThread;
public GameSurface(Context context) {
super(context);
// Make Game Surface focusable so it can handle events.
this.setFocusable(true);
// Sét callback.
this.getHolder().addCallback(this);
a = 10;
b = 20;
gamesField = new int[a*b];
}
class Field extends View {
private Rect mRectField;
private Rect mRectSquare;
private Paint mPaint;
private Paint mPaintErase;
public Field(Context context, AttributeSet attrs) {
super(context, attrs);
mRectField = new Rect();
mRectSquare = new Rect();
mPaint = new Paint();
mPaintErase = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x80000000);
mPaintErase.setStyle(Paint.Style.STROKE);
mPaintErase.setColor(0x80FFFFFF);
}
@Override
protected void onDraw(Canvas canvas) {
Rect rf = mRectField;
Rect rs = mRectSquare;
Paint paint = mPaint;
Paint paintErase = mPaintErase;
canvas.drawRect(0,0,501,1001,paint);
for (int j=0; j < b; j++){
for (int i=0; i < a; i++){
if ( gamesField[i+a*j] == 1 ) {
canvas.drawRect(25+i*50,25+j*50,75+i*50,75+j*50,paint);
}
}
}
super.onDraw(canvas);
}
@Override
public void disappearFullSolidRows() {
//...........
}
@Override
public boolean setShape(int generatedShape) {
sh = generatedShape;
switch (generatedShape) {
//.......
}
public boolean naturalFallingDown(){
//.....
}
public void moveLeft(){
//.....
}
Вот GameThread:
package com.tetris.classictetris;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class GameThread extends Thread {
private boolean running;
private GameSurface gameSurface;
private SurfaceHolder surfaceHolder;
public GameThread(GameSurface gameSurface, SurfaceHolder surfaceHolder) {
this.gameSurface = gameSurface;
this.surfaceHolder = surfaceHolder;
}
@Override
public void run() {
long startTime = System.nanoTime();
while(running) {
Canvas canvas = null;
try {
// Get Canvas from Holder and lock it.
canvas = this.surfaceHolder.lockCanvas();
// Synchronized
synchronized (canvas) {
this.gameSurface.update();
this.gameSurface.draw(canvas);
}
}catch(Exception e) {
// Do nothing.
} finally {
if(canvas!= null) {
// Unlock Canvas.
this.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
long now = System.nanoTime() ;
// Interval to redraw game
// (Change nanoseconds to milliseconds)
long waitTime = (now - startTime)/1000000;
if(waitTime < 10) {
waitTime= 10; // Millisecond.
}
System.out.print(" Wait Time="+ waitTime);
try {
// Sleep.
this.sleep(waitTime);
} catch(InterruptedException e) {
}
startTime = System.nanoTime();
System.out.print(".");
}
}
public void setRunning(boolean running) {
this.running = running;
}
}