In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to write the code for Java Tetris game". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Effect picture
The interface here doesn't feel very good-looking, but I don't think it's a big problem, as long as the function is in place!
Realization idea
Two canvases:
Canvas 1: parts used to draw static things, such as game area borders, grids, scoring area boxes, next area boxes, buttons, etc., that do not need to be refreshed.
Canvas 2: used to draw dynamic parts of the game, such as grid model, grid movement, rotation deformation, elimination, integral display, next graphic display, etc.
Code implementation creation window
First of all, create a game form class GameFrame, inherited to JFrame, used to display on the screen (window objects), each game has a window, set the window title, size, layout, etc.
/ * * Game form class * / public class GameFrame extends JFrame {public GameFrame () {setTitle ("Tetris"); / / set title setSize (488,476); / / set size setLayout (new BorderLayout ()); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) / / Click the close button to close the program setLocationRelativeTo (null); / / set the center setResizable (false); / / do not allow modification of interface size}} canvas 1
Create a panel container BackPanel and inherit it to JPanel
/ * * background canvas class * / public class BackPanel extends JPanel {BackPanel panel=this; private JFrame mainFrame=null; / / initialization related parameters public BackPanel (JFrame frame) {this.setLayout (null); this.setOpaque (false); this.mainFrame = frame; mainFrame.setVisible (true);}}
Create another Main class to launch the window.
Public class Main {/ / main class public static void main (String [] args) {GameFrame frame = new GameFrame (); BackPanel panel = new BackPanel (frame); frame.add (panel); frame.setVisible (true); / / set display}}
Right-click to execute the Main class, and the window is created
Create menus and menu options
Create menu
Private void initMenu () {/ / create menus and menu options jmb = new JMenuBar (); JMenu jm1 = new JMenu ("Games"); jm1.setFont (new Font ("imitating Song", Font.BOLD, 15); / / set the font displayed on the menu JMenu jm2 = new JMenu ("help") Jm2.setFont (new Font ("imitating Song", Font.BOLD, 15); / / setting the font displayed on the menu JMenuItem jmi1 = new JMenuItem ("start a new game"); JMenuItem jmi2 = new JMenuItem ("quit"); jmi1.setFont ("imitating Song", Font.BOLD, 15)) Jmi2.setFont (new Font ("imitating Song", Font.BOLD, 15); JMenuItem jmi3 = new JMenuItem ("operating instructions"); jmi3.setFont (new Font ("imitating Song", Font.BOLD, 15)); JMenuItem jmi4 = new JMenuItem ("failure decision") Jmi4.setFont (new Font, Font.BOLD, 15); jm1.add (jmi1); jm1.add (jmi2); jm2.add (jmi3); jm2.add (jmi4); jmb.add (jm1) Jmb.add (jm2); mainFrame.setJMenuBar (jmb); / / menu Bar on JFrame jmi1.addActionListener (this); jmi1.setActionCommand ("Restart"); jmi2.addActionListener (this); jmi2.setActionCommand ("Exit"); jmi3.addActionListener (this) Jmi3.setActionCommand ("help"); jmi4.addActionListener (this); jmi4.setActionCommand ("lost");}
Implement ActionListener and rewrite the method actionPerformed
Implementation of actionPerformed method
Draw the game area
Draw the border of the game area
/ / draw the border private void drawBorder (Graphics g) {BasicStroke bs_2=new BasicStroke (12L BasicStroke. CAPPROUND ROUNDMagazine BasicStroke.JOINGUITER); Graphics2D gcircle 2d = (Graphics2D) g; g_2d.setColor (new Color (128128128)); g_2d.setStroke (bs_2); RoundRectangle2D.Double rect = new RoundRectangle2D.Double (6,6,313-1,413-1,2,2); g_2d.draw (rect);}
Draw the secondary area on the right (credit, next, button, etc.)
/ / draw the right area border private void drawBorderRight (Graphics g) {BasicStroke bs_2=new BasicStroke (12L Magnetics BasicStroke. JOINGITER); Graphics2D gcircle 2d = (Graphics2D) g; g_2d.setColor (new Color (128128128)); g_2d.setStroke (bs_2); RoundRectangle2D.Double rect = new RoundRectangle2D.Double (336,6140-1,413-1,2,2) G_2d.draw (rect); / / g_2d.drawRect (336,6140413);}
Override the paint method in BackPanel and call the two region drawing methods just now.
Draw the score area and the next area
/ / draw the integral area private void drawCount (Graphics g) {BasicStroke bs_2=new BasicStroke (2L BasicStroke. CAPPROUND ROUNDND BasicStroke.JOINGINGITER); Graphics2D gkeeper 2d = (Graphics2D) g; g_2d.setColor (new Color (0recover0)); g_2d.setStroke (bs_2); g_2d.drawRect (350,17,110,80) / score g.setFont (new Font ("Song style", Font.BOLD, 20); g.drawString ("score:", 380,40);} / / draw the next region private void drawNext (Graphics g) {BasicStroke bs_2=new BasicStroke (2L BasicStroke. G_2d.setStroke (bs_2); g_2d.drawRect (350,120,110,120); / / score g.setFont (new Font ("Song style", Font.BOLD, 20); g.drawString ("next:", 360,140);}
Draw the grid (15 columns and 20 rows)
/ / draw the grid private void drawGrid (Graphics g) {Graphics2D gloss2d = (Graphics2D) g; g_2d.setColor (new Color (255255255150)); int x1q12; int y1y2y20; int x2q312; int y2q20; for (int I = 0; I = 0 JMQ -) {/ / from the bottom to the top if (y > = jaquaj > 0) {/ / remove the row and the top row, and move all down, that is, this row is equal to the data of the previous row block = stack [I] [JMU1] If {block.setY (block.getY () + 1);} stack [I] [j] = block } else if (jackers 0) {/ / first line, clear stack [I] [j] = null;}}
Points rules: 100min for 1 line, 300min for 2lines, 600min for 3lines, 1000 points for 4lines
Show next
This is actually not difficult:
1. When you create the current model, create the next model and draw it.
two。 After the current model has hit bottom, set the next model as the current model.
3. Create a new model as the next model at the same time.
/ / create a model public void createModel (int type) {if (type==0) {/ / curModel = new Model at the beginning of the game; nextModel = new Model (xjournal yjournal this);} else {/ / curModel = nextModel in the game NextModel = new Model (xonomy this);}}
Draw 'next' in the paint method and display it in the next area on the right
/ / next model if {List blocks = nextModel.getBlocks (); Block block=null; for (int I = 0; I < blocks.size (); iTunes +) {block= (Block) blocks.get (I) Block.drawNext (g) }} join the automatic downward thread and start / / the game thread to automatically move down private class GameThread implements Runnable {@ Override public void run () {while (true) {if ("start" .equals (gameFlag)) {curModel.move (false, 1) } try {Thread.sleep (300);} catch (InterruptedException e) {e.printStackTrace ();}}
Finally, add points, button control, the end of the game, restart, and so on.
This is the end of the content of "how to write the code for the Java Tetris game". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.