testaaa.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import React, { Component } from 'react'
  2. import {
  3. StyleSheet,
  4. View,
  5. Text,
  6. ActivityIndicator,
  7. TextInput,
  8. TouchableOpacity,
  9. ImageBackground,
  10. Dimensions,
  11. Image,
  12. PixelRatio,
  13. PanResponder,
  14. } from 'react-native'
  15. import SplashScreen from 'react-native-splash-screen'
  16. const { height, width } = Dimensions.get('window')
  17. class testaaa extends Component {
  18. constructor(props) {
  19. super(props)
  20. this.state = {
  21. gameDivs: [],
  22. nextDivs: [],
  23. }
  24. this.TouchX = 0
  25. this.TouchY = 0
  26. this.downI = 0
  27. this.flag = true
  28. // 手势---------------------------------------
  29. this._panResponder = PanResponder.create({
  30. // 要求成为响应者:
  31. onStartShouldSetPanResponder: (evt: Object, gestureState: Object) => true,
  32. onStartShouldSetPanResponderCapture: (
  33. evt: Object,
  34. gestureState: Object
  35. ) => true,
  36. onMoveShouldSetPanResponder: (evt: Object, gestureState: Object) => true,
  37. onMoveShouldSetPanResponderCapture: (evt: Object, gestureState: Object) =>
  38. true,
  39. onPanResponderGrant: (evt: Object, gestureState: Object) => {
  40. // let moveY = Math.floor((evt.nativeEvent.pageX - 60) / 20);
  41. // if (moveY >= 7) {
  42. // moveY = 7;
  43. // } else if (moveY <= 0) {
  44. // moveY = -1;
  45. // }
  46. // this.TouchY = moveY;
  47. // this.init();
  48. // console.log(evt, "onPanResponderGrant ---11111111111111");
  49. // console.log(gestureState, "onPanResponderGrant ---2222222222");
  50. // 开始手势操作。给用户一些视觉反馈,让他们知道发生了什么事情!
  51. // gestureState.{x,y} 现在会被设置为0
  52. },
  53. onPanResponderMove: (evt: Object, gestureState: Object) => {
  54. let moveY = Math.floor((gestureState.moveX - 50) / 20)
  55. if (moveY >= 6) {
  56. moveY = 6
  57. } else if (moveY <= 0) {
  58. moveY = 0
  59. }
  60. this.TouchX = moveY
  61. this.init()
  62. // 最近一次的移动距离为gestureState.move{X,Y}
  63. // 从成为响应者开始时的累计手势移动距离为gestureState.d{x,y}
  64. },
  65. onPanResponderTerminationRequest: (evt: Object, gestureState: Object) =>
  66. true,
  67. onPanResponderRelease: (evt: Object, gestureState: Object) => {
  68. // 用户放开了所有的触摸点,且此时视图已经成为了响应者。
  69. // 一般来说这意味着一个手势操作已经成功完成。
  70. },
  71. onPanResponderTerminate: (evt: Object, gestureState: Object) => {
  72. // 另一个组件已经成为了新的响应者,所以当前手势将被取消。
  73. },
  74. onShouldBlockNativeResponder: (evt: Object, gestureState: Object) => {
  75. // 返回一个布尔值,决定当前组件是否应该阻止原生组件成为JS响应者
  76. // 默认返回true。目前暂时只支持android。
  77. return true
  78. },
  79. })
  80. // 手势---------------------------------------
  81. }
  82. componentDidMount() {
  83. SplashScreen.hide()
  84. this.setIntervalFn(true)
  85. this.init()
  86. }
  87. // 定时器
  88. setIntervalFn(isOpen) {
  89. if (isOpen) {
  90. // flag 避免重复点击
  91. if (this.flag) {
  92. this.flag = false
  93. this.settime = setInterval(() => {
  94. this.downOpera()
  95. }, 100)
  96. }
  97. } else {
  98. this.flag = true
  99. clearInterval(this.settime)
  100. }
  101. }
  102. init() {
  103. const nextDatas = [[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
  104. gameDatas = [
  105. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  106. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  107. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  108. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  109. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  110. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  111. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  112. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  113. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  114. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  115. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  116. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  117. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  118. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  119. [2, 0, 0, 2, 2, 2, 2, 2, 2, 2],
  120. [2, 2, 0, 2, 2, 2, 2, 2, 2, 2],
  121. [2, 2, 2, 2, 2, 2, 2, 0, 2, 2],
  122. [2, 2, 2, 0, 2, 2, 2, 2, 2, 2],
  123. [2, 2, 2, 2, 0, 2, 2, 2, 2, 2],
  124. [0, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  125. ]
  126. for (let i = 0; i < nextDatas.length; i++) {
  127. for (let j = 0; j < nextDatas[0].length; j++) {
  128. if (nextDatas[i][j] != 0) {
  129. gameDatas[this.TouchY + i][this.TouchX + j] = nextDatas[i][j]
  130. }
  131. }
  132. }
  133. this.setGame(gameDatas, nextDatas)
  134. }
  135. downOpera() {
  136. this.TouchY = this.downI++
  137. if (this.TouchY >= 16) {
  138. this.TouchY = 16
  139. this.downI = 0
  140. this.setIntervalFn()
  141. }
  142. this.init()
  143. }
  144. setGame(gameDatas, nextDatas) {
  145. if (
  146. gameDatas &&
  147. gameDatas.length > 0 &&
  148. nextDatas &&
  149. nextDatas.length > 0
  150. ) {
  151. this.setState({
  152. gameDivs: this.squareFn(gameDatas, true),
  153. nextDivs: this.squareFn(nextDatas),
  154. })
  155. } else if (gameDatas && gameDatas.length > 0) {
  156. this.setState({
  157. gameDivs: this.squareFn(gameDatas, true),
  158. })
  159. } else if (nextDatas && nextDatas.length > 0) {
  160. this.setState({
  161. nextDivs: this.squareFn(nextDatas),
  162. })
  163. }
  164. }
  165. squareFn(value: Array) {
  166. if (!value) return
  167. const gameDivs = []
  168. for (let i = 0; i < value.length; i++) {
  169. let gameDiv = []
  170. for (let j = 0; j < value[0].length; j++) {
  171. let smallClass = ''
  172. if (value[i][j] == 1) {
  173. smallClass = 'current'
  174. } else if (value[i][j] == 2) {
  175. smallClass = 'done'
  176. }
  177. const newNode = (
  178. <View
  179. key={`${i}:${j}`}
  180. style={[
  181. styles.small,
  182. styles[smallClass],
  183. {
  184. top: i * 20,
  185. left: j * 20,
  186. },
  187. ]}
  188. />
  189. )
  190. if (value[i][j] != 0) {
  191. gameDiv.push(newNode)
  192. }
  193. }
  194. gameDivs.push(gameDiv)
  195. }
  196. return gameDivs
  197. }
  198. render() {
  199. return (
  200. <View style={{ flex: 1 }}>
  201. <View style={styles.container}>
  202. <View
  203. {...this._panResponder.panHandlers}
  204. style={{
  205. width: 202,
  206. height: 401,
  207. borderColor: 'black',
  208. borderLeftWidth: 1,
  209. borderRightWidth: 1,
  210. borderBottomWidth: 1,
  211. position: 'absolute',
  212. backgroundColor: '#F2FAFF',
  213. }}
  214. >
  215. {this.state.gameDivs}
  216. </View>
  217. <View style={{ position: 'absolute', left: 250 }}>
  218. <View
  219. style={{
  220. width: 82,
  221. height: 82,
  222. borderWidth: 1,
  223. borderColor: 'black',
  224. backgroundColor: '#F2FAFF',
  225. }}
  226. >
  227. {this.state.nextDivs}
  228. </View>
  229. <Text>已用时:0s</Text>
  230. <Text>已得分:0分</Text>
  231. <TouchableOpacity
  232. onPress={() => {
  233. this.setIntervalFn()
  234. }}
  235. >
  236. <Text>停止</Text>
  237. </TouchableOpacity>
  238. <TouchableOpacity
  239. onPress={() => {
  240. this.setIntervalFn(true)
  241. }}
  242. >
  243. <Text>开始</Text>
  244. </TouchableOpacity>
  245. </View>
  246. </View>
  247. </View>
  248. )
  249. }
  250. }
  251. const styles = StyleSheet.create({
  252. container: {
  253. flex: 1,
  254. position: 'absolute',
  255. flexDirection: 'row',
  256. paddingTop: 20,
  257. paddingLeft: 20,
  258. top: 150,
  259. left: 20,
  260. },
  261. small: {
  262. width: 20,
  263. height: 20,
  264. position: 'absolute',
  265. },
  266. current: {
  267. backgroundColor: 'pink',
  268. borderColor: 'red',
  269. borderWidth: 1,
  270. },
  271. done: {
  272. backgroundColor: 'gray',
  273. borderColor: 'black',
  274. borderWidth: 1,
  275. },
  276. })
  277. export default testaaa