Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of the classic tank war game realized by Java

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to write the code of the classic tank war game in Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

Introduction

90 tank war, a very classic game, at that time, each person with a small partner a handle, moved on a small bench to sit in front of the TV, the body shook with the handle from time to time, sometimes serious, frowning, sometimes cheering, smiling, devoted to the game, in the elimination of tanks, guarding checkpoints, tank upgrading, promotion, but also dancing, high-five celebration Now when I think about it, it is still fresh in my memory and has endless aftertaste! So I wrote one with java to find out how I felt at that time and abuse the computer by the way. Hee (ming laughter).

Realize

When drawing, draw the icon of the eagle at the bottom of the interface with g.drawImage, and then use drawImage to surround it with an earthen wall, which can withstand a wave when attacked by the enemy, but the earthen wall is very fragile.

Create Wall class, properties x, y are coordinates, properties width, height are length and width, property type is type, earth wall value is 0, steel wall value is 1, home (the old bird) is 2. These three share this class, create instance objects of Wall, pass in different type values, and create different walls.

Programmer analysis: just play! To piece together a small wall like 5 in the following image into 6, this thick wall requires 4 small pieces (put into 2 rows and 2 columns), each of which is 30 pixels long and wide.

/ / column 1: int x1 / 60 / int x2 / 90 / int width=30;int height=30;int oy=60;int / 0 / int count=6;for (int I = 0 / 10)

< count; i++) { y=oy+i*30; wall = new Wall(wallImage,x1,y,width,height,0); walls.add(wall); wall = new Wall(wallImage,x2,y,width,height,0); walls.add(wall);} 上述代码中,2行6列,x1是第1列x坐标,x2是第2列x坐标,oy是y方向初始坐标,然后依次按30递增,这样就定义好Wall对象了,然后将wall实例对象放到集合中,方便绘制。 创建抽象Tank类,定义几个主要的方法(fire开火、move移动等),PTank 是玩家坦克类继承了Tank,实现了相关的方法。 添加键盘事件,上下左右为方向移动事件(上对应数字1,右对应数字2,下对应3,左对应4),F键和空格设定为开火事件(开火也要根据这些数字来确定炮弹的方向)。 //按下键盘@Overridevoid keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_F: case KeyEvent.VK_SPACE: fire(); break; case KeyEvent.VK_UP: case KeyEvent.VK_W: setDir(1); move(); break; case KeyEvent.VK_RIGHT: case KeyEvent.VK_D: setDir(2); move(); break; case KeyEvent.VK_DOWN: case KeyEvent.VK_S: setDir(3); move(); break; case KeyEvent.VK_LEFT: case KeyEvent.VK_A: setDir(4); move(); break; }} move方法开始做的是键盘按一下移动10像素,感觉一卡卡的,并且很容易有过道会卡住,为了更丝滑,改造为监听到一次移动事件就移动30像素,用线程分3次执行每次10像素,在坦克移动过程中键盘的移动指令暂时失效,坦克移动完毕后移动指令恢复。 void doMove(){//总共30,分3次走每次 10,用线程 if(!alive) return ; if(isMove) return ; isMove=true; new Thread(new Runnable() { @Override public void run() { while (isMove){ count++; go(); if(count==3){ count=0; isMove=false; } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start();}//执行位移void go(){ //设定位移 switch (dir) { case 1: y-=speed; break; case 2: x+=speed; break; case 3: y+=speed; break; case 4: x-=speed; break; }} 判断边界很好处理,只要坦克的坐标x游戏区域的高就认定为出界,不允许移动即可。 //判断左边界、上边界if(tank.getX()gameHeight){ return false;//不能移动} 坦克与墙体的碰撞检查: 1.判断每一块小墙体的4个点是否在tank的范围内(因为墙体比坦克小),只要有一个点满足条件则判定为不能移动,否则可以移动。 2.如果不能移动则需要恢复坦克这次所移动的位置,以保持坦克没有移动(因为设定了预移动,方便计算位置,下方的图都是经过预移动的) (1).一个点在区域内 (2)两个点在区域内 (3)4个点都在区域内 以下代码是取到墙体4个角的坐标,采用 || 或的方式,有一个满足条件则返回不可以移动。 //判断墙体与坦克是否碰撞@Overrideboolean isPoint(Wall wall) { //因为墙比坦克小,所以只需要判断墙的4个点是否在 坦克范围内,如果有则表示碰撞了 //左上角 int x1 = wall.getX(); int y1 = wall.getY(); //右上角 int x2 = wall.getX()+wall.getWidth(); int y2 = wall.getY(); //右下角 int x3 = wall.getX()+wall.getWidth(); int y3 = wall.getY()+wall.getHeight(); //左下角 int x4 = wall.getX(); int y4 = wall.getY()+wall.getHeight(); //只要有一个点在范围内,则判断为碰撞 if(comparePoint(x1,y1)|| comparePoint(x2,y2)||comparePoint(x3,y3)||comparePoint(x4,y4) ){ return true; } return false;} boolean comparePoint(int x,int y){ //大于左上角,小于右下角的坐标则肯定在范围内 if(x>

This.x & y > this.y & & x

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report