基于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

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja