-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
App.js
143 lines (132 loc) · 4.21 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, Dimensions, TouchableWithoutFeedback } from 'react-native';
import Bird from './components/Bird'
import Obstacles from './components/Obstacles'
export default function App() {
const screenWidth = Dimensions.get("screen").width
const screenHeight = Dimensions.get("screen").height
const birdLeft = screenWidth / 2
const [birdBottom, setBirdBottom]= useState(screenHeight / 2)
const [obstaclesLeft, setObstaclesLeft]= useState(screenWidth)
const [obstaclesLeftTwo, setObstaclesLeftTwo]= useState(screenWidth + screenWidth/2 + 30)
const [obstaclesNegHeight, setObstaclesNegHeight]= useState(0)
const [obstaclesNegHeightTwo, setObstaclesNegHeightTwo]= useState(0)
const [isGameOver, setIsGameOver]= useState(false)
const [score, setScore]= useState(0)
const gravity = 3
let obstacleWidth = 60
let obstacleHeight = 300
let gap = 200
let gameTimerId
let obstaclesTimerId
let obstaclesTimerIdTwo
//start bird falling
useEffect(() => {
if (birdBottom > 0) {
gameTimerId = setInterval(() => {
setBirdBottom(birdBottom => birdBottom - gravity)
},30)
return () => {
clearInterval(gameTimerId)
}
}
//if i dont have birdBottom as a dependecy, it wont stop
}, [birdBottom])
console.log(birdBottom)
const jump = () => {
if (!isGameOver && (birdBottom < screenHeight)) {
setBirdBottom(birdBottom => birdBottom + 50)
console.log('jumped')
}
}
//start first obstacle
useEffect(() => {
if (obstaclesLeft > -60) {
obstaclesTimerId = setInterval(() => {
setObstaclesLeft(obstaclesLeft => obstaclesLeft - 5)
}, 30)
return () => {
clearInterval(obstaclesTimerId)
}
} else {
setScore(score => score +1)
setObstaclesLeft(screenWidth)
setObstaclesNegHeight( - Math.random() * 100)
}
}, [obstaclesLeft])
//start second obstacle
useEffect(() => {
if (obstaclesLeftTwo > -60) {
obstaclesTimerIdTwo = setInterval(() => {
setObstaclesLeftTwo(obstaclesLeftTwo => obstaclesLeftTwo - 5)
}, 30)
return () => {
clearInterval(obstaclesTimerIdTwo)
}
} else {
setScore(score => score +1)
setObstaclesLeftTwo(screenWidth)
setObstaclesNegHeightTwo( - Math.random() * 100)
}
}, [obstaclesLeftTwo])
//check for collisions
useEffect(() => {
console.log(obstaclesLeft)
console.log(screenWidth/2)
console.log(obstaclesLeft > screenWidth/2)
if (
((birdBottom < (obstaclesNegHeight + obstacleHeight + 30) ||
birdBottom > (obstaclesNegHeight + obstacleHeight + gap -30)) &&
(obstaclesLeft > screenWidth/2 -30 && obstaclesLeft < screenWidth/2 + 30 )
)
||
((birdBottom < (obstaclesNegHeightTwo + obstacleHeight + 30) ||
birdBottom > (obstaclesNegHeightTwo + obstacleHeight + gap -30)) &&
(obstaclesLeftTwo > screenWidth/2 -30 && obstaclesLeftTwo < screenWidth/2 + 30 )
)
)
{
console.log('game over')
gameOver()
}
})
const gameOver = () => {
clearInterval(gameTimerId)
clearInterval(obstaclesTimerId)
clearInterval(obstaclesTimerIdTwo)
setIsGameOver(true)
}
return (
<TouchableWithoutFeedback onPress={jump}>
<View style={styles.container}>
{isGameOver && <Text style={{fontSize: '30px'}}>{score}</Text>}
<Bird
birdBottom = {birdBottom}
birdLeft = {birdLeft}
/>
<Obstacles
color={'green'}
obstacleWidth = {obstacleWidth}
obstacleHeight = {obstacleHeight}
randomBottom = {obstaclesNegHeight}
gap = {gap}
obstaclesLeft = {obstaclesLeft}
/>
<Obstacles
color={'yellow'}
obstacleWidth = {obstacleWidth}
obstacleHeight = {obstacleHeight}
randomBottom = {obstaclesNegHeightTwo}
gap = {gap}
obstaclesLeft = {obstaclesLeftTwo}
/>
</View>
</TouchableWithoutFeedback>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'red',
},
})