基于react-native实现情侣小游戏

2024-02-27 21:48

本文主要是介绍基于react-native实现情侣小游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


title: 基于react-native实现情侣小游戏
date: 2018-06-25 11:19:33
tags: react-native

一、背景

前段时间,突发奇想,想要自己做一款能够上架的app,就根据react-native进行了开发,其中具有的功能点就是情侣了解度的测试,通过一些情侣应该知道的问题进行测试双方的了解程度,了解的越多,则得分也就越高,在这个app里边区分男方和女方,用户点击进如对应的题库进行答题,最终获取分值.

二、效果

演示效果

三、下载安装与查看

1.首先,通过命令进行克隆项目

git clone https://github.com/suwu150/LoveYouDeeply.git

也可以直接到网站https://github.com/suwu150/LoveYouDeeply进行直接下载到本地
2.在下载之后,进入到项目中,执行下面命令进行安装依赖

npm install

确保安装没有出错,然后运行下面命令
react-native run-ios或者react-native run-android
注意,在mac系统中可直接运行react-native run-ios命令,windows系统中,运行react-native run-android这个之前需要确定有模拟器是启动着的或者有真机是连接着的。
按照上面的过程实施之后,就可以看到效果了.

四、代码及实现过程

1.项目结构如下所示:

项目结构
其中,具体代码写在src中,在src中`assets`中放置静态资源,components中放置使用到的组件,constants中放置静态常量,pages中放置具体的页面,utils中放置工具类

1.主页面代码(home)

/*** Created by jkwu on 2018/6/18.*/
import React, { Component } from 'react';
import { Drawer, List } from 'antd-mobile-rn';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { View, Text, TouchableOpacity, Alert } from 'react-native';
import StringDistinction from 'react-native-string-distinction';
import styleDict from '../../constants/styleDict';
import LocalKeyStore from '../../utils/storageUtil';import { maleQuestions, femaleQuestions } from '../../constants/questions';export default class Home extends Component {static navigationOptions = ({ navigation }) => {const { params } = navigation.state;return {title: '',headerLeft: null,headerRight: (<TouchableOpacityonPress={() => {params && params.onOpenChange && params.onOpenChange();}}><Ionicons name="md-more" size={28} color="#fff" style={{ marginRight: 15 }} /></TouchableOpacity>),};};constructor(props) {super(props);this.state = {  open: false };}componentDidMount() {const { navigation } = this.props;navigation && navigation.setParams({ onOpenChange: this._onOpenChange });LocalKeyStore.setKey('femaleQuestions', femaleQuestions, (error) => {if (error) {Alert.alert(error.message, '');} else {// Alert.alert('保存成功', '');}});LocalKeyStore.setKey('maleQuestions', maleQuestions, (error) => {if (error) {Alert.alert(error.message, '');} else {// Alert.alert('保存成功', '');}});}_onOpenChange = () => {this.setState({  open: !this.state.open });};_toContent = (gender) => {const questionType = gender === 'male' ? 'maleQuestions' : 'femaleQuestions';LocalKeyStore.getKey(questionType, (error, questions) => {const { navigation } = this.props;if (!error) { navigation.navigate('Content', { gender, questions });  }});};_onPress = (router) => {const { navigation } = this.props;navigation.navigate(router);};render() {const itemWidth = styleDict.windowW / 2;const itemHeight = styleDict.windowH - 10;const sidebarList = [<List.Item onClick={() => this._onPress('About')}><View><Text>关于软件</Text></View></List.Item>,<List.Item onClick={() => this._onPress('Instructions')}><View><Text>使用说明</Text></View></List.Item>,<List.Item onClick={() => this._onPress('QuestionAdd')}><View><Text>添加问题</Text></View></List.Item>,<List.Item onClick={() => this._onPress('QuestionList')}><View><Text>问题列表</Text></View></List.Item>,<List.Item onClick={() => this._onPress('Version')}><View><Text>版本 1.0</Text></View></List.Item>,];return (<View style={{ flex: 1 }}><DrawerenableDragHandleposition="right"style={{ position: 'relative', height: styleDict.windowH }}contentStyle={{ color: '#A6A6A6', textAlign: 'center', paddingTop: 42 }}sidebar={sidebarList}open={this.state.open}drawerWidth={styleDict.windowW / 2}drawerBackgroundColor="#fff"onOpenChange={this.onOpenChange}><View style={{ height: 150, justifyContent: 'center', alignItems: 'center' }}><Text style={{ fontSize: 30, color: '#ffbbef' }}>选择主人公</Text></View><View style={{ flexDirection: 'row', justifyContent: 'center',alignItems: 'center', height: 300 }}><TouchableOpacity onPress={() => this._toContent('male')}><View style={{ flex: 1, width: itemWidth, height: itemHeight, backgroundColor: '#fcff81',justifyContent: 'center', alignItems: 'center'}}><StringDistinctionvalue={'我是男主'}delimiter={'男'}frontStyle={{ fontSize: 20, color: '#fd7251' }}delimiterStyle={{ fontSize: 30, color: '#92cbfd' }}behindStyle={{ fontSize: 26, color: '#fd7251' }}/></View></TouchableOpacity><TouchableOpacity onPress={() => this._toContent('female')}><View style={{ flex: 1, width: itemWidth, height: itemHeight, backgroundColor: '#ffbbef',justifyContent: 'center', alignItems: 'center'}}><StringDistinctionvalue={'我是女主'}delimiter={'女'}frontStyle={{ fontSize: 20, color: '#fd7251' }}delimiterStyle={{ fontSize: 30, color: '#fd697d' }}behindStyle={{ fontSize: 26, color: '#fd7251' }}/></View></TouchableOpacity></View></Drawer></View>);}
}

2.内容界面(content)代码如下所示:

import React, { Component } from 'react';
import { View, Text, Image, StyleSheet } from 'react-native';
import SwipeCards from 'react-native-swipe-cards';
import Card from './card';
import NoMoreCard from './noMoreCard';
import styleDict from '../../constants/styleDict';
import randomStyle from '../../utils/randomStyle';import femaleQuestions from '../../constants/female_questions.json';
import maleQuestions from '../../constants/male_questions.json';export default class Home extends Component {constructor(props) {super(props);const { navigation } = this.props;const { params } = navigation.state;this.state = {gender: params.gender,questions: (params.gender === 'male' ? maleQuestions : femaleQuestions) || [],totalScore: 0};}_handleYup = (card) => {// 右划-否console.log(`Yup for ${card.text}`);this.setState({totalScore: this.state.totalScore - card.score <= 0 ? 0 : this.state.totalScore - card.score});};_handleNope = (card) => {// 左划-是console.log(`Nope for ${card.text}`);this.setState({totalScore: this.state.totalScore + card.score});};_handleMaybe = (card) => {// 上划console.log(`Maybe for ${card.text}`);this.setState({totalScore: this.state.totalScore + (card.score / 2)});};render() {const explainItem = styleDict.windowW / 3;return (<View style={{ flex: 1, alignItems: 'center' }}><View style={{ flexDirection: 'row', alignItems: 'center', width: styleDict.windowW,height: 30, position: 'absolute', top: 0, zIndex: 999 }}><Viewstyle={{ width: explainItem, backgroundColor: '#ffa6f3', height: 30, borderBottomRightRadius: 30,alignItems: 'center', justifyContent: 'center'}}><Textstyle={{ alignSelf: 'flex-start', textAlign: 'left' }}>{'左滑选是'}</Text></View><Viewstyle={{ width: explainItem, backgroundColor: '#ffa6f3', height: 30, borderRadius: 30,alignItems: 'center', justifyContent: 'center' }}><Textstyle={{ alignSelf: 'center', width: explainItem, textAlign: 'center' }}>{'上滑也许是吧'}</Text></View><Viewstyle={{ width: explainItem, backgroundColor: '#ffa6f3', height: 30, borderBottomLeftRadius: 30,alignItems: 'center', justifyContent: 'center' }}><Textstyle={{ alignSelf: 'flex-end', width: explainItem, textAlign: 'right' }}>{'右滑选否'}</Text></View></View><View style={{ flex: 1 }}><SwipeCardscards={this.state.questions}renderCard={(cardData) => <Card {...cardData} />}renderNoMoreCards={() => <NoMoreCard />}handleYup={this._handleYup}handleNope={this._handleNope}handleMaybe={this._handleMaybe}yupTextStyle={{ color: '#ff6559' }}yupText="否"yupStyle={{ borderColor: '#ff6559' }}nopeStyle={{ borderColor: '#3d7d29' }}nopeTextStyle={{ color: '#3d7d29' }}nopeText="是"maybeText="也许是吧"hasMaybeAction/></View><Imagesource={require('../../assets/images/heart.jpg')}style={{ width: styleDict.windowW, height: 50, resizeMode: Image.resizeMode.stretch, opacity: 0.7 }}/><View style={{height: 150,width: styleDict.windowW,justifyContent: 'center',alignItems: 'center',backgroundColor: this.state.gender === 'male' ? '#ffe144' : '#ffa0c4'}}><Text style={randomStyle()}>{this.state.totalScore}</Text></View></View>);}
}
五、已完成功能以及待完成功能
  • [+].增加用户自己提交问题的功能
  • [].增加远程服务器,进行账号数据保存,进行实时更新问题列表
六、仓库地址

欢迎访问代码仓库:https://github.com/suwu150/LoveYouDeeply 点赞加星星

这篇关于基于react-native实现情侣小游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/753696

相关文章

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本