Skip to content

Instantly share code, notes, and snippets.

@darkwave
Created January 17, 2015 10:15
Show Gist options
  • Save darkwave/fc333eda4d4f38f03e63 to your computer and use it in GitHub Desktop.
Save darkwave/fc333eda4d4f38f03e63 to your computer and use it in GitHub Desktop.
Movimento con tastiera
PImage sfondo;
PImage player1;
PImage player2;
PImage gem;
int x1 = 100;
int y1 = 300;
int x2 = 500;
int y2 = 300;
int pointX;
int pointY;
boolean player1Right = false;
boolean player1Left = false;
boolean player1Up = false;
boolean player1Down = false;
boolean player2Right = false;
boolean player2Left = false;
boolean player2Up = false;
boolean player2Down = false;
void setup() {
size(600, 600, P3D);
sfondo = loadImage("arena.png");
player1 = loadImage("player1.png");
player2 = loadImage("player2.png");
gem = loadImage("point.png");
pointX = (int) random(width);
pointY = (int) random(height);
imageMode(CENTER);
}
void draw() {
if (player1Right) {
x1 = x1 + 2;
}
if (player1Left) {
x1 = x1 - 2;
}
if (player1Up) {
y1 = y1 -2;
}
if (player1Down) {
y1 = y1 + 2;
}
if (player2Right) {
x2 = x2 + 2;
}
if (player2Left) {
x2 = x2 - 2;
}
if (player2Up) {
y2 = y2 -2;
}
if (player2Down) {
y2 = y2 + 2;
}
float distance = dist(x1, y1, pointX, pointY);
if (distance < 12.5) {
pointX = (int) random(width);
pointY = (int) random(height);
println("Punto al ROSSO!");
}
float distance2 = dist(x2, y2, pointX, pointY);
if (distance2 < 12.5) {
pointX = (int) random(width);
pointY = (int) random(height);
println("Punto al GIALLO!");
}
image(sfondo, width / 2, height / 2);
image(player1, x1, y1);
image(player2, x2, y2);
image(gem, pointX, pointY);
}
void keyPressed() {
if (key == 'd') {
player1Right = true;
}
if (key == 'a') {
player1Left = true;
}
if (key == 'w') {
player1Up = true;
}
if (key == 's') {
player1Down = true;
}
if (keyCode == RIGHT) {
player2Right = true;
}
if (keyCode == LEFT) {
player2Left = true;
}
if (keyCode == UP) {
player2Up = true;
}
if (keyCode == DOWN) {
player2Down = true;
}
}
void keyReleased() {
if (key == 'd') {
player1Right = false;
}
if (key == 'a') {
player1Left = false;
}
if (key == 'w') {
player1Up = false;
}
if (key == 's') {
player1Down = false;
}
if (keyCode == RIGHT) {
player2Right = false;
}
if (keyCode == LEFT) {
player2Left = false;
}
if (keyCode == UP) {
player2Up = false;
}
if (keyCode == DOWN) {
player2Down = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment