Created
November 15, 2018 12:06
-
-
Save dinarname/e8c9f840ae56b20ff44edbaf05fd2ad9 to your computer and use it in GitHub Desktop.
Using object's variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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