JAVA学习day42--飞机大战12定时器

这篇博客展示了如何在Java游戏中使用定时器(Timer)创建一个每10毫秒执行一次的任务,用于游戏循环更新。作者通过创建World类,设置了窗口大小、添加了Sky、Hero、Enemy和Bullet等游戏元素,并在main方法中启动了游戏窗口和定时器任务,实现了基本的游戏框架。
摘要由CSDN通过智能技术生成

定时器

package day19;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.TimerTask;
import java.util.Timer;

/** 整个游戏世界*/
public class World extends JPanel{
	public static final int WINTH = 400;//窗口的宽
	public static final int HEIGHT = 700;//窗口的高
	
	private Sky sky = new Sky();
	private Hero hero  = new Hero();
	private FlyingObject[] enemies = {};//敌人(小敌机,大敌机,小蜜蜂)
	private Bullet[] bullets = {};//子弹数组
	
	/** 启动程序的执行*/
	void action(){
		Timer timer = new Timer();//定时器对象
		int intervel = 10;//以毫秒为单位
		timer.schedule(new TimerTask(){
			public void run(){//定时做的事--每10毫秒执行
				System.out.println(111);
			}
		},intervel,intervel);//定时计划
		

		
		}

	
	

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		World world = new World();
		frame.add(world);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
		frame.setSize(WINTH,HEIGHT);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		world.action();


	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值