This commit is contained in:
2026-02-24 21:08:46 -06:00
parent f87854fca1
commit 3d87fdeb5f
23 changed files with 1551 additions and 1086 deletions

View File

@@ -1,86 +1,127 @@
// main.js
var draw = use('draw2d')
var config = use('config')
var color = use('color')
var random = use('random')
var core = use('core')
var camera = use('camera')
var compositor = use('compositor')
var input = use('input')
var shape = use('shape2d')
var text2d = use('text2d')
prosperon.camera.transform.pos = [0,0]
var paddleW = 10, paddleH = 80
var p1 = {x: 30, y: config.height*0.5, speed: 300}
var p2 = {x: config.width-30, y: config.height*0.5, speed: 300}
var ball = {x: 0, y: 0, vx: 220, vy: 150, size: 10}
var GW = 480, GH = 270
var paddleW = 8, paddleH = 50, speed = 200
var ballSize = 8
var bvx = 220, bvy = 150
var score1 = 0, score2 = 0
function resetBall() {
ball.x = config.width*0.5
ball.y = config.height*0.5
// give it a random vertical bounce
ball.vy = (random.random()<0.5 ? -1:1)*150
// keep horizontal speed to the same magnitude
ball.vx = ball.vx>0 ? 220 : -220
// Cameras: game at pixel-art res, HUD at native
var game_cam = camera.make({width: GW, height: GH, pos: {x: GW / 2, y: GH / 2}})
var hud_cam = camera.make({width: 960, height: 540, pos: {x: 480, y: 270}})
// Action mapping
input.configure({
action_map: {
p1_up: ['w'],
p1_down: ['s'],
p2_up: ['up'],
p2_down: ['down']
}
})
// Retained shapes — game plane
var midline = shape.rect({
pos: {x: GW / 2, y: GH / 2}, width: 2, height: GH,
fill: {r: 0.3, g: 0.3, b: 0.3, a: 1}, plane: 'game'
})
var p1 = shape.rect({
pos: {x: 20, y: GH / 2}, width: paddleW, height: paddleH,
fill: {r: 1, g: 1, b: 1, a: 1}, plane: 'game'
})
var p2 = shape.rect({
pos: {x: GW - 20, y: GH / 2}, width: paddleW, height: paddleH,
fill: {r: 1, g: 1, b: 1, a: 1}, plane: 'game'
})
var ball = shape.rect({
pos: {x: GW / 2, y: GH / 2}, width: ballSize, height: ballSize,
fill: {r: 1, g: 1, b: 1, a: 1}, plane: 'game', groups: ['glow']
})
// HUD plane — score text
var score_label = text2d({
text: "0 0", pos: {x: 420, y: 490},
plane: 'hud', size: 32, color: {r: 1, g: 1, b: 1, a: 1}
})
function reset_ball() {
ball.pos.x = GW / 2
ball.pos.y = GH / 2
bvy = (bvy > 0 ? -1 : 1) * 150
bvx = bvx > 0 ? -220 : 220
}
resetBall()
var update = function(dt) {
// Move paddles: positive Y is up, so W/↑ means p.y += speed
if (input.keyboard.down('w')) p1.y += p1.speed*dt
if (input.keyboard.down('s')) p1.y -= p1.speed*dt
// Paddle 2 movement (ArrowUp = up, ArrowDown = down)
if (input.keyboard.down('i')) p2.y += p2.speed*dt
if (input.keyboard.down('k')) p2.y -= p2.speed*dt
// Clamp paddles to screen
if (p1.y < paddleH*0.5) p1.y = paddleH*0.5
if (p1.y > config.height - paddleH*0.5) p1.y = config.height - paddleH*0.5
if (p2.y < paddleH*0.5) p2.y = paddleH*0.5
if (p2.y > config.height - paddleH*0.5) p2.y = config.height - paddleH*0.5
// Move ball
ball.x += ball.vx*dt
ball.y += ball.vy*dt
// Bounce top/bottom
if (ball.y+ball.size*0.5>config.height || ball.y-ball.size*0.5<0) ball.vy = -ball.vy
// Check paddle collisions
// p1 bounding box
var left1 = p1.x - paddleW*0.5, right1 = p1.x + paddleW*0.5
var top1 = p1.y + paddleH*0.5, bottom1 = p1.y - paddleH*0.5
// p2 bounding box
var left2 = p2.x - paddleW*0.5, right2 = p2.x + paddleW*0.5
var top2 = p2.y + paddleH*0.5, bottom2 = p2.y - paddleH*0.5
// ball half-edges
var l = ball.x - ball.size*0.5, r = ball.x + ball.size*0.5
var b = ball.y - ball.size*0.5, t = ball.y + ball.size*0.5
// Collide with paddle 1?
if (r>left1 && l<right1 && t>bottom1 && b<top1)
ball.vx = abs(ball.vx)
// Collide with paddle 2?
if (r>left2 && l<right2 && t>bottom2 && b<top2)
ball.vx = -abs(ball.vx)
// Check left/right out-of-bounds
if (r<0) { score2++; resetBall() }
if (l>config.width) { score1++; resetBall() }
// Compositor: game plane with bloom on ball, HUD overlay
var comp_config = {
clear: {r: 0, g: 0, b: 0, a: 1},
planes: [
{name: 'game', camera: game_cam, resolution: {width: GW, height: GH}, presentation: 'letterbox'},
{name: 'hud', camera: hud_cam, resolution: {width: 960, height: 540}, presentation: 'stretch'}
],
group_effects: {
glow: {effects: [{type: 'bloom', threshold: 0.3, intensity: 2}]}
}
}
var hud = function() {
// Clear screen black
draw.rectangle({x:0, y:0, width:config.width, height:config.height}, [0,0,0,1])
core.start({
width: 960, height: 540, title: "Pong",
// Draw paddles
draw.rectangle({x:p1.x - paddleW*0.5, y:p1.y - paddleH*0.5, width:paddleW, height:paddleH}, color.white)
draw.rectangle({x:p2.x - paddleW*0.5, y:p2.y - paddleH*0.5, width:paddleW, height:paddleH}, color.white)
update: function(dt) {
var down = input.player1().down()
// Draw ball
draw.rectangle({x:ball.x - ball.size*0.5, y:ball.y - ball.size*0.5, width:ball.size, height:ball.size}, color.white)
// Move paddles
if (down.p1_up) p1.pos.y += speed * dt
if (down.p1_down) p1.pos.y -= speed * dt
if (down.p2_up) p2.pos.y += speed * dt
if (down.p2_down) p2.pos.y -= speed * dt
// Simple score display
var msg = score1 + " " + score2
draw.text(msg, {x:0, y:10, width:config.width, height:40}, null, 0, color.white, 0)
}
// Clamp paddles
var hh = paddleH / 2
if (p1.pos.y < hh) p1.pos.y = hh
if (p1.pos.y > GH - hh) p1.pos.y = GH - hh
if (p2.pos.y < hh) p2.pos.y = hh
if (p2.pos.y > GH - hh) p2.pos.y = GH - hh
// Move ball
ball.pos.x += bvx * dt
ball.pos.y += bvy * dt
// Bounce top/bottom
var bs = ballSize / 2
if (ball.pos.y - bs < 0 || ball.pos.y + bs > GH) bvy = -bvy
// Paddle collisions
var bx = ball.pos.x, by = ball.pos.y
var pw = paddleW / 2, ph = paddleH / 2
if (bx - bs < p1.pos.x + pw && bx + bs > p1.pos.x - pw &&
by + bs > p1.pos.y - ph && by - bs < p1.pos.y + ph)
bvx = abs(bvx)
if (bx + bs > p2.pos.x - pw && bx - bs < p2.pos.x + pw &&
by + bs > p2.pos.y - ph && by - bs < p2.pos.y + ph)
bvx = -abs(bvx)
// Scoring
if (bx < 0) {
score2++
score_label.text = score1 + " " + score2
reset_ball()
}
if (bx > GW) {
score1++
score_label.text = score1 + " " + score2
reset_ball()
}
},
render: function() {
return compositor.execute(compositor.compile(comp_config))
}
})