added green buff
JFrame interface is still Flicker is changed to Frame and double buffering technology is added. The interface does not flicker, but it should burn too much space
Modified the survival and death The condition is the corresponding function body
Encapsulation of the code, enhancement of the random appearance of Buff
< p>/**
* Green buff
* Protect the aircraft from injury for five seconds
* @author Xiaofan knocks the code
*
*/
public class GreenBuff extends GameObject{
int buffstarttime=-1;//Determine whether the buff has been eaten and record the time
public GreenBuff() {
width=Constant.GREEN_BUFF_WIDTH;
height=Constant.GREEN_BUFF_HEIGHT;
x=Math.random()*Constant.FRAME_WIDTH;
y=30;
speed=6 ;
}
//Display
public void draw(Graphics g) {
if(y
if(buffstarttime==-1)//Have not been eaten and not eaten
{
Color c=g.getColor();
g.setColor(Color.GREEN);
y+ =speed;
g.fillOval((int)x, (int)y, width, height);
g.setColor(c);
}
Else {
Buffstarttime=-1;//Out of screen
}
}
}
Double buffering technology
Image ImageBuffer = null;
Graphics GraImage = null;
//Override the update method public void update(Graphics g){ ImageBuffer = createImage(this.getWidth(), this.getHeight()); //Create graphics buffer GraImage = ImageBuffer.getGraphics(); Get graphics buffer The graphics context of the area paint(GraImage); //Call the paint method GraImage.dispose(); //Release graphics context resources g.drawImage(ImageBuffer, 0, 0, this); //Draw the graphics buffer onto the screen)