2016-12-04 21 views
1

我試圖做一個簡單的馬里奧遊戲爲我校的項目,到目前爲止,它的順利,但由於某種原因,圖形繼續當我的癩蛤蟆GIF正在閃爍,當我移動我的馬里奧的性格。我怎樣才能阻止呢?Java的圖形保持閃動

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

//Questions: 

//1. How do I get gravity? 
//2. How do I get it to hit the higher block and then fall down? 
//3. Sun as circle 
//4. How do I get the background to move with the character 
//5. Get Mario to move (as an animation) 
//6. Think flashing entrance screen for name with polygon 
//7. Figure out how to fit in an arc somewhere (Bird) 

public class Mario extends Applet implements KeyListener,ActionListener 
{ 
    private Image lvlOne; //Level 1 Background 
    private Image marioLeft; 
    private Image marioRight; 
    private Image toad; 
    private int level; 
    private int x; 
    private int y; 
    private int marioIncrements; 
    private int marioLocation; 
    private int upDown; 
    private int toadLocation; 
    private boolean rightLeft; 
    private double scroll; 
    private Timer timer; 

    public void init() 
    { 
     level = -1; //Name and Assignment 
     lvlOne = getImage(getDocumentBase(), "background.png"); //Level 1 
     marioLeft = getImage(getDocumentBase(), "marioLeft.png"); //Mario Looking Left 
     marioRight = getImage(getDocumentBase(), "marioRight.png"); //Mario Looking Right 
     toad = getImage(getDocumentBase(), "toads.gif"); 
     addKeyListener(this); //Starts Key Listener 
     marioIncrements = 10; 
     marioLocation = 100; 
     upDown = 700; 
     toadLocation = 2400; 
     rightLeft = true; 
     timer = new Timer(10,this); // timer sends a signal to our applet every 10 milliseconds 
     timer.start(); 
     scroll = 0.5; 
    } 

    public void actionPerformed (ActionEvent ae){ // timer signals come here 

    } 



    public void keyReleased(KeyEvent ke){ 
    } 

    public void keyPressed(KeyEvent ke){ 
     int zzz = ke.getKeyCode(); 
     switch(zzz){ 
      case(KeyEvent.VK_ENTER): 
      level+= 1; //Enter Key Starts Game at Level 1 
      break; 

      case(KeyEvent.VK_RIGHT): //Move Right 
      marioLocation = marioLocation + marioIncrements; 
      break; 

      case(KeyEvent.VK_LEFT): //Move Left 
      marioLocation = marioLocation - marioIncrements; 
      //rightLeft ^= true; //THIS IS TEMPORARY 
      break; 

      case(KeyEvent.VK_SPACE): 
      upDown = upDown - 260; //Jump 
      break; 

      case(KeyEvent.VK_UP): 
      upDown = upDown - 260; //Another Option to Jump 
      break; 

      case(KeyEvent.VK_DOWN): 
      upDown = upDown + 260; //Temp go Down I NEED GRAVITY 
      break; 

     } 
     repaint(); 
    } 

    public void keyTyped(KeyEvent ke){ 
    } 

    public void paint(Graphics g) 
    { 
     //Determines Background to set based on level 
     if(level == -1){ 
      setBackground(Color.BLACK); 
      g.setColor(Color.RED); 
      g.setFont(new Font("Comic Sans MS",Font.PLAIN,300)); 
      g.drawString("A PROJECT BY", 200,500); //Displays Project 
      g.setColor(Color.WHITE); 
      g.setFont(new Font("Comic Sans MS",Font.PLAIN,300)); 
      g.drawString("SUNNY GANDHI", 25,900);//Displays Name 
      g.setFont(new Font("Comic Sans MS",Font.PLAIN,70)); 
      g.setColor(Color.GREEN); 
      g.drawString("Press Enter to Skip All This Boring Stuff", 25,100); 

     } 
     else if(level ==0){ 
      g.drawImage(lvlOne, 0, 0, getWidth(), getHeight(),this); 

      //Figure out HOW TO DRAW A SUN AS A CIRCLE 
      //g.setColor(Color.YELLOW); 
      //g.drawOval(10, 10, 5, 5); 

      //Draw Rectangle Here 
      g.setColor(Color.RED); 
      g.setFont(new Font("Comic Sans MS",Font.PLAIN,150)); 
      g.drawString("MARIO BROS", 1000,500); //Displays Game Title 
      g.setColor(Color.GREEN); 
      g.setFont(new Font("Comic Sans MS",Font.PLAIN,70)); 
      g.drawString("Press Enter to Begin", 1050,600);//Displays Game Title 

     } 
     else if(level == 1){ //Level 1 
      g.drawImage(lvlOne, 0, 0, getWidth(), getHeight(),this); 
      if(rightLeft == true){ //Change Mario Direction/Location 
       g.drawImage(marioRight, marioLocation, upDown, 100, 150,this); //Draws beginning Mario 
      } 
      else if(rightLeft == false){ //Change Mario Direction/Location 
       g.drawImage(marioLeft, marioLocation, upDown, 100, 150,this); 
      } 
      //First Set of Blocks 

      //First Block 
      g.setColor(coloLibGandhi.BROWN()); 
      g.drawRect(700,590,100,100); 
      g.fillRect(700,590,100,100); 
      //Second Block 
      g.setColor(Color.BLACK); 
      g.drawRect(600,590,100,100); 
      g.fillRect(600,590,100,100); 
      //Third Block 
      g.setColor(Color.GREEN); 
      g.drawRect(500,590,100,100); 
      g.fillRect(500,590,100,100); 
      //Middle Higher Block 
      g.setColor(Color.BLACK); 
      g.drawRect(600,200,100,100); 
      g.fillRect(600,200,100,100); 

      //Second Set of Blocks 
      //First Block 
      g.setColor(coloLibGandhi.BROWN()); 
      g.drawRect(1500,590,100,100); 
      g.fillRect(1500,590,100,100); 
      //Second Block 
      g.setColor(Color.BLACK); 
      g.drawRect(1600,590,100,100); 
      g.fillRect(1600,590,100,100); 
      //Third Block 
      g.setColor(Color.GREEN); 
      g.drawRect(1700,590,100,100); 
      g.fillRect(1700,590,100,100); 

      //Toad 
      g.drawImage(toad, toadLocation, 700, 100, 150, this); 

     } 
     else if(level == 2){ 
     } 
     else if(level == 3){ 


     } 

     } 
    } 

忽略我所做的一些評論,以及一些未使用的變量 - 我將在稍後討論這些變量。

謝謝!

+0

它閃爍可能是因爲您擦除所有內容然後繪製新的幀。一時之間,一切都消失了。 – Gendarme

+0

@Gendarme我將如何解決這個問題 - 我必須把一些新的東西,我不能擺脫重繪聲明。或者,我應該把它放在不同的地方? –

+0

你的繪畫方法開始時應該有super.paint(g)。 – camickr

回答

0
Image bck=createImage(getWidth(), getHeigh()); 
Graphics g2=bck.getGraphics(); 

做這個g2上的所有畫;那麼

paint(graphics g) { 
    g.draw(bck 0, 0, this); 
} 

public void update(Graphics g) { 
    paint(g); 
}