最近,学校安排来深圳尚学堂实训,我又重新拿起了我的Java,故此更新一个月的Java实训,今天主要是一个Java小游戏--坦克大战的坦克的移动的简单实现。(20180715)
1、首先,我们建立一个com.hbust.app的包,继续创建一个TankGame的类,今天的内容主要如下:
游戏的入口
* 1:产生一个游戏主窗口的界面。实现窗口的关闭的功能。
* 2: 实现窗口绘制的功能,绘制一些基本的图形出来。添加按键事件。
* 3:绘制一个坦克出来,然后可以通过键盘控制坦克四向移动。
代码如下:
package com.hbust.app;
import com.hbust.frame.GameFrame;
/**
* This is the entrance to the game.
* 1.creates an interface for the main window of the game. Realize the function of window closing.
* 2.draw a tank out and then control the tank's four-way movement via the keyboard.
*
*@author gc
*
**/
public class TankGame {
public static void main(String[] args) {
//Creates a Frame object.
new GameFrame();
}
}
2、接着,创建一个com.hbust.frame的包,以及一个GameFrame的类,在这个类中,我们实现了游戏的基本窗口,以及相应的事件监听器。
代码如下:
package com.hbust.frame;
import com.hbust.constant.Constant;
import com.hbust.entity.Tank;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Game main window class, in which all drawing functions are completed
* @author gc
*
* */
//Inheriting the Frame class provided by Java
public class GameFrame extends Frame{
private Tank myTank;
//Constructor to initialize the window
public GameFrame() {
initFrame();
initWindowListener();
initMyTank();
}
//the method to initialize MyTank
private void initMyTank() {
myTank = new Tank(100, 100, Tank.DIR_UP);
}
private void initFrame(){
//Title of window
setTitle(Constant.GAME_TITLE);
//Set window size does not change
// setResizable(false);
//Set the initial size of the window
setSize(Constant.FRAME_WIDTH, Constant.FRAME_HEIGHT);
//Set the starting position of the window(mediate)
setLocation(Constant.SYS_SCREEN_W-Constant.FRAME_WIDTH>>1,
Constant.SYS_SCREEN_H-Constant.FRAME_HEIGHT>>1);
//Because the window is still hidden after it's been created.
//Display the current game window
setVisible(true);
}
/**
* Add listening events to the game window
*
* */
private void initWindowListener(){
//Adding an event method to a window
addWindowListener(new WindowAdapter() {
//Processing window events
public void windowClosing(WindowEvent e) {
//When the close button is clicked, the method is executed
//Click close virtual machine
System.exit(0);
}
});
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
// called when a key is released
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
// called when a key is pressed
public void keyPressed(KeyEvent e) {
// e saves information about pressed keys
// get the virtual key value of the pressed key
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_A://left
myTank.setDir(Tank.DIR_LEFT);
myTank.setX(myTank.getX()-5);
repaint();
break;
case KeyEvent.VK_D://right
myTank.setDir(Tank.DIR_RIGHT);
myTank.setX(myTank.getX()+5);
repaint();
break;
case KeyEvent.VK_W://up
myTank.setDir(Tank.DIR_UP);
myTank.setY(myTank.getY()-5);
repaint();
break;
case KeyEvent.VK_S://down
myTank.setDir(Tank.DIR_DOWN);
myTank.setY(myTank.getY()+5);
repaint();
break;
}
}
});
}
private int x = 500, y = 500;
/**
* All plotted work is to be completed within this method .
* g is a brush object . The instantiation of g is system - assisted .
* g is the brush that the system provides to us and can be used directly
* paint cannot call .repaint () directly:
* */
public void paint(Graphics g) {
myTank.draw(g);
}
}
3、为了代码比较好审查,我们将一些定义的常量单独做一个文件包,放在com.hbust.constant的Constant类中。
代码如下:
package com.hbust.constant;
/**
* All the literal constants used in the game
* @author gc
*
* */
public class Constant {
//Game title
public static final String GAME_TITLE = "my happy hour!";
//The width and height of the main window
public static final int FRAME_WIDTH = 800;
public static final int FRAME_HEIGHT = 600;
//Get the height and width of the current screen
public static final int SYS_SCREEN_W = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
public static final int SYS_SCREEN_H = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
}
4、创建一个坦克的实体类,主要完成坦克的简单绘制以及简单的功能的实现。放在com.hbust.entity的Tank的类中。
代码如下:
package com.hbust.entity;
import java.awt.Color;
import java.awt.Graphics;
/**
* A class used to describe a tank
* analysis:
* tank attributes:
* tank functions:
*
* @author gc*/
public class Tank {
// define the four directions of the tank
public static final int DIR_UP = 0;
public static final int DIR_DOWN = 1;
public static final int DIR_LEFT = 2;
public static final int DIR_RIGHT = 3;
// Several states of a tank
public static final int STATE_STAND = 0;
public static final int STATE_MOVE = 1;
public static final int STATE_DIE = 2;
public static final int STATE_FIRE = 3;
private int x,y; // coordinate
private int width = 30;//Width
private int dir;//direction
private int speed;//speed
private int state;//States of tank
private Color color = Color.ORANGE;//Color of tank
//The Construction method of Tank
public Tank(int x, int y, int dir) {
super();
this.x = x;
this.y = y;
this.dir = dir;
}
/**
* the drawing method of tank
* @param g*/
public void draw(Graphics g) {
//Sets the color of the brush to the specified color
g.setColor(color);
g.fillRect(x, y, width, width);
//Draw a black border
g.setColor(Color.BLACK);
g.drawRect(x, y, width, width);
//Draw a gun barrel(the half of tank's width)
int w = width>>1;
switch (dir) {
case DIR_UP:
g.drawLine(x+w, y-w, x+w, y+w);
break;
case DIR_DOWN:
g.drawLine(x+w, y+w, x+w, y+3*w);
break;
case DIR_LEFT:
g.drawLine(x-w, y+w, x+w, y+w);
break;
case DIR_RIGHT:
g.drawLine(x+3*w, y+w, x+w, y+w);
break;
}
}
/**the method to set direction of tank
* @param dir
*/
public void setDir(int dir){
this.dir = dir;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
效果如图,可以完成坦克的四个方位的运动:
哈哈,感觉尚学堂的学习强度还是比较大,早九点到晚九点,中午就休息两个小时,写代码也开始尝试用英文写注释,虽然只能用百度翻译,但是慢慢来吧!加油!