好的,我需要很多帮助,所以无论有什么建议都会非常感激!我正在尝试编写一个跑步/跳跃游戏,玩家/球必须尽可能快地避开积木到达终点线。现在,我的两个主要问题是跳跃动作和创建计时器。当我的玩家跳跃时,它会上升,然后下降。 我在Circle类中包含了以下代码:
public void horiz(int val){
for(int c = 0; c<val+1; c++){
x++;
repaint();}
}
public void vert(int val){
y += val;
}
public void down(int val){
y += val;
}
然后通过按钮执行:
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Button){
if(e.getSource() == run)
player.horiz(10);
else if (e.getSource()== jump){
player.vert(-10);
}
repaint();
collision();
}
为了让玩家上升,然后退缩,我试过了......
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Button){
if(e.getSource() == run)
player.horiz(10);
else if (e.getSource()== jump){
player.vert(-10);
repaint();
player.down(10);
}
repaint();
collision();
}
但是我觉得我需要等几秒才能回来。我已经阅读了一些关于swing,util和机器人定时器的内容,但我不知道在这里实现它们/在何处实现它们。
我的另一个问题是创建一个计时器,显示自游戏开始以来经过的时间。我的事情这可以与跳跃计时器类似地执行,但是,再次,我不确定我应该使用哪种类型的计时器。 如果我是对的,我不能使用swing,因为我的程序是用AWT编写的?请原谅可怕的颜色,我仍在搞清楚基础知识。非常感谢你给我的任何帮助建议! :)
以下是完整代码:
import java.awt.*;
import java.awt.Rectangle;
import java.awt.Shape;
import javafx.scene.shape.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Timer;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import java.applet.Applet;
public class TryingAgain extends Applet
implements ActionListener{
//creates arrays of rect values
int[]xA = new int[10];
int[]yA = new int[10];
int[]widthA = new int[10];
int[]heightA = new int[10];
private Rectangle rectangle;
//creates buttons to move player
private Button run = new Button("Run");
private Button jump = new Button("Jump");
//creates player and obstacles
private Circle player = new Circle(110,110,20);
private makeRect block = new makeRect();
private finishLine line = new finishLine();
//initiates the buttons with actionListener
public void init(){
this.setSize(new Dimension(1300,500));
setBackground(Color.GREEN);
add(run);
add(jump);
run.addActionListener(this);
jump.addActionListener(this);
}
//draws the player and blocks on the screen
public void paint(Graphics g){
player.draw(g);
block.draw(g);
line.draw(g);
}
//if methods to be control movement
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Button){
if(e.getSource() == run)
player.horiz(10);
else if (e.getSource()== jump){
player.vert(-10);
}
repaint();
collision();
}
}
public void collision(){
if(crashTest() == true){
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}
if(winTest() == true){
JOptionPane.showMessageDialog(this, "You Finished!", "You Finished!", JOptionPane.PLAIN_MESSAGE);
}
}
class Circle{
private int radius;
private int x,y;
public Circle(){
x = 110; y = 110;
radius = 20;
}
public Circle(int x0, int y0, int rad){
x = x0; y = y0; radius = rad;
}
public void draw(Graphics g){
g.setColor(Color.CYAN);
g.fillOval(x - radius, y-radius, 2*radius, 2*radius);
}
public void horiz(int val){
for(int c = 0; c<val+1; c++){
x++;
repaint();}
}
public void vert(int val){
y += val;
}
public void down(int val){
y += val;
}
public Rectangle getBounds(){
return new Rectangle(x-radius, y-radius, 2*radius, 2*radius);
}
}
class makeRect{
private int Xmax = 150;
private int Xmin = 50;
private int Wmax = 50;
private int Hmax = 25;
private int Wmin = 10;
private int Hmin = 5;
Random rand = new Random();
int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;
//fills arrays
{for(int i = 0; i < 10; i++){
Random rand = new Random();
int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;
if(i>0)
xA[i] = (randx+xA[i-1]);
else
xA[i] = 160;
yA[i] = randh+110;
widthA[i] = randw;
heightA[i] = randh;
}
}
private int x, y, width, height;
public makeRect(){
x = 150; y = 120; width = 30; height = 10;
}
public void makeRect(int x0, int y0, int w0, int h0){
x = x0; y = y0; width = w0; height = h0;
}
public void draw(Graphics g) {
g.setColor(Color.ORANGE);
{for(int i = 0; i < 10; i++){
g.fillRect(xA[i], yA[i], heightA[i], widthA[i]);
}}
}
public Rectangle getBounds(){
return new Rectangle(randx, 110+randh, 30, 10);
}
}
class finishLine{
private int x, y, width, height;
public void finishLine(int x0, int y0, int w0, int h0){
x = x0; y = y0; width = w0; height = h0;
}
public void draw(Graphics g){
g.setColor(Color.MAGENTA);
g.fillRect(1200, 80, 30, 30);
}
}
public boolean crashTest(){
boolean end = false;
{for(int i = 0; i<10; i++){
if(player.getBounds().intersects(new Rectangle(xA[i], yA[i], heightA[i], widthA[i])))
end = true;
}
return end;
}
}
public boolean winTest(){
boolean win = false;
if(player.getBounds().intersects(new Rectangle(1200, 80, 30, 30)))
win = true;
return win;
}
}