awacke1 commited on
Commit
1a22f42
·
verified ·
1 Parent(s): f5cbf6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -82
app.py CHANGED
@@ -1,91 +1,168 @@
1
  import streamlit as st
2
- import random
3
- from game_dev_snippet import run_game
4
- from mad_lib_generator import generate_mad_lib
5
-
6
- st.set_page_config(layout="wide")
7
-
8
- st.title("Game Development and Creative Writing Showcase")
9
-
10
- col1, col2 = st.columns(2)
11
-
12
- with col1:
13
- st.header("Game Development")
14
- st.code(run_game.__code__.co_code, language="python")
15
- if st.button("Run Game"):
16
- st.write("Game running in a separate window...")
17
- run_game()
18
-
19
- with col2:
20
- st.header("Mad Lib Generator")
21
- if st.button("Generate Mad Lib"):
22
- st.write(generate_mad_lib())
23
-
24
- st.header("D&D SVG Sprite Animation")
25
- st.components.v1.html("""
26
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" width="400" height="400">
27
- <style>
28
- @keyframes move {
29
- 0% { transform: translateX(0); }
30
- 50% { transform: translateX(100px); }
31
- 100% { transform: translateX(0); }
32
  }
33
- .sprite { animation: move 4s infinite; }
34
- </style>
35
-
36
- <g class="sprite">
37
- <!-- Wizard sprite -->
38
- <path d="M50,150 L70,100 L90,150 Z" fill="#4a0e4e" />
39
- <circle cx="80" cy="90" r="20" fill="#c8a2c8" />
40
- <path d="M70,90 Q80,70 90,90" stroke="#4a0e4e" stroke-width="2" fill="none" />
41
- </g>
42
-
43
- <!-- Background elements -->
44
- <rect x="0" y="150" width="200" height="50" fill="#3a7d44" />
45
- <circle cx="30" cy="30" r="15" fill="#f9d71c" />
46
- </svg>
47
- """, height=400)
48
-
49
- st.header("Three.js 3D App")
50
- st.components.v1.html("""
51
- <!DOCTYPE html>
52
- <html>
53
- <head>
54
- <meta charset="utf-8">
55
- <title>Three.js Cube</title>
56
- <style>
57
- body { margin: 0; }
58
- canvas { display: block; }
59
- </style>
60
- </head>
61
- <body>
62
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
63
- <script>
64
- const scene = new THREE.Scene();
65
- const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
66
- const renderer = new THREE.WebGLRenderer();
67
- renderer.setSize(400, 300);
68
- document.body.appendChild(renderer.domElement);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- const geometry = new THREE.BoxGeometry();
71
- const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
72
- const cube = new THREE.Mesh(geometry, material);
73
- scene.add(cube);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  camera.position.z = 5;
 
 
76
 
77
- function animate() {
78
- requestAnimationFrame(animate);
79
- cube.rotation.x += 0.01;
80
- cube.rotation.y += 0.01;
81
- renderer.render(scene, camera);
82
  }
83
- animate();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  </script>
85
- </body>
86
- </html>
87
- """, height=300)
88
 
89
- if __name__ == "__main__":
90
- st.sidebar.title("Navigation")
91
- st.sidebar.info("Use the buttons and sections above to interact with different components of the app.")
 
1
  import streamlit as st
2
+ import base64
3
+
4
+ st.set_page_config(layout="wide", page_title="Game Dev and 3D Showcase")
5
+
6
+ st.title("Game Development and 3D Content Showcase")
7
+
8
+ # HTML5 Game
9
+ st.header("HTML5 Game")
10
+ html5_game = """
11
+ <canvas id="gameCanvas" width="400" height="300"></canvas>
12
+ <script>
13
+ const canvas = document.getElementById('gameCanvas');
14
+ const ctx = canvas.getContext('2d');
15
+ let x = canvas.width / 2;
16
+ let y = canvas.height - 30;
17
+ let dx = 2;
18
+ let dy = -2;
19
+ const ballRadius = 10;
20
+
21
+ function drawBall() {
22
+ ctx.beginPath();
23
+ ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
24
+ ctx.fillStyle = "#0095DD";
25
+ ctx.fill();
26
+ ctx.closePath();
 
 
 
 
 
27
  }
28
+
29
+ function draw() {
30
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
31
+ drawBall();
32
+
33
+ if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
34
+ dx = -dx;
35
+ }
36
+ if (y + dy > canvas.height - ballRadius || y + dy < ballRadius) {
37
+ dy = -dy;
38
+ }
39
+
40
+ x += dx;
41
+ y += dy;
42
+ }
43
+
44
+ setInterval(draw, 10);
45
+ </script>
46
+ """
47
+ st.components.v1.html(html5_game, height=350)
48
+
49
+ # Mad Lib Generator
50
+ st.header("Mad Lib Generator")
51
+
52
+ templates = [
53
+ "The {adjective} {noun} {verb} over the {adjective} {noun}.",
54
+ "In a {adjective} land, a {noun} and a {noun} went on a {adjective} adventure.",
55
+ "The {noun} {verb} {adverb} while the {adjective} {noun} watched in amazement."
56
+ ]
57
+
58
+ parts_of_speech = {
59
+ "adjective": ["brave", "mysterious", "colorful", "gigantic", "tiny"],
60
+ "noun": ["wizard", "dragon", "knight", "castle", "forest"],
61
+ "verb": ["flew", "danced", "sang", "fought", "explored"],
62
+ "adverb": ["quickly", "silently", "gracefully", "fiercely", "carefully"]
63
+ }
64
+
65
+ def generate_mad_lib():
66
+ import random
67
+ template = random.choice(templates)
68
+ for part in parts_of_speech:
69
+ while "{" + part + "}" in template:
70
+ template = template.replace("{" + part + "}", random.choice(parts_of_speech[part]), 1)
71
+ return template
72
+
73
+ if st.button("Generate Mad Lib"):
74
+ st.write(generate_mad_lib())
75
+
76
+ # A-Frame 3D Scene
77
+ st.header("A-Frame 3D Scene")
78
+ aframe_scene = """
79
+ <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
80
+ <a-scene embedded style="height: 400px;">
81
+ <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
82
+ <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
83
+ <a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
84
+ <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
85
+ <a-sky color="#ECECEC"></a-sky>
86
+ </a-scene>
87
+ """
88
+ st.components.v1.html(aframe_scene, height=450)
89
 
90
+ # Three.js 3D Model Viewer
91
+ st.header("Three.js 3D Model Viewer")
92
+ threejs_viewer = """
93
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
94
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/OBJLoader.js"></script>
95
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
96
+ <div id="model-container" style="width: 100%; height: 400px;"></div>
97
+ <script>
98
+ let scene, camera, renderer, model;
99
+
100
+ function init() {
101
+ scene = new THREE.Scene();
102
+ camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
103
+ renderer = new THREE.WebGLRenderer();
104
+ renderer.setSize(window.innerWidth, 400);
105
+ document.getElementById('model-container').appendChild(renderer.domElement);
106
+
107
+ const light = new THREE.PointLight(0xffffff, 1, 100);
108
+ light.position.set(0, 0, 10);
109
+ scene.add(light);
110
 
111
  camera.position.z = 5;
112
+ animate();
113
+ }
114
 
115
+ function animate() {
116
+ requestAnimationFrame(animate);
117
+ if (model) {
118
+ model.rotation.x += 0.01;
119
+ model.rotation.y += 0.01;
120
  }
121
+ renderer.render(scene, camera);
122
+ }
123
+
124
+ function loadModel(url, fileType) {
125
+ let loader;
126
+ if (fileType === 'obj') {
127
+ loader = new THREE.OBJLoader();
128
+ } else if (fileType === 'glb') {
129
+ loader = new THREE.GLTFLoader();
130
+ }
131
+
132
+ loader.load(url, function(object) {
133
+ if (model) {
134
+ scene.remove(model);
135
+ }
136
+ model = fileType === 'obj' ? object : object.scene;
137
+ scene.add(model);
138
+ });
139
+ }
140
+ init();
141
+ </script>
142
+ """
143
+ st.components.v1.html(threejs_viewer, height=450)
144
+
145
+ # File uploader for 3D models
146
+ uploaded_file = st.file_uploader("Upload a 3D model (OBJ or GLB)", type=['obj', 'glb'])
147
+
148
+ if uploaded_file is not None:
149
+ # Get the file extension
150
+ file_extension = uploaded_file.name.split('.')[-1].lower()
151
+
152
+ # Encode the file
153
+ encoded_file = base64.b64encode(uploaded_file.read()).decode()
154
+
155
+ # Create a data URL
156
+ data_url = f"data:application/octet-stream;base64,{encoded_file}"
157
+
158
+ # JavaScript to load the model
159
+ load_model_js = f"""
160
+ <script>
161
+ loadModel("{data_url}", "{file_extension}");
162
  </script>
163
+ """
164
+ st.components.v1.html(load_model_js, height=0)
165
+ st.success(f"Loaded {uploaded_file.name}")
166
 
167
+ st.sidebar.title("Navigation")
168
+ st.sidebar.info("Use the sections above to interact with different components of the app.")