乒乓球游戏的闪烁问题

时间:2014-06-19 22:44:16

标签: java applet awt pong

你好,我在这场乒乓球比赛中遇到了这些闪烁的问题,并希望其他人可以引导我朝着正确的方向前进。在这一段时间里待了一个小时或两个小时,无法弄明白。

非常感谢??比如球和得分是最差的,当我嵌入游戏时几乎完全消失。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pong extends Applet implements MouseMotionListener, KeyListener {

int       doubleb,i,my,bx,by,px,py,compx,compy,width,height,speedx,speedy,bwidth,bheight,pwidth,pheight,score;
boolean started;
private Timer timer1;

    @Override
public void init() { 
    setSize(800,500); //setting screan size, declaring background color and some of my listeners
    width = getSize().width;
    height = getSize().height;
    setBackground(Color.black);
    pheight = 120;
    pwidth = 15;
    bheight = 30;
    bwidth = 30;
    addKeyListener(this); //listers used for the mouse and keybored input
    addMouseMotionListener(this);
    px = 35;
    compx = width - 35 - pwidth;
    newgame();
    timer1 = new Timer(10,new ActionListener() {
                @Override
        public void actionPerformed(ActionEvent e) { //momvement, physics, collusion
            height = getSize().height;
            width = getSize().width;
            bx += speedx;
            by += speedy;
            if (by <= 0 || by + bheight >= height) {
                speedy = -speedy;
            }
            if (bx <= px + pwidth && by + bheight >= py && by <= py + pheight && bx > px) {
                speedx = -speedx;
                ++score;
            }
            if (bx + bwidth >= compx && by + bheight >= compy && by <= compy + pheight && bx < compx + pwidth) {
                speedx = -speedx;
            }
            if (speedx < 0) {
                if (compy + pheight / 2 != height / 2) {
                    if (compy + pheight / 2 > height / 2) {
                        compy -= -speedx;
                    }
                    else {
                        compy += -speedx;
                    }
                }
            }
            else {
                if (by + bheight / 2 <= compy + pheight / 2) {
                    compy -= speedx;
                }
                else {
                    compy += speedx;
                }
            }
            if (compy < 0) {
                compy = 0;
            }
            if (compy + pheight > height) {
                compy = height - pheight;
            }
            if (bx + bwidth < 0) {
                py = height / 2 - pheight / 2;
                timer1.stop();
                started = false;
            }
            repaint();
        }    
    });
}

    @Override
public void mouseMoved(MouseEvent e) { 
    if (started) {
        my = e.getY();
        if (my + pheight / 2 > height) {
            my = height - pheight / 2;
        }
        if (my < pheight / 2) {
            my = pheight / 2;
        }
        py = my - pheight / 2;
        repaint();
    }
}

    @Override
public void mouseDragged(MouseEvent e) { }

    @Override
public void paint(Graphics g) {

    Font font1 = new Font("Arial", Font.BOLD, 18); //set font 
    Font font2 = new Font("Arial", Font.BOLD,40); //set font
    g.setColor(Color.white);
    g.drawRect(0,0,width - 1,height - 1);
    g.fillRect(px,py,pwidth,pheight);
    g.fillRect(compx,compy,pwidth,pheight);
    g.setFont(font1);
    g.drawString("Score: " + score,20,20); //paints the sorce 

            if (started) {
        g.fillArc(bx,by,bwidth,bheight,0,360);
    }
    else {
        g.setFont(font2); //set font
        g.setColor(Color.green); //set color
        g.drawString("Pong",width / 2 - 46,height / 2 - 16); //draws text to the screen
        g.setFont(font1);
        g.drawString("Press 's' to start",width / 2 - 69,height / 2 + 30);              
        }
    }

public void newgame() {
    py = height / 2 - pheight / 2;
    compy = py;
    bx = width / 2 - bwidth / 2;
    by = height / 2 - bheight / 2;
    speedx = 10;
    speedy = 10;
    score = 0;
}

    @Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyChar() == 's') {
        started = true;
        newgame();
        timer1.start();
    }
}

    @Override
public void keyTyped(KeyEvent e) { }

    @Override
public void keyReleased(KeyEvent e) { }

}

1 个答案:

答案 0 :(得分:0)

您可能需要查看double buffering

基本上,您遇到的问题是您清除屏幕以重新绘制它。但是,在Graphics对象完全更新之前,它会被绘制到屏幕上。这导致半成品屏幕,以及明显的闪烁效果。

对此的修复是首先将图像绘制到单独的Graphics对象,然后在完成后,将Graphics对象与正在显示的对象交换。

可以找到更多信息here