在Processing中尝试制作屏幕保护程序的3个移动部分,得到了一个超快的部分.有什么想法吗



我创建了一个屏幕保护程序,可以在屏幕上来回切换。我试图将原始代码放入OOP中(我很擅长OOP(,这样我就可以有多个版本的OOP。相反,我看到了一个以异常快的速度旅行的例子。

如何使instance1instance2instance3分离而不是合并为一?

注意事项:在类screenSavers中,C代表构造函数参数,以区别于顶部的原始变量。CS代表特定类,以避免与顶部的原始变量混淆。(是的,这很令人困惑。我不知道我为什么这么做(

巨大的代码墙提醒

import java.lang.Math;
//defining dimensions and initial coords for the laptop frame
float frameW = 120;
float frameH = frameW * 3.0/4.0;
float frameX = random(frameW * 2, width);
float frameY = random(frameW * 2, height);
//defining variables are used in the movement of the laptop
float xSpeed = random(frameW / 50,frameH / 15);
float ySpeed = random(2,5);
boolean moveLeft = false;
boolean moveUp = false;
//defining the colours used in the program, as well as the alpha and stroke widths
int alpha = 255;
color baseColor = color(random(100, 255), random(100, 255), random(100, 255), alpha);
color accentColor = color(random(100, 255), random(100, 255), random(100, 255), alpha);
color baseColorAlpha;
color accentColorAlpha;
color bgColor = 0;
color outline = 255;
int outerCircleWeight = 5;
int laptopStrokeWeight = 1;
//initial states of little ellipses on the screen
float startingPoint = 7.5;
float leftDotRaise = 0;
boolean leftDown = false;
float centerDotRaise = 0;
boolean centerDown = false;
float rightDotRaise = 0;
boolean rightDown = false;
//variable for the radian at which the circles outside the laptop are at
float circleOrbit;
//variable for roundness of any initially circular shapes (ellipses, orbits, etc)
float specialRadii = 200;
//square-ification button
int buttonX = 200;
int buttonY = 200;
boolean buttonState = true;
int buttonTextSize = 30;
String offMessage = "Revert";
String onMessage = "Squarify";
String buttonMessage = onMessage;

public class screenSavers {

float frameWCS;
float xSpeedCS;
float ySpeedCS;
boolean moveLeftCS;
boolean moveUpCS;
int alphaCS;
color baseColorCS;
color accentColorCS;
color bgColorCS;
color outlineCS;
int outerCircleWeightCS;
int laptopStrokeWeightCS;
float startingPointCS;
float leftDotRaiseCS;
boolean leftDownCS;
float centerDotRaiseCS;
boolean centerDownCS;
float rightDotRaiseCS;
boolean rightDownCS;
float specialRadiiCS;
int buttonXCS;
int buttonYCS;
boolean buttonStateCS;
int buttonTextSizeCS;
String offMessageCS;
String onMessageCS;
String buttonMessageCS;
float circleOrbitCS;

public screenSavers (float frameWC, float xSpeedC, float ySpeedC, boolean moveLeftC, boolean moveUpC, int alphaC, color bgColorC, color outlineC, int outerCircleWeightC, int laptopStrokeWeightC, float startingPointC, float leftDotRaiseC, boolean leftDownC, float centerDotRaiseC, boolean centerDownC, float rightDotRaiseC, boolean rightDownC, float specialRadiiC, int buttonXC, int buttonYC, boolean buttonStateC, int buttonTextSizeC, String offMessageC, String onMessageC, String buttonMessageC, float circleOrbitC){

frameWCS = frameWC;
xSpeedCS = xSpeedC;
ySpeedCS = ySpeedC;
moveLeftCS = moveLeftC;
moveUpCS = moveUpC;
alphaCS = alphaC;
bgColorCS = bgColorC;
outlineCS = outlineC;
outerCircleWeightCS = outerCircleWeightC;
laptopStrokeWeightCS = laptopStrokeWeightC;
startingPointCS = startingPointC;
leftDotRaiseCS = leftDotRaiseC;
leftDownCS = leftDownC;
centerDotRaiseCS = centerDotRaiseC;
centerDownCS = centerDownC;
rightDotRaiseCS = rightDotRaiseC;
rightDownCS = rightDownC;
specialRadiiCS = specialRadiiC;
buttonXCS = buttonXC;
buttonYCS = buttonYC;
buttonStateCS = buttonStateC;
buttonTextSizeCS = buttonTextSizeC;
offMessageCS = offMessageC;
onMessageCS = onMessageC;
buttonMessageCS = buttonMessageC;
circleOrbitCS = circleOrbitC;

}

public void main(String[] args){


}

public void updateAlpha(){

/*
updates alpha versions of color variables that account for the alpha of the color, we have to put this
so that it updates every frame, otherwise the transparency will only update with every bounce. This way
the color updates with each bounce but the transparency constantly updates every frame (or 60 times per second)
*/
baseColorAlpha = color(red(baseColor),green(baseColor),blue(baseColor), alpha);
accentColorAlpha = color(red(accentColor),green(accentColor),blue(accentColor), alpha);

/*
transparency of objects changes with X position of the mouse, 1st quarter of the screen
= 25% brightness, 2nd quarter =  50%, 3rd quarter = 75%, 4th quarter = 100%
*/

if (mouseX < width / 3){
alpha = 75;
} else 
if (mouseX > width / 3 && mouseX < 2 * width / 3) {
alpha = 150;
} else
if (mouseX > width * 2 / 3 && mouseX < width){
alpha = 255;
}

}

void resetBackground(){

//reset background to prevent copies of the laptop from generating
background(bgColor);
//set outline color
stroke(outline);
fill(baseColorAlpha);

}

void updateRadii (){

//changing radii of shapes based on button
if (buttonStateCS == true){

specialRadiiCS = 2 * frameW;

} else {

specialRadiiCS = 0;

}

}


void generateComputer(){

//making the frame
rect(frameX, frameY, frameW, frameH);

//making the screen
fill(accentColorAlpha);
rect(frameX, frameY, frameW - frameW / 5, frameH - frameH * 4.0/15.0);

//switching back to the base color for the moving ellipses on the screen
fill(baseColorAlpha);

//creating the laptop keyboard
fill(accentColorAlpha);
beginShape(); 
vertex(frameX - frameW / 2, frameY + frameH / 2);
vertex(frameX - (frameW / 2 + 15), frameY + frameH / 2 + 15);
vertex(frameX + frameW / 2 + 15, frameY + frameH / 2 + 15);
vertex(frameX + frameW / 2, frameY + frameH / 2);
endShape();

}

void dots(){

rect(frameX - frameW / 5,frameY + leftDotRaise + startingPoint,frameH / 5,frameH / 5,specialRadiiCS);

//moving the dots
if (leftDotRaise >= frameH * (11.0 / 75.0)){
leftDown = false;
} else
if (leftDotRaise <= frameH * (-11.0 / 75.0)){
leftDown = true;
}

if (leftDown == true){
leftDotRaise += frameH / 75;
} else if (leftDown == false){
leftDotRaise -= frameH / 75;
}


rect(frameX,frameY + centerDotRaise, frameH / 5, frameH / 5,specialRadiiCS);

if (centerDotRaise >= frameH * (11.0 / 75.0)){
centerDown = false;
} else
if (centerDotRaise <= frameH * (-11.0 / 75.0)){
centerDown = true;
}

if (centerDown == true){
centerDotRaise += frameH / 75;
} else if (centerDown == false){
centerDotRaise -= frameH / 75;
}



rect(frameX + frameW / 5,frameY + rightDotRaise - startingPoint,frameH / 5,frameH / 5,specialRadiiCS);

if (rightDotRaise >= frameH * (11.0 / 75.0)){
rightDown = false;
} else
if (rightDotRaise <= frameH * (-11.0 / 75.0)){
rightDown = true;
}

if (rightDown == true){
rightDotRaise += frameH / 75;
} else if (rightDown == false){
rightDotRaise -= frameH / 75;
}

startingPoint = 0;

}




void generateOrbitals(){

//creating and animated the outer rotating circles
pushMatrix();
translate(frameX, frameY);
rotate(circleOrbitCS);
translate(frameW * 1.5, frameH / -10);
circleOrbitCS += 0.01;
rect(0, 0, frameW / 2, frameW / 2, specialRadiiCS);
popMatrix();

pushMatrix();
translate(frameX, frameY);
rotate(circleOrbitCS);
translate(frameW * -1.5, frameH / 10);
circleOrbitCS += 0.01;
rect(0, 0, frameW / 2, frameW / 2, specialRadiiCS);
popMatrix();

pushMatrix();
translate(frameX, frameY);
rotate(circleOrbitCS);
translate(frameH / 10, frameW * 1.5);
circleOrbit += 0.01;
rect(0, 0, frameW / 2, frameW / 2, specialRadiiCS);
popMatrix();

pushMatrix();
translate(frameX, frameY);
rotate(circleOrbit);
translate(frameH / -10, frameW * -1.5);
circleOrbit += 0.01;
rect(0, 0, frameW / 2,frameW / 2, specialRadiiCS);
popMatrix();

}




void generateOuterCircle(){

noFill();
stroke(accentColor);
strokeWeight(outerCircleWeight);
rect(frameX, frameY, frameW * 4, frameW * 4,specialRadii);
strokeWeight(laptopStrokeWeight);

}

void bounce(){

if (frameX + 2 * frameW > width) {

moveLeft = true;
baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);
accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);

} else if (frameX - 2 * frameW < 0) {

moveLeft = false;
baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);
accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);

}

if (frameY + 8.0/3.0 * frameH > height) {

moveUp = true;
baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);
accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);

} else if (frameY - 8.0/3.0 * frameH < 0) {

moveUp = false;
baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);
accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha);

}

if (moveLeft == true) {

xSpeed = abs(xSpeed) * -1;

} else {

xSpeed = abs(xSpeed);

}

if (moveUp == true) {

ySpeed = abs(ySpeed) * -1;

} else {

ySpeed = abs(ySpeed);


}

}

void updateCoords(){

//update coords  
println("Screensaver Icon is currently at (" + frameX + ", " + frameY + ')');

}

void generateSquareButton(){

//square-ification button


fill(accentColor);
rect(buttonXCS,buttonYCS, 200, 200, specialRadiiCS);
fill(255);
textSize(buttonTextSizeCS);
text(buttonMessageCS,buttonXCS,buttonYCS+5);

}

void alignShapes(){

//letting the program know that we are referencing X and Y coords of a rectangle or text box by its center
rectMode(CENTER);
textAlign(CENTER);

}

void testForButton(){

//check if the mouse was pressed at the button's coordinates
if (mouseX > buttonXCS - 100  && mouseY > buttonYCS - 100 && mouseX < buttonXCS + 50 && mouseY < buttonYCS + 50){

//if the button is on, turn it off
if (buttonStateCS == true){

buttonStateCS = false;
buttonMessageCS = offMessageCS;

} else
//if the button is off, turn it on
if (buttonStateCS == false){

buttonStateCS = true;
buttonMessageCS = onMessageCS;

}

}

}

void move(){

frameX += xSpeed;
frameY += ySpeed;

}

void display(){

updateAlpha();
resetBackground();
updateRadii();
generateComputer();
dots();
generateOrbitals();
generateOuterCircle();
bounce();
move();
updateCoords();
generateSquareButton();

}



}



//creating the first instance of the screensaver as an object
screenSavers instance1 = new screenSavers(frameW,xSpeed,ySpeed,moveLeft,moveUp,alpha,bgColor,outline,outerCircleWeight,laptopStrokeWeight,startingPoint,leftDotRaise,leftDown,centerDotRaise, centerDown, rightDotRaise, rightDown, specialRadii, buttonX, buttonY, buttonState, buttonTextSize, offMessage, onMessage, buttonMessage,circleOrbit);
//creating the second instance of the screensaver as an object
screenSavers instance2 = new screenSavers(frameW,xSpeed,ySpeed,moveLeft,moveUp,alpha,bgColor,outline,outerCircleWeight,laptopStrokeWeight,startingPoint,leftDotRaise,leftDown,centerDotRaise, centerDown, rightDotRaise, rightDown, specialRadii, buttonX, buttonY, buttonState, buttonTextSize, offMessage, onMessage, buttonMessage,circleOrbit);
//creating the third instance of the screensaver as an object
screenSavers instance3 = new screenSavers(frameW,xSpeed,ySpeed,moveLeft,moveUp,alpha,bgColor,outline,outerCircleWeight,laptopStrokeWeight,startingPoint,leftDotRaise,leftDown,centerDotRaise, centerDown, rightDotRaise, rightDown, specialRadii, buttonX, buttonY, buttonState, buttonTextSize, offMessage, onMessage, buttonMessage,circleOrbit);

//setup loop, runs ONCE
void setup() {
/*note: to see the console update the X and Y, comment out fullScreen();  
and un-comment size(1000,1000); */  

//enters fullscreen, regardless of device resolution  
fullScreen();  

//uncomment this for seeing coords updated  
//size(1000,1000);  


//makes the framerate go from 24 to a smoother 60 frames. If I don't add this your eyes will hurt
frameRate(60);






instance1.alignShapes();
instance2.alignShapes();
instance3.alignShapes();


}
void draw() {

instance1.display();
instance2.display();
instance3.display();


}

//////////////////////////////////////////////////////////////////

//do something when mouse is pressed
void mousePressed(){

instance1.testForButton();
instance2.testForButton();
instance3.testForButton();


}```

现在,您可以使用完全相同的属性初始化所有3个实例。它们只是";组合的";因为你做的都一样。您应该确定您希望它们之间有什么不同,并传递不同的属性。

其他注意事项:

  • 为什么screenSavers有main((函数?删除

  • 请将其重命名为ScreenSaver。

  • 不要区分使用C和CS。只要给它们起同样的名字。但是使用this关键字来区分类vars。示例:this.frameW = frameW;

  • 对if语句使用适当的缩进。

顺便说一句,几天后找到了它,我正在寻找this关键字

相关内容

最新更新