From dcbdd23b24deee465e25ac93f07918c3ba9ebb7c Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Wed, 1 Oct 2025 18:25:31 +1000 Subject: [PATCH] Collision --- app.py | 52 ++++++++++++++++++++++++++++++++++++++++++++---- index.html | 2 ++ index.js | 15 ++++++++++---- sprites.js | 10 ++++++++++ sprites/ufo.png | Bin 0 -> 775 bytes 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 sprites.js create mode 100644 sprites/ufo.png diff --git a/app.py b/app.py index 2afff7c..884e104 100644 --- a/app.py +++ b/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) \ No newline at end of file diff --git a/index.html b/index.html index 7ed940c..d9257fb 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ + @@ -8,5 +9,6 @@ + \ No newline at end of file diff --git a/index.js b/index.js index 0669b1d..916a002 100644 --- a/index.js +++ b/index.js @@ -9,11 +9,18 @@ function sendData(data) { }) .then(res => res.json()) .then(data => { + // Clear Canvas ctx.clearRect(0, 0, 480, 270); - ctx.beginPath(); - ctx.arc(data.xpos, data.ypos, 10, 0, Math.PI * 2); - ctx.fill(); + + // Draw in Player 1 + ctx.drawImage(ufo, data.player1.xpos-spriteData.ufo.width/2, data.player1.ypos-spriteData.ufo.height/2) reset = false + + // Draw Hitboxes (For debugging) + let plat = data.map1.platforms + for (i = 0; i < plat.length; i++) { + ctx.fillRect(plat[i].x, plat[i].y, plat[i].width, plat[i].height) + } }) .catch(err => console.error('Error:', err)); } @@ -58,4 +65,4 @@ document.addEventListener('keyup', function (e) { // Start Game var reset = true -setInterval(() => sendData({reset, keyDown}), 16); \ No newline at end of file +setInterval(() => sendData({reset, keyDown, spriteData}), 16); \ No newline at end of file diff --git a/sprites.js b/sprites.js new file mode 100644 index 0000000..4ef38ce --- /dev/null +++ b/sprites.js @@ -0,0 +1,10 @@ +const ufo = new Image(); +ufo.src = "sprites/ufo.png"; + +const spriteData = { + "ufo": {} +}; +ufo.onload = function () { + spriteData.ufo.width = this.width; + spriteData.ufo.height = this.height; +} \ No newline at end of file diff --git a/sprites/ufo.png b/sprites/ufo.png new file mode 100644 index 0000000000000000000000000000000000000000..0d826a4794929d7323c4eecf2cde4e4f64365310 GIT binary patch literal 775 zcmeAS@N?(olHy`uVBq!ia0vp^8X(L8Bp6mseZCP$F(rAsyDxK|T%InDAr_~5r&#B|au8`r7n>E~A;x$~_2q&E zCl)U$=`N{%Gc_YNNNv#qh1f}Y4if~6w90}u@JpZA8@Y2s`F`WgcDtV!bl>kyt9fc~ z5b7|GGhjwALnWI?vV(Dv4V&%qQ*zA5Il9ix;!=3ImeZMOlfc|1kK(KQ3~uONkN419 zAGN_zh+)T98OBF%GpBA5o-%vW_X2KC0T#|jW(6j$`*yuw&g=C1QR=mmLENqvl1<)E zKB%$r+^H#-kN&tNw?gVh#sAs7s!S~m6F>hszqNFZ&xvHilWpG)G%}>N-(0f5e+K9N zsr+qbOC~ZTwl~|HskpFbt=p%hxUj`vR~ND;)Et`sZgSW9V*hvZ{yJ73y}K&EbC+qx z@6)nb3nJArWBLU%KP57GOp4hq?Y@tp(e3x{+!Z zfcMJNH2LX|H{UN&@nqZ*o+{Y7s3h=P&fQ&Cs|}Z5OS_nR^}B}6885l$&2wy>Ry8#3 zed|-1b2jt)u`p3qj+;q&udb!-JCXI$QbkGjd2w*z@|n*chwR()hgZE$qt)1yIrzzI z>-d=NNOSMC$6Gj zm4U%OgBl+c4Y~O#nQ4`{HT35m`2f_Q0k@$fGdH!kBr&%DQ;!MM5;^fVD}Z_!JYD@< J);T3K0RWOHK?(o> literal 0 HcmV?d00001