Chase color Russian square source code

xiaoxiao2021-03-06  43

// First file: Game.java

Package realgame;

Import javax.swing.uimanager; import java.awt. *;

Public class game {boolean packframe = false;

// Construct the application public Game () {GameFrame frame = new GameFrame (); // Validate frames that have preset sizes // Pack frames that have useful preferred size info, eg from their layout if (packFrame) {frame.pack ( );} else {frame.validate ();} // Center the window Dimension screenSize = Toolkit.getDefaultToolkit () getScreenSize ();. Dimension frameSize = frame.getSize (); if (frameSize.height> screenSize.height) { frameSize.height = screenSize.height;} if (frameSize.width> screenSize.width) {frameSize.width = screenSize.width;} frame.setLocation ((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible (true); frame.setDefaultCloseOperation (3);} // main method public static void main (String [] args) {try {UIManager.setLookAndFeel ( "javax.swing.plaf . Metal.Metallookandfeel ");} catch (exception e) {E.PrintStackTrace (); NEW game (); }

/ / ======================================================================================================================================================================================================================================================================= One file, put the following contents into .package realgame;

Import java.awt. *; import java.awt.Event. *; import javax.swing. *; import javax.swing.event. *; import java.util. *; import javax.swing.timer; import java.aw .....

class GameFrame extends JFrame {private static final int WIDTH = 225; private static final int HEIGHT = 295; GamePanel gamePanel; public GameFrame () {setTitle ( "color blind search Tetris!"); setSize (WIDTH, HEIGHT); this.setResizable (false); GamePanel GamePanel = new gamepanel (); Container ContentPane = getContentPane (); contentpane.add (gamepanel); // GamePanel.Start (); try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}} Private void jbinit () throws exception {this.setResizable (false);}}

Class GamePanel Extends JPanel = 500; Private Static Final Int Rows = 20; Private Static Final Int Cols = 10; Private Static Int KeyEventValid = 1; TIMER T;

Private static int top = rows-1; private static int score; private int [】 [] screen = new int [rows] [cols]; // The information on the original screen, the score is from this private int [] [ ] View = new int [ROWS] [COLS]; // block in Screen Display View View

Private block presentblock; // Current activity block

Private block nextblock; // Next block to preview Private string stop = ""

// Block Position Private Int Row on the Screen; Private Int COL; Private INT

Public gamepanel () {keyhandler listener = new keyhandler (); addkeyListener; NextBlock = new block (0); col = (int) (cols-nextblock.maxcol); Precol = Coll This.setBackground; this.setLayout (New borderLayout ()); controlpanel cp = new controlPanel (); cp.addKeylistener (listner); cp.jl.addkeyListener (listner); cp.js.addKeylistener Listener; cp.startjb.addkeylistener; cp.Endgame.addKeylistener (listener); this.add (borderLayout.South, CP);}

Public void PaintComponent (graphics g) {super.paintcomponent (g); graphics2d g2 = (graphics2d) g;

// Draw a large box of the display area DrawBigRec (G2);

// According to the content of the View, the corresponding plaid FOR (INT i = 0; I

// Picture of preview area Final float xpos = 140.0F; Final float ypos = 60.0f; font f = new font ("serif", font.bold, 18); g2.setfont (f); g2.setpaint (Color). Red); g2.drawstring ("score", xpos, ypos); g2.SetPaint (color.green); g2.drawstring ("" score, xpos, ypos 20);

G2.SETPAINT (Color.green); G2.DrawString (STOP, XPOS, YPOS-30);

Final Double Prexpos = 140.0F; Final Double PREYPOS = 140.0f; Double preted = 60.0;

Rectangle2d Rect = New Rectangle2d.double (Prexpos, preypos, preted, prede); g2. setcolor; g2.fill (Rect);

Rectangle2d innerRect = new reccTangle2d.double (Prexpos 10, preypos 10, predege-20, preted-20); g2.setcolor (color.white); g2.fill (InnerRect);

For (int i = 0; i

} // ~ Panel's drawback

Public Boolean MoveLeft () {Return (Go (Row, Col-1));

Public Boolean Moveright () {Return (Go (Row, COL 1));}

Public Boolean MoveDown () {Return (Go (Row 1, Col));

// Return true if a collision occurs; Private Boolean Crash (int R, INT C, Block Temp) {Boolean Flag = False; IF ((R Temp.maxrow> Rows) || (C Temp.maxcol> Cols) Return True;

For (int i = 0; i ROWS ) || ((C Presentblock.maxcol> cols) || (c <0)) Return False;

For (int i = 0; i

IF (Flag) {row = r; col = C; setView ();

Return flag;}

/ / Draw a cell; Private Void Drawanelex (INT I, INT J, Graphics2D G2, Double XPOS, Double Ypos) {

Final Int Ed Margin = 10; Final Int InnerMargin = 1;

Rectangle2d Rect = New Rectangle2d.double (XPOS Margin J * 10, YPOS Margin i * 10, Edge, Edge);

G2.SETPAINT (Color.Red); G2.DRAW (Rect);

Rect = new rectangle2d.double (XPOS Margin J * 10 InnerMargin, Ypos Margin i * 10 InnerMargin, Edge-InnerMargin, Edge-InnerMargin); int colorchoser = 0; colorchoser = (int) (Math.random () * 11); if (colorchoser == 0) g2.SetPaint (color.blue); else if (colorchoser == 1) g2.setpaint (color.green); else if (colorchoser == 2) g2.setpaint (Color.range); Else if (colorchoser == 3) g2.setpaint (color.yellow); Else if (colorchoser == 4) g2.setpaint (color.magenta); Else if (colorchoser == 5) G2. SetPaint (color.red); else if (colorchoser == 6) g2.setpaint (color.yellow); else if (colorchoser == 7) g2.setpaint (color.cyan); else if (colorchoser == 8) G2 .SETPAINT (New Color (200, 100, 150)); Else IF (colorchoser == 9) g2.setpaint (color.green); Else IF (colorchoser == 10) g2.setpaint (color.pink); g2.fill (Rect) }

// Draw the outer box of the display area Private Void DrawBigRec (graphics2d g2) {final double r_width = 120; Final Double REC_HEIGHT = 220;

Rectangle2D rect = new Rectangle2D.Double (0,0, REC_WIDTH, REC_HEIGHT); Rectangle2D innerRect = new Rectangle2D.Double (10,10, REC_WIDTH-20, REC_HEIGHT-20); g2.setPaint (Color.orange); g2.fill (REC); g2.SetPaint (color.white); g2.fill (innerRect);

// Set the view private void setView () {// copy the contents in Screen into the View in the View (INT i = 0; I

// Projerate Block into View; for (int i = 0; i

// event handling private class KeyHandler implements KeyListener {Block temp = null; public void keyPressed (KeyEvent event) {int keyCode = event.getKeyCode (); if (keyCode == KeyEvent.VK_LEFT) {if (keyEventValid == 1) if () ()) setView ();} else if (keycode == keyevent.vk_right) {if (KeyEventValid == 1) IF (Moveright ()) setView ();} else f (keycode == keyevent.vk_down) { IF (KeyEventValid == 1) IF (MoveDown ()) setView ();} else if (keycode == keyevent.vk_up) {if (keyeventvalid == 1) {temp = new block (presentblock.bodytype); Temp.Rotate ();

IF (! Crash (Rash (ROW, COL, TEMP)) {Presentblock.Rotate (); setView ();} temp = null;

}} // ~ up}

Public void keyreleased (keyevent evenet) {} public void keytyped (keyevent evenet) {}}

Public Boolean isfocusTraversable () {Return True;}

Private int winScore () {int = 0; int cur = rows-1; while (Cur> = TOP) {IF (AllDone (CUR)) {inc = 100; score = 100; Removeme (Cur, TOP); TOP Else Cur -;} // ~ while return inc;} private boolean alldone (int R) {boolean flag = true; for (int C = 0; c t; r -) {for (int C = 0; c

If (! flag) {keyeventvalid = 0; // first lock anchor (); sett tempscore = Winscore (); if ((Tempscore> = 100) && (Tempscore) <300) STOP = " Well, "Else IF (Tempscore> = 300) stop =" Yes "; Else STOP =" "; setView ();

Presentblock = nextblock; row = 0;

/ / Ensure that DO {Col = (int) each time you enter;} while (col == precol); precol = col;

KeyEventValid = 1; NextBlock = new block (0); setView (); system.gc ();} if (top == 0) {keyeventvalid = 0; stop = "Fortunate"; setView (); t.stop );}}} Public void start () {KeyEventValid = 1; for (int R = 0; r

} // ~ start

Private void anchor () {

For (int R = 0; r

}

Private void settop () {int mytop = 0; int c = 0; while (mytop

Private void setspeed (int speted) {t.setdelay (clockinterval);

Private class controlpanel extends jpanel {jlabel jl = new jlabel ("speed control"); jslider js = new jslider (1, 5, 1); jbutton startjb = new jbutton ("start"); jbutton endgame = new jbutton ("End "); public ControlPanel () {setBackground (Color.orange); js.setBackground (Color.orange); startJB.setBackground (Color.orange); endGame.setBackground (Color.orange); setLayout (new GridLayout (2,2 )); Add (jl); add (js); js.addchange ()); font font = new font ("serif", font.plain, 12); add (startjb); jl.setfont (font) ); Startjb.setfont (font); startjb.addactionListener (New StartAction ()); add (endgame); endgame.setfont (font); endgame.addactionListener (new endction ());}

Private Class StartAction Implements ActionListener {Public Void ActionPerformed (ActionEvent Event) {Start ();}}

PRIVATE EndAction Implements ActionListener {Public Void ActionPerformed (ActionEvent Event) {System.exit (3);}}

private class speedTune implements ChangeListener {public void stateChanged (ChangeEvent event) {JSlider slider = (JSlider) event.getSource (); clockInterval = 1000 / slider.getValue (); if (! t = null) setSpeed ​​(slider.getValue () Startjb.RequestFocus ();}}

} // ~ CP

} // ~

Class Block {public int [] [] bodyinfo = new int [4] [4]; public int maxRow; public int body.

Public Block (INT Shape) {Final INT TYPES = 20; INT I; IF (Math.random () * types) 1; Else I = Shape; SetBody (i); Bodytype = i;} // ~ con

// setbody should be set to set the Bodytype Private Void SetBody (INT i) {

Switch (i) {casse 1: // (vertical bar) for (int R = 0; r

Break; case 5: // Reverse flash electrical bodyinfo [0] [1] = 1; BodyInfo [1] [0] = 1; bodyinfo [1] [1] = 1; BodyInfo [2] [0] = 1; MaxRow = 3; Break; Case 6: // Anti-flash flat body bodyinfo [0] [0] = 1; bodyinfo [0] [1] = 1; bodyinfo [1] [1] = 1; bodyinfo [ 1] [2] = 1; maxRow = 2; maxcol = 3; Break;

Case 7: // Soil BodyInfo [0] [1] = 1; Bodyinfo [1] [0] = 1; Bodyinfo [1] [1] = 1; BodyInfo [1] [2] = 1; maxrow = 2 = 3; Break; case 8: // Soil (90) Bodyinfo [0] [0] = 1; BodyInfo [1] [0] = 1; Bodyinfo [1] [1] = 1; bodyinfo [2 ] [0] = 1; MaxRow = 3; MaxCol = 2; Break; Case 9: // Soil (180) Bodyinfo [0] [0] = 1; BodyInfo [0] [1] = 1; BodyInfo [0 ] [2] = 1; Bodyinfo [1] [1] = 1; maxRow = 2; MaxCol = 3; Break; Case 10: // Soil (270) Bodyinfo [0] [1] = 1; BodyInfo [1 ] [0] = 1; BODYINFO [1] [1] = 1; Bodyinfo [2] [1] = 1; maxRow = 3; MaxCol = 2; Break; Case 11: // Anti-7-shaped BodyInfo [0] [ 0] = 1; bodyinfo [0] [1] = 1; Bodyinfo [1] [0] = 1; Bodyinfo [2] [0] = 1; maxRow = 3; MaxCol = 2; Break; Case 12: // Anti-7-shaped (90) BODYINFO [0] [0] = 1; Bodyinfo [0] [1] = 1; Bodyinfo [0] [2] = 1; BodyInfo [1] [2] = 1; MaxRow = 2; Break; Case 13: // Anti-7-shaped (180) Bodyinfo [0] [1] = 1; BodyInfo [1] [1] = 1; BodyInfo [2] [1] = 1 Bodyinfo [2] [0] = 1; maxRow = 3; MaxCol = 2; Break; Case 14: // Anti-7-shaped (270) Bodyinfo [0] [0] = 1; BodyInfo [1] [0] = 1; bodyinfo [1] [1] = 1; BodyInfo [1] [2] = 1; maxRow = 2; MaxCol = 3; Break;

Case 15: // 7 glyphs BODYINFO [0] [0] = 1; Bodyinfo [0] [1] = 1; Bodyinfo [1] [1] = 1; BodyInfo [2] [1] = 1; maxrow = 3 = 2; Break; case 16: // 7 font (90) bodyinfo [0] [2] = 1; bodyinfo [1] [0] = 1; bodyinfo [1] [1] = 1; bodyinfo [ 1] [2] = 1; MaxRow = 2; MaxCol = 3; Break; Case 17: // 7 glyph (180) BodyInfo [0] [0] = 1; BodyInfo [1] [0] = 1; BodyInfo [ 2] [0] = 1; Bodyinfo [2] [1] = 1; maxRow = 3; MaxCol = 2; Break; Case 18: // 7 glyph (270) BodyInfo [0] [0] = 1; BodyInfo [ 0] [1] = 1; Bodyinfo [0] [2] = 1; BodyInfo [1] [0] = 1; maxRow = 2; MaxCol = 3; Break;

Case 19: // field shape bodyinfo [0] [0] = 1; bodyinfo [0] [1] = 1; bodyinfo [1] [0] = 1; BodyInfo [1] [1] = 1; maxrow = 2 MaxCol = 2; Break; Case 20: // field type bodyinfo [0] [0] = 1; Bodyinfo [0] [1] = 1; Bodyinfo [1] [0] = 1; BodyInfo [1] [ 1] = 1; MaxRow = 2; MaxCol = 2; Break; / * case 21: for (int C = 0; c

Case 3: clear (); setbody (4); bodytype = 4; Break; case 4: clear (); setbody (3); bodytype = 3; Break;

Case 5: CLEAR (); setBody (6); bodytype = 6; break; case 6: clear (); setbody (5); bodytype = 5; Break;

Case 7: CLEAR (); setBody (8); BODYTYPE = 8; Break; Case 8: Clear (); setBody (9); bodytype = 9; break; case 9: clear (); setbody (10); bodytype = 10; Break; Case 10: Clear (); setBody (7); bodytype = 7; Break;

Case 11: clear (); setBody (12); bodytype = 12; Break; case 12: clear (); setbody (13); bodytype = 13; break; case 13: clear (); setBody (14); bodytype = 14; Break; Case 14: Clear (); setbody (11); bodytype = 11; break; case 15: clear (); setbody (16); bodytype = 16; break; case 16: clear (); setbody (17 Bodytype = 17; Break; Case 17: Clear (); setBody (18); bodytype = 18; break; case 18: clear (); setBody (15); bodytype = 15; Break;

Case 19: Break; Case 20: Break; / * Case 21: Clear (); setBody (1); bodytype = 1; break; cas 22; cament (); setbody (4); bodytype = 4; break; cas 23 : clear (); setbody (6); bodytype = 6; break; * /}}

Private void clear () {for (int R = 0; r

转载请注明原文地址:https://www.9cbs.com/read-70797.html

New Post(0)