EquationFrenzy游戏

时间:2015-01-19 19:23:14

标签: java

大家好,我需要一些帮助才能让它发挥作用。在你看代码之前我只想告诉你我在做什么。我想这样做,当数字击中方框并且它适合顶部的方程时,它将增加1点并改变方程。这是我有的: (我知道代码不是很好,但这是我的第一个大项目之一,我只是在寻找一些帮助)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EquationFrenzy
{

    public static void main(String[] args)
    {
        //Construct JFame object as a container for other objects
        JFrame frame = new JFrame("EquationFrenzy");
        //Set the dimensions of the window
        frame.setSize(500, 500);
        //Creates a pane for content
        JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(1, 0, 0, 0));
        //add content pane to a frame
        frame.setContentPane(pane);

        DrawingCanvas canvas = new DrawingCanvas(); 
        //create a timer which refers to the object canvas 
        Timer myTimer = new Timer (100, canvas);//100 milliseconds
        myTimer.start();
        pane.add(canvas);
        frame.addKeyListener(canvas);
        frame.setFocusable(true);

        //Make the frame full when program is run
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        //Display the frame - window
        frame.setVisible(true);
        //Bring the window to front
        frame.toFront();
    }
}

//implement action listener
class DrawingCanvas extends JPanel implements ActionListener, KeyListener
{
    //define random coordinates of the number
    int xPosition = (int)(getWidth()* Math.random());
    int yPosition = 0;
    int xPosition2 = (int)(getWidth()* Math.random());
    int yPosition2 = 0;
    int xPosition3 = (int)(getWidth()* Math.random());
    int yPosition3 = 0;
    int xPosition4 = (int)(getWidth()* Math.random());
    int yPosition4 = 0;
    int xPosition5 = (int)(getWidth()* Math.random());
    int yPosition5 = 0;
    int xPosition6 = (int)(getWidth()* Math.random());
    int yPosition6 = 0;
    int xPosition7 = (int)(getWidth()* Math.random());
    int yPosition7 = 0;
    int xPosition8 = (int)(getWidth()* Math.random());
    int yPosition8 = 0;
    int xPosition9 = (int)(getWidth()* Math.random());
    int yPosition9 = 0;
    int xPosition10 = (int)(getWidth()* Math.random());
    int yPosition10 = 0;
    int xPosition11 = (int)(getWidth()* Math.random());
    int yPosition11 = 0;
    int xPosition12 = (int)(getWidth()* Math.random());
    int yPosition12 = 0;
    int xPosition13 = (int)(getWidth()* Math.random());
    int yPosition13 = 0;
    int xPosition14 = (int)(getWidth()* Math.random());
    int yPosition14 = 0;
    int xPosition15 = (int)(getWidth()* Math.random());
    int yPosition15 = 0;
    int boxxPosition = 0;
    int boxyPosition = getHeight();
    int boxWidth = 70;
    int boxHeight = 50;
    int score;

    int yspeed = (int)(20* Math.random() + 10);
    int yspeed2 = (int)(21* Math.random() + 10);
    int yspeed3 = (int)(22* Math.random() + 10);
    int yspeed4 = (int)(23* Math.random() + 10);
    int yspeed5 = (int)(24* Math.random() + 10);
    int yspeed6 = (int)(25* Math.random() + 10);
    int yspeed7 = (int)(24* Math.random() + 10);
    int yspeed8 = (int)(23* Math.random() + 10);
    int yspeed9 = (int)(22* Math.random() + 10);
    int randomNumber = (int) (9* Math.random() + 1);
    int randomNumber2 = (int) (9* Math.random() + 1);
    int randomNumber3 = (int) (9* Math.random() + 1);
    int randomNumber4 = (int) (9* Math.random() + 1);
    int randomNumber5 = (int) (9* Math.random() + 1);
    int randomNumber6 = (int) (9* Math.random() + 1);
    int randomNumber7 = (int) (9* Math.random() + 1);
    int randomNumber8 = (int) (9* Math.random() + 1);
    int randomNumber9 = (int) (9* Math.random() + 1);
    int randomNumber10 = (int) (9* Math.random() + 1);
    int randomNumber11 = (int) (9* Math.random() + 1);
    int randomNumber12 = (int) (9* Math.random() + 1);
    int randomNumber13 = (int) (9* Math.random() + 1);
    int randomNumber14 = (int) (9* Math.random() + 1);
    int randomNumber15 = (int) (9* Math.random() + 1);
    int random = (int) (9* Math.random() + 1);
    int randomequation = (int) (9* Math.random() + 1);

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setFont(new Font("TimesRoman", Font.BOLD, 50));
        //get the current window width and height
        int screenWidth = getWidth();
        int screentHeight =getHeight();
        int number = 3;
        int number2 = random;
        int equation = random * randomequation;
        g.setColor(Color.black);
        //draw a ball
        g.drawString("" + randomNumber + "", xPosition, yPosition);
        g.drawString("" + randomNumber2 + "", xPosition2, yPosition2);
        g.drawString("" + randomNumber3 + "", xPosition3, yPosition3);
        g.drawString("" + randomNumber4 + "", xPosition4, yPosition4);
        g.drawString("" + randomNumber5 + "", xPosition5, yPosition5);
        g.drawString("" + randomNumber6 + "", xPosition6, yPosition6);
        g.drawString("" + randomNumber7 + "", xPosition7, yPosition7);
        g.drawString("" + randomNumber8 + "", xPosition8, yPosition8);
        g.drawString("" + randomNumber9 + "", xPosition9, yPosition9);
        g.drawString("" + randomNumber10 + "", xPosition10, yPosition10);
        g.drawString("" + randomNumber11 + "", xPosition11, yPosition11);
        g.drawString("" + randomNumber12 + "", xPosition12, yPosition12);
        g.drawString("" + randomNumber13 + "", xPosition13, yPosition13);
        g.drawString("" + randomNumber14 + "", xPosition14, yPosition14);
        g.drawString("" + randomNumber15 + "", xPosition15, yPosition15);
        //Draw equation
        g.drawString("__ X " + number2 + " = " + equation + "", 100, 100);

        //Draw score
        g.drawString("Points: " + score + "", 1200, 100);

        //draw black box
        g.setColor(Color.black);
        g.fillRect(boxxPosition, getHeight()- boxHeight, boxWidth, boxHeight); 
    }

    //write the content for actionPerformed by a timer
    public void actionPerformed(ActionEvent e) 
    { 
        //if the ball reaches the bottom side of the window change the direction 
        if (yPosition > getHeight())
        {
            yPosition = 0;
            xPosition = (int)(getWidth()* Math.random());
            randomNumber = (int) (9* Math.random() + 1);
            yspeed = (int)(20* Math.random() + 10);
        }
        yPosition += yspeed;

        repaint();
        if (yPosition2 > getHeight())
        {
            yPosition2 = 0;
            xPosition2 = (int)(getWidth()* Math.random());
            randomNumber2 = (int) (9* Math.random() + 1);
            yspeed2 = (int)(23* Math.random() + 10);
        }
        yPosition2 += yspeed2;

        repaint();
        if (yPosition3 > getHeight())
        {
            yPosition3 = 0;
            xPosition3 = (int)(getWidth()* Math.random());
            randomNumber3 = (int) (9* Math.random() + 1);
            yspeed3 = (int)(25* Math.random() + 10);
        }
        yPosition3 += yspeed3;

        repaint();
        if (yPosition4 > getHeight())
        {
            yPosition4 = 0;
            xPosition4 = (int)(getWidth()* Math.random());
            randomNumber4 = (int) (9* Math.random() + 1);
            yspeed4 = (int)(27* Math.random() + 10);
        }
        yPosition4 += yspeed4;

        repaint();
        if (yPosition5 > getHeight())
        {
            yPosition5 = 0;
            xPosition5 = (int)(getWidth()* Math.random());
            randomNumber5 = (int) (9* Math.random() + 1);
            yspeed5 = (int)(19* Math.random() + 10);
        }
        yPosition5 += yspeed5;

        repaint();
        if (yPosition6 > getHeight())
        {
            yPosition6 = 0;
            xPosition6 = (int)(getWidth()* Math.random());
            randomNumber6 = (int) (9* Math.random() + 1);
            yspeed6 = (int)(17* Math.random() + 10);
        }
        yPosition6 += yspeed6;

        repaint();
        if (yPosition7 > getHeight())
        {
            yPosition7 = 0;
            xPosition7 = (int)(getWidth()* Math.random());
            randomNumber7 = (int) (9* Math.random() + 1);
            yspeed7 = (int)(29* Math.random() + 10);
        }
        yPosition7 += yspeed7;

        repaint();
        if (yPosition8 > getHeight())
        {
            yPosition8 = 0;
            xPosition8 = (int)(getWidth()* Math.random());
            randomNumber8 = (int) (9* Math.random() + 1);
            yspeed8 = (int)(30* Math.random() + 10);
        }
        yPosition8 += yspeed8;

        repaint();
        if (yPosition9 > getHeight())
        {
            yPosition9 = 0;
            xPosition9 = (int)(getWidth()* Math.random());
            randomNumber9 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition9 += yspeed9;

        repaint();
        if (yPosition10 > getHeight())
        {
            yPosition10 = 0;
            xPosition10 = (int)(getWidth()* Math.random());
            randomNumber10 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition10 += yspeed4;

        repaint();
        if (yPosition11 > getHeight())
        {
            yPosition11 = 0;
            xPosition11 = (int)(getWidth()* Math.random());
            randomNumber11 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition11 += yspeed2;

        repaint();
        if (yPosition12 > getHeight())
        {
            yPosition12 = 0;
            xPosition12 = (int)(getWidth()* Math.random());
            randomNumber12 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition12 += yspeed6;

        repaint();
        if (yPosition13 > getHeight())
        {
            yPosition13 = 0;
            xPosition13 = (int)(getWidth()* Math.random());
            randomNumber13 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition13 += yspeed8;

        repaint();
        if (yPosition14 > getHeight())
        {
            yPosition14 = 0;
            xPosition14 = (int)(getWidth()* Math.random());
            randomNumber14 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition14 += yspeed;

        repaint();
        if (yPosition15 > getHeight())
        {
            yPosition15 = 0;
            xPosition15 = (int)(getWidth()* Math.random());
            randomNumber15 = (int) (9* Math.random() + 1);
            yspeed9 = (int)(20* Math.random() + 10);
        }
        yPosition15 += yspeed9;

        repaint();
    }

    //write the content for all key methods
    public void keyPressed(KeyEvent e) 
    { 
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                boxxPosition -= 10; break; 
            case KeyEvent.VK_RIGHT:
                boxxPosition += 10; break; 
        }
        repaint();
    }

    public void keyReleased(KeyEvent e) 
    { 

    }

    public void keyTyped(KeyEvent e) 
    { 

    }
}

由于

1 个答案:

答案 0 :(得分:0)

  

当数字到达方框并且它符合顶部的方程式时,它将增加1个点并改变方程式

我假设你不知道如何开始这部分编码,因为我没有看到任何代码处理黑匣子与你的数字之间的冲突。

您需要以下代码:

  • 检测到碰撞(在某种程度上,您已在数字和屏幕底部之间执行此操作
  • 检测号码是否正确
  • 添加积分
  • 更改等式

让我知道您正在努力解决哪些问题。如果您愿意,我也可以尝试帮助您改进当前的代码。