Omnibus commited on
Commit
a553ce2
1 Parent(s): c278732

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -0
app.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ load_js=""" <script src="//cdn.jsdelivr.net/npm/phaser@3.11.0/dist/phaser.js"></script>
4
+ """
5
+
6
+
7
+ game_js="""
8
+ var config = {
9
+ type: Phaser.AUTO,
10
+ width: 800,
11
+ height: 600,
12
+ physics: {
13
+ default: 'arcade',
14
+ arcade: {
15
+ gravity: { y: 300 },
16
+ debug: false
17
+ }
18
+ },
19
+ scene: {
20
+ preload: preload,
21
+ create: create,
22
+ update: update
23
+ }
24
+ };
25
+
26
+ var player;
27
+ var stars;
28
+ var bombs;
29
+ var platforms;
30
+ var cursors;
31
+ var score = 0;
32
+ var gameOver = false;
33
+ var scoreText;
34
+
35
+ var game = new Phaser.Game(config);
36
+
37
+ function preload ()
38
+ {
39
+ this.load.image('sky', 'assets/sky.png');
40
+ this.load.image('ground', 'assets/platform.png');
41
+ this.load.image('star', 'assets/star.png');
42
+ this.load.image('bomb', 'assets/bomb.png');
43
+ this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });
44
+ }
45
+
46
+ function create ()
47
+ {
48
+ // A simple background for our game
49
+ this.add.image(400, 300, 'sky');
50
+
51
+ // The platforms group contains the ground and the 2 ledges we can jump on
52
+ platforms = this.physics.add.staticGroup();
53
+
54
+ // Here we create the ground.
55
+ // Scale it to fit the width of the game (the original sprite is 400x32 in size)
56
+ platforms.create(400, 568, 'ground').setScale(2).refreshBody();
57
+
58
+ // Now let's create some ledges
59
+ platforms.create(600, 400, 'ground');
60
+ platforms.create(50, 250, 'ground');
61
+ platforms.create(750, 220, 'ground');
62
+
63
+ // The player and its settings
64
+ player = this.physics.add.sprite(100, 450, 'dude');
65
+
66
+ // Player physics properties. Give the little guy a slight bounce.
67
+ player.setBounce(0.2);
68
+ player.setCollideWorldBounds(true);
69
+
70
+ // Our player animations, turning, walking left and walking right.
71
+ this.anims.create({
72
+ key: 'left',
73
+ frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
74
+ frameRate: 10,
75
+ repeat: -1
76
+ });
77
+
78
+ this.anims.create({
79
+ key: 'turn',
80
+ frames: [ { key: 'dude', frame: 4 } ],
81
+ frameRate: 20
82
+ });
83
+
84
+ this.anims.create({
85
+ key: 'right',
86
+ frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
87
+ frameRate: 10,
88
+ repeat: -1
89
+ });
90
+
91
+ // Input Events
92
+ cursors = this.input.keyboard.createCursorKeys();
93
+
94
+ // Some stars to collect, 12 in total, evenly spaced 70 pixels apart along the x axis
95
+ stars = this.physics.add.group({
96
+ key: 'star',
97
+ repeat: 11,
98
+ setXY: { x: 12, y: 0, stepX: 70 }
99
+ });
100
+
101
+ stars.children.iterate(function (child) {
102
+
103
+ // Give each star a slightly different bounce
104
+ child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
105
+
106
+ });
107
+
108
+ bombs = this.physics.add.group();
109
+
110
+ // The score
111
+ scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });
112
+
113
+ // Collide the player and the stars with the platforms
114
+ this.physics.add.collider(player, platforms);
115
+ this.physics.add.collider(stars, platforms);
116
+ this.physics.add.collider(bombs, platforms);
117
+
118
+ // Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
119
+ this.physics.add.overlap(player, stars, collectStar, null, this);
120
+
121
+ this.physics.add.collider(player, bombs, hitBomb, null, this);
122
+ }
123
+
124
+ function update ()
125
+ {
126
+ if (gameOver)
127
+ {
128
+ return;
129
+ }
130
+
131
+ if (cursors.left.isDown)
132
+ {
133
+ player.setVelocityX(-160);
134
+
135
+ player.anims.play('left', true);
136
+ }
137
+ else if (cursors.right.isDown)
138
+ {
139
+ player.setVelocityX(160);
140
+
141
+ player.anims.play('right', true);
142
+ }
143
+ else
144
+ {
145
+ player.setVelocityX(0);
146
+
147
+ player.anims.play('turn');
148
+ }
149
+
150
+ if (cursors.up.isDown && player.body.touching.down)
151
+ {
152
+ player.setVelocityY(-330);
153
+ }
154
+ }
155
+
156
+ function collectStar (player, star)
157
+ {
158
+ star.disableBody(true, true);
159
+
160
+ // Add and update the score
161
+ score += 10;
162
+ scoreText.setText('Score: ' + score);
163
+
164
+ if (stars.countActive(true) === 0)
165
+ {
166
+ // A new batch of stars to collect
167
+ stars.children.iterate(function (child) {
168
+
169
+ child.enableBody(true, child.x, 0, true, true);
170
+
171
+ });
172
+
173
+ var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
174
+
175
+ var bomb = bombs.create(x, 16, 'bomb');
176
+ bomb.setBounce(1);
177
+ bomb.setCollideWorldBounds(true);
178
+ bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
179
+ bomb.allowGravity = false;
180
+
181
+ }
182
+ }
183
+
184
+ function hitBomb (player, bomb)
185
+ {
186
+ this.physics.pause();
187
+
188
+ player.setTint(0xff0000);
189
+
190
+ player.anims.play('turn');
191
+
192
+ gameOver = true;
193
+ }
194
+
195
+ """
196
+
197
+ with gr.Blocks() as app:
198
+ gr.HTML("""
199
+ <!doctype html>
200
+ <html lang="en">
201
+ <head>
202
+ <meta charset="UTF-8" />
203
+ <title>Game Test</title>
204
+ <style type="text/css">
205
+ body {
206
+ margin: 0;
207
+ }
208
+ </style>
209
+ </head>
210
+ <body>
211
+ </body>
212
+ </html>""")
213
+
214
+ app.load(None,None,None,_js=load_js)
215
+ app.load(None,None,None,_js=game_js)
216
+ app.launch()