Collision
This commit is contained in:
52
app.py
52
app.py
@@ -4,13 +4,32 @@ from tokenize import String
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_cors import CORS
|
||||
|
||||
player1 = {"name": "Player 1", "xpos": 1, "ypos": 1}
|
||||
player1 = {"name": "Player 1", "xpos": 1, "ypos": 1, "xvel": 0, "yvel": 0}
|
||||
|
||||
map1 = {
|
||||
"platforms": [
|
||||
{"x": 30, "y": 100, "width": 100, "height": 100},
|
||||
{"x": 0, "y": 260, "width": 400, "height": 10}
|
||||
]
|
||||
}
|
||||
|
||||
data = {}
|
||||
|
||||
def checkCollision():
|
||||
global data
|
||||
collision = False
|
||||
for i in map1['platforms']:
|
||||
if ((player1['ypos']+data['spriteData']['ufo']['height']/2 >= i['y']) & (player1['ypos']-data['spriteData']['ufo']['height']/2 <= i['y']+i['height']) & (player1['xpos']+data['spriteData']['ufo']['height']/2 >= i['x']) & (player1['xpos']-data['spriteData']['ufo']['width']/2 <= i['x']+i['width'])):
|
||||
collision = True
|
||||
|
||||
return(collision)
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
@app.route('/api/send', methods=['POST'])
|
||||
|
||||
def receive_data():
|
||||
global data
|
||||
data = request.get_json()
|
||||
# Input data is the form [reset(bool), isPressed(json)]
|
||||
|
||||
@@ -18,18 +37,43 @@ def receive_data():
|
||||
if (data['reset']):
|
||||
player1['xpos'] = 0
|
||||
player1['ypos'] = 0
|
||||
player1['xvel'] = 0
|
||||
player1['yvel'] = 0
|
||||
|
||||
# Handle key presses
|
||||
if (data['keyDown']['ArrowUp'] == True):
|
||||
player1['ypos'] -= 1
|
||||
if (data['keyDown']['ArrowDown'] == True):
|
||||
player1['ypos'] += 1
|
||||
if (checkCollision()):
|
||||
player1['ypos'] -= 1
|
||||
player1['yvel'] = 2
|
||||
else:
|
||||
player1['ypos'] -= 1
|
||||
if (data['keyDown']['ArrowDown'] == True):
|
||||
print('LOW LOW LOW LOW LOW LOW LOW LOW')
|
||||
if (data['keyDown']['ArrowRight'] == True):
|
||||
player1['xpos'] += 1
|
||||
if (checkCollision()):
|
||||
player1['xpos'] -= 1;
|
||||
if (data['keyDown']['ArrowLeft'] == True):
|
||||
player1['xpos'] -= 1
|
||||
if (checkCollision()):
|
||||
player1['xpos'] += 1;
|
||||
|
||||
return jsonify(player1)
|
||||
# Velocity
|
||||
player1['xpos'] += player1['xvel']
|
||||
player1['ypos'] -= player1['yvel']
|
||||
|
||||
if (checkCollision()):
|
||||
player1['ypos'] += player1['yvel']
|
||||
player1['yvel'] = 0
|
||||
|
||||
# Handle Gravity
|
||||
player1['yvel'] -= 0.05
|
||||
|
||||
return jsonify({
|
||||
"player1": player1,
|
||||
"map1": map1,
|
||||
})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user