安卓简单小游戏开发:比较数字大小(Number Game)

安卓简单小游戏开发:比较数字大小(Number Game)


简单介绍
1.画面会出现2个数字
2.点击你认为大的数字,结果正确你的分数会增加,错误你的分数会减少
3.一道题解决之后自动出现下一道题


我先发截图先看一下效果!
1.画面显示图
在这里插入图片描述
2.点击左侧6按钮,分数会增加一分
在这里插入图片描述
3.为了确认重新再试验一下,再次选择右侧的数字8,当然分数又会增加一分,会变2分
在这里插入图片描述
4.最后我们要看一下,我们选择错误数字的时候也是否会执行我们所要的结果
在这里插入图片描述
5.是的一切没问题!


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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Number Game"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.139" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="252dp"
        android:layout_marginRight="252dp"
        android:onClick="buttonOnClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="252dp"
        android:layout_marginLeft="252dp"
        android:onClick="buttonOnClick2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="52dp"
        android:text="맞춘 개수:0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="409dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="181dp"
        android:text="어느게 더 클 까 ?"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

java代码

package com.example.numbergame;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private int num1;
    private int num2;
    private int points;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        roll();
    }



    private void roll(){
        Random r=new Random();
        num1=r.nextInt(9);
        num2=r.nextInt(9);
        while (num2==num1){								//当出现两位相同的数字的时候第2位数字会改变,
            num2=r.nextInt(9);						   //直到两位数字不一样为止
        }
        Button btn1=(Button)findViewById(R.id.button);
        Button btn2=(Button)findViewById(R.id.button2);
        btn1.setText(""+ num1);
        btn2.setText(""+ num2);
       }

    public void buttonOnClick(View view) {
        if(num1>num2){
            Toast.makeText(this,"correct",Toast.LENGTH_SHORT).show();
            points++;
            TextView txt=(TextView)findViewById(R.id.textView2);
            txt.setText(""+ points);
        }
        else {
            Toast.makeText(this, "incorrect", Toast.LENGTH_SHORT).show();
            points--;
            TextView txt = (TextView) findViewById(R.id.textView2);
            txt.setText("" + points);
        }

        roll();

    }
    public void buttonOnClick2(View view) {
        if(num1<num2){
            Toast.makeText(this,"correct",Toast.LENGTH_SHORT).show();
            points++;
            TextView txt=(TextView)findViewById(R.id.textView2);
            txt.setText(""+points);

        }else {
            Toast.makeText(this, "incorrect", Toast.LENGTH_SHORT).show();
            points--;
            TextView txt = (TextView) findViewById(R.id.textView2);
            txt.setText("" + points);
        }

        roll();
    }

}

至于xml代码里为什么有韩语:因为这是在来韩国当交换生的时候写的简单的Android Homework

  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
学习 Python 开发窗口化小游戏,如猜数字,是一个很好的实践编程技能和理解事件驱动编程的方式。在 Python 中,你可以使用 `pygame` 库来创建这样的游戏,它提供了丰富的图形界面和游戏编程功能。 首先,你需要安装 `pygame`,可以通过 pip 来安装: ```bash pip install pygame ``` 接下来,我们可以简单地概述一下制作猜数字游戏的基本步骤: 1. **初始化 Pygame**: ```python import pygame pygame.init() ``` 2. **设置窗口**: ```python screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Guess the Number") ``` 3. **生成随机数**: ```python secret_number = random.randint(1, 100) ``` 4. **主循环**: - 获取用户输入 - 检查输入并更新游戏状态 - 渲染游戏画面 - 更新屏幕 - 处理事件(如关闭窗口) 5. **猜数过程**: - 显示提示信息(比如“太低了”或“太高了”) - 当玩家猜中数字时,显示胜利消息并结束游戏 6. **退出游戏**: ```python if event.type == pygame.QUIT: pygame.quit() quit() ``` 下面是基础版本的猜数字游戏代码片段: ```python import pygame import random # ... (步骤2-5的代码) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 用户输入 guess = int(pygame.font.SysFont('Arial', 30).render(str(input_box.get_text()), True, (0, 0, 0))) input_box.updateguess(guess) # 检查猜测并更新游戏状态 if guess == secret_number: pygame.time.delay(1000) # 延迟1秒显示胜利消息 victory_message = "Congratulations! You guessed the number!" screen.blit(victory_message, (50, 200)) elif guess < secret_number: feedback = "Too low!" else: feedback = "Too high!" # 渲染反馈 screen.blit(feedback, (50, 150)) # 更新屏幕 pygame.display.flip() pygame.quit() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值