android打地鼠小游戏源码
时间: 2023-07-30 10:04:40 浏览: 110
打地鼠安卓小游戏源码
5星 · 资源好评率100%
以下是一个简单的 Android 打地鼠小游戏的代码示例,供参考:
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private GridLayout gridLayout;
private int score = 0;
private TextView scoreText;
private static final int ROWS = 3;
private static final int COLUMNS = 4;
private static final int NUM_MOLES = 6;
private Mole[] moles = new Mole[NUM_MOLES];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = findViewById(R.id.grid_layout);
scoreText = findViewById(R.id.score_text);
gridLayout.setColumnCount(COLUMNS);
gridLayout.setRowCount(ROWS);
for (int i = 0; i < NUM_MOLES; i++) {
Mole mole = new Mole(this);
mole.setMoleListener(new MoleListener() {
@Override
public void onMoleWhacked() {
score++;
scoreText.setText("Score: " + score);
}
});
moles[i] = mole;
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
gridLayout.addView(moles[(i * COLUMNS + j) % NUM_MOLES]);
}
}
}
@Override
protected void onResume() {
super.onResume();
for (Mole mole : moles) {
mole.start();
}
}
@Override
protected void onPause() {
super.onPause();
for (Mole mole : moles) {
mole.stop();
}
}
}
```
Mole.java
```java
public class Mole extends AppCompatImageView implements View.OnClickListener {
private static final int ANIMATION_DURATION = 500;
private Handler handler;
private MoleListener listener;
private boolean isUp = false;
private boolean isRunning = false;
public Mole(Context context) {
super(context);
setImageResource(R.drawable.mole_down);
setOnClickListener(this);
handler = new Handler();
}
public void setMoleListener(MoleListener listener) {
this.listener = listener;
}
public void start() {
if (!isRunning) {
isRunning = true;
handler.postDelayed(moleRunnable, getRandomDelay());
}
}
public void stop() {
isRunning = false;
handler.removeCallbacks(moleRunnable);
}
private Runnable moleRunnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
if (isUp) {
animate().alpha(0).setDuration(ANIMATION_DURATION).withEndAction(downRunnable);
} else {
setImageResource(R.drawable.mole_up);
animate().alpha(1).setDuration(ANIMATION_DURATION);
handler.postDelayed(downRunnable, getRandomDuration());
}
isUp = !isUp;
handler.postDelayed(moleRunnable, getRandomDelay());
}
}
};
private Runnable downRunnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
setImageResource(R.drawable.mole_down);
}
}
};
private int getRandomDelay() {
return (int) (Math.random() * 3000 + 1000);
}
private int getRandomDuration() {
return (int) (Math.random() * 1000 + 500);
}
@Override
public void onClick(View v) {
if (isUp) {
if (listener != null) {
listener.onMoleWhacked();
}
animate().alpha(0).setDuration(ANIMATION_DURATION).withEndAction(downRunnable);
isUp = false;
}
}
}
```
MoleListener.java
```java
public interface MoleListener {
void onMoleWhacked();
}
```
布局文件 activity_main.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<GridLayout
android:id="@+id/grid_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
app:columnCount="4"
app:rowCount="3" />
<TextView
android:id="@+id/score_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Score: 0"
android:textSize="20sp" />
</RelativeLayout>
```
图片资源
在 drawable 文件夹中添加 mole_down.png 和 mole_up.png 两张图片,分别代表地鼠下蹲和跳起来的状态。
这是一个简单的打地鼠小游戏,仅供参考。
阅读全文