-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy.py
184 lines (145 loc) · 3.86 KB
/
flappy.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
import sys
import pandas as pd
import time
from random import randint
import os
from threading import Timer
try:
import msvcrt
PLATFORM = "win"
except ImportError:
PLATFORM = "unix"
import tty
import termios
from select import select
def getch():
if PLATFORM == "win":
ch = msvcrt.getch()
return ch
elif PLATFORM == "unix":
fd = sys.stdin.fileno()
old_setting = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
i, o, e = select([sys.stdin.fileno()], [], [], 5)
if i:
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_setting)
return ch
def key_pressed():
global KEY_PRESSED, KEY
KEY = getch()
if KEY:
KEY_PRESSED = True
class Board:
'''
Board is dataframe.
Example:
01234567
0.......X
1.......X
2..@.....
3........
4.......X
@ - bird
X - column
. - empty
'''
def __init__(self):
self.size_x = 15
self.size_y = 9
self.DF = pd.DataFrame([[' ' for x in range(0, self.size_x)] for y in range(0, self.size_y)])
self.columns = []
def draw(self):
self.insertColumns()
self.insertBird()
#print (self.DF)
print ('=' * self.size_x)
for row in self.DF.values.tolist():
print (''.join(row))
print ('=' * self.size_x)
self.clearBoard()
def insertBird(self):
# collision
if Bird.pos_y < 0 or Bird.pos_y > self.size_y-1:
sys.exit('The bird flew off.')
elif self.DF[Bird.pos_x][Bird.pos_y] == 'X':
sys.exit('You broke the bird - GAME OVER')
else:
self.DF[Bird.pos_x][Bird.pos_y] = '@'
def createColumn(self):
self.columns.append(Column())
def insertColumns(self):
for col in self.columns.copy():
self.DF[col.pos_x][col.pos_y] = 'X'
col.move()
if col.pos_x < 0:
del self.columns[0]
def clearBoard(self):
self.DF = pd.DataFrame([[' ' for x in range(0, self.size_x)] for y in range(0, self.size_y)])
class Bird:
'''
Object with cordinates.
'''
def __init__(self):
self.pos_x = 2
self.pos_y = 3
def changePosY(self, y):
self.pos_y -= y
class Column:
'''
Column object is initialized with random parameters - hight and hole size.
It is created in last column in dataframe by default.
It has the property of which cells occupy.
'''
def __init__(self):
self.hole_size = randint(1, 3)
self.height = randint(0, 9-self.hole_size)
self.pos_x = Board.size_x-1
self.pos_y = [x for x in range(0,self.height)] + [x for x in range(self.height+self.hole_size, Board.size_y)]
def move(self):
self.pos_x -= 1
# game parameters
tick = 0.35
column_step = 8
# init
i = 0
KEY_PRESSED = False
KEY = ''
Board = Board()
Bird = Bird()
while True:
# key event
key_event = Timer(tick, key_pressed)
key_event.start()
time.sleep(tick)
# clearing
if PLATFORM =='win':
os.system('cls')
elif PLATFORM == 'unix':
os.system('clear')
# new column checker
i += 1
if i % column_step == 0:
Board.createColumn()
# key pressed checker - only for windows
# if msvcrt.kbhit():
# Bird.changePosY(1)
# if msvcrt.getch() == b'q':
# break
# else:
# Bird.changePosY(-1)
if KEY_PRESSED:
if KEY == b'q':
key_event.cancel()
sys.exit('Exited')
KEY_PRESSED = False
Bird.changePosY(1)
else:
Bird.changePosY(-1)
# draw
print (f'Score: {i}')
Board.draw()
print ('Press any key to fly up and <q> to quit.')