awacke1 commited on
Commit
61f9d9b
ยท
1 Parent(s): 6b1c5dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -62
app.py CHANGED
@@ -1,63 +1,116 @@
1
  import streamlit as st
2
- import plotly.graph_objs as go
3
- from bokeh.plotting import figure
4
- from bokeh.models import ColumnDataSource
5
- import random
6
- import os
7
-
8
- # Define initial data
9
- x_data = [0]
10
- y_data = [0]
11
-
12
- # Define Plotly figure
13
- plotly_fig = go.Figure()
14
- plotly_fig.add_trace(go.Scatter(x=x_data, y=y_data, mode='lines'))
15
-
16
- # Define Bokeh figure
17
- bokeh_fig = figure(plot_width=400, plot_height=400)
18
- bokeh_data = ColumnDataSource(data=dict(x=x_data, y=y_data))
19
- bokeh_fig.line('x', 'y', source=bokeh_data)
20
-
21
- # Define Streamlit app
22
- st.title("Infinite Graphics Demo")
23
- st.plotly_chart(plotly_fig)
24
- st.bokeh_chart(bokeh_fig)
25
-
26
- # Define data file path
27
- data_file = 'data.txt'
28
-
29
- # Define loop to update data and display new graphs
30
- while True:
31
- # Read current data from file
32
- with open(data_file, 'r') as f:
33
- x_data, y_data = [float(x) for x in f.read().split(',')]
34
-
35
- # Generate new data
36
- x_new = x_data[-1] + 1
37
- y_new = y_data[-1] + random.randint(-10, 10)
38
-
39
- # Add new data to arrays
40
- x_data.append(x_new)
41
- y_data.append(y_new)
42
-
43
- # Update text file with new data
44
- with open(data_file, 'w') as f:
45
- f.write(','.join([str(x_data[-1]), str(y_data[-1])]))
46
-
47
- # Update Plotly figure
48
- plotly_fig.data[0].x = x_data
49
- plotly_fig.data[0].y = y_data
50
-
51
- # Update Bokeh figure
52
- bokeh_data.data = dict(x=x_data, y=y_data)
53
-
54
- # Display new graphs
55
- st.plotly_chart(plotly_fig)
56
- st.bokeh_chart(bokeh_fig)
57
-
58
- # Update code with new data file path
59
- with open(__file__, 'r') as f:
60
- code = f.read()
61
- code = code.replace("'data.txt'", f"'{data_file}'")
62
- with open(__file__, 'w') as f:
63
- f.write(code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import time
4
+
5
+ # Define game board size
6
+ BOARD_HEIGHT = 20
7
+ BOARD_WIDTH = 10
8
+
9
+ # Define shapes of tetrominoes
10
+ SHAPES = [
11
+ np.array([
12
+ [1, 1],
13
+ [1, 1]
14
+ ]),
15
+
16
+ np.array([
17
+ [1, 1, 1],
18
+ [0, 1, 0]
19
+ ]),
20
+
21
+ np.array([
22
+ [1, 1, 1],
23
+ [1, 0, 0]
24
+ ]),
25
+
26
+ np.array([
27
+ [1, 1, 1],
28
+ [0, 0, 1]
29
+ ]),
30
+
31
+ np.array([
32
+ [1, 1, 0],
33
+ [0, 1, 1]
34
+ ]),
35
+
36
+ np.array([
37
+ [0, 1, 1],
38
+ [1, 1, 0]
39
+ ]),
40
+
41
+ np.array([
42
+ [1, 1, 1, 1]
43
+ ])
44
+ ]
45
+
46
+ # Define colors for each tetromino
47
+ COLORS = [
48
+ "yellow",
49
+ "orange",
50
+ "cyan",
51
+ "blue",
52
+ "purple",
53
+ "green",
54
+ "red"
55
+ ]
56
+
57
+ # Define initial game state
58
+ board = np.zeros((BOARD_HEIGHT, BOARD_WIDTH))
59
+ current_shape = None
60
+ current_shape_pos = (0, 0)
61
+ current_shape_color = None
62
+ score = 0
63
+ game_over = False
64
+
65
+ def get_random_shape():
66
+ shape_idx = np.random.randint(len(SHAPES))
67
+ shape = SHAPES[shape_idx]
68
+ color = COLORS[shape_idx]
69
+ return shape, color
70
+
71
+ def place_shape_on_board(board, shape, position, color):
72
+ shape_height, shape_width = shape.shape
73
+ board_height, board_width = board.shape
74
+
75
+ # Check if shape can fit on the board at the given position
76
+ if position[0] + shape_height > board_height or position[1] + shape_width > board_width:
77
+ return False
78
+
79
+ # Check if there are any overlapping blocks
80
+ if np.any(board[position[0]:position[0]+shape_height, position[1]:position[1]+shape_width] * shape):
81
+ return False
82
+
83
+ # Place shape on the board
84
+ board[position[0]:position[0]+shape_height, position[1]:position[1]+shape_width] += shape * color
85
+
86
+ return True
87
+
88
+ def check_if_row_is_complete(board, row):
89
+ return np.all(board[row, :])
90
+
91
+ def remove_row(board, row):
92
+ board[row+1:,:] = board[row:-1,:]
93
+ board[0,:] = 0
94
+
95
+ def remove_completed_rows(board):
96
+ for row in range(board.shape[0]):
97
+ if check_if_row_is_complete(board, row):
98
+ remove_row(board, row)
99
+
100
+ def update_game_state():
101
+ global board, current_shape, current_shape_pos, current_shape_color, score, game_over
102
+
103
+ # Move shape down by one position
104
+ new_shape_pos = (current_shape_pos[0] + 1, current_shape_pos[1])
105
+
106
+ # Check if shape can be placed on the board
107
+ if not place_shape_on_board(board, current_shape, new_shape_pos, current_shape_color):
108
+ # Shape cannot be placed, lock it in place
109
+ place_shape_on_board(board, current_shape, current_shape_pos, current_shape_color)
110
+
111
+ # Check if any rows have been completed
112
+ remove_completed_rows(board)
113
+
114
+ # Check if game is over
115
+ if np.any(board[0,:]):
116
+ game_over = True