java简易王者荣耀
时间: 2023-12-03 18:40:24 浏览: 126
以下是一个简单的Java王者荣耀小游戏的实现,包括背景和野怪类:
引用:
```java
package com.sxt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GameFrame extends Frame {
Image bg = GameUtil.getImage("images/bg.jpg");
Image planeImg = GameUtil.getImage("images/plane.png");
Plane plane = new Plane(planeImg,250,250);
Shell[] shells = new Shell[50];
Explode bao;
Date startTime = new Date();
Date endTime;
int period;//游戏持续的时间
@Override
public void paint(Graphics g) {//自动被调用。g相当于一只画笔
Color c = g.getColor();
g.drawImage(bg,0,0,null);
plane.drawSelf(g);//画飞机
//画出所有的炮弹
for(int i=0;i<shells.length;i++){
shells[i].draw(g);
//飞机和炮弹的碰撞检测
boolean peng = shells[i].getRect().intersects(plane.getRect());
if(peng){
plane.live = false;
if(bao==null){
bao = new Explode(plane.x,plane.y);
endTime = new Date();
period = (int)((endTime.getTime()-startTime.getTime())/1000);
}
bao.draw(g);
break;
}
}
if(!plane.live){
g.setColor(Color.RED);
Font f = new Font("宋体",Font.BOLD,50);
g.setFont(f);
g.drawString("时间:"+period+"秒",int)plane.x,(int)plane.y);
}
g.setColor(c);
}
//帮助我们反复重画窗口!
class PaintThread extends Thread{
@Override
public void run() {
while(true){
repaint();//重画
try {
Thread.sleep(40);//1s=1000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//定义键盘监听的内部类
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
/**
* 初始化窗口
*/
public void launchFrame(){
this.setTitle("尚学堂学员_飞机大战");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
this.setLocation(300,50);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start();//启动重画窗口的线程
addKeyListener(new KeyMonitor());//给窗口增加键盘监听
//初始化50个炮弹
for(int i=0;i<shells.length;i++){
shells[i] = new Shell();
}
}
public static void main(String[] args) {
GameFrame f = new GameFrame();
f.launchFrame();
}
}
```
引用:
```java
package com.sxt;
import java.awt.*;
public class WildMonster extends GameObject{
Image img = GameUtil.getImage("images/monster.png");
double speed = 3;
double degree;
public WildMonster() {
x = 200;
y = 200;
width = 50;
height = 50;
degree = Math.random()*Math.PI*2;
}
public void draw(Graphics g){
g.drawImage(img,(int)x,(int)y,null);
x += speed*Math.cos(degree);
y += speed*Math.sin(degree);
if(x<0||x>Constant.GAME_WIDTH-width){
degree = Math.PI-degree;
}
if(y<30||y>Constant.GAME_HEIGHT-height){
degree = -degree;
}
}
@Override
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);
}
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)