Skip to content

Instantly share code, notes, and snippets.

@dinarname
Created November 15, 2018 12:06
Show Gist options
  • Save dinarname/e8c9f840ae56b20ff44edbaf05fd2ad9 to your computer and use it in GitHub Desktop.
Save dinarname/e8c9f840ae56b20ff44edbaf05fd2ad9 to your computer and use it in GitHub Desktop.
Using object's variables
Packman p1;
Meal m1;
float distance;
void setup() {
size(400, 400);
p1 = new Packman();
m1 = new Meal();
}
void draw() {
background(255);
p1.display();
p1.control();
m1.display();
distance = dist(p1.x, p1.y, m1.x, m1.y);
if (distance <= p1.size / 2 + m1.size / 2) {
m1 = new Meal();
p1.size = p1.size + 10;
}
}
class Packman {
float x, y, size, speed;
Packman() {
x = random(0, width);
y = random(0, height);
size = 20;
speed = 3;
}
void display() {
rectMode(CENTER);
fill(#FDFF90);
rect(x, y, size, size);
}
void control() {
if (keyPressed) {
if (key == 'd') {
x = x + speed;
} else if (key =='a') {
x = x - speed;
} else if (key =='s') {
y = y + speed;
} else if (key =='w') {
y = y - speed;
}
}
// Отзеркалниание персонажа
if (x > width) {
x = 0;
} else if (x < 0) {
x = width;
}
if (y > height) {
y = 0;
} else if (y < 0) {
y = height;
}
}
}
class Meal {
float x, y, size;
Meal() {
x = random(0, width);
y = random(0, height);
size = 20;
}
void display() {
rectMode(CENTER);
fill(#B3E3A0);
rect(x, y, size, size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment