如何解决一个图像中的Applet的俄罗斯方块游戏(How to settle an an image

2019-11-01 11:18发布

我使用小程序开发的俄罗斯方块游戏。 我已经生成随机的形状,并使其移动。 但我不知道如何使它沉淀在底部。 我的形状正在消失,一旦它到达屏幕的结束。 请帮我在这方面。 这里去我的代码。

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

Answer 1:

你的代码只绘制一个单独的块。 必须将所有生成的块的坐标,并在每场比赛帧重绘他们。



文章来源: How to settle an an image in Applet for tetris game