安卓小游戏 2048 新手练手项目 完整代码(含注释)
最编程
2024-08-15 10:14:04
...
看了极客的安卓2048的开发教程,大概了解了一下思路,然后自己就开始写了。后来发现这个设计思路不是太好,不方便加移动动画,就只加了创建卡片和合并的动画,不过用来练手还可以。
游戏截图如下:
如果想拷贝到本地运行的话,注意修改和包名相关的地方
或者在创建工程的时候按照以下命名:
项目名: Game2048
包名: pers.hurric.game2048
AndroidManifest.xml
仅需设置screenOrientation
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pers.hurric.game2048">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/score"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline6"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/buttonReplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_replay"
android:textColor="@android:color/holo_blue_light"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.12" />
<TextView
android:id="@+id/textHighestScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/highest_score"
android:textColor="@android:color/holo_purple"
android:textSize="18sp"
android:textStyle="italic"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<pers.hurric.game2048.GameView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintStart_toStartOf="@+id/guideline6"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.26" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05109489" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />
</androidx.constraintlayout.widget.ConstraintLayout>
string.xml
<resources>
<string name="app_name">Game2048</string>
<string name="score">Score : 0</string>
<string name="highest_score">Highest Score : 0</string>
<string name="button_replay">Restart</string>
</resources>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="gameViewBackgroundColor">#D2B48C</color>
<color name="backgroundColor0">#DEB887</color>
<color name="textColor0">#00000000</color>
<color name="textColor2">#333300</color>
<color name="textColor4">#333300</color>
<color name="textColorCommon">#FFFAFA</color>
<color name="backgroundColor2">#FDF5E6</color>
<color name="backgroundColor4">#FFE4B5</color>
<color name="backgroundColor8">#FFE4C4</color>
<color name="backgroundColor16">#FFDAB9</color>
<color name="backgroundColor32">#FF7F50</color>
<color name="backgroundColor64">#FF4500</color>
<color name="backgroundColor128">#FF8C00</color>
<color name="backgroundColor256">#FFD700</color>
<color name="backgroundColor512">#9ACD32</color>
<color name="backgroundColor1024">#483D8B</color>
<color name="backgroundColor2048">#4B0082</color>
<color name="backgroundColorBiggerThan2048">#000000</color>
</resources>
MainActivity.java
package pers.hurric.game2048;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String FILE_NAME = "MyData";
private final String HIGHEST_SCORE = "highest_score";
private TextView textScore;
private TextView textHighestScore;
private Button buttonReplay;
private int score = 0;
private int highestScore = 0;
public static MainActivity mainActivity;
public MainActivity(){
mainActivity = this;
}
public void addScore(int score) {
this.score += score;
textScore.setText("Score : " + this.score);
//更新最高分
updateHighestScore(this.score);
}
private void updateHighestScore(int score){
if(score > highestScore){
highestScore = score;
textHighestScore.setText("HighestScore : " + score);
//存储最高分
SharedPreferences shp = getSharedPreferences(FILE_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putInt(HIGHEST_SCORE, highestScore);
editor.apply();
}
}
public void clearScore(){
score = 0;
textScore.setText("Score : " + 0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textScore = findViewById(R.id.textScore);
textHighestScore = findViewById(R.id.textHighestScore);
buttonReplay = findViewById(R.id.buttonReplay);
//读取最高分
SharedPreferences shp = getSharedPreferences(FILE_NAME, MODE_PRIVATE);
highestScore = shp.getInt(HIGHEST_SCORE, 0);
textHighestScore.setText("HighestScore : " + highestScore);
buttonReplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GameView.gameView.replayGame();
}
});
}
private boolean isExit = false;
class ExitHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 0){
isExit = false;//修改状态为退出
}
}
};
ExitHandler mHandler = new ExitHandler();
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if(!isExit){
isExit = true;
Toast.makeText(this, "再按一次退出游戏", Toast.LENGTH_SHORT).show();
//延迟更改状态信息
mHandler.sendEmptyMessageDelayed(0, 2000);
}
else{
finish();
}
}
return false;
}
}
GameView.java
自定义游戏面板
package pers.hurric.game2048;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.
上一篇:
C语言实现2048游戏的完整代码