首页>代码>java Swing模拟下雪雪花飞舞堆积效果>/Snow/src/com/zhcpt/day02/game/SnowPanel.java
package com.zhcpt.day02.game;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;


import javax.swing.ImageIcon;

import javax.swing.JPanel;


public class SnowPanel extends JPanel implements KeyListener {

	static ArrayList<Snow> snows = new ArrayList<Snow>();
	boolean isStart = false;//是否开启游戏
	boolean pause = false;//是否开启游戏
	int count = snows.size();
	
	int num = 0;//消除字符
	int lostNum = 0;//漏掉的字符数量
	int time = 60;//睡眠的时间 掉落的速度
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		// super.paint(g);
		// System.out.println(new ImageIcon("image/bg.png").getImage().getSource());
		 g.drawImage(new ImageIcon("image/Winter.jpg").getImage(),0,0,800,700,null);
		 g.setFont(new Font("",Font.BOLD, 20));
		 g.setColor(Color.yellow);
		 if(isStart){
			 snows.add(new Snow());
			 for (int i = 0; i < snows.size(); i++) {
			     Snow s = snows.get(i);
			    
			     g.drawImage(s.img, s.x, s.y,s.size,s.size,null);
			    }
		 }else {
			 g.setFont(new Font("",Font.BOLD, 30)); 
			 g.drawString("按下S键带你看一场浪漫的雪花",250, 290);
			
		 }if (pause) {
			g.drawString("按空格键继续游戏", 250, 290);
		}
		 try {
				Thread.sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			repaint();
		
	}
	public void action() {
		while (true) {
			if (isStart) {
				if (!pause) {//不可以是暂停状态
					for (int i = 0; i < snows.size(); i++) {
						snows.get(i).down();
						
						if (snows.get(i).y>600) {
						//snows[i] = new Snow();
							snows.get(i).y=600;
						count ++;
						}if (snows.get(i).y==500) {
							snows.add(new Snow());
						}

					}
				}
			}
			try {
				Thread.sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			repaint();
			}
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if (!isStart&&(char)code == 'S') {
			//开始
			isStart=true;//开启
			//设置为原始状态
			num=0;
			count=snows.size();
			time=60;
			for (int i = 0; i < snows.size(); i++) {
				
				snows.add(new Snow());
			}
			
			
		}else if (!isStart&&(char)code =='Q') {
			System.exit(0);
		}else if (isStart&&(char)code ==32) {
			if(pause){
				pause=false;//如果是暂停状态就停止暂停
			}else {
				pause=true;//暂停
			}
		
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
最近下载更多
Cold_sangs  LV3 2022年6月6日
543666826  LV33 2022年3月12日
qsyqa0  LV6 2021年9月4日
1234567zz  LV7 2020年8月13日
new_docker  LV15 2020年7月27日
最代码官方  LV167 2020年7月19日
最近浏览更多
11111444 2022年12月3日
暂无贡献等级
微信网友_6191697646571520  LV6 2022年11月23日
暂无贡献等级
sqee121  LV1 2022年9月4日
lcy111  LV5 2022年9月1日
Cold_sangs  LV3 2022年6月6日
liys1234  LV9 2022年4月22日
3188681084  LV1 2022年3月24日
543666826  LV33 2022年3月12日
xp95323  LV14 2022年1月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友