Start at the center of the sketch
in
Programming Questions
•
10 months ago
Hi,
I'm trying to make a simple class working. A 1px rectangle should be drwan at the center of the sketch but it starts from 0, 0. What's wrong with my code?
I'm trying to make a simple class working. A 1px rectangle should be drwan at the center of the sketch but it starts from 0, 0. What's wrong with my code?
- class Drunkrect {
- int x;
- int y;
- Drunkrect () {
- x = width / 2;
- y = height / 2;
- }
- void display() {
- fill (0);
- rect (x, y, 1, 1);
- }
- void step() {
- int choice = int (random (4));
- if (choice == 0) {
- x++;
- }
- else if (choice == 1) {
- x--;
- }
- else if (choice == 2) {
- y++;
- }
- else if (choice == 3) {
- y--;
- }
- }
- }
- Drunkrect w = new Drunkrect();
- void setup () {
- size (800, 800);
- background (255);
- }
- void draw () {
- w.display();
- w.step();
- }
1