我创建了一个简单的程序,通过你按下按钮的次数来填充一米,最终导致一个"捣碎按钮获胜"风格的游戏。我设置了每次按空格键时,仪表填充 10/440,如果您达到最大值 440,您就赢了。我遇到的问题是如何使增量添加更平滑、不那么突然;就像你在给自行车轮胎打气一样:连续的,更少的增量。这是有问题的代码:
import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*; //for files
public class MyClicker
{
public static void main(String args[])
{
new Clicker(); //Make a window
}
}
class Clicker extends Frame implements KeyListener, MouseListener
{
// global variables
private final static int SCHEIGHT=768,SCWIDTH=1024;
// direction constants
final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
// movement change constants
final int X = 0,Y = 1,Z = 2;
final int TITLE = 30, STATUS = 40;
final static int size = 2;
Image myPic;
private boolean gameNotOver = true;
private boolean keyPressed = false;
private Image myScreen;
private int whichScreen;
private int timePassed;
private int numScreen;
private int maxAmt=450;
private int timer;
private int amt;
public Clicker()
{
setSize(SCWIDTH,SCHEIGHT);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
timer = -1;
this.setVisible(true);
out.println(amt);
this.addKeyListener(this);
gameLoop();
}
public void gameLoop()
{
do
{
if (keyPressed)
{
amt = amt + 50;
out.println(keyPressed + " " +gameNotOver+" "+ "Time "+timer);
this.repaint();
keyPressed = false;
}
if(amt >= 400)
gameNotOver = false;
pause(1000);
timer++;
this.setVisible(true);
//this.repaint();
pause(0);
if(amt > maxAmt - 10)
amt = maxAmt - 10;
}
while (gameNotOver);
if(!gameNotOver)
{
pause(5000);
System.exit(0);
}
}
public int getAmt()
{
return amt;
}
public void paint(Graphics pen)
{
if(whichScreen==0)
{
pen.drawImage(myPic,100,200,506,279,this); //Draws the image
pen.setFont(new Font("Timesroman", Font.ITALIC, 50));
pen.drawString("Welcome to the Clicker Test", 200, 75);
pen.setFont(new Font("Timesroman", Font.ITALIC, 40));
pen.drawString("Created by Cody Coulter",150,150);
pen.setFont(new Font("Timesroman", Font.ITALIC, 25));
pause(200); // 2000 final
whichScreen++;
doubleBuffer(pen);
}
else
{
myScreen =createImage(getSize().width,getSize().height);
Graphics o = myScreen.getGraphics();
doubleBuffer(o);
pen.drawImage(myScreen,0,0,this);
}
}
public void doubleBuffer(Graphics pen) // Draws the window
{
pause (500);
numScreen++;
if (numScreen > 0)
{
setBounds(0,0,SCWIDTH,SCHEIGHT);
Color HPRed = new Color(213, 0, 0);
pen.setColor(Color.BLACK);
setTitle("Clicker Test -- Cody Coulter."+
"To click, press the space bar");
pen.setFont(new Font("Timesroman",Font.PLAIN,48));
pen.drawString("Clicker Test ",350,75);
pen.setFont(new Font("TimesRoman",Font.BOLD,33));
pen.fillRect((SCWIDTH/4), (SCHEIGHT/3), 50, maxAmt);
pen.drawString(""+timer, 350, 150);
pen.setColor(HPRed);
pen.fillRect((SCWIDTH/4)+5, (SCHEIGHT/3)+5, 40, amt);
if(!gameNotOver)
{
if(amt >= maxAmt-10)
{
pen.setColor(HPRed);
pen.fillRect(SCWIDTH/4-75, SCHEIGHT/3, SCWIDTH/2+150, SCHEIGHT/3-60);
pen.setColor(Color.GRAY);
pen.fillRect(SCWIDTH/4-65, SCHEIGHT/3+10, SCWIDTH/2+130, SCHEIGHT/3-80);
pen.setColor(Color.YELLOW);
pen.setFont(new Font("Verdana", Font.BOLD, 40));
pen.drawString("You got it!", SCWIDTH/2-290, SCHEIGHT/2-20);
}
}
this.repaint();
}
}
public void keyPressed(KeyEvent e)
{
keyPressed = true;
setTitle(""+ KeyEvent.getKeyText(e.getKeyCode()));
System.out.println("hit + "+ KeyEvent.getKeyText(e.getKeyCode())+ " " + keyPressed);
switch(e.getKeyCode())
{
case KeyEvent.VK_SPACE: if(amt < maxAmt - 10)
amt= amt + 10;
break;
case KeyEvent.VK_A: if(amt < maxAmt - 10)
amt = amt + 10;
break;
}
}
public void mouseClicked(MouseEvent m)
{
}
public void mouseEntered(MouseEvent m)
{
}
public void mouseExited(MouseEvent m)
{
}
public void mousePressed(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void update(Graphics G)
{
paint(G);
}
public static void pause (long r)
{
try
{
Thread.sleep(r);
}
catch (Exception e)
{
out.println(" sleep error " + e);
}
}
} //end class
这是完全最新的代码:
import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*; //for files
public class MyClicker
{
public static void main(String args[])
{
new Clicker(); //Make a window
}
}
class Clicker extends Frame implements KeyListener, MouseListener
{
// global variables
private final static int SCHEIGHT=768,SCWIDTH=1024;
// direction constants
final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
// movement change constants
final int X = 0,Y = 1,Z = 2;
final int TITLE = 30, STATUS = 40;
final static int size = 2;
Image myPic;
private boolean gameNotOver = true;
private boolean keyPressed = false;
private Image myScreen;
private int whichScreen;
private int timePassed;
private int numScreen;
private int maxAmt=450;
private int dispAmt;
private int actAmt;
private double timer;
public Clicker()
{
setSize(SCWIDTH,SCHEIGHT);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
timer = -1;
this.setVisible(true);
this.addKeyListener(this);
gameLoop();
}
public void gameLoop()
{
do
{
if (keyPressed)
{
out.println(keyPressed + " " +gameNotOver+" "+ "Time "+timer);
keyPressed = false;
}
if(dispAmt > maxAmt-10)
dispAmt = maxAmt;
if(actAmt >= maxAmt-10 || dispAmt >= maxAmt-10)
gameNotOver = false;
if(dispAmt < actAmt)
dispAmt++;
timer++;
this.setVisible(true);
// this.repaint();
// pause(0);
// if(actAmt > maxAmt - 10 || dispAmt > maxAmt - 10)
// actAmt = maxAmt - 10;
this.repaint();
}
while (gameNotOver);
if(!gameNotOver)
{
pause(5000);
System.exit(0);
}
}
public int getAmt()
{
return actAmt;
}
public void paint(Graphics pen)
{
if(whichScreen==0)
{
pen.drawImage(myPic,100,200,506,279,this); //Draws the image
pen.setFont(new Font("Timesroman", Font.ITALIC, 50));
pen.drawString("Welcome to the Clicker Test", 200, 75);
pen.setFont(new Font("Timesroman", Font.ITALIC, 40));
pen.drawString("Created by Cody Coulter",150,150);
pen.setFont(new Font("Timesroman", Font.ITALIC, 25));
pause(200); // 2000 final
whichScreen++;
doubleBuffer(pen);
}
else
{
myScreen =createImage(getSize().width,getSize().height);
Graphics o = myScreen.getGraphics();
doubleBuffer(o);
pen.drawImage(myScreen,0,0,this);
}
}
public void doubleBuffer(Graphics pen) // Draws the window
{
numScreen++;
if (numScreen > 0)
{
setBounds(0,0,SCWIDTH,SCHEIGHT);
Color HPRed = new Color(213, 0, 0);
pen.setColor(Color.BLACK);
setTitle("Clicker Test -- Cody Coulter."+
"To click, press the space bar");
pen.setFont(new Font("Timesroman",Font.PLAIN,48));
pen.drawString("Clicker Test ",350,75);
pen.setFont(new Font("TimesRoman",Font.BOLD,33));
pen.fillRect((SCWIDTH/4), (SCHEIGHT/3), 50, maxAmt);
pen.drawString(""+timer/1000, 350, 150);
pen.drawString(""+dispAmt+" / "+actAmt, 350, 200);
pen.setColor(HPRed);
pen.fillRect((SCWIDTH/4)+5, (SCHEIGHT/3)+5, 40, dispAmt);
if(!gameNotOver)
{
if(actAmt >= maxAmt-10)
{
pen.setColor(HPRed);
pen.fillRect(SCWIDTH/4-75, SCHEIGHT/3, SCWIDTH/2+150, SCHEIGHT/3-60);
pen.setColor(Color.GRAY);
pen.fillRect(SCWIDTH/4-65, SCHEIGHT/3+10, SCWIDTH/2+130, SCHEIGHT/3-80);
pen.setColor(Color.YELLOW);
pen.setFont(new Font("Verdana", Font.BOLD, 40));
pen.drawString("You got it!", SCWIDTH/2-290, SCHEIGHT/2-20);
}
}
this.repaint();
}
}
public void keyPressed(KeyEvent e)
{
keyPressed = true;
setTitle(""+ KeyEvent.getKeyText(e.getKeyCode()));
System.out.println("hit + "+ KeyEvent.getKeyText(e.getKeyCode())+ " " + keyPressed);
switch(e.getKeyCode())
{
case KeyEvent.VK_SPACE:actAmt = actAmt+10;
break;
case KeyEvent.VK_A: actAmt = actAmt+10;
break;
}
this.repaint();
}
public void mouseClicked(MouseEvent m)
{
}
public void mouseEntered(MouseEvent m)
{
}
public void mouseExited(MouseEvent m)
{
}
public void mousePressed(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void update(Graphics G)
{
paint(G);
}
public static void pause (long r)
{
try
{
Thread.sleep(r);
}
catch (Exception e)
{
out.println(" sleep error " + e);
}
}
} //end class
- 摆脱暂停(
1000),你可以让它暂停(10)或围绕它的东西。
每次在循环中调用 this.repaint()(在 if 语句之外)。
将 amt 拆分为两个变量:实际金额和显示金额。 仅绘制显示的金额,并且按键仅增加实际金额。
每帧,如果显示量小于实际量,则将其增加 1。
更新。。。
- 从双缓冲区例程中删除暂停 (500),并从绘制中删除暂停 (200)。 一般来说,除非你需要"什么都不做",否则不要暂停。 游戏从不"什么都不做",它们几乎总是有一个核心循环,至少是渲染。