android studio打地鼠
时间: 2023-11-20 13:56:24 浏览: 183
以下是使用Android Studio实现打地鼠小游戏的步骤:
1.创建一个新的Android Studio项目,选择Empty Activity模板。
2.在app/build.gradle文件中添加SurfaceView库的依赖:
```gradle
implementation 'androidx.appcompat:appcompat:1.2.0'
```
3.在app/src/main/java目录下创建一个名为“game”的新包,并在其中创建一个名为“GameView”的新类。GameView类将扩展SurfaceView类,并实现SurfaceHolder.Callback接口。
4.在GameView类中添加必要的变量和方法,例如:
```java
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private GameThread gameThread;
private List<Mole> moles;
private int score;
private Paint scorePaint;
private boolean isGameOver;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
gameThread = new GameThread(getHolder(), this);
moles = new ArrayList<>();
score = 0;
scorePaint = new Paint();
scorePaint.setColor(Color.WHITE);
scorePaint.setTextSize(50);
isGameOver = false;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameThread.setRunning(true);
gameThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
gameThread.setRunning(false);
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
retry = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (isGameOver) {
restartGame();
} else {
for (Mole mole : moles) {
if (mole.contains(event.getX(), event.getY())) {
mole.hit();
score++;
break;
}
}
}
}
return true;
}
private void restartGame() {
moles.clear();
score = 0;
isGameOver = false;
for (int i = 0; i < 9; i++) {
moles.add(new Mole(this));
}
}
public void update() {
if (isGameOver) {
return;
}
for (Mole mole : moles) {
mole.update();
}
if (score >= 50) {
isGameOver = true;
}
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.GREEN);
for (Mole mole : moles) {
mole.draw(canvas);
}
canvas.drawText("Score: " + score, 50, 100, scorePaint);
if (isGameOver) {
Paint gameOverPaint = new Paint();
gameOverPaint.setColor(Color.WHITE);
gameOverPaint.setTextSize(100);
canvas.drawText("Game Over", 200, 500, gameOverPaint);
}
}
}
```
5.在game包中创建一个名为“GameThread”的新类,该类将扩展Thread类,并在run()方法中调用GameView的update()和draw()方法。
6.在game包中创建一个名为“Mole”的新类,该类将表示地鼠,并包含必要的变量和方法,例如:
```java
public class Mole {
private static final int WIDTH = 200;
private static final int HEIGHT = 200;
private static final int MAX_LIFE = 60;
private static final int MIN_LIFE = 20;
private static final int MAX_SPEED = 20;
private static final int MIN_SPEED = 5;
private static final int MAX_RADIUS = 80;
private static final int MIN_RADIUS = 40;
private static final int MAX_COLOR = 255;
private static final int MIN_COLOR = 100;
private GameView gameView;
private int x;
private int y;
private int radius;
private int speed;
private int life;
private int color;
public Mole(GameView gameView) {
this.gameView = gameView;
Random random = new Random();
x = random.nextInt(gameView.getWidth() - WIDTH);
y = random.nextInt(gameView.getHeight() - HEIGHT);
radius = random.nextInt(MAX_RADIUS - MIN_RADIUS) + MIN_RADIUS;
speed = random.nextInt(MAX_SPEED - MIN_SPEED) + MIN_SPEED;
life = random.nextInt(MAX_LIFE - MIN_LIFE) + MIN_LIFE;
color = Color.rgb(random.nextInt(MAX_COLOR - MIN_COLOR) + MIN_COLOR,
random.nextInt(MAX_COLOR - MIN_COLOR) + MIN_COLOR,
random.nextInt(MAX_COLOR - MIN_COLOR) + MIN_COLOR);
}
public void update() {
x += speed;
if (x < 0 || x > gameView.getWidth() - WIDTH) {
speed = -speed;
}
life--;
if (life <= 0) {
gameView.getMoless().remove(this);
gameView.getMoless().add(new Mole(gameView));
}
}
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(color);
canvas.drawCircle(x + WIDTH / 2, y + HEIGHT / 2, radius, paint);
}
public boolean contains(float x, float y) {
return x >= this.x && x <= this.x + WIDTH && y >= this.y && y <= this.y + HEIGHT;
}
public void hit() {
life -= 10;
if (life <= 0) {
gameView.getMoless().remove(this);
gameView.getMoless().add(new Mole(gameView));
}
}
}
```
7.在MainActivity类中设置屏幕方向为横向,并将GameView添加到布局中:
```java
public class MainActivity extends AppCompatActivity {
private GameView gameView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
gameView = findViewById(R.id.game_view);
}
}
```
8.在res/layout目录下创建一个名为“activity_main.xml”的新布局文件,并添加一个GameView控件:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.playmouse.GameView
android:id="@+id/game_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
```
9.运行应用程序,即可开始打地鼠小游戏。
阅读全文