从0开始用Android studio开发一个像素风24点小游戏(第0天)

从0开始用Android studio开发一个像素风24点小游戏(第0天)

前言:

        从这篇文章开始我会记录自己从0开始用Android studio一步一步开发一个安卓小游戏(24点小游戏),本合集主要用于自己记录自己的开发过程,顺带给同样学习完Android程序开发想动手实现项目的人一些启发。

使用的软件:Android studio 2025.1.3

语言: java

第0天目标:设计出基础的ui界面,并实现.xml代码

首先开发一个游戏,需要有哪些步骤呢,也许刚刚学完Android程序开发的你也很迷茫,没关系,我也是如此,但在AI工具遍地的今天,我们可以问问它们

可以看到,这是豆包给我的答案,主要分为 前期准备、核心开发、测试优化、发布维护 四个步骤,那么今天是第0天,我们就从前期准备开始

前期准备:

1.需求定位与分析:

        我们要动手实现的是一个24点小游戏的开发,不知道大家有没有玩过24点小游戏,大概就是随机从牌堆里抽取四张牌,通过加减乘除的组合如果能使得其等于24,则为胜利。很显然这是一款益智类小游戏。

        因为只是简单的加减法组合,所以用户的话没有太大限制和定位,从小学生开始都能玩,难度自然而然也同理,没有什么限制

        画风我考虑的是像素风格,因为个人喜好加上资源比较好找,我也会把我使用的像素资源网站放在这,上面有很多免费的像素资源     传送门

        核心功能清单:其实这一步也就决定了今天的ui界面设计,首先肯定是四张可以刷新切换的卡片,供用户选择交互等,因为设计运算符的组合和计算,输入框和加减乘除括号按钮也是必要的,另外还有功能按钮:清空(清空输入框),提交(用于提交用户输入的答案并验证),刷新(刷新牌面实现随机抽牌)按钮分别负责对应的功能。

2.技术选型:

       因为游戏并不复杂,这点我觉得没有太多,就是java语言,用Android studio开发

3.原型设计与资源准备:

        根据上述分析,其实我们已经可以简单的设计出一个ui界面的草图了,主要是三层

第一层:四张扑克,用于提供牌面信息,点击牌对应数字会出现在输入框上

第二层:输入框+加减乘除括号按钮,用于给用户点击实现各种数字和符号的组合

第三层:功能按钮,主要有清空(清空输入框),提交(用于提交用户输入的答案并验证),刷新(刷新牌面实现随机抽牌)按钮分别负责对应的功能。

下面是我用电脑画图工具画的一个基础ui草图:

理论存在!实践开始!启动Android studio设计出第一个自己的游戏布局!

1.分析布局:

        很明显通过上面的ui图一眼就能看出,这是一个三层式的界面,第一层是牌,第二层输入框+按钮,第三层功能按钮

        我采用的是根布局为 ConstraintLayout,作为 “大容器” 通过约束关系控制三个核心区域的垂直排列;每个区域内部用 LinearLayout 实现水平方向的组件分布,结合 layout_weight 实现响应式宽度适配。

1. 扑克牌展示区(card_container)
        容器类型:LinearLayout(水平排列)
        组件:4 个 ImageView(展示四张游戏牌)
        布局逻辑:
        水平方向通过 gravity="center" 居中显示;
        用 layout_marginHorizontal 和 layout_marginStart/End 控制牌与牌之间的间隔,避免拥挤;
        paddingStart/paddingEnd 控制牌组与屏幕左右边缘的距离。

<LinearLayout
        android:id="@+id/card_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/card1"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginEnd="16dp"
            app:src***pat="@drawable/six" />

        <ImageView
            android:id="@+id/card2"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginHorizontal="16dp"
            app:src***pat="@drawable/six" />

        <ImageView
            android:id="@+id/card3"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginHorizontal="16dp"
            app:src***pat="@drawable/six" />

        <ImageView
            android:id="@+id/card4"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginStart="16dp"
            app:src***pat="@drawable/six" />
    </LinearLayout>

2. 输入与运算按钮区(input_container)
        容器类型:LinearLayout(水平排列)
        组件:1 个 EditText(输入框) + 6 个 Button(加减乘除、左右括号)
        布局逻辑:
        EditText 通过 layout_weight="3" 占据较大宽度,方便用户输入计算式;
        所有按钮 layout_weight="0.8",均匀分配剩余宽度,高度统一为 45dp 保证视觉整齐;
        margin 控制组件之间的间隔,避免按钮粘连。

<LinearLayout
        android:id="@+id/input_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintTop_toBottomOf="@+id/card_container"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

    
        <EditText
            android:id="@+id/et_expression"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="3"
            android:hint="请输入计算式(如 6+6+6+6)"
            android:inputType="text"
            android:layout_marginEnd="8dp"
            android:paddingHorizontal="10dp"
            android:background="@android:drawable/editbox_background" />

   
        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="+" />

        <Button
            android:id="@+id/btn_sub"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="-" />

        <Button
            android:id="@+id/btn_mul"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="×" />

        <Button
            android:id="@+id/btn_div"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="÷" />

        <Button
            android:id="@+id/btn_right"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="(" />

        <Button
            android:id="@+id/btn_left"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginStart="8dp"
            android:text=")" />
    </LinearLayout>

3. 功能按钮区(third_container)
        容器类型:LinearLayout(水平排列)
        组件:3 个 Button(清空、提交、刷新牌面)
        布局逻辑:
        三个按钮 layout_weight="1",均匀平分宽度,高度 45dp 与上层按钮保持一致;
        marginHorizontal 控制按钮之间的间隔。

 <LinearLayout
        android:id="@+id/third_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintTop_toBottomOf="@+id/input_container"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginHorizontal="8dp"
            android:text="清空" />

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginHorizontal="8dp"
            android:text="提交" />

        <Button
            android:id="@+id/btn_Refresh"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginHorizontal="8dp"
            android:text="刷新牌面" />
    </LinearLayout>

2.完整代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.***/apk/res/android"
    xmlns:app="http://schemas.android.***/apk/res-auto"
    xmlns:tools="http://schemas.android.***/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/card_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/card1"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginEnd="16dp"
            app:src***pat="@drawable/six" />

        <ImageView
            android:id="@+id/card2"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginHorizontal="16dp"
            app:src***pat="@drawable/six" />

        <ImageView
            android:id="@+id/card3"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginHorizontal="16dp"
            app:src***pat="@drawable/six" />

        <ImageView
            android:id="@+id/card4"
            android:layout_width="120dp"
            android:layout_height="180dp"
            android:layout_marginStart="16dp"
            app:src***pat="@drawable/six" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/input_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintTop_toBottomOf="@+id/card_container"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

 
        <EditText
            android:id="@+id/et_expression"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="3"
            android:hint="请输入计算式(如 6+6+6+6)"
            android:inputType="text"
            android:layout_marginEnd="8dp"
            android:paddingHorizontal="10dp"
            android:background="@android:drawable/editbox_background" />

 
        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="+" />

        <Button
            android:id="@+id/btn_sub"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="-" />

        <Button
            android:id="@+id/btn_mul"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="×" />

        <Button
            android:id="@+id/btn_div"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="÷" />

        <Button
            android:id="@+id/btn_right"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginHorizontal="2dp"
            android:text="(" />

        <Button
            android:id="@+id/btn_left"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="0.8"
            android:layout_marginStart="8dp"
            android:text=")" />
    </LinearLayout>

   
    <LinearLayout
        android:id="@+id/third_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintTop_toBottomOf="@+id/input_container"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginHorizontal="8dp"
            android:text="清空" />

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginHorizontal="8dp"
            android:text="提交" />

        <Button
            android:id="@+id/btn_Refresh"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginHorizontal="8dp"
            android:text="刷新牌面" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

design图和运行效果图展示:

        至此,第0天的目标已完成,由于博主是学生,不一定会严格按天更新,主要是出于记录自己的开发过程,当然我一定不会断更!能引发志同道合的朋友的共鸣我也很开心,喜欢这个系列就关注吧~

第0天涉及的图片资源: 像素风格扑克牌下载传送门

转载请说明出处内容投诉
CSS教程网 » 从0开始用Android studio开发一个像素风24点小游戏(第0天)

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买