minors changes
This commit is contained in:
46
src/pong.ts
46
src/pong.ts
@@ -1,4 +1,10 @@
|
|||||||
|
|
||||||
|
/* Keys
|
||||||
|
Player 1: W/S
|
||||||
|
Player 2: Up/Down
|
||||||
|
Grid On-Off: G
|
||||||
|
*/
|
||||||
|
|
||||||
// import {component, score, line} from "./class.js";
|
// import {component, score, line} from "./class.js";
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
@@ -42,48 +48,50 @@ function startGame()
|
|||||||
const playerSpeed = Math.floor(w/75);
|
const playerSpeed = Math.floor(w/75);
|
||||||
const ballSpeed = Math.floor(w/75);
|
const ballSpeed = Math.floor(w/75);
|
||||||
|
|
||||||
let pos = new VectorInteger();
|
let pos = new VectorInteger;
|
||||||
// Component
|
// Component
|
||||||
pos.x = 0; pos.y = 0;
|
pos.assign(0, 0);
|
||||||
wall_top = new Rectangle(pos, "grey", w, wallSize);
|
wall_top = new Rectangle(pos, "grey", w, wallSize);
|
||||||
pos.x = 0; pos.y = h-wallSize;
|
pos.assign(0, h-wallSize);
|
||||||
wall_bottom = new Rectangle(pos, "grey", w, wallSize);
|
wall_bottom = new Rectangle(pos, "grey", w, wallSize);
|
||||||
|
|
||||||
pos.x = 0+pw; pos.y = h_mid-ph/2;
|
pos.assign(0+pw, h_mid-ph/2);
|
||||||
player1 = new Player(pos, "white", pw, ph);
|
player1 = new Player(pos, "white", pw, ph);
|
||||||
pos.x = w-pw-pw; pos.y = h_mid-ph/2;
|
pos.assign(w-pw-pw, h_mid-ph/2);
|
||||||
player2 = new Player(pos, "white", pw, ph);
|
player2 = new Player(pos, "white", pw, ph);
|
||||||
player1.speed = playerSpeed;
|
player1.speed = playerSpeed;
|
||||||
player2.speed = playerSpeed;
|
player2.speed = playerSpeed;
|
||||||
|
|
||||||
pos.x = w_mid-ballSize/2; pos.y = h_mid-ballSize/2;
|
pos.assign(w_mid-ballSize/2, h_mid-ballSize/2);
|
||||||
ball = new Ball(pos, "white", ballSize);
|
ball = new Ball(pos, "white", ballSize);
|
||||||
ball.speed = ballSpeed;
|
ball.speed = ballSpeed;
|
||||||
ball.dir.x = -0.8;
|
ball.dir.x = -0.8;
|
||||||
ball.dir.y = +0.2;
|
ball.dir.y = +0.2;
|
||||||
|
|
||||||
pos.x = w_mid-w/8; pos.y = w/12;
|
// w = w/8
|
||||||
|
// h = w/12
|
||||||
|
pos.assign(w_mid-scoreSize*1.6, scoreSize*1.5);
|
||||||
score1 = new TextNumericValue(pos, "white", scoreSize);
|
score1 = new TextNumericValue(pos, "white", scoreSize);
|
||||||
pos.x = w_mid+w/8-scoreSize/2; pos.y = w/12;
|
pos.assign(w_mid+scoreSize*1.1, scoreSize*1.5);
|
||||||
score2 = new TextNumericValue(pos, "white", scoreSize);
|
score2 = new TextNumericValue(pos, "white", scoreSize);
|
||||||
score1.value = 0;
|
score1.value = 0;
|
||||||
score2.value = 0;
|
score2.value = 0;
|
||||||
|
|
||||||
pos.x = w_mid-midLineSize/2; pos.y = 0+wallSize;
|
pos.assign(w_mid-midLineSize/2, 0+wallSize);
|
||||||
midLine = new Line(pos, "white", midLineSize, h-wallSize*2, 15);
|
midLine = new Line(pos, "white", midLineSize, h-wallSize*2, 15);
|
||||||
|
|
||||||
// Grid
|
// Grid
|
||||||
pos.x = 0; pos.y = h_mid;
|
pos.assign(0, h_mid);
|
||||||
w_grid_mid = new Rectangle(pos, "darkgreen", w, gridSize);
|
w_grid_mid = new Rectangle(pos, "darkgreen", w, gridSize);
|
||||||
pos.x = 0; pos.y = h/4;
|
pos.assign(0, h/4);
|
||||||
w_grid_u1 = new Rectangle(pos, "darkgreen", w, gridSize);
|
w_grid_u1 = new Rectangle(pos, "darkgreen", w, gridSize);
|
||||||
pos.x = 0; pos.y = h-h/4;
|
pos.assign(0, h-h/4);
|
||||||
w_grid_d1 = new Rectangle(pos, "darkgreen", w, gridSize);
|
w_grid_d1 = new Rectangle(pos, "darkgreen", w, gridSize);
|
||||||
pos.x = w_mid; pos.y = 0;
|
pos.assign(w_mid, 0);
|
||||||
h_grid_mid = new Rectangle(pos, "darkgreen", gridSize, h);
|
h_grid_mid = new Rectangle(pos, "darkgreen", gridSize, h);
|
||||||
pos.x = w/4; pos.y = 0;
|
pos.assign(w/4, 0);
|
||||||
h_grid_u1 = new Rectangle(pos, "darkgreen", gridSize, h);
|
h_grid_u1 = new Rectangle(pos, "darkgreen", gridSize, h);
|
||||||
pos.x = w-w/4; pos.y = 0;
|
pos.assign(w-w/4, 0);
|
||||||
h_grid_d1 = new Rectangle(pos, "darkgreen", gridSize, h);
|
h_grid_d1 = new Rectangle(pos, "darkgreen", gridSize, h);
|
||||||
|
|
||||||
score1.update(); // first Text draw init the custom font (graphic leftover ortherwise) (a better solution ?)
|
score1.update(); // first Text draw init the custom font (graphic leftover ortherwise) (a better solution ?)
|
||||||
@@ -284,6 +292,10 @@ class Vector {
|
|||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
assign(x: number, y: number) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VectorInteger extends Vector {
|
class VectorInteger extends Vector {
|
||||||
@@ -377,7 +389,7 @@ interface Moving {
|
|||||||
|
|
||||||
|
|
||||||
class MovingRectangle extends Rectangle implements Moving {
|
class MovingRectangle extends Rectangle implements Moving {
|
||||||
dir: Vector = {x: 0.0, y: 0.0};
|
dir: Vector = new Vector(0,0);
|
||||||
speed: number = 1;
|
speed: number = 1;
|
||||||
constructor(pos: VectorInteger, color: string, width: number, height: number) {
|
constructor(pos: VectorInteger, color: string, width: number, height: number) {
|
||||||
super(pos, color, width, height);
|
super(pos, color, width, height);
|
||||||
@@ -515,7 +527,7 @@ class Line extends Rectangle {
|
|||||||
update() {
|
update() {
|
||||||
let ctx = pong.ctx;
|
let ctx = pong.ctx;
|
||||||
ctx.fillStyle = this.color;
|
ctx.fillStyle = this.color;
|
||||||
let pos: Vector = {x: 0, y: 0};
|
let pos: VectorInteger = new VectorInteger;
|
||||||
let i = 0;
|
let i = 0;
|
||||||
while (i < this.segmentCount)
|
while (i < this.segmentCount)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user