Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Organise code
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Aug 2, 2024
1 parent 66a8f7b commit 8fe60a6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
6 changes: 6 additions & 0 deletions MovementDirection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum MovementDirection {
UP = 0,
RIGHT = 1,
DOWN = 2,
LEFT = 3
}
40 changes: 40 additions & 0 deletions MovingSquare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class MovingSquare {

private movementDirection: MovementDirection = MovementDirection.UP;

private x: number;
private y: number;

constructor(startingX: number, startingY: number) {
this.x = startingX;
this.y = startingY;

this.move(this.x, this.y);
}

private redraw() {
basic.clearScreen();
led.plot(this.x, this.y);
}

private move(x: number, y: number) {
this.x = x;
this.y = y;

this.redraw();
}

public onButtonAPressed() {
this.movementDirection = (this.movementDirection + 1) % 4
}

public onButtonBPressed() {
const angleDegrees = 90.0 - this.movementDirection * 90;
const angleRadians = angleDegrees * Math.PI / 180.0;

const xMovement = Math.round(Math.cos(angleRadians));
const yMovement = Math.round(Math.sin(angleRadians));

this.move(this.x + xMovement, this.y + yMovement);
}
}
48 changes: 0 additions & 48 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,3 @@
enum MovementDirection {
UP = 0,
RIGHT = 1,
DOWN = 2,
LEFT = 3
}

class MovingSquare {

private movementDirection: MovementDirection = MovementDirection.UP;

private x: number;
private y: number;

constructor(startingX: number, startingY: number) {
this.x = startingX;
this.y = startingY;

this.move(this.x, this.y);
}

private redraw() {
basic.clearScreen();
led.plot(this.x, this.y);
}

private move(x: number, y: number) {
this.x = x;
this.y = y;

this.redraw();
}

public onButtonAPressed() {
this.movementDirection = (this.movementDirection + 1) % 4
}

public onButtonBPressed() {
const angleDegrees = 90.0 - this.movementDirection * 90;
const angleRadians = angleDegrees * Math.PI / 180.0;

const xMovement = Math.round(Math.cos(angleRadians));
const yMovement = Math.round(Math.sin(angleRadians));

this.move(this.x + xMovement, this.y + yMovement);
}
}

const movingSquare = new MovingSquare(2, 2);

input.onButtonPressed(Button.A, () => movingSquare.onButtonAPressed());
Expand Down
4 changes: 3 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
},
"files": [
"main.ts",
"README.md"
"README.md",
"MovementDirection.ts",
"MovingSquare.ts"
],
"testFiles": [
"test.ts"
Expand Down

0 comments on commit 8fe60a6

Please sign in to comment.