本人目前是大三考研党,之前有学过一些杂七杂八的知识,但是没有办法利用这些知识制作一些真正有用的东西出来。后来自学了android开发,学会使用了android studio,终于决定利用android studio 开发一个游戏出来,也算是我一直以来的梦想吧。但是由于制作水平有限,很多部分只能停留在文字上,请各位看客多见谅。
本博客会介绍本人在此游戏开发中的过程与困难,希望能够帮助同在学习android开发的同学。
(本文章创作于游戏框架已基本完成时)
1、游戏主页面(开始页面)
开始页面是游戏的重中之重。我最开始利用android studio开发时,曾制作过一个纯文字剧情类的游戏(后面因为故事情节创作停滞加上代码很费时间,最后游戏至今尚未完成,如果有兴趣的童鞋可以看看这个链接:https://pan.baidu.com/s/1SxBmWYbLqLuXW3peKkbdJQ
提取码:rjzb)那个游戏的页面并没怎么做好。
当然这是题外话了,回到正题,如果你已经有自学过android studio的使用,并有着一定的基础,你就知道一个页面需要一个java文件和与其对应的xml文件,这样才是一个完整的页面并能够正常显示。因此,下面的每一个模块我都会给出java文件和于其对应的xml文件的实现,以后将不再赘述。
作为游戏的开始页面,美观大气自然是必要的。然而本人美术水平有限,再加上没有经费支撑,最后只能选择自己制作一个较为生动的页面。下面是页面的具体效果。
虽然看起来比较一般,但确实拥有了一定的动态效果。实现起来也非常简单:我使用了多个TextView,填入对应的文字,随后将他们的visiability属性设为INVISABLE。随后我在主文件编写函数对他们的属性进行调整 ,就形成了这样的闪烁的效果。
同时,我在主页面添加了四个按钮,分别是开始游戏,读取存档,游戏介绍和设置。这样乍一看,是不是真的有内味了?这主页面虽然并不是特别美观,但是也确实实现了一个游戏开始页面所需要达到的效果。当然,这只是一个开头,我们接下来需要做的事远比编写开始页面要复杂。
(下面是开始页面的java文件和xml文件的代码)
package com.example.dungeonsimulator;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.media.MediaPlayer;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Canvas;
import android.app.Person;
import java.util.Timer;
import java.util.TimerTask;
import android.view.KeyEvent;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3,setting;
public static MediaPlayer player=null;
Timer timer;
public static int back=0;
public static person lead;
public static int du=0;
public static float volume=1.0f;
int i=0;
public static int num=0;
public static int weapon_num=9;
TextView textview5,textview6,textview4,textview7,textview8;
@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
main.text="";
main.index=0;
main.refresh=true;
lead=new person("",0,0,0,0,0,0,0,10,0,10);
lead.level=1;
lead.exp=lead.level_exp[0];
lead.nowexp=0;
num=0;
lead.gold=0;
lead.init();
main.first=false;
for(int i=0;i<lead.have_weapons.length;i++){
lead.have_weapons[i]=0;
}
for(int i=0;i<lead.weapon.length;i++){
lead.weapon[i]=-1;
}
for(int i=0;i<lead.skills.length;i++){
lead.skills[i]=-1;
}
for(int i=0;i<lead.have_skills.length;i++){
lead.have_skills[i]=0;
}
for(int i=0;i<lead.have_consume.length;i++){
lead.have_consume[i]=0;
}
(这部分代码在后面才会用到)
*/
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
setting=findViewById(R.id.setting);
textview4=findViewById(R.id.textView4);
textview5=findViewById(R.id.textView5);
textview6=findViewById(R.id.textView6);
textview7=findViewById(R.id.textView7);
textview8=findViewById(R.id.textView8);
player=MediaPlayer.create(this,R.raw.main);
player.setVolume(volume,volume);
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
player.start();
player.setLooping(true);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
Intent intent=new Intent(MainActivity.this,choose_job.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
MainActivity.du=1;
Intent intent=new Intent(MainActivity.this,dudang.class);
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
Intent intent=new Intent(MainActivity.this,introduction.class);
startActivity(intent);
}
});
setting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
Intent intent=new Intent(MainActivity.this,setting.class);
startActivity(intent);
}
});
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==0){
i+=Math.random()*10;
if(i%4==0){
textview4.setVisibility(View.VISIBLE);
}
if(i%5==0){
textview5.setVisibility(View.VISIBLE);
}
if(i%6==0){
textview6.setVisibility(View.VISIBLE);
}
if(i%7==0){
textview7.setVisibility(View.VISIBLE);
}
if(i%8==0){
textview8.setVisibility(View.VISIBLE);
}
if(i%9==0){
textview4.setVisibility(View.INVISIBLE);
textview5.setVisibility(View.INVISIBLE);
textview6.setVisibility(View.INVISIBLE);
textview7.setVisibility(View.INVISIBLE);
textview8.setVisibility(View.INVISIBLE);}
}
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
},0,1000);
}
}
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:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地牢"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="45sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.182" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="模拟器"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.309" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="v1.0"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.41" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女妖"
android:visibility="invisible"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.108"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.38" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="灰熊"
android:visibility="invisible"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.814"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.263" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圣骑士"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="40sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.821"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.656" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="老鼠"
android:visibility="invisible"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.304"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.109" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="骷髅王"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="45sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.322"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.956" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始冒险"
android:textSize="20sp"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.581" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续冒险"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.707" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏介绍"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.83" />
<com.example.dungeonsimulator.Rainplay
android:id="@+id/rainplay2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.005" />
</androidx.constraintlayout.widget.ConstraintLayout>
xml文件的RainPlay模块(从一个大佬那里借鉴的,忘了是谁了,见谅):
package com.example.dungeonsimulator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;
public class Rainplay extends BaseView {
List<RainItem> list = new ArrayList<RainItem>();
//控制雨滴的数量
private int num = 80;
public Rainplay(Context context) {
super(context);
}
public Rainplay(Context context, AttributeSet attrs) {
super(context, attrs);
//与xml链接起来
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RainView);
num = ta.getInteger(R.styleable.RainView_rainnum,80);
ta.recycle();
}
@Override
protected void drawsub(Canvas canvas) {
for (RainItem item : list) {
item.draw(canvas);
}
}
@Override
protected void move() {
for (RainItem item : list) {
item.movestep();
}
}
/**
* 因为获取长宽是放在layout之后才可以获取,所以需要
* 放在线程里面初始化
*/
@Override
protected void init() {
for (int i = 0; i < num; i++) {
RainItem item = new RainItem(getHeight(), getWidth());
list.add(item);
}
}
}
2、游戏加载页面
一款游戏,对游戏的背景介绍也是十分重要的。因此我特意写了一个页面用于介绍这款游戏的背景,便于游玩的人能够知道游戏的背景,不至于玩的糊里糊涂。
既然说到这里,就不得不简单介绍一下我为这个游戏设置的背景,男主作为勇者,要去偶然发现的地牢中进行探险。在此之前,老国王已然消失在地牢的深处,正是在这样一个人人自危的时间点,男主毅然决然地选择了前进。(差不多得了哈哈)
这次的页面的编写与开始页面类似,通过定时器的设置使得原本被设置为隐藏不可见的TextView内容在到达对应时间时显示出来,从而达到逐条出现的效果。当然应该会有更好的办法达到这一点,然而我选择了较容易实现的办法,当然代码量也显得较为冗余。
下面是页面的具体效果。
(由于制作成Gif图有问题,没办法只好截图)
代码:
story.java
package com.example.dungeonsimulator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.media.MediaPlayer;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Canvas;
import java.util.Timer;
import java.util.TimerTask;
import android.view.KeyEvent;
public class story extends AppCompatActivity {
Button button1,button2,button5;
EditText editText1;
ImageView imageview2;
MediaPlayer player=null;
Context context;
AttributeSet attr;
Canvas canvas;
Timer timer;
int i=0;
TextView textview1,textview2,textview3,textview4,textview5,textview6,textview7,textview8,textview9;
TextView textview10,textview11,textview12,textview13;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story);
SharedPreferences sharedPreferences= getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("story",1);
editor.commit();
imageview2=findViewById(R.id.imageView2);
textview1=findViewById(R.id.textView1);
textview2=findViewById(R.id.textView2);
textview3=findViewById(R.id.textView3);
textview4=findViewById(R.id.textView4);
textview5=findViewById(R.id.textView5);
textview6=findViewById(R.id.textView6);
textview7=findViewById(R.id.textView7);
textview8=findViewById(R.id.textView8);
textview9=findViewById(R.id.textView9);
textview10=findViewById(R.id.textView10);
textview11=findViewById(R.id.textView11);
textview12=findViewById(R.id.textView12);
textview13=findViewById(R.id.textView13);
imageview2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(i>=13){
timer.cancel();
Intent intent=new Intent(story.this,choose_job.class);
startActivity(intent);}
}
});
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==0){
i+=1;
if(i==1){
textview1.setVisibility(View.VISIBLE);
}
if(i==2){
textview2.setVisibility(View.VISIBLE);
}
if(i==3){
textview3.setVisibility(View.VISIBLE);
}
if(i==4){
textview4.setVisibility(View.VISIBLE);
}
if(i==5){
textview5.setVisibility(View.VISIBLE);
}
if(i==6){
textview6.setVisibility(View.VISIBLE);
}
if(i==7){
textview7.setVisibility(View.VISIBLE);
}
if(i==8){
textview8.setVisibility(View.VISIBLE);
}
if(i==9){
textview9.setVisibility(View.VISIBLE);
}
if(i==10){
textview10.setVisibility(View.VISIBLE);
}
if(i==11){
textview11.setVisibility(View.VISIBLE);
}
if(i==12){
textview12.setVisibility(View.VISIBLE);
}
if(i==13){
textview13.setVisibility(View.VISIBLE);
}
}
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
},0,2200);
}
}
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"
android:background="#000000"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从前的王国繁荣兴旺,欣欣向荣。"
android:textColor="@color/white"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.045" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="老国王爱护子民,把一切都管理的井井有条。"
android:textColor="@color/white"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.103" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="直到那一天......"
android:textColor="@color/white"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.157" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="仆人在庄园发现了一个尘封多年的地牢入口。"
android:textColor="@color/white"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.208" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="那是噩梦的开端。"
android:visibility="invisible"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.268" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:text="老国王身先士卒进入地牢,却从此音讯隔绝。"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.328" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:text="后来的人进入后,竟发现里面充满了怪物。"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进入地牢的人几乎没有再出来的。"
android:visibility="invisible"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.46" />
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="唯一进入地牢再出来的只有皇城的老将军。"
android:visibility="invisible"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:text="而当人们问起他的时候,他只是摇摇头。"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.592" />
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="wrap_content"
android:text="对于我们的勇者,"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.656" />
<TextView
android:id="@+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="他传奇的故事,将在这里开始....."
android:textColor="@color/white"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.723" />
<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:text="点击任意处以继续........"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.954" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/a"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="5dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、选职页面
本游戏作为一款类RPG的打怪升级类游戏,没有职业的选择可不行。
因此我编写了这个页面用于供玩家选择他们喜欢的职业。玩家可以通过还是那句话,朴实无华的游戏页面,但是也确实完成了相应的功能。
我对这个游戏设定了四个职业,分别是战士,刺客,魔法师和新皇。设置更多职业当然也可以,但是也会更复杂。每个职业都有对应的特点,战士防高血高,但是魔法值较低,刺客在战斗中可以永远先手,且攻击力很高,对血量较低的怪物可以做到一击必杀,但是对应的是其防御和血量相较战士比较低;魔法师拥有着大量的魔法值,可以不断释放技能,然而各个方面都不如前两者,而新皇相对中庸,他突出的点在于他有着高额的幸运值。说到这里,就不得不说一下,这款游戏的设定了,对于攻击,幸运值越高,闪避率越高,一旦成功闪避将不受到伤害。
回到正题,下面是选职页面的实现效果。
choose_job.java
package com.example.dungeonsimulator;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.graphics.Canvas;
import java.util.Timer;
public class choose_job extends AppCompatActivity {
Button button1,button2,button3,button4;
EditText editText1;
MediaPlayer player=null;
Context context;
AttributeSet attr;
Canvas canvas;
Timer timer;
int i=0;
TextView textview5,textview6,textview4,textview7,textview8;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_job);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.lead=new person("剑士",1,15,10,5,100,20,20,10,2,100);
Intent intent = new Intent(choose_job.this, stage.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.lead=new person("刺客",11,20,5,5,80,1,10,10,2,10000);
MainActivity.lead.First_Hand=true;
Intent intent = new Intent(choose_job.this, stage.class);
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.lead=new person("魔法师",1,10,10,20,70,15,10,40,2,70);
MainActivity.lead.set_lucky(2);
Intent intent = new Intent(choose_job.this, stage.class);
startActivity(intent);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.lead=new person("新皇",1,12,10,5,100,15,15,10,20,90);
Intent intent = new Intent(choose_job.this, stage.class);
startActivity(intent);
}
});
}
}
choose_job.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"
android:background="#000000"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="勇者,选择你的职业吧:"
android:textColor="@color/white"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.038" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="剑士:拥有不俗的攻击力与防御力,可惜不擅长魔法,适合新手游玩。"
android:layout_below="@id/button1"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/button1" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="剑士"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="刺客:由于拥有敏锐的观察能力,因此总能在战斗中占得先机。(战斗中刺客永远先手)同时其也拥有很强的攻击能力。适合老手。"
android:textColor="@color/white"
android:layout_below="@id/button2"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/button2"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="刺客"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.374" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="魔法师"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.566" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button3"
android:text="魔法师:极其擅长魔法,因而其魔法值足以让其释放多次魔法。然而与之对应的是孱弱的身体,攻击和防御都较弱。"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/button3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新皇"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.745" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button4"
android:layout_alignBottom="@id/button4"
android:layout_marginTop="4dp"
android:text="新皇:初登龙位,拥有很高的龙气,这也使得其受到百姓的爱戴,运气值极高!但同时其攻击与防御等都只是中等。"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/button4"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
4.设置页面
每个游戏都有一个设置页面,以便于玩家对游戏中的一些设置进行调整,以达到最好的游戏效果。那我们的这款游戏自然也要有。
对于一些基本的操作,我们需要对其功能进行编写,如存档,开/关音乐等。
下面是页面的具体实现效果:
其中开关音乐的操作相对来说并不复杂,只需要为音乐设置一个全局变量,然后在这里进行调整即可,而存档/读档的功能相对比较复杂,如果可以的话,我希望放在下一篇中进行详细讲述,这里就不过多赘述了。
下面是代码实现:
setting.java
package com.example.dungeonsimulator;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.media.MediaPlayer;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Canvas;
import android.app.Person;
import java.util.Timer;
import java.util.TimerTask;
import android.view.KeyEvent;
public class setting extends AppCompatActivity {
Button button1,button2,button3,button5,button6,button7;
EditText editText1;
MediaPlayer player=null;
Timer timer;
int i=0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
button5=findViewById(R.id.button5);
button6=findViewById(R.id.button6);
button7=findViewById(R.id.button7);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(setting.this,cundang.class);
startActivity(intent);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.volume=1.0f;
MainActivity.player.setVolume(MainActivity.volume,MainActivity.volume);
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.volume=0.0f;
MainActivity. player.setVolume(MainActivity.volume,MainActivity.volume);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(setting.this,dudang.class);
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(setting.this,MainActivity.class);
MainActivity.back=0;
startActivity(intent);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(MainActivity.back==0){
MainActivity.player.release();
Intent intent=new Intent(setting.this,MainActivity.class);
startActivity(intent);}
else{
Intent intent=new Intent(setting.this,main.class);
startActivity(intent);
}
}
});
}}
setting.xml文章来源:https://www.toymoban.com/news/detail-431716.html
<?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"
android:background="@color/black"
>
<TextView
android:id="@+id/textView7"
android:layout_width="258dp"
android:layout_height="36dp"
android:text="欢迎加q讨论:1076051806"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.588"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976" />
<TextView
android:id="@+id/textView8"
android:layout_width="199dp"
android:layout_height="80dp"
android:text="设置"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.561"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.073" />
<Button
android:id="@+id/button6"
android:layout_width="72dp"
android:layout_height="48dp"
android:text="开"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.415"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.188" />
<Button
android:id="@+id/button1"
android:layout_width="167dp"
android:layout_height="58dp"
android:text="存档"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.576"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.281" />
<Button
android:id="@+id/button2"
android:layout_width="167dp"
android:layout_height="61dp"
android:text="读档"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.577"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.398" />
<Button
android:id="@+id/button7"
android:layout_width="73dp"
android:layout_height="48dp"
android:text="关"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.695"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.187" />
<Button
android:id="@+id/button3"
android:layout_width="162dp"
android:layout_height="58dp"
android:text="回到主页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.538"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.769" />
<TextView
android:id="@+id/textView9"
android:layout_width="121dp"
android:layout_height="56dp"
android:text="游戏音乐:"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.117"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.197" />
<TextView
android:id="@+id/textView1"
android:layout_width="107dp"
android:layout_height="94dp"
android:text="存档:"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.115"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.324" />
<TextView
android:id="@+id/textView2"
android:layout_width="107dp"
android:layout_height="94dp"
android:text="读档:"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.111"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.006"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
(今天就先更新到这里吧,如果有人希望看到后续,记得在评论区私信我,我会尽快赶出来的,游戏app链接:链接:https://pan.baidu.com/s/1l_Fet3NdGlLSwcWn8Wrn5A
提取码:s07o,目前只有前十五级的怪物以及装备,主要是忙着准备考研,没有时间再写下去,当然如果有人催更,我也会接着写下去的哈哈)文章来源地址https://www.toymoban.com/news/detail-431716.html
到了这里,关于Android 开发--利用android studio 制作简单文字打怪升级游戏(伪地牢类)1.开始的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!