Dieser Artikel beschreibt das von dem Java Swing Framework implementierte gierige Schlangenspiel. Teilen Sie es für Ihre Referenz wie folgt weiter:
Java ist eine hochrangige Sprache und ist für den Hintergrund bei Spielen geeignet, aber Sie können sie auch verwenden, um Spiele zu machen. Wenn Sie nichts zu tun haben, können Sie es verwenden, um einige kleine Spiele zu schreiben, um vorzeitiges Altern zu verhindern, haha!
Rede nicht mehr
Das Folgende sind die Arbeiten, die ich zuvor geübt habe. Ich habe keine Angst, dass alle Witze machen. Ich habe eine Woche gebraucht, um es zu schaffen.
Der Quellcode ist für alle zu lernen.
Verwenden Sie Java Swing JFrame Jpanel Jbutton natürlich können Sie auch AWT verwenden
Schauen wir uns den Laufeffekt an:
Spezifischer Code:
Paket tcs;/** * * * * @author tx */import java.awt.color; import Java.awt.Container; Import Java.awt.font; import Java.awt.graphics; Import Java.awt.awt.event; java.awt.event.mouseevent; import java.awt.event.mouselistener; import Java.util.ArrayList; Import Java.util.Arrays; Java.util.Collection; Import Java.util.random; javax.swing.jframe; import javax.swing.jpanel; public class snack erweitert public arrayList <the Treasure> bw = new ArrayList <geschäfte> (); öffentlicher Körper [] B = neuer Körper [5]; Public String State = ""; public arrayList <Point> p = new ArrayList <Point> (); öffentliche statische Int -Score; public snack () {this.addKeyListener (this); Shengc (); } public void shengc () {für (int i = 0; i <b.Length; i ++) {b [i] = new Body (); B [i] .x = 10 - i * 10; B [i] .y = 150; }} public int x = 0, y = 0; public void Paint (Grafik g) {Super.Paint (g); G.SetColor (neue Farbe (165,41,10); // RGB -Methode zum Definieren von Farbe G.Setfont (neue Schriftart (font.sans_serif, font.bold, 20)); für (int i = 0; i <B.Length; i ++) {Body z1 = b [i]; g.drawstring ("o", b [i] .x, b [i] .y); } G.SetColor (color.Blue); g.setfont (neue Schriftart (font.sans_serif, font.bold, 20)); G.Drawstring ("Score:" + Score, 30, 30); Paintjs (g); Paintbw (g); } public void paintjs (Grafik g) {g.setColor (color.black); if (state.length ()> 1) {g.drawstring (State, 140, 200); }} public void Paintbw (Grafik g) {G.Setfont (neue Schriftart (font.sans_serif, font.bold, 25)); G.SetColor (color.red); für (int i = 0; i <bw.size (); i ++) {g.drawstring ("o", bw.get (i) .x, bw.get (i) .y); }} public boolean yj () {if ((b [0] .x <400 && b [0] .x> 0) && (b [0] .y <400 && b [0] .y> 0)) {return false; } else {state = "game Over"; zurückkehren; }} public void stmove () {if (pzjc () == false && (yj () == false)) {b [0] .speed = 8; // Hier können Sie die Geschwindigkeit erhöhen und die Schwierigkeit b [0] .Move () erhöhen; P.Add (neuer Punkt (b [0] .x, b [0] .y, b [0] .fx)); if (p.size ()> B.Length) {p.remove (p.get (0)); // system.out.println (p.size ()); }}} public int jl (Körper A, Schatz b) {int jl = 0; jl = (int) math.sqrt ((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); Rückkehr JL; } // vorübergehend nutzlos public void ssmove () {if (p.size ()> = b.Length) {für (int i = 0; i <b.Length - 1; i ++) {b [i+1] .fx = p.get (i) .fx; B [i + 1] .x = p.get (i) .x; B [i + 1] .y = p.get (i) .y; }}} Random r = new random (); public void bzbw () {if (bw.size () <1) {treasure s = new treasure (); SX = R.Nextint (300) + 50; bw.add (s); }} public void bwxs () {Timer t = new Timer (); t.Schedule (neuer Timertask () {public void run () {}}, 0, 8000); } public boolean pzjc () {für (int i = 1; i <p.size (); i ++) {if (p.get (0) .Equals (p.get (i)) {state = "game Over"; zurückkehren; }} return false; } public void crush () {if (bw.size ()> 0) {if (p.get (0) .equals (p.get (i))) {state = "game Over"; zurückkehren; }} return false; } public void crush () {if (bw.size ()> 0) {if (jl (b [0], bw.get (0)) <8) {bw.remove (0); B = Arrays.Copyof (B, B.Length + 1); B [B.Length - 1] = New Body (); Punktzahl += 10; }}} public void GameOver () {Mouselistener k = new Mouseadapter () {public void mouseclicked (mouseEvent e) {super.mouseclicked (e); State = ""; B = Arrays.Copyof (b, 5); p.clear (); Shengc (); Punktzahl = 0; bt.setvisible (falsch); }}; if (state.length ()> 1) {this.add (bt); bt.setvisible (true); Bt.SetBounds (150, 150, 100, 30); bt.addmouselistener (k); } if (bt.isvisible () == false) {this.remove (bt);} this.requestfocus (); } public void zmaction () {Timer Timer = new Timer (); Timer.Schedule (neuer TimerTask () {public void run () {bzbw (); // treasure stmove (); // Schlangenkopfbewegung ssmove (); // Snake Body Movement Crush (); // Kollisionserkennungsdetektion Gameover (); repaint ();}}, 10, 83); } public static void main (String [] args) {jframe jf = new JFrame ("wulin.com-Greed Snake Game Test"); Jf.SetBounds (0, 0, 400, 400); jf.setvisible (true); Jf.SetLayout (NULL); Container c = neuer Container (); c = jf.getContentPane (); C.Setbackground (Farbe.White); jf.setDefaultCloseOperation (jframe.exit_on_close); Snack S = neuer Snack (); S.Setvisible (wahr); S.Setbounds (0, 0, 600, 600); S.Setlocation (0, 0); S.Setbackground (color.orange); jf.add (s); s.zmaction (); s.Requestfocus (); } public void keytyPed (keyEvent e) {} public void keypressed (keyEvent e) {int k = e.getKeyCode (); Switch (k) {case keyEvent.vk_up: if (b [0] .fx! = "sz" && b [0] .fx! = "xz") {b [0] .fx = "sz"; } brechen; case keyEvent.vk_down: if (b [0] .fx! = "sz" && b [0] .fx! = "xz") {b [0] .fx = "xz"; } brechen; case keyEvent.vk_left: if (b [0] .fx! = "zz" && b [0] .fx! = "yz") {b [0] .fx = "xz"; } brechen; case keyEvent.vk_left: if (b [0] .fx! = "zz" && b [0] .fx! } brechen; case keyEvent.vk_right: if (b [0] .fx! = "zz" && b [0] .fx! = "yz") {b [0] .fx = "yz"; } brechen; } repaint (); } public void keyRepest (KeyEvent e) {}}Körperklasse
Paket tcs; public class Body {public int x = 0; public int y = 0; public int speed; getStr() { return str;}public void setStr(String str) { this.str = str;}public void sz(){ this.y+=-speed;}public void xz(){ this.y+=speed;}public void zz(){ this.x+=-speed;}public void yz(){ this.x+=speed;}public void move(){ if (fx == "xz") {xz (); } if (fx == "sz") {sz (); } if (fx == "zz") {zz (); } if (fx == "yz") {yz (); }}}Schätze
Paket TCS; Public Class Treasure {public int x; public int y; public String str;}Punktklasse
Paket tcs; public class Point {public int x; public int y; public String fx; public point (int x, int y, String fx) {this.x = x; this.y = y; this.fx = fx;} public boolean Equals (Objekt o) {if (o Instanzpunkt) {Punkt p = (Punkt) o; if (px == this.x && p.y == this.y) {return true; }} if (o == this) {return true;} if (o == null) {return false;} return false;}}Für weitere Informationen zu Java -Algorithmen können Leser, die an dieser Website interessiert sind, die Themen "Java -Datenstruktur und Algorithmus -Tutorial", "Zusammenfassung der Java -Operation DOM -Knoten -Tipps", "Zusammenfassung der Java -Datei- und Verzeichnisoperationstipps" und "Zusammenfassung der Java -Cache -Operation Tipps" anzeigen
Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.