//defining classes
Player player1;
void setup() {
size(1200, 800);
player1 = new Player(width/2, 100, 60);
}
void draw() {
background(0);
stroke(255);
strokeWeight(3);
// draw players
fill(255);
noStroke();
player1.run();
}
void keyPressed() {
player1.pressed((key == ‘a’ || key == ‘A’), (key == ‘d’ || key == ‘D’),
(key == ‘w’ || key == ‘W’), (key == ‘s’ || key == ‘S’));
}
void keyReleased() {
player1.released((key == ‘a’ || key == ‘A’), (key == ‘d’ || key == ‘D’),
(key == ‘w’ || key == ‘W’), (key == ‘s’ || key == ‘S’));
}
//player variables
float x, y;
float dx = 0, dy = 0;
float speedX = 4;
float speedY = 4;
int size;
class Player {
Player(int newX, int newY, int newSize) {
x = newX;
y = newY;
size = newSize;
}
void run() {
display();
movePlayer();
}
void movePlayer() {
x += dx * speedX;
y += dy * speedY;
}
void display() {
ellipse(x, y, size, size);
}
void pressed(boolean left, boolean right, boolean up, boolean down) {
if (left) dx = -1;
if (right) dx = 1;
if (up) dy = -1;
if (down) dy = 1;
}
void released(boolean left, boolean right, boolean up, boolean down) {
if (left) dx = 0;
if (right) dx = 0;
if (up) dy = 0;
if (down) dy = 0;
}
}