New direction for Flask Web App
- 2 minsShift
Going to change the path and make a true web app. Before, I was making a basic static website where as now, I am aimed to make a simple rock, paper, scissors game vs a computer.
First version of backend
from flask import Flask, jsonify, request
import random
app = Flask(__name__)
@app.route('/play', methods=['POST'])
def play():
moves = ['rock', 'paper', 'scissors']
user_move = request.json.get('move')
if user_move not in moves:
return jsonify({'error': 'Invalid move'}), 400
computer_move = random.choice(moves)
if user_move == computer_move:
result = 'draw'
elif (user_move == 'rock' and computer_move == 'scissors') or \
(user_move == 'paper' and computer_move == 'rock') or \
(user_move == 'scissors' and computer_move == 'paper'):
result = 'win'
else:
result = 'lose'
return jsonify({'user_move': user_move, 'computer_move': computer_move, 'result': result})
if __name__ == '__main__':
app.run(debug=True)
Steps to test backend
Run the app with
python3 app.py
Test with cURL
curl -X POST -H "Content-Type: application/json" -d '{"move": "rock"}' http://localhost:5000/play
Future
Going to add some more steps later as well.
Signing Off
Thank you all.
Best regards, Tyler
Connect with me on YouTube or just consider subscribing! Follow my Persevus channel for more game dev updates as well! Thanks!
Check out my YouTube!