Snake (two-dimensional array)

Source: Internet
Author: User

This is my thinking a day of the game, missed two lessons, heartache Ah, total time 2 weeks, moved!!!

Import Java.awt.borderlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.keyevent;import Java.awt.event.keylistener;import Javax.swing.jbutton;import Javax.swing.JFrame; Import Javax.swing.joptionpane;import Javax.swing.jpanel;import Javax.swing.jspinner;import Javax.swing.event.changeevent;import Javax.swing.event.changelistener;public class SupervisoryControlDesk extends JFrame implements Actionlistener,changelistener, KeyListener {private static final long Serialversionuid = 1l;protected D Ataofmatrix Dataofmatrix; Paint_canvas Paint_canvas; Gluttonoussnake Gluttonoussnake; JButton Btn_control, Btn_reflesh; JSpinner spinner;public Supervisorycontroldesk () {Super ("Snake-(Peace of Mind)");d Ataofmatrix = new Dataofmatrix ();p Aint_canvas = New Paint_canvas (Dataofmatrix); gluttonoussnake = new Gluttonoussnake (this, Dataofmatrix, Paint_canvas); SetLayout (new BorderLayout ());p Lusnorth ();p luscenter () setbounds (0, 615, 678); Setdefaultcloseoperation (Exit_on_cloSE); setvisible (true);} private void Pluscenter () {Getcontentpane (). Add (Paint_canvas);} private void Plusnorth () {Btn_control = new JButton ("Start"); Btn_control.addactionlistener (this); spinner = new JSpinner (); Spinner.setvalue (); Spinner.addchangelistener (this); JPanel panel = new JPanel (), Getcontentpane (). Add (Panel, "North");p Anel.add (Btn_control);p anel.add (spinner); Btn_ Reflesh = new JButton ("Reflesh");p Anel.add (Btn_reflesh); Btn_reflesh.addactionlistener (this);} public static void Main (string[] args) {new Supervisorycontroldesk ();} @Overridepublic void actionperformed (ActionEvent e) {if (E.getactioncommand (). Equalsignorecase ("Start")) {Btn_ Control.settext ("End"); Btn_control.addkeylistener (this); GluttonousSnake.timer.restart (); Gluttonoussnake.isstart = True;dataofmatrix.reflesh ();} if (E.getactioncommand (). Equalsignorecase ("End")) {Btn_control.settext ("Start"); Btn_control.removekeylistener ( this); GluttonousSnake.timer.stop (); gluttonoussnake.isstart = false;} if (e.getsource () = = Btn_reflesh){if (Dataofmatrix! = null) {Dataofmatrix.reflesh ();p aint_canvas.repaint (); Btn_control.requestfocus ();}}} @Overridepublic void statechanged (ChangeEvent e) {try {int size = Integer.parseint ("" +spinner.getvalue ()); if (size< 10| | size>1000) {size = 500;} GluttonousSnake.timer.setDelay (size); Btn_control.requestfocus ();} catch (NumberFormatException E1) {Joptionpane.showconfirmdialog (this, "the value entered is wrong, please enter!!");}} @Overridepublic void keytyped (KeyEvent e) {//not} @Overridepublic void keypressed (KeyEvent e) {// Target Gluttonoussnake.snakemove_listiner (e);} @Overridepublic void keyreleased (KeyEvent e) {//Not}}
public class Dataofmatrix {int foodnumber = 10;int width, height;//column int[][] snakematrix;public Dataofmatrix () {Set (30,3 0);d Ecoratefood ();} public void Set (int width,int height) {this.width = Width;this.height = Height;snakematrix = new int[width][height];//test data for (int i = 0; i < width; i++) {//snakematrix[i][0] = 4;//snakematrix[i][height-1] = 4;//}//for (int j = 0; J < H Eight J + +) {//snakematrix[0][j] = 4;//snakematrix[width-1][j] = 4;//}}public void Decoratefood () {//Decoratefood decorate food int[] tem p = Decoratefood_number (); for (int i = 0; i < temp.length; i++) {int text_i = Temp[i]/height;int text_j = temp[i]% h Eight;snakematrix[text_i][text_j] = 4;//food for 4}}public int[] Decoratefood_number () {//decorate the number of food in the specified range int[] mat = new INT[FO odnumber];for (int i = 0; i < mat.length; i++) {Mat[i] = Isexist (Mat, GetRandomNumber ());} return mat;} public int isexist (int[] mat, int. rand) {for (int i = 0; i < mat.length; i++) {if (Rand = = Mat[i]) {rand = Getrandomnum ber (); rand = IsexiSt (Mat, Rand);}} return rand; public int GetRandomNumber () {int temp = (int) (Math.random () * +)% (width * height); return temp; public void Reflesh () {for (int i = 0; i < snakematrix.length; i++) {for (int j = 0; J < Snakematrix[i].length; J + +) {Snakematrix[i][j] = 0;}} Decoratefood (); System.out.println ("Refresh");}}

Import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.keyevent;import Javax.swing.timer;public class Gluttonoussnake implements ActionListener {static int countsnake_number = 6; Supervisorycontroldesk Supervisorycontroldesk;dataofmatrix Dataofmatrix; Paint_canvas Paint_canvas; Snakepartly[] Smallsnake; Timer Timer;boolean isstart;public gluttonoussnake (Supervisorycontroldesk Supervisorycontroldesk,dataofmatrix Dataofmatrix, Paint_canvas paint_canvas) {This.supervisorycontroldesk = Supervisorycontroldesk;this.dataofmatrix = Dataofmatrix;this.paint_canvas = Paint_canvas;bornfromsnakebody (); timer = new timer (this); Timer.setdelay (500);} private void Bornfromsnakebody () {smallsnake = new snakepartly[17];//No. 0 no//1+10+1 head-body-tail, first unused, used for direction int[] temp = new int [4];temp[1] = 1;//default down for (int i = 0; i < smallsnake.length; i++) {Smallsnake[i] = new snakepartly (15-i, 8, temp);} Smallsnake[1].setshape (1); Smallsnake[2].setshape (2); Smallsnake[3].setshape (2); Smallsnake[4].setshape (2); Smallsnake[5].setshape (3);} public void Snakemove_listiner (KeyEvent e) {Smallsnake[0].reflesh (); if (e.getkeycode () = = keyevent.vk_up) {smallsnake[ 0].directions[0] =-1;} if (e.getkeycode () = = Keyevent.vk_down) {smallsnake[0].directions[1] = 1;} if (e.getkeycode () = = Keyevent.vk_left) {Smallsnake[0].directions[2] =-1;} if (e.getkeycode () = = Keyevent.vk_right) {smallsnake[0].directions[3] = 1;}} public void Snake_move () {//Erase last element dataofmatrix.snakematrix[smallsnake[countsnake_number-1].x][smallsnake[ COUNTSNAKE_NUMBER-1].Y] = 0;for (int i = countsnake_number-1; i > 0; i--) {//I = 5 initial not less than 5//change direction for (int j = 0; J & Lt Smallsnake[i].directions.length; J + +) {Smallsnake[i].directions[j] = smallsnake[i-1].directions[j];} int text_i = smallsnake[i].x + smallsnake[i].directions[0]+ smallsnake[i].directions[1];int text_j = smallSnake[i].y + SM allsnake[i].directions[2]+ smallsnake[i].directions[3];//against the wall with clockwise direction//0 on 1 2 left 3 right if (Text_j < 0) {//Left Text_j =Dataofmatrix.width-1;} if (Text_j = = dataofmatrix.width) {//Right text_j = 0;} if (Text_i < 0) {//on text_i = dataofmatrix.height-1;} if (text_i = = dataofmatrix.height) {//under text_i = 0;}  Eat from the beginning, the tail length if (i = = 1 && dataofmatrix.snakematrix[text_i][text_j] = = 4) {//gluttonous, can only be the head to eat countsnake_number++;for (int J = 0; J < 4; J + +) {smallsnake[countsnake_number-1].directions[j] =smallsnake[countsnake_number-2].directions[j];} Smallsnake[countsnake_number-1].x =smallsnake[countsnake_number-2].x+-(Smallsnake[countsnake_number-1]. Directions[0]+smallsnake[countsnake_number-1].directions[1]); Smallsnake[countsnake_number-1].y =smallSnake[ countsnake_number-2].y+-(Smallsnake[countsnake_number-1].directions[2]+smallsnake[countsnake_number-1]. DIRECTIONS[3]); smallsnake[countsnake_number-2].body_shape = 2;smallsnake[countsnake_number-1].body_shape = 3;} Change the position of the snake smallsnake[i].x = Text_i;smallsnake[i].y = text_j;//Put the data of the snake into the array dataofmatrix.snakematrix[text_i][text_j] = Smallsnake[i].body_shape;}} @Overridepublic VOID actionperformed (ActionEvent e) {//Timer call if (!isstart) {return;} if (Countsnake_number > 15) {//Snake peeling, can also eat snake skin System.out.println ("eat full"); Countsnake_number = 6;smallsnake[countsnake_ Number-1].body_shape = 3;isstart = false;dataofmatrix.snakematrix[0][0] = 10;paint_canvas.repaint (); SupervisoryControlDesk.btn_control.setText ("Start"); return;} Snake_move ();p aint_canvas.repaint ();}} Class Snakepartly {public int x, y;public int[] directions = new Int[4];p ublic int body_shape;public snakepartly (int. x, in T y, int[] location) {Set (x, Y, location, 0);//default does not draw}public void Reflesh () {if (directions! = null) {for (int i = 0; I &lt ; Directions.length; i++) {Directions[i] = 0;}}} public void set (int x, int y, int[] directions, int body_shape) {this.x = X;this.y = y;for (int i = 0; i < DIRECTIONS.L Ength; i++) {This.directions[i] = directions[i];} This.body_shape = Body_shape;} public void Setshape (int shape) {this.body_shape = shape;} public void setlocation (int[] "location" {for (int i = 0; i < lOcation.length; i++) {Directions[i] = Location[i];}} public void setlocation (int n) {int temp[] = new Int[4];if (n = = 0 | | n = = 2) {Temp[n] =-1;} if (n = = 1 | | n = = 3) {Temp[n] = 1;} setlocation (temp);}}
Import Java.awt.canvas;import java.awt.color;import Java.awt.font;import Java.awt.graphics;public class Paint_canvas Extends Canvas {private static final long Serialversionuid = 1l;dataofmatrix Dataofmatrix;boolean ischangcolor;public Pai Nt_canvas (Dataofmatrix dataofmatrix) {This.dataofmatrix = dataofmatrix;this.setsize (600, 600);} @Overridepublic void Paint (Graphics g) {if (Dataofmatrix! = null) {if (dataofmatrix.snakematrix[0][0]==10) {Font Fontgameover = new Font ("Arial", Font.Bold, G.setfont (fontgameover); G.setcolor (color.red); g.DrawString ("Game Over", 120, 180);}  for (int i = 0; i < dataofmatrix.width; i++) {G.setcolor (Color.Blue); G.drawline (I *, 0, I * 20, 600);//Column}for (int j = 0; J < Dataofmatrix.height; J + +) {G.setcolor (Color.Blue); G.drawline (0, J *, +, J * 20);//Line}for (int i = 0; i < dataofmatrix.width; i++) {for (int j = 0; J < Dataofmatrix.height; J + +) {if (dataofmatrix.snakematrix[i][j] = = 4) {if (Ischangcolor) {g.setcolor (color.blue);} Else{g.setcolor (color.red);} Ischangcolor =!ischangcolor;g.filloval (J * 20,i * 20, 20, 20);//Line}if (dataofmatrix.snakematrix[i][j] = = 3) {G.setcolor (C Olor.black); G.drawline (J *, I *, J * + 20);//Upper G.drawline (J * + Ten, I *, J *, I * 20 + 20);//Middle , left G.drawline (J * + Ten, I *, J * + 20,i * 20 + 20);//Middle, right bottom}if (dataofmatrix.snakematrix[i][j] = 2) {G.setcolo R (Color.gray); G.fillrect (J * 20,i * 20, 20, 20);} if (dataofmatrix.snakematrix[i][j] = = 1) {G.setcolor (color.black); G.filloval (J * 20,i * 20, 20, 15);}}}}




















Snake (two-dimensional array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.