0

我使用小程序开发俄罗斯方块游戏。我生成了随机形状并使其移动。但我不知道如何让它在底部安定下来。一旦到达屏幕的末端,我的形状就会消失。请在这方面帮助我。这是我的代码。

import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;



public class TetrisGame
{
    public static void main (String argsp[])
    {
        JFrame frame = new JFrame("Tetris");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final AppletClass applet = new AppletClass();
        frame.getContentPane().add(applet);
        applet.init();
        Thread t = new Thread(new Runnable()
        {

            @Override
            public void run() 
            {
                while(true)
                {
                    try 
                    {
                        Thread.sleep(1000);
                        applet.repaint();
                    }
                    catch (InterruptedException e) 
                    {
                        e.printStackTrace();
                    }               
                }
            }

        });
        t.start();

        frame.setBounds(10,10,410,610);
        frame.setVisible(true);
    }
}

class AppletClass extends Applet implements KeyListener
{

    private static final long serialVersionUID = 1L;
    static int xpos = 190;
    static int ypos =0;
    static int random1;
    static int reached_end=1;


    public void init()
    {
             System.out.println("aaa");
        setFocusable(true);
        requestFocus();
        addKeyListener(this);

    }
    public void paint (Graphics g)
    {

        generateRandomShapes(g);
    }

    public void generateRandomShapes (Graphics g)
    {

        int i=0;
        int x[] = new int[4];
        int y[] = new int[4];

        if(reached_end==1)
        {
            random1=(int)Math.floor(Math.random()*100);
            ypos=0;
        }

        x[0]=xpos;
        ypos =ypos+30;
        y[0]=ypos;

        if(ypos>420)
        {
            reached_end=1;
            ypos=0;
        }
        else
            reached_end=0;





        switch(random1%7)
        {
                case 0:
                    //straight line
                    for(i = 0;i<3;i++)
                    {
                        x[i+1]=x[i];
                        y[i+1]=y[i]+30;
                    }
                    break;
                case 1:
                    //L shape
                    for(i = 0;i<2;i++)
                    {
                        x[i+1]=x[i];
                        y[i+1]=y[i]+30;
                    }
                    x[i+1]=x[i]+30;
                    y[i+1]=y[i];
                    break;
                case 2:
                    //reverse L shape
                    for(i = 0;i<2;i++)
                    {
                        x[i+1]=x[i];
                        y[i+1]=y[i]+30;
                    }
                    x[i+1]=x[i]-30;
                    y[i+1]=y[i];
                    break;
                case 3:
                    //square shape
                    i=0;
                        x[i+1]=x[i];
                        y[i+1]=y[i]+30;
                    i++;
                        x[i+1]=x[i]+30;
                        y[i+1]=y[i];
                    i++;
                        x[i+1]=x[i];
                        y[i+1]=y[i]-30;
                    break;
                case 4:
                    //Inverse T shape
                    x[i+1]=x[i]-30;
                    y[i+1]=y[i]+30;
                    for(i = 1;i<3;i++)
                    {
                        x[i+1]=x[i]+30;
                        y[i+1]=y[i];
                    }
                    break;
                case 5:
                    //S shape
                    x[i+1]=x[i]-30;
                    y[i+1]=y[i]+30;
                    i++;
                    x[i+1]=x[i]+30;
                    y[i+1]=y[i];
                    i++;
                    x[i+1]=x[i]+30;
                    y[i+1]=y[i]-30;
                    break;
                case 6:
                    //Z shape
                    x[i+1]=x[i]+30;
                    y[i+1]=y[i];
                    i++;
                    x[i+1]=x[i];
                    y[i+1]=y[i]+30;
                    i++;
                    x[i+1]=x[i]+30;
                    y[i+1]=y[i];
                    break;

        }
        g.setColor(Color.BLACK);
        for(i=0;i<4;i++)
        {
            g.fillRect(x[i], y[i], 30, 30);
        }


    }

    public void keyReleased(KeyEvent e){;}
     public void keyTyped(KeyEvent e){;}

    public void keyPressed(KeyEvent e) 
    {
        System.out.println("Kkk "+e.getKeyCode());
        if(e.getKeyCode() == KeyEvent.VK_LEFT)
     {
        if(xpos>=30)
        {
            xpos=xpos-30;
        }
        System.out.println("xpos is :" + xpos);
     }
     if(e.getKeyCode() == KeyEvent.VK_RIGHT)
     { 
         xpos=xpos+30;
     }

     if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_SPACE)
     {
         ypos=420;
     }
 }
}

TIA

4

1 回答 1

0

您的代码仅绘制一个块。您必须存储所有生成块的坐标并在每个游戏帧重绘它们。

于 2013-01-21T06:28:10.757 回答