Welcome to enigma blog

zhaozj2021-02-16  34

public class Game extends java.awt.Frame {public Game () {setTitle ( "Tetris game - Ghost Valley"); addWindowListener (new java.awt.event.WindowAdapter () {public void windowClosing (java.awt.event. WindowEvent E) {Dispose (); System.exit (0);}}); Gamecanvas Gamecanvas = New Gamecanvas (); setLayout (new java.awt.borderLayout ()); add (gamecanvas, "center"); setResizable false); setLocation (... (java.awt.Toolkit.getDefaultToolkit () getScreenSize () width-gameCanvas.getPreferredSize () width) / 2, (java.awt.Toolkit.getDefaultToolkit () getScreenSize () height-.. Gamecanvas.getPreferredSize (). HEIGHT / 2); Pack (); show ();} public static void main (String args []) {new game ();} java.awt.canvas urgents runnable { Public static final int bksize = 5; public static final int bdWidth = 20; public static final int bdheight = 30; // Inner Class Block Pr Ivate class block {public boolean bkdata [] [] = new boolean [bksize] [bksize]; public int RPOS, CPOS; Public Block (Final Boolean Bkdata1 [] [], Int rpos, int CPOS) {//this.bkdata = (Boolean [] []) bkdata1.clone (); for (int i = 0; i

For (int J = 0; J

THISBKDATA [I] [j] = bkdata1 [i] [j]; this.rpos = rpos; this.cpos = cpos;} public block (final block bk) {//this.bkdata = (Boolean [] [ ]) BK.BKDATA.CLONE (); for (int i = 0; i

For (int J = 0; J

THIS.BKDATA [I] [J] = BK.BKDATA [I] [J]; this.rpos = bk.rpos; this.cpos = bk.cpos;} PUBLIC VOID DoAction (int action) {switch (action) { case Message.MOVE_UP: this.rpos--; break; case Message.MOVE_DOWN: this.rpos ; break; case Message.MOVE_LEFT: this.cpos--; break; case Message.MOVE_RIGHT: this.cpos ; break; case Message .ROTATE_CLOCK: {Final INT X0 = Bksize / 2; Final Int Y0 = Bksize / 2; // Boolean Bkdata1 [] [] = (Boolean [] []) this.bkdata.clone (); boolean bkdata1 [] [] = New boolean [bksize] [bksize]; for (int i = 0; ifor (int J = 0; J

BKDATA1 [I] [J] = this.bkdata [i] [j]; for (int x = 0; x

For (int y = 0; y

THIS.BKDATA [Y] = BKDATA1 [X] [Y]; Break;} case message.Rotate_anticlock: {Final Int x0 = bksize / 2; Final Int Y0 = Bksize / 2; // Boolean bkdata1 [] [] = (boolean [] []) this.bkdata.clone (); boolean bkdata1 [] [] = new bolean [bksize] [bksize]; for (int i = 0; i

For (int J = 0; J

BKDATA1 [I] [J] = this.bkdata [i] [j]; for (int x = 0; x

For (int y = 0; y

THISBKDATA [-Y 2 * x0] [x] = bkdata1 [x] [y]; break;}}}} // inner class board / private class board}}} // inner class board / private class board {public boolean bddata [] [] = new boolean [ BDHEIGHT] [BDWIDTH]; public board () {clear ();} public board (final board) {//this.bddata = (boolean [] []) Board.bddata.clone (); for (int i = 0; IFOR (int J = 0; J

this.bddata [i] [j] = Board.bddata [i] [j];} public void clear () {for (int i = 0; i

For (int J = 0; J

BDDATA [I] [J] = false;}}

// Inner class Message / private class Message {public static final int MOVE_UP = 1; public static final int MOVE_DOWN = 2; public static final int MOVE_LEFT = 3; public static final int MOVE_RIGHT = 4; public static final int ROTATE_CLOCK = 5; // Rotate Clock-Wise Public Static Final Int Rotate_anticlock = 6; // Rotate AnticLock-Wise Public Message (Int Action) {this.Action = action;} public int getAction; } // Inner class Queue /// fields of class Game private Board m_board = new Board (); // only access by the message processor thread private block m_block = null; // block currently existing private MessageQueue m_msgQueue = new MessageQueue ( ); // awt thread by a message processor and access private BlockDataPoolQueue m_blockDataPoolQueue = new BlockDataPoolQueue (); private int m_S = 0; / method run main message processing thread public void run () {boolean bkdataPool [] [] [] =

{{{False, false, true, false, false}, {false, false, true, false, false}, {false, false, true, false, false}, {false, false, true, false, false}, {False, False, true, false, false}}, {false, false, false, false, false}, {false, true, true, true, false}, {false, false, true, false, false}, {False, False, True, false, false}, {false, false, true, false, false}, false, false, false, false, false}}, {{false, false, false, false, false}, {False, False, True, False, true, true, true, false}, {false, false, true, false, false}, {false, false, false, false, false}}} {False, False, false, false, false}, {false, false, false, false, false}, {false, true, true, true, false}, {false, true, false, true, false}, {FALSE , False, False, false, false}}, {{false, false, false, false, false}, {false, false, false, false, false} , {False, true, true, {false, false, true, false, false}, false, false, false, false, false}}}; repainTrequest (); while (true) {switch ( m_S) {case 0: try {Thread.currentThread (.) sleep (50);} catch (InterruptedException ex) {} break; case 1: Block bk1 = new Block (m_blockDataPoolQueue.getNextBlockData (), 0, (BDWIDTH-BKSIZE )/2);

IF (iScollided (M_Board, BK1)) {m_block = BK1; M_S = 2;} else m_s = 3; repaintRequest (); break; case 2: message msg; synchronized (m_msgqueue) // Wait a new message {while (WHILE) m_msgqueue.empty ()) {Try {m_msgqueue.wait ();} catch (interruptedException e) {}} msg = (message) m_msgqueue.dl ();} block bk2 = new block (m_block); bk2.doAction (MSG) .getAction ()); if (! iScollided (M_Board, BK2)) {// Action can perform m_block.doAction (msg.getAction ()); m_s = 2; repaintRequest ();} else if () == Message.Move_down) {// Down movement The action cannot perform Merge (M_Board, m_block); m_block = null; packupboard (m_board); m_s = 1; repaintRequest ();} Break; Case 3: try {thread.currentthread (). Sleep (50);

} CatCH (Exception EX) {} Break;}}} / method iSclLided / Private Boolean ISCOLLIDED (Final Block BK1, Final Block BK2) // Determine whether the two blocks overlap {for (int R1 = 0; R1 {INT R2 = R1 BK1.RPOS-BK2.RPOS; IF (R2> = 0 && r2

{

For (int C1 = 0; C1

{

INT C2 = C1 BK1.CPOS-BK2.CPOS;

IF (C2> = 0 && C2

IF (BK1.BKDATA [R1] [C1] && Bk2.bkdata [R2] [C2])

Return True;

}

}

}

Return False;

}

Private Boolean IsCollid (Final Board Board, Final Block Block) / / Determine whether a block is overlapping

{

For (int i = 0; i

For (int J = 0; J

IF (Block.rpos i> = 0 && Block.rpos i

= 0 && Block.cpos J

IF (block.bkdata [i] [j] && board.bddata [i block.rpos] [j block.cpos])

Return True;

Else IF (block.bkdata [i] [j])

Return True;

}

}

Return False;

}

Private Void Merge (Board Board, Final Block Block) // Merged Board <=== Block

{

For (int i = 0; i

For (int J = 0; J

IF (block.rpos i> = 0 && block.rpos i <= bdheight-1 && block.cpos j> = 0 && block.cpos j <= bdwidth-1)

Board.bddata [block.rpos i] [block.cpos j] | = block.bkdata [i] [j];

}

Private int packupboard (Board board) // eliminate

{

INT LINESDELETED = 0;

For (INT i = bdheight-1; i> = 0;) {// Detect the i-th line

Boolean full = true;

For (int J = 0; J

IF (! board.bddata [i] [j]) {

Full = false;

Break;

}

}

IF (full) {// 第 第 行 行

For (int K = I-1; k> = 0; k -) {

For (int C = 0; C

Board.bddata [K 1] [C] = Board.bddata [K] [C];

}

For (int C = 0; C

Board.bddata [0] [C] = false; LINESDELETED ;

Else

I-;

}

Return LinesDeled;

}

// Constructor final int x0 = 10, y0 = 15; final int cellsize = 12; public java.awt.dimension getPreferredSize () {return new java.awt.dimension (2 * x0 cellsize * bdwidth, 2 * Y0 Cellsize * BDHEIGHT);} public GameCanvas () {setBackground (java.awt.Color.black); addKeyListener (new java.awt.event.KeyAdapter () {public void keyPressed (java.awt.event.KeyEvent e) {switch ( e.getKeyCode ()) {case java.awt.event.KeyEvent.VK_LEFT: postMessage (new Message (Message.MOVE_LEFT)); break; case java.awt.event.KeyEvent.VK_RIGHT: postMessage (new Message (Message.MOVE_RIGHT )); Break; case java.awt.event.keyevent.vk_down: postmessage (new message.move_down); Break; Case Java.awt.Event.Keyevent.vk_up: postMessage (new Message (Message.ROTATE_CLOCK)); break; case java.awt.event.KeyEvent.VK_ENTER: postMessage (new Message (Message.ROTATE_ANTICLOCK)); break; case java.awt.event.KeyEvent.VK_PAGE_UP: postMessage ( New Message (Message.Move_UP); Break; Case Java.awt.Event.keyevent.vk_space: if (m_s == 0) m_s = 1; Break; Case Java.awt.Event.Keyevent.vk_f1: if (m_s = = 3) {m_Board.clear ();

m_s = 0; repaintRequest ();}}}}; new thread () {public void run () {while (TRUE) {TRY {Sleep (500);} catch (interruptedException E) {} Postmessage (New Message Message.move_down));}}} .start (); new thread (this) .start (); // message processing thread} public void postmessage (message msg) {if (m_s == 1 || m_s == 2 ) {synchronized (m_msgQueue) {m_msgQueue.en (msg); m_msgQueue.notify ();}}} public void repaintRequest () {repaint ();} public void paint (java.awt.Graphics g) {// draw m_board G.SetColor (java.awt.color.blue); g.drawRect (X0, Y0, Cellsize * bdwidth-1, cellsize * bdHeight-1); switch (m_s) {case 1: CA SE 2: G.SetColor (java.awt.color.yellow); Break; Case 3: G.SetColor (java.awt.color.lightgray); Break;} for (int i = 0, y = y0; IFOR) INT J = 0, x = X0; J

IF (m_board.bddata [i] [j]) g.fillRect (x, y, cellsize-1, cellsize-1);}} // Draw next block show g.setcolor (java.awt.color.lightgray); Boolean nextblockdata [] [] = m_blockdataPoolQueue.peeknextblockdata (); for (int i = 0, y = y0; i

For (int J = 0, x = x0 (bdwidth-bksize) * cellsize; J

IF (NextBlockData [i] [j]) g.drawRect (x 1, y 1, cellsize-1, cellsize-1);}} // DRAW M_BLOCK IF (M_S == 2) {G.SetColor (Java .awt.color.red; for (int i = 0, y = y0 m_block.rpos * cellsize; ifor (int J = 0, x = x0 m_block.cpos * cellsize; J

IF (m_block.bkdata [i] [j]) g.fillRect (x, y, cellsize-1, cellsize-1);}}} g.setColor (java.awt.color.red); g.setFont (New Java.awt (). getFontName (). G.Getfont (). getStyle (), 16)); Switch (m_s) {case 0: g.drawstring ("Press the space button to start the game!" , 50, 100); Break; Case 3: g.drawstring ("The game ends, press F1 to start!", 20, 100); Break;}}

private class BlockDataPoolQueue extends Queue {public BlockDataPoolQueue () {this.currentBlockData = bkdataPool [ran.nextInt (bkdataPool.length)];} public boolean [] [] peekNextBlockData () {return this.currentBlockData;} public boolean [] [] getNextBlockData () {boolean [] [] bkd = this.currentBlockData; this.currentBlockData = bkdataPool [ran.nextInt (bkdataPool.length)]; return bkd;} private boolean [] [] currentBlockData = null; private java.util. Random Ran = new java.util.random (); private boolean bkdatapool [] [] [] =

{{{False, false, true, true, false}, {false, false, true, false, false}, {false, false, true, false, false}, {false, true, true, false, false}, {False, False, False, false, false}}, {{false, false, false, false, false}, {false, true, true, false, false}, {false, false, true, false, false}, {False, False, True, True, False}, {False, False, False, false, false}}, {{false, false, false, false, false}, {false, false, true, false, false}, {False, true, true, true, false}, false, false}, false, false}, false, false, false, false, false}}, {{False, False, False, false, false}, {False, False, false, false, false}, {false, true, true, true, false}, {false, false, false, true, false}, false, false, false, false, false}}} {False, false, false, false, false}, {false, true, false, false, false}, {false, false, true, false, false}, {False, False, false, false, false}, false, false}}, false, false}}, {{false, false, false, false, false}, {false, false, false, false, false}, FALSE {False, false, true, false, false}, {false, false, false, false, false}, {false, false, false, false, false}}, {{false, false, false, false, false}, {False, true, true, false, false}, {false, true, true, false, false}, false, false, false, false, false}, {false, false, false, false, false}}}} {False, False, false, false, false}, false, false, false, false}, {false, true, true, true, false}, {false, false, true, false, false}, {FALSE , False, False, False, False}}};

} Private name {private node head; private node tail; public queue () {head = new node (); tail = new node (); head.prev = tail; tail; head.next = tail; tail. Prev = head;} ​​public void en (object item) {node q = new node (); q.data = item; q.next = tail; q.prev = tail.prev; tail.prev.next = q; tail .prev = q;} public object dl () {if (! EMPTY ()) {node p = head.next; head.next.next.prev = head; head.next = head.next.next; returnitp. Data;} else return null;} public object peek () {if (! EMPTY ()) Return head.Next.data; else return null;} public boolean EMPTY () {return (head.next == tail) || (tail.prev == head);} public void clear () {head.next = tail; tail.prev = head;} ​​public int elemnum () {Int Num = 0; for (Node Q = head.next ;! q = tail; q = q.next) num ; return num;} private class Node {Object data; Node prev; Node next;}} // inner class MessageQueue private class MessageQueue {private Node head; private Node tail; Public messagequeue () {head = new node (); tail =

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

New Post(0)