当前位置:网站首页>Let our tanks move happily
Let our tanks move happily
2022-06-11 18:36:00 【C_ x_ three hundred and thirty】
List of articles
Create a parent tank class
- 🪂🪂🪂 This class is used as a subclass to create your own tanks and enemy tanks in the future
- It's easy to implement It mainly defines some attributes
- The position of the tank
- The speed of the tank
- The direction of the tank
public class Tank { // object-oriented !!!!!!! Give full expression to private int x; private int y; private int direct; private int speed =1; public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public void moveUp(){ y-=speed; } public void moveDown(){ y+=speed; } public void moveLeft(){ x-=speed; } public void moveRight(){ x+=speed; } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } public Tank(int direct) { this.direct = direct; } public Tank(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } }
Create your own tank
public class Hero extends Tank {
public Hero(int x, int y) {
super(x, y);
}
}
*️*️*️ move
To get the tank moving , We must implement an interface on the sketchpad class we define
Event monitoring mechanism
public class Mypanel extends JPanel implements KeyListener{ }And then you have to implement all of his methods , But we only use one of these methods for this requirement
@Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W){ if(hero.getY()>0){ hero.moveUp(); } hero.setDirect(0); } else if (e.getKeyCode()==KeyEvent.VK_D) { if(hero.getX()+60<1000){ hero.moveRight(); } hero.setDirect(1); } else if (e.getKeyCode()==KeyEvent.VK_S) { if (hero.getY()+60<750){ hero.moveDown(); } hero.setDirect(2); }else if (e.getKeyCode()==KeyEvent.VK_A) { if (hero.getX()>0){ hero.moveLeft(); } hero.setDirect(3); } this.repaint(); Be careful !!! This line of code is the point ! Don't forget to write !!! I said in my last blog ,paint Method is called by the system , Redrawing will be called automatically , So when we press the keyboard keys every time, we need to call the sketchpad redrawing operation again , Only in this way can we see the effect of the dynamic movement of the tank , Without this redrawn method call , There is no way to see the dynamic effect , You can only see that the tank keeps rotating in place } @Override public void keyReleased(KeyEvent e) { }
- ️*️️*️️*️ One last thing to remember , To put This event listening mechanism is added to our framework , Because everything we do is based on JFrame Framework implementation , So in Tank The event listening mechanism should be added to the construction method of To take effect
public TankGame03() {
mp=new Mypanel();
this.add(mp);
this.addKeyListener(mp);// Add an event listening mechanism to the framework
this.setSize(1030,810);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
边栏推荐
- 2022成年礼,致每一位高考学子
- DataNode的启动流程
- SA token single sign on SSO mode 2 URL redirection propagation session example
- Use transformers to convert TF model to pytorch model
- Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots
- 高并发架构设计
- On the problem that the while loop condition in keil does not hold but cannot jump out
- 防止敌方坦克重叠
- Mysql深入完全学习---阶段1---学习总述
- [Golang]力扣Leetcode - 292. Nim 游戏(数学)
猜你喜欢

* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解

全志科技T3開發板(4核ARM Cortex-A7)——MQTT通信協議案例

使用Visdom對損失函數進行監控

Introduction to basic use and pit closure of BigDecimal

Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case

使用mysql判断日期是星期几

Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots

ubuntu 安装psql以及运行相关命令

Force deduction questions -- create a string based on a binary tree

Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
随机推荐
Mysql从0到1的完全深入学习--阶段二---基础篇
The nearest common ancestor of binary tree
Force buckle 31 next arrangement
Niu Ke brushes the question - no two
论工作流选型
下载代码,并编译环境的问题
map和set
TR-069 protocol introduction
Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons
基于TI AM5728 + Artix-7 FPGA开发板(DSP+ARM) 5G通信测试手册
Swagger2简单使用
Niuke brush questions part8
[C语言]压缩字符串并添加标记字符
力扣39题组合总和
全志科技T3开发板(4核ARM Cortex-A7)——MQTT通信协议案例
系统的可扩展型
vim常用命令
高并发架构设计
软件测试技术复习
Niuke brush questions part6