当前位置:网站首页>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);
}
边栏推荐
- [C语言]限制查找次数,输出次数内查找到的最大值
- Niuke's brush question -- judgment of legal bracket sequence
- H.264概念
- Niuke brush questions part6
- 力扣33题,搜索旋转排序数组
- 264 Concepts
- Niu Ke's question -- finding the least common multiple
- Some thoughts on how to do a good job of operation and maintenance management
- Summary of common mysql/redis interview questions
- 用户信息管理的功能开发
猜你喜欢
![[golang] leetcode - 292 Nim games (Mathematics)](/img/82/54c3f6be9d08687b42cba0487380f0.png)
[golang] leetcode - 292 Nim games (Mathematics)

On the sequence traversal of binary tree Ⅱ
Mysql深入完全学习---阶段1---学习总述

基于TI AM5728 + Artix-7 FPGA开发板(DSP+ARM) 5G通信测试手册

Use egg Js+mongodb simple implementation of curdapi

Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons

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

H.264概念

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

Quanzhi T3 development board (4-core arm cortex-a7) - detailed explanation of logo display during system startup
随机推荐
DataNode的启动流程
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
DC-DC自举电容(BOOT)几个问题
[c language] limit the number of searches and output the maximum value found in the number of internal searches
[C语言]用结构体按分数高低降序输出学生的姓名和分数
. Net core redis hyperloglog type
学习使用LSTM和IMDB评论数据进行情感分析训练
SAP BTP 上 workflow 和 Business Service 的 project 管理
实现可以继续上局
添加自己喜欢的背景音乐
排序的循环链表
v-for循环遍历
力扣刷题——二叉树的层序遍历Ⅱ
软件开发的整体流程
SA token single sign on SSO mode 2 URL redirection propagation session example
Combination sum of 39 questions
用户信息管理的功能开发
力扣31 下一个排列
[c language] shift elements after sorting elements of an array
Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots