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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Aug 2, 2024
2 parents 2c5d187 + 570625f commit 0cd9c80
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 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
}
43 changes: 43 additions & 0 deletions MovingSquare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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);

input.onButtonPressed(Button.A, () => movingSquare.onButtonAPressed());
input.onButtonPressed(Button.B, () => movingSquare.onButtonBPressed());
}

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);
}
}
51 changes: 0 additions & 51 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1 @@
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());
input.onButtonPressed(Button.B, () => movingSquare.onButtonBPressed());
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 0cd9c80

Please sign in to comment.