Just touch the Java text box drawing knowledge point, and then can follow the teacher's reminders to do some simple game, to jframe deepen, the following on the greedy snake give some source code, in fact, the implementation of the code is not much, just a little processing can be understood, the following code has comments!
First Class Shake class
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.util.Random;
public class Snake {
public int w;// radius
public int x, y;// spherical coordinates
Color co;// Colors
public int dir = 4;
Public Snake (int w,int x, int y, Color col) {
THIS.W = W;
this.x = x;
This.y = y;
this.co = col;
}
public void Draw (Graphics g) {
G.setcolor (CO);// Set the color of the fill
G.fillarc (Snakegame.off_w+x*w, Snakegame.off_h+y*w, W, w,0,360);// set Rectangle,4 parameters,xy coordinates, high heel width
}
}
The second class of Snakegame
Import java.awt.*;
Import Java.awt.event.KeyAdapter;
Import java.awt.event.KeyEvent;
Import java.util.*;
Import Javax.swing.JFrame;
public class Snakegame extends jframe{
Public final static int screen_w = 800,screen_h = 600;// set screen size
Public final static int off_w = 200,off_h = 100;// Sets the width height of the edit box from the edge of the screen
int key;// record up and down key keycode
Boolean ispress;// determine if the button is pressed
Graphics offg;// Brushes
Image img;// Canvas
arraylist<snake> Snakes = new arraylist<snake> ();// container for storing snakes
private static final Snakegame g = new Snakegame ();
public static Snakegame Get_game () {
return g;
}
Private Snakegame () {
SetBounds (100,100,screen_w,screen_h);// set screen size and start coordinates
Settitle ("Snakegame");// Set caption
SetVisible (TRUE);// Whether the setting is displayed
Setdefaultcloseoperation (exit_on_close);// Set small red fork
Addkeylistener (New Keyadapter () {// keyboard listener
@Override
public void keypressed (KeyEvent e) {// press keyboard event
TODO auto-generated Method Stub
Ispress = true;
Key = E.getkeycode ();
}
@Override
public void keyreleased (KeyEvent e) {// loose keyboard event
TODO auto-generated Method Stub
Ispress = false;
key =-1;
}
});
}
public int snakew = (screen_w-2*off_w)/40;// Set the size of a snake
Snake food;// Food
Snake head;// Snake head
public Random rd = new random ();
public void Paint (Graphics g) {
if (img = = null) {
img = createimage (screen_w,screen_h);
OFFG = Img.getgraphics ();
int xx = Rd.nextint ((screen_w-2*off_w)/snakew);
int yy = Rd.nextint ((screen_w-2*off_w)/snakew);
Food = new Snake (Snakew, XX, yy, color.red);// randomly produce foods
Head = new Snake (snakew, 0, 0, Color.yellow);
Snakes.add (head);// Add a snake head
}
Offg.setcolor (Color.White);
Offg.fillrect (0, 0, Screen_w, screen_h);
Offg.setcolor (Color.Black);
Offg.fillrect (Off_w,off_h, (Screen_w-2*off_w), (Screen_h-2*off_h));
judging the key press down to take effect
if (ispress) {
Snake head = snakes.get (0);// get a snake head
Switch (key) {// snake Head Direction change, up or down
Case 38:
if (head.dir! = 2) {
Head.dir = 1;
}
Break
Case 40:
if (head.dir! = 1) {
Head.dir = 2;
}
Break
Case 37:
if (head.dir! = 4) {
Head.dir = 3;
}
Break
Case 39:
if (head.dir! = 3) {
Head.dir = 4;
}
Break
}
}
Snake head = snakes.get (0);
after eating food, produce a food at random
if (head.x = = Food.x && Head.y = = food.y) {
Food.x = Rd.nextint ((screen_w-2*off_w)/snakew);
Food.y = Rd.nextint ((screen_w-2*off_w)/snakew);
Snakes.add (New Snake (Snakew, Head.x, Head.y, Color.Blue));
}// First Snake and then snake head
for (int i = Snakes.size ()-1; i > 0; i--) {
Snakes.get (i). x = Snakes.get (i-1). x;
Snakes.get (i). y = Snakes.get (i-1). Y;
}
Switch (head.dir) {
Case 1:
head.y--;
Break
Case 2:
head.y++;
Break
Case 3:
head.x--;
Break
Case 4:
head.x++;
Break
}
if (Head.x > 39) {
head.x = 0;
}else if (head.x < 0) {
Head.x = 39;
}
if (Head.y > 39) {
HEAD.Y = 0;
}else if (Head.y < 0) {
Head.y = 39;
}
Food.draw (OFFG);
for (int i = 0; i < snakes.size (); i++) {
Snakes.get (i). Draw (OFFG);
}
G.drawimage (Img,0,0,null);
Repaint ();/ /Redraw
try {
Thread.Sleep (150);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
The third class of Test class Maintest
public class Maintest {
public static void Main (string[] args) {
Snakegame g = Snakegame.get_game ();
}
}
[Java first game] JFrame text box under the snake