The Gameface class mainly implements the main game interface.
Import java.awt.*;
Import java.awt.event.KeyEvent;
Import Java.awt.event.KeyListener;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import javax.swing.*;
public class Gameface {
public static Dimension GetSize () {//Set the desired form size
return new Dimension (1024,590);
}
public static point GetLocation () {//calculates the upper-left coordinate of the form
Toolkit Toolkit=toolkit.getdefaulttoolkit (); Get Toolkit Instances
Dimension screensize=toolkit.getscreensize (); Get the monitor size
When the monitor is less than the form times wrong and ends the program
if ((Screensize.width<getsize (). width) | | (Screensize.height<getsize (). Height)) {
Joptionpane.showmessagedialog (NULL, "Display resolution is at least" +getsize (). width+ "*" +getsize (). Height, "", Joptionpane.warning_ MESSAGE);
System.exit (0); Terminating programs
}
int x= (Screensize.width-getsize (). width)/2; Calculate the upper-left corner coordinates
int y= (Screensize.height-getsize (). height)/2;
return new Point (x, y); Returns the calculated coordinates
}
public static void Run () {
JFrame frame=new JFrame ("Sniper tank (case-insensitive version)"); Game Forms
Frame.setlocation (GetLocation ()); Set the form position centered
Frame.setsize (1024,590); Set Form size
Frame.setdefaultcloseoperation (Jframe.dispose_on_close); Set close form to destroy form
Frame.setresizable (FALSE); Set form size is not variable
Set the form icon
Frame.seticonimage (Toolkit.getdefaulttoolkit (). GetImage (GameFace.class.getResource ("char_bg.png"));
Gamejpanel pan=new Gamejpanel (); Drawing panel (custom canvas)
Pan.go (); Start a game, start a timed task
Frame.addkeylistener (PAN); Binding keyboard Events
Frame.add (PAN); Add a panel to a form
Frame.setvisible (TRUE); Show form
}
}
Class Gamejpanel extends JPanel implements keylistener{
Private static final long serialversionuid = 1L;
Private Image Background=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("background.jpg")); Background image
Private Image Bong0=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong0.png")); Exploded pictures
Private Image Bong1=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong1.png"));
Private Image Bong2=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong2.png"));
Private Image Bong3=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong3.png"));
Private Image Bong4=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong4.png"));
Private Image Bong5=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong5.png"));
Private Image Bong6=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("bong6.png"));
Private Image Char_bg=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("char_bg.png")); Letter background picture
Sqllink sql=new Sqllink (); Link Database
Boolean Firststart =true; Game Boot Tag
int num=4; Game Start Countdown
private static long start=0; Used to store the game start time
private static long end = 0; Used to store game end time
private static int score=1000; Initial score 1000 points
private static int highscore=1000; Maximum score for this Council
private static int level=1; For storage grade
private static int number=1; The number of words used to store the screen
private static int count=0; For storing the number of hits
private static int upcount=5; The number of words required to store the upgrade
private static int speed=100; Used to store game speed
private static int bonus=10; For storing extra points
private static int minus=120; For holding the deduction points
private static int[] changelevel=new int[2];
private static int[] changegame=new int[4];
private static char[] c=new char[106]; Used to hold the letters in the screen and the corresponding coordinates
private static int[] x=new int [106];
private static int[] y=new int[106];
private static Timer Timer=null; Defining a Timed Task object
private static int xx=0; The letter coordinates were knocked out, the mark was knocked out
private static int yy=0;
Boolean playflag=true;
Public Gamejpanel () {//Construction-time initialization
Firststart=true; Artboard instance, the first boot flag is changed
for (int i=0;i<c.length;i++) {//Initialize character array
c[i]= ' 0 ';
}
}
public void init () {//Generate a set of random letters
for (int i=0;i<number;i++) {
SetChar (i);
}
}
public void SetChar (int i) {//Get one of the random 26 letters and their location
C[i]= (char) (Math.random () *26+65);
x[i]= (int) (Math.random () *600+200);
y[i]=-20;
}
public void Paint (graphics g) {//Drawing
Super.paint (g); Get the drawing capabilities of the parent class
G.drawimage (background,0, 0, this); Draw a background picture
if (Firststart) {//Start Game, Countdown 3 seconds
G.setcolor (color.red);
G.setfont (New Font ("Founder Shu", font.bold,50));
Switch (num) {
Case 3:
g.DrawString ("Tank Attack!") ", 390, 275);
Break
Case 2:
g.DrawString ("Please be prepared!") ", 365, 275);
Break
Case 1:
g.DrawString ("Fight! ", 440, 275);
Break
Default
Break
}
num--;
try {
Thread.Sleep (1000); Draw 1 breaks for 1 seconds
} catch (Interruptedexception e) {
E.printstacktrace ();
}
if (num<0) {//Countdown end, Initialize, get start time of game
Firststart=false;
Init ();
Start=system.currenttimemillis ();
}
}
if (Playflag) {//If there is an abort letter, the explosion effect is displayed
G.drawimage (bong0,xx-33, yy-60, this);
G.drawimage (bong1,xx-33, yy-60, this);
G.drawimage (bong2,xx-33, yy-60, this);
G.drawimage (bong3,xx-33, yy-60, this);
G.drawimage (bong4,xx-33, yy-60, this);
G.drawimage (bong5,xx-33, yy-60, this);
G.drawimage (bong6,xx-33, yy-60, this);
Playflag=false;
}
if (score<0) {//score is negative, game over
End=system.currenttimemillis (); Get End time
At the end of the game, print the highest score of this board, print the cumulative number of typing, print grade, print survival time
Image List_bg=toolkit.getdefaulttoolkit (). GetImage (GameJPanel.class.getResource ("list_bg.jpg")); Background image
G.drawimage (List_bg,310,0,this);
G.setcolor (Color.Black);
G.setfont (New Font ("Beurk", font.bold,60));
g.DrawString ("Game over", 344,152);
G.setfont (New Font ("XXFarEastFont-Xingkai", font.plain,30));
g.DrawString ("You live Together:" + (End-start)/1000+ "seconds", 330, 280);
g.DrawString ("Your highest is divided into:" +highscore+ "cent", 330, 330);
g.DrawString ("You've hit the right" +count+ "letters", 330, 380);
g.DrawString ("Your rank is:" +level, 330, 430);
G.setcolor (color.red);
G.setfont (New Font ("Beurk", font.bold,60));
g.DrawString ("Game over", 340,150);
Sql.sethistory (Loginface.username, Highscore, Count, (End-start)/1000);
Timer.cancel (); Turn off timed tasks
return; Stop this drawing
}
Set Screen Letter Basic properties
G.setfont (New Font ("Vivian", font.bold,22));
for (int i=0;i<c.length;i++) {//Draw letters and their background pictures
if (c[i]!= ' 0 ') {
G.drawimage (char_bg,x[i]-31, y[i]-21, this);
G.setcolor (New Color (255,255,255,80));
G.filloval (x[i]-14, Y[i]-22, 45, 30);
G.setcolor (Color.yellow);
g.DrawString (c[i]+ "", x[i]+2,y[i]+1);
G.setcolor (color.red);
g.DrawString (c[i]+ "", X[i],y[i]);
}
}
Draw game data (rank, score, number of letters, upgrade criteria)
G.setcolor (Color.Black);
G.setfont (New Font ("Italic", font.bold,25));
g.DrawString ("rank:" +level,22,32);
g.DrawString ("Score:" +score,22,67);
G.setfont (New Font ("Italic", font.bold,35));
g.DrawString ("War", 937,347);
g.DrawString ("Performance", 922,382);
G.setfont (New Font ("Italic", font.bold,20));
g.DrawString ("Already eliminated tank" +count+ "vehicle", 822,422);
g.DrawString ("Upgrade still needs to be eliminated", 854,452);
g.DrawString (upcount+ "Tank", 907,482);
G.setcolor (Color.green);
G.setfont (New Font ("Italic", font.bold,25));
g.DrawString ("rank:" +level,20,30);
g.DrawString ("Score:" +score,20,65);
G.setfont (New Font ("Italic", font.bold,35));
g.DrawString ("War", 935,345);
g.DrawString ("Performance", 920,380);
G.setfont (New Font ("Italic", font.bold,20));
g.DrawString ("Already eliminated tank" +count+ "vehicle", 820,420);
g.DrawString ("Upgrade still needs to be eliminated", 852,450);
g.DrawString (upcount+ "Tank", 905,480);
}
public void Go () {//define a method to start the game
Timer=new Timer (); Set up timed tasks
Timer.schedule (New TimerTask () {
New TimerTask () {} Anonymous inner class, overriding Run method
public void Run () {
try{
Thread.Sleep (speed); Every time you take a break speed millisecond
}catch (Exception ex) {
Ex.printstacktrace ();
}
for (int i=0;i<c.length;i++) {//Letter Drop
y[i]++;
if (y[i]>590&&c[i]!= ' 0 ') {//If the boundary has been dropped, then the score is deducted and the letter is regenerated
Score-=minus;
SetChar (i);
}
}
Repaint (); Re-painting
}
},0,5); Start now and execute the above code every 5 milliseconds at a later time
}
Keyboard monitoring
public void keypressed (KeyEvent key) {///press a key on the keyboard to listen for keyboard input characters
Char Ckey=key.getkeychar (); Get Keyboard Entry Letters
if (ckey!=65535) {
if (97<=ckey&&ckey<123) {//Convert lowercase letters to uppercase
Ckey=character.touppercase (Ckey);
}
int index=-1;
for (int i=0;i<c.length;i++) {//Traverse screen letter, whether there is the same
if (ckey==c[i]&&y[i]>10&&ckey!= ' 0 ') {
Index=i;
Break
}
}
if (index!=-1) {//If there is the same, then add points, and eliminate this letter, generate a new letter
Score+=bonus;
Xx=x[index]; Save the deleted letter coordinates and set the Playflag to True
Yy=y[index];
Playflag=true;
count++; Number of words plus one, upgrade conditions minus one
upcount--;
SetChar (index);
if (upcount==0) {//upgrade and recalculate the number of typing required for the next upgrade
Changelevel=sql.getlevel (count);
LEVEL=CHANGELEVEL[0];
UPCOUNT=CHANGELEVEL[1];
Changegame=sql.getgame (level);
NUMBER=CHANGEGAME[0];
SPEED=CHANGEGAME[1];
BONUS=CHANGEGAME[2];
MINUS=CHANGEGAME[3];
SetChar (LEVEL-1); When upgrading, letters generate more than one
}
if (Score>highscore) {//When the current score is greater than the maximum tick of this board, refresh the highest score
Highscore=score;
}
}else{
Score-=minus; Otherwise the input letter does not exist, the deduction points
}
}
}
The following two listener events are not used, empty
public void keyreleased (KeyEvent arg0) {}
public void keytyped (KeyEvent arg0) {}
}
Windows-side typing game-game on