How to settle an an image in Applet for tetris game

牧云@^-^@ 提交于 2019-12-12 01:34:20

问题


I developing tetris game using applets. I have generated random shapes and making it move. But i don't know how to make it settle at the bottom. My shapes are vanishing once it reaches the end of the screen. Please help me on this regard. Here goes my code.

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


回答1:


Your code only draws a single block. You must store the coordinates of all generated block and redraw them at every game frame.



来源:https://stackoverflow.com/questions/14433321/how-to-settle-an-an-image-in-applet-for-tetris-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!