Skip to content

Instantly share code, notes, and snippets.

@CSchoel
Created January 27, 2015 13:10
Show Gist options
  • Save CSchoel/3d178e74674b9ccf56be to your computer and use it in GitHub Desktop.
Save CSchoel/3d178e74674b9ccf56be to your computer and use it in GitHub Desktop.
Simple single-player Pong
//Autor: Christopher Schölzel
float x,y;
float vx,vy;
float radius;
float stickX,stickY;
float stickW,stickH;
boolean gameOver;
int hits;
int maxHits;
void setup() {
size(300,300);
radius = 10;
stickX = width - 50;
stickW = 10;
stickH = 50;
reset();
}
void reset() {
x = width/2.0;
y = height/2.0;
vx = random(2,3);
vy = random(2,3);
if (hits > maxHits) maxHits = hits;
hits = 0;
gameOver = false;
}
void move() {
x += vx;
y += vy;
stickY = mouseY;
if (x <= radius) { //Ball am linken Rand
vx *= -1;
} else if (x >= width-radius) { //Ball am rechten Rand => gameOver
gameOver = true;
} else if (abs(y-mouseY) < stickH/2.0 && abs(x-stickX) < stickW/2.0) {
//Schläger getroffen
hits++;
if(vx > 0) vx *= -1;
//Schlägergeschwindigkeit berechnen
float stickVy = mouseY-pmouseY;
vy += stickVy/2.0; //Teil der Geschwindigkeit auf Ball übertragen
}
if (y <= radius || y >= height-radius) { //Ball am oberen oder unteren Rand
vy *= -1;
}
}
void draw() {
background(0);
if (!gameOver) {
move();
}
fill(255);
ellipse(x,y,radius*2,radius*2);
rect(stickX-stickW/2.0,stickY-stickH/2.0,stickW,stickH);
if(gameOver) {
textAlign(CENTER);
textSize(20);
text("Game Over!\nPress space to restart.",width/2.0,height/2.0);
}
textSize(20);
textAlign(LEFT);
text("Treffer: "+hits,5,20);
textAlign(RIGHT);
text("Highscore: "+maxHits,width-5,20);
}
void keyPressed() {
//restart mit space
if (gameOver && key == ' ') {
reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment