Created
November 15, 2021 11:09
-
-
Save igor-kamil/fd9c6f2a42d6890e67df724f9e4a1058 to your computer and use it in GitHub Desktop.
KP3 animacia pong
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
float circleX, circleY; | |
int size = 10; | |
float deltaX, deltaY; | |
int rectX, rectY; | |
int rectWidth, rectHeight; | |
float cR, cG, cB; | |
void setup() { | |
frameRate(150); | |
background(0); | |
size(800, 600); | |
rectX = 0; | |
rectY = 200; | |
rectWidth = 50; | |
rectHeight = 80; | |
circleX = width / 2; | |
circleY = height / 2; | |
initDelta(); | |
changeColor(); | |
} | |
void draw() { | |
if (gameOver()) { | |
println("prehral"); | |
background(0); | |
textAlign(CENTER); | |
text("game over \n(click mouse to start again)", width/2, height/2); | |
return; | |
} | |
backgroundWithAlpha(0); | |
drawRect(rectX, rectY); | |
drawCircle(circleX, circleY); | |
moveCircle(); | |
if (circleX >= width) { | |
deltaX = deltaX * -1.1; | |
changeColor(); | |
} | |
if (circleY >= height || circleY <= 0) { | |
deltaY = deltaY * -1.1; | |
changeColor(); | |
} | |
if (circleX <= rectWidth && circleY > rectY && circleY < (rectY+rectHeight)) { | |
deltaX = deltaX * -1.1; | |
changeColor(); | |
} | |
// void side of the space | |
fill(255, 0, 50); | |
rect(0,0,1, height); | |
} | |
void drawRect(float posX, float posY) { | |
fill(0,111,222); | |
rect(posX, posY, rectWidth, rectHeight); | |
} | |
void drawCircle(float posX, float posY) { | |
fill(cR,cG,cB); | |
circle(posX, posY, size); | |
} | |
void moveCircle() { | |
circleX = circleX + deltaX; | |
circleY = circleY + deltaY; | |
} | |
void initDelta() | |
{ | |
deltaX = 1 + random(0,1); | |
deltaY = 1 + random(0,1); | |
} | |
void changeColor() | |
{ | |
cR=random(100,255); | |
cG=random(100,255); | |
cB=random(100,255); | |
} | |
void backgroundWithAlpha(float bgColor) | |
{ | |
fill(bgColor, 5); | |
noStroke(); | |
rect(0,0,width, height); | |
} | |
boolean gameOver() { | |
return (circleX <= 0); | |
} | |
void mouseMoved() { | |
rectY = mouseY; | |
} | |
void mouseClicked() { | |
setup(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment