Java Swing version Bulimia snake games

Source: Internet
Author: User

Package Test002;import Java.awt.color;import Java.awt.graphics;import java.awt.event.keyevent;import Java.awt.event.keylistener;import Java.io.serializable;import Java.util.linkedlist;import Java.util.Random;import Java.util.timer;import Java.util.timertask;import Javax.swing.jframe;import Javax.swing.joptionpane;import javax.swing.jpanel;/* * window + panel + fixed * 2--on the interface to draw the snake * 3--let the snake move * 4--Let the snake listen to the keyboard direction key command * 5--randomly generated beans and painted on the interface * 6--command snakes eat beans and long body * 7--Wall GA Me over! * 8--bites his tail GAME over! * 9--fix bug */public class Snakegame extends JFrame implements keylistener{private static final long serialversionuid = 1L; private int FW = 800; private int fh =;p rivate sp SP = null;p rivate int sex = 200; Snake joint element attribute private int sey = 200; private int sesize =;p rivate se se = null;p rivate Timer timer = new timer ();p rivate linkedlist<se> ses = new L Inkedlist<se> ();p rivate String direction = "Up"; The direction of the snake walking private int bx; Bean attribute private int by; private int bsize = sesize;p rivate Bean bean =Null;p rivate Random rand = new Random ();p rivate Boolean bean_is_eated = False;p ublic snakegame () {this.setalwaysontop (tr UE); this.setundecorated (true); This.getcontentpane (). SetBackground (Color.Black); this.setsize (800, 600); This.setlocationrelativeto (null); SP = new sp (); This.add (sp); this.setvisible (true); This.addkeylistener (this);}  Class SP extends jpanel{private static final long serialversionuid = 1l;public SP () {This.setopaque (false); This.getse (); Call to create a snake method This.getbean_not_on_snakebody ();//call to create a Bean method Timer.schedule (new TimerTask () {@Overridepublic void Run () {// The task that needs to be performed if ("Up". Equalsignorecase (direction)) {ses.removelast ();//Remove the last element//Add an element to the original first element Ses.addfirst (new SE ( Ses.getfirst (). Getsex (), Ses.getfirst (). Getsey ()-ses.getfirst (). Getsesize (), Ses.getfirst (). Getsesize ()));// On the basis of the original direction the tail grows a joint element object if (bean_is_eated) {ses.addlast (New SE (Ses.getlast (). Getsex (), Ses.getlast (). Getsey () + Ses.getlast (). Getsesize (), Ses.getlast (). Getsesize ())); bean_is_eated = false;} Judging if the wall against the wall GAME OVER! if (Ses.getfirst (). Getsey () <0) {Joptionpane.showmessagedialog (sp, "GAME over!", "Wall tip", joptionpane.default_ OPTION); Runtime.getruntime (). exit (0);}} if ("left". Equalsignorecase (direction)) {ses.removelast ();//Remove the last element//Add an element to the original first element Ses.addfirst (new SE ( Ses.getfirst (). Getsex ()-ses.getfirst (). Getsesize (), Ses.getfirst (). Getsey (), Ses.getfirst (). Getsesize ());// On the basis of the original direction the tail grows a joint element object if (bean_is_eated) {ses.addlast (New SE (Ses.getlast (). Getsex () +ses.getlast (). Getsesize (), Ses.getlast (). Getsey (), Ses.getlast (). Getsesize ())); bean_is_eated = false;} Determine if you are hitting the wall against the GAME! if (Ses.getfirst (). Getsex () <0) {Joptionpane.showmessagedialog (sp, "GAME over!", "Wall tip", joptionpane.default_ OPTION); Runtime.getruntime (). exit (0);}} if ("Down". Equalsignorecase (direction)) {ses.removelast ();//Remove the last element//add a new element based on the original first element Ses.addfirst (new SE ( Ses.getfirst (). Getsex (), Ses.getfirst (). Getsey () +ses.getfirst (). Getsesize (), Ses.getfirst (). Getsesize ()));// The tail grows on the base of the original direction of a joint element object if (bean_is_eated) {ses.addlast (nEW SE (Ses.getlast (). Getsex (), Ses.getlast (). Getsey ()-ses.getlast (). Getsesize (), Ses.getlast (). Getsesize ()); Bean_ is_eated = false;} Determine if you are hitting the wall against the GAME! if (Ses.getfirst (). Getsey () +sesize>fh) {Joptionpane.showmessagedialog (sp, "GAME over!", "Wall tip", Joptionpane.default_option); Runtime.getruntime (). exit (0);}} if ("Right". Equalsignorecase (direction)) {ses.removelast ();//Remove the last element//Add an element based on the original first element Ses.addfirst (new SE ( Ses.getfirst (). Getsex () +ses.getfirst (). Getsesize (), Ses.getfirst (). Getsey (), Ses.getfirst (). Getsesize ());// On the basis of the original direction the tail grows a joint element object if (bean_is_eated) {ses.addlast (New SE (Ses.getlast (). Getsex ()-ses.getlast (). Getsesize (), Ses.getlast (). Getsey (), Ses.getlast (). Getsesize ())); bean_is_eated = false;} Determine if you are hitting the wall against the GAME! if (Ses.getfirst (). Getsex () +sesize>fw) {Joptionpane.showmessagedialog (sp, "GAME over!", "Wall tip", Joptionpane.default_option); Runtime.getruntime (). exit (0);}} The snake bites its own body's judgment for (int i = 1; i < ses.size (); i++) {if (Ses.getfirst (). Getsex () ==ses.get (i). Getsex () && Ses.getfirst (). Getsey () ==ses.get (i). Getsey () {joptionpane.showmessagedialog (SP), "You are a pig, bite your body!" "," self-inflicted, brother! ", joptionpane.default_option); Runtime.getruntime (). exit (0);}}  Sp.eatbean (); Method of calling Pacman Sp.repaint ();}} , 0, 200);} @Overridepublic void Paint (Graphics g) {for (int i = 0; i < ses.size (); i++) {//Draw beans G.setcolor (color.white); G.fill3drec T (BEAN.GETBX (), Bean.getby (), Bean.getbsize (), Bean.getbsize (), true);//Draw the Snake if (i==0) {g.setcolor (color.red);} Else{g.setcolor (Color.green);} G.fill3drect (Ses.get (i). Getsex (), Ses.get (i). Getsey (), Ses.get (i). Getsesize (), Ses.get (i). Getsesize (), True);}} public void Eatbean () {///The Bean is reached//the head coordinate of the snake and the coordinates of the bean are coincident if (Ses.getfirst (). Getsex () ==BEAN.GETBX () && Ses.getfirst (). Getsey () ==bean.getby ()) {bean = new bean (); Sp.getbean_not_on_snakebody (); bean_is_eated = True;}} public void Getbean_not_on_snakebody () {This.getbean ();//* beans can not appear on the body of the snake with BX, by the same time and the body of the snake every element of the comparison is guaranteed not congruent for (int i = 0; I < ses.size (); i++) {if (Ses.get (i). Getsex () = = BEAN.GETBX ()&& Ses.get (i). Getsey () = = Bean.getby ()) {This.getbean ();}}} public void Getbean () {//* cannot appear on the right edge of bx! = fw//* The x-coordinate of the bean must be divisible (the remainder is 0) the size of the snake BODY element bx% Sesize = 0while (true) {bx = R And.nextint (FW); SYSTEM.OUT.PRINTLN (BX); if (bx! = FW && bx%sesize==0) {break;}} * cannot appear on the bottom of the border by! = FH//* The y-coordinate of the beans must be divisible (the remainder is 0) the size of the snake body element by% Sesize = 0 for (;;) {by = Rand.nextint (FH), if (by! = fh && by%sesize==0) {break;}} Bean = new Bean (bx, by, bsize); System.out.println (bean);}  public void Getonesedata () {///get a snake joint element data}public void Getse () {//Get a Snake joint object for (int i = 0; i < 4; i++) {se = new se (Sex, Sey+sesize*i, sesize); Ses.add (SE);}} public void Getsnake () {//Get a whole snake}} @Overridepublic void keypressed (KeyEvent e) {//direction cannot be changed to the opposite direction for example: Left cannot be modified to Rightif (E . getKeyCode () ==keyevent.vk_left &&! " Right ". Equalsignorecase (direction)) {direction = ' left ';} if (E.getkeycode () ==keyevent.vk_down &&! " Up ". Equalsignorecase (direction)) {direction = ' down ';} if (E.getkeycode () ==keyevent.vk_right &&! " Left ". Equalsignorecase (direction)) {direction = ' right ';} if (E.getkeycode () ==keyevent.vk_up &&! " Down ". Equalsignorecase (direction)) {direction = ' up ';} if (E.getkeycode () ==32) {Runtime.getruntime (). exit (0);}} public static void Main (string[] args) {new Snakegame ();} @Overridepublic void keytyped (KeyEvent e) {} @Overridepublic void keyreleased (KeyEvent e) {}}class SE implements Serializa ble{/** * Snake's Joint object */private static final long serialversionuid = 1l;private int sex; private int sey; private int sesize ;p ublic SE () {super ();//TODO auto-generated constructor stub}public SE (int sex, int sey, int sesize) {super (); this.sex = Sex;this.sey = Sey;this.sesize = sesize;} public int Getsex () {return sex;} public void setsex (int sex) {this.sex = sex;} public int Getsey () {return sey;} public void Setsey (int sey) {This.sey = Sey;} public int getsesize () {return sesize;} public void setsesize (int sesize) {this.sesize = sesize;} @Overridepublic int hashcode () {Final INT Prime = 31;int result = 1;result = Prime * result + Sesize;result = Prime * result + Sex;result = Prime * result + sey; return result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false;se other = (SE) obj;if (sesize! = other.sesize) return false;if (sex! = Other.sex) return false;if (Sey! = Other.sey) return False;return true;} @Overridepublic String toString () {return "SE [sesize=" + Sesize + ", sex=" + Sex + ", sey=" + Sey + "]";}} Class Bean implements Serializable {/** * Bean object */private static final long serialversionuid = 1l;private int bx;p rivate int by; private int bsize;p ublic bean () {super ();//TODO auto-generated constructor stub}public Bean (int bx, int by, int bsize) { Super (); this.bx = bx;this.by = By;this.bsize = bsize;} public int getbx () {return bx;} public void setbx (int bx) {THIS.BX = bx;} public int Getby () {return by;} public void Setby (int.) {this.by = by;} public int GETBSIZe () {return bsize;} public void setbsize (int bsize) {this.bsize = bsize;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + Bsize;result = prime * Resul T + bx;result = Prime * result + By;return result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; Bean other = (bean) obj;if (bsize! = other.bsize) return false;if (bx! = OTHER.BX) return false;if (by! = other.by) return FA Lse;return true;} @Overridepublic String toString () {return "Bean [bsize=" + Bsize + ", bx=" + bx + ", by=" + by + "]";}}

Java Swing version Bulimia snake games

Related Article

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.