|
1 | 1 | from flask import Flask, render_template_string |
2 | | -import random |
3 | 2 |
|
4 | 3 | app = Flask(__name__) |
5 | 4 |
|
6 | 5 | HTML_PAGE = """ |
7 | 6 | <!DOCTYPE html> |
8 | 7 | <html lang="en"> |
9 | 8 | <head> |
10 | | - <meta charset="UTF-8"> |
11 | | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
12 | | - <title>Rock Paper Scissors - Azure Demo</title> |
13 | | - <style> |
14 | | - body { |
15 | | - font-family: Arial, sans-serif; |
16 | | - background: linear-gradient(135deg, #667eea, #764ba2); |
17 | | - height: 100vh; |
18 | | - display: flex; |
19 | | - justify-content: center; |
20 | | - align-items: center; |
21 | | - margin: 0; |
22 | | - color: #fff; |
23 | | - } |
24 | | - .game { |
25 | | - text-align: center; |
26 | | - background: rgba(0,0,0,0.5); |
27 | | - padding: 30px; |
28 | | - border-radius: 15px; |
29 | | - box-shadow: 0 8px 20px rgba(0,0,0,0.3); |
30 | | - } |
31 | | - h1 { |
32 | | - margin-bottom: 20px; |
33 | | - } |
34 | | - .choices button { |
35 | | - margin: 10px; |
36 | | - padding: 10px 20px; |
37 | | - border: none; |
38 | | - background: #ff9800; |
39 | | - color: white; |
40 | | - border-radius: 8px; |
41 | | - cursor: pointer; |
42 | | - font-size: 16px; |
43 | | - transition: 0.3s; |
44 | | - } |
45 | | - .choices button:hover { |
46 | | - background: #e68900; |
47 | | - } |
48 | | - #result { |
49 | | - margin-top: 20px; |
50 | | - font-size: 20px; |
51 | | - font-weight: bold; |
52 | | - } |
53 | | - </style> |
| 9 | + <meta charset="UTF-8"> |
| 10 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 11 | + <title>Emoji Catch Game - Azure Demo</title> |
| 12 | + <style> |
| 13 | + body { |
| 14 | + margin: 0; |
| 15 | + display: flex; |
| 16 | + justify-content: center; |
| 17 | + align-items: center; |
| 18 | + height: 100vh; |
| 19 | + background: linear-gradient(135deg, #ff9966, #ff5e62); |
| 20 | + font-family: Arial, sans-serif; |
| 21 | + } |
| 22 | + canvas { |
| 23 | + background: #fff; |
| 24 | + border-radius: 10px; |
| 25 | + box-shadow: 0 6px 20px rgba(0,0,0,0.3); |
| 26 | + } |
| 27 | + #score { |
| 28 | + position: absolute; |
| 29 | + top: 20px; |
| 30 | + left: 50%; |
| 31 | + transform: translateX(-50%); |
| 32 | + font-size: 24px; |
| 33 | + color: white; |
| 34 | + font-weight: bold; |
| 35 | + } |
| 36 | + </style> |
54 | 37 | </head> |
55 | 38 | <body> |
56 | | - <div class="game"> |
57 | | - <h1>✊ ✋ ✌ Rock Paper Scissors</h1> |
58 | | - <p>Choose one:</p> |
59 | | - <div class="choices"> |
60 | | - <button onclick="play('rock')">Rock</button> |
61 | | - <button onclick="play('paper')">Paper</button> |
62 | | - <button onclick="play('scissors')">Scissors</button> |
63 | | - </div> |
64 | | - <div id="result"></div> |
65 | | - </div> |
| 39 | + <div id="score">Score: 0</div> |
| 40 | + <canvas id="gameCanvas" width="400" height="600"></canvas> |
| 41 | +
|
| 42 | + <script> |
| 43 | + const canvas = document.getElementById("gameCanvas"); |
| 44 | + const ctx = canvas.getContext("2d"); |
| 45 | +
|
| 46 | + let basket = { x: 160, y: 550, width: 80, height: 20 }; |
| 47 | + let emojis = ["🍕","🎩","🚀","🐱","🍔","⚡","🌈","🎮"]; |
| 48 | + let falling = []; |
| 49 | + let score = 0; |
66 | 50 |
|
67 | | - <script> |
68 | | - function play(choice) { |
69 | | - fetch(`/play?choice=${choice}`) |
70 | | - .then(response => response.json()) |
71 | | - .then(data => { |
72 | | - document.getElementById("result").innerHTML = |
73 | | - "You chose: " + data.user + "<br>" + |
74 | | - "Computer chose: " + data.computer + "<br><br>" + |
75 | | - "<b>" + data.result + "</b>"; |
76 | | - }); |
| 51 | + function drawBasket() { |
| 52 | + ctx.fillStyle = "#0078D4"; |
| 53 | + ctx.fillRect(basket.x, basket.y, basket.width, basket.height); |
| 54 | + } |
| 55 | +
|
| 56 | + function drawEmojis() { |
| 57 | + ctx.font = "28px Arial"; |
| 58 | + falling.forEach(e => { |
| 59 | + ctx.fillText(e.emoji, e.x, e.y); |
| 60 | + }); |
| 61 | + } |
| 62 | +
|
| 63 | + function updateEmojis() { |
| 64 | + falling.forEach(e => e.y += 3); |
| 65 | + falling = falling.filter(e => { |
| 66 | + if (e.y > basket.y && e.x > basket.x && e.x < basket.x + basket.width) { |
| 67 | + score += 10; |
| 68 | + document.getElementById("score").innerText = "Score: " + score; |
| 69 | + return false; |
77 | 70 | } |
78 | | - </script> |
| 71 | + return e.y < 600; |
| 72 | + }); |
| 73 | + if (Math.random() < 0.03) { |
| 74 | + falling.push({ emoji: emojis[Math.floor(Math.random()*emojis.length)], x: Math.random()*370, y: 0 }); |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + function gameLoop() { |
| 79 | + ctx.clearRect(0, 0, 400, 600); |
| 80 | + drawBasket(); |
| 81 | + drawEmojis(); |
| 82 | + updateEmojis(); |
| 83 | + requestAnimationFrame(gameLoop); |
| 84 | + } |
| 85 | +
|
| 86 | + document.addEventListener("keydown", (e) => { |
| 87 | + if (e.key === "ArrowLeft" && basket.x > 0) basket.x -= 20; |
| 88 | + if (e.key === "ArrowRight" && basket.x < 320) basket.x += 20; |
| 89 | + }); |
| 90 | +
|
| 91 | + gameLoop(); |
| 92 | + </script> |
79 | 93 | </body> |
80 | 94 | </html> |
81 | 95 | """ |
|
84 | 98 | def home(): |
85 | 99 | return render_template_string(HTML_PAGE) |
86 | 100 |
|
87 | | -@app.route("/play") |
88 | | -def play(): |
89 | | - from flask import request, jsonify |
90 | | - user_choice = request.args.get("choice") |
91 | | - choices = ["rock", "paper", "scissors"] |
92 | | - comp_choice = random.choice(choices) |
93 | | - |
94 | | - if user_choice == comp_choice: |
95 | | - result = "It's a Tie!" |
96 | | - elif (user_choice == "rock" and comp_choice == "scissors") or \ |
97 | | - (user_choice == "paper" and comp_choice == "rock") or \ |
98 | | - (user_choice == "scissors" and comp_choice == "paper"): |
99 | | - result = "🎉 You Win!" |
100 | | - else: |
101 | | - result = "😢 You Lose!" |
102 | | - |
103 | | - return jsonify({"user": user_choice, "computer": comp_choice, "result": result}) |
104 | | - |
105 | 101 | if __name__ == "__main__": |
106 | 102 | app.run(host="0.0.0.0", port=8000) |
107 | 103 |
|
0 commit comments