The implementation principle of game movement:
After you draw the tank, define a Swich statement in the protected void onkeyevent (int key) method to receive the keyboard key
According to the direction of the key to call tank mobile method, of course, you must first write the tank move method. Add and subtract tank according to incoming direction
The coordinates of the picture are moved.
Implementation steps:
1. Create a new parent class named element
1 Public classElement {//parent class for all objects2 intx; 3 inty; 4 intWidth//the width of the picture5 intHeight//picture of the high6String ImagePath;//Store Picture Path7 8 Public voidDraw () {//Draw method draws an object9 Try {Ten Drawutils.draw (ImagePath, x, y); One}Catch(IOException e) { A //TODO Auto-generated catch block - e.printstacktrace (); - } the } -}Element
2. Create a new enumeration class named direction
1 Public enum Direction { // Define an enumeration class 2 up,down,left,right 3 }
Direction
3. Let the tank class inherit the element class and add the Move method
1 Public classTankextendsElement {2 Private intSpeed = 64;//speed of movement3 PublicTank (intXintY) {//define a method for constructing a tank4 This. x = x;//setting of coordinates5 This. y =y;6 This. ImagePath = "Res/img/tank_u.gif";//picture path of tank7 }8 9 Public voidMove (Direction dir) {//How to move tankTen Switch(dir) {//move according to the received direction One CaseUp : AY-=Speed ; - Break; - the CaseDown : -Y + =Speed ; - Break; - CaseLeft : +X-=Speed ; - Break; + CaseRight : AX + =Speed ; at Break; - } - } -}Tank
4. Set the call of the Move method in the Onkeyevent method in the Settings class
1 Public classTankworldextendsWindow {2Tank Tank;//define a Tank object3List<element> list;//defining a Collection storage object4 5 PublicTankworld (String title,intWidthintHeightintfps) {6 Super(title, width, height, fps);7 //TODO Auto-generated constructor stub8 }9 Ten @Override One protected voidonCreate () { A Try { -Soundutils.play ("Res/snd/start.wav");//Game Start Music -}Catch(IOException e) { the //TODO Auto-generated catch block - e.printstacktrace (); - } -List =NewCopyonwritearraylist<> ();//can traverse additions and deletions collection +Tank =NewTank (0, 0);//the generated tank object coordinates -List.add (tank);//storing a tank object to a collection + } A at @Override - protected voidOnmouseevent (intKeyintXinty) { - //TODO Auto-generated method stubs - - } - in @Override - protected voidOnkeyevent (intKey) {//Keyboard Monitoring Method to Switch(key) {//performed according to the button received + CaseKEYBOARD.KEY_UP://Up ARROW key -Tank.move (direction.up);//How to move tank the Break; * $ CaseKeyboard.key_down://down ARROW keyPanax Notoginseng Tank.move (direction.down); - Break; the + CaseKeyboard.key_left://left ARROW key A Tank.move (direction.left); the Break; + - CaseKeyboard.key_right://RIGHT ARROW key $ Tank.move (direction.right); $ Break; - - } the - }Wuyi the @Override - protected voidondisplayupdate () { Wu for(Element e:list) {//looping through objects in a collection -E.draw ();//draw an object in the interface About } $ } - -}Set Class
Essay says:
At present, the effect of mobile implementation is not perfect, can only move sideways. Material can be found on the Internet some of the more cool alternative.
You need to know enough about inheritance and interfaces to do more with less. You do not have to define a property method for an object repeatedly.
Implementation of game Movement