表白烟花代码
天天敲代码的朋友,有没有想过代码也可以变得很酷炫又浪漫?今天就教大家用Python模拟出绽放的烟花,工作之余也可以随时让程序为自己放一场烟花秀。
创新互联公司是专业的花都网站建设公司,花都接单;提供成都做网站、网站设计、外贸营销网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行花都网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
python炫酷烟花表白源代码
这个有趣的小项目并不复杂,只需一点可视化技巧,100余行Python代码和程序库Tkinter,最后我们就能达到下面这个效果:
学完本教程后,你也能做出这样的烟花秀。
整体概念梳理
我们的整个理念比较简单。
我们这里通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生"膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。经过一定时间后,粒子会进入"自由落体”阶段,也就是由于重力因素它们开始坠落到地面,仿若绽放后熄灭的烟花。
烟花的java程序
// Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov Date: 2003-1-8 9:49:09
// Home Page : - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: jhanabi.java
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.util.Random;
public class jhanabi extends Applet
implements Runnable
{
public jhanabi()
{
m_mouseX = 0;
m_mouseY = 0;
m_sleepTime = 5;
isError = false;
isInitialized = false;
rand = new Random();
bits = 10000;
bit_px = new double[bits];
bit_py = new double[bits];
bit_vx = new double[bits];
bit_vy = new double[bits];
bit_sx = new int[bits];
bit_sy = new int[bits];
bit_l = new int[bits];
bit_f = new int[bits];
bit_p = new int[bits];
bit_c = new int[bits];
ru = 50;
rv = 50;
}
public void init()
{
String s = getParameter("para_bits");
if(s != null)
bits = Integer.parseInt(s);
s = getParameter("para_max");
if(s != null)
bit_max = Integer.parseInt(s);
s = getParameter("para_blendx");
if(s != null)
ru = Integer.parseInt(s);
s = getParameter("para_blendy");
if(s != null)
rv = Integer.parseInt(s);
s = getParameter("para_sound");
if(s != null)
bit_sound = Integer.parseInt(s);
m_nAppX = size().width;
m_nAppY = size().height;
m_centerX = m_nAppX / 2;
m_centerY = m_nAppY / 2;
m_mouseX = m_centerX;
m_mouseY = m_centerY;
resize(m_nAppX, m_nAppY);
pixls = m_nAppX * m_nAppY;
pixls2 = pixls - m_nAppX * 2;
pix0 = new int[pixls];
offImage = new MemoryImageSource(m_nAppX, m_nAppY, pix0, 0, m_nAppX);
offImage.setAnimated(true);
dbImg = createImage(offImage);
for(int i = 0; i pixls; i++)
pix0[i] = 0xff000000;
sound1 = getAudioClip(getDocumentBase(), "firework.au");
sound2 = getAudioClip(getDocumentBase(), "syu.au");
for(int j = 0; j bits; j++)
bit_f[j] = 0;
isInitialized = true;
start();
}
public void run()
{
while(!isInitialized)
try
{
Thread.sleep(200L);
}
catch(InterruptedException interruptedexception) { }
do
{
for(int j = 0; j pixls2; j++)
{
int k = pix0[j];
int l = pix0[j + 1];
int i1 = pix0[j + m_nAppX];
int j1 = pix0[j + m_nAppX + 1];
int i = (k 0xff0000) 16;
int k1 = ((((l 0xff0000) 16) - i) * ru 8) + i;
i = (k 0xff00) 8;
int l1 = ((((l 0xff00) 8) - i) * ru 8) + i;
i = k 0xff;
int i2 = (((l 0xff) - i) * ru 8) + i;
i = (i1 0xff0000) 16;
int j2 = ((((j1 0xff0000) 16) - i) * ru 8) + i;
i = (i1 0xff00) 8;
int k2 = ((((j1 0xff00) 8) - i) * ru 8) + i;
i = i1 0xff;
int l2 = (((j1 0xff) - i) * ru 8) + i;
int i3 = ((j2 - k1) * rv 8) + k1;
int j3 = ((k2 - l1) * rv 8) + l1;
int k3 = ((l2 - i2) * rv 8) + i2;
pix0[j] = i3 16 | j3 8 | k3 | 0xff000000;
}
rend();
offImage.newPixels(0, 0, m_nAppX, m_nAppY);
try
{
Thread.sleep(m_sleepTime);
}
catch(InterruptedException interruptedexception1) { }
} while(true);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(dbImg, 0, 0, this);
}
public void start()
{
if(isError)
return;
isRunning = true;
if(runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if(runner != null)
{
runner.stop();
runner = null;
}
}
public boolean mouseMove(Event event, int i, int j)
{
m_mouseX = i;
m_mouseY = j;
return true;
}
public boolean mouseDown(Event event, int i, int j)
{
m_mouseX = i;
m_mouseY = j;
int k = (int)(rand.nextDouble() * 256D);
int l = (int)(rand.nextDouble() * 256D);
int i1 = (int)(rand.nextDouble() * 256D);
int j1 = k 16 | l 8 | i1 | 0xff000000;
int k1 = 0;
for(int l1 = 0; l1 bits; l1++)
{
if(bit_f[l1] != 0)
continue;
bit_px[l1] = m_mouseX;
bit_py[l1] = m_mouseY;
double d = rand.nextDouble() * 6.2800000000000002D;
double d1 = rand.nextDouble();
bit_vx[l1] = Math.sin(d) * d1;
bit_vy[l1] = Math.cos(d) * d1;
bit_l[l1] = (int)(rand.nextDouble() * 100D) + 100;
bit_p[l1] = (int)(rand.nextDouble() * 3D);
bit_c[l1] = j1;
bit_sx[l1] = m_mouseX;
bit_sy[l1] = m_nAppY - 5;
bit_f[l1] = 2;
if(++k1 == bit_max)
break;
}
if(bit_sound 1)
sound2.play();
return true;
}
public boolean mouseExit(Event event, int i, int j)
{
m_mouseX = i;
m_mouseY = j;
return true;
}
void rend()
{
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
for(int k = 0; k bits; k++)
switch(bit_f[k])
{
default:
break;
case 1: // '\001'
bit_vy[k] += rand.nextDouble() / 50D;
bit_px[k] += bit_vx[k];
bit_py[k] += bit_vy[k];
bit_l[k]--;
if(bit_l[k] == 0 || bit_px[k] 0.0D || bit_py[k] 0.0D || bit_px[k] (double)m_nAppX || bit_py[k] (double)(m_nAppY - 3))
{
bit_c[k] = 0xff000000;
bit_f[k] = 0;
} else
if(bit_p[k] == 0)
{
if((int)(rand.nextDouble() * 2D) == 0)
bit_set((int)bit_px[k], (int)bit_py[k], -1);
} else
{
bit_set((int)bit_px[k], (int)bit_py[k], bit_c[k]);
}
break;
case 2: // '\002'
bit_sy[k] -= 5;
if((double)bit_sy[k] = bit_py[k])
{
bit_f[k] = 1;
flag2 = true;
}
if((int)(rand.nextDouble() * 20D) == 0)
{
int i = (int)(rand.nextDouble() * 2D);
int j = (int)(rand.nextDouble() * 5D);
bit_set(bit_sx[k] + i, bit_sy[k] + j, -1);
}
break;
}
if(flag2 bit_sound 0)
sound1.play();
}
void bit_set(int i, int j, int k)
{
int l = i + j * m_nAppX;
pix0[l] = k;
}
private int m_nAppX;
private int m_nAppY;
private int m_centerX;
private int m_centerY;
private int m_mouseX;
private int m_mouseY;
private int m_sleepTime;
private boolean isError;
private boolean m_isPaintFinished;
boolean isRunning;
boolean isInitialized;
Thread runner;
int pix0[];
MemoryImageSource offImage;
Image dbImg;
int pixls;
int pixls2;
Random rand;
int bits;
double bit_px[];
double bit_py[];
double bit_vx[];
double bit_vy[];
int bit_sx[];
int bit_sy[];
int bit_l[];
int bit_f[];
int bit_p[];
int bit_c[];
int bit_max;
int bit_sound;
int ru;
int rv;
AudioClip sound1;
AudioClip sound2;
}
java烟花程序看不懂,求解释!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class Fireworks extends Applet implements MouseListener, Runnable {
int x, y;//记录鼠标点击的坐标
int top, point;//好像没用到
public void init() {
x = 0;
y = 0;
setBackground(Color.black);// 设置背景色为黑色
addMouseListener(this);//添加鼠标监听
}
public void paint(Graphics g) {
}
public static void main(String args[]) {
Fireworks applet = new Fireworks();
JFrame frame = new JFrame("TextAreaNew");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {//右上角的叉
System.exit(0);
}
});
frame.add(applet, BorderLayout.CENTER);
frame.setSize(800, 400);//程序的框框大小
applet.init();
applet.start();
frame.setVisible(true);//
}
public void run() {
// 变量初始化
Graphics g1;
g1 = getGraphics();//这是画笔,得到一个画笔
int y_move, y_click, x_click;
int v;//用于计算等待的时间
x_click = x;
y_click = y;//把点击的坐标保存下来,因为其它线程会去改这两个坐标。
y_move = 400;//用来计算变动的那个点,现在是屏幕的最大高度
v = 3;
int r, g, b;
while (y_move y_click)//如果点击的位置小于最大高度。
{
g1.setColor(Color.black);//画笔设成黑色
g1.fillOval(x_click, y_move, 5, 5);//画圆,圆点在点击的X轴,程序界面的最高点,长为5,宽为5
y_move -= 5;//最高点-5
r = (((int) Math.round(Math.random() * 4321)) % 200) + 55;
g = (((int) Math.round(Math.random() * 4321)) % 200) + 55;
b = (((int) Math.round(Math.random() * 4321)) % 200) + 55;//rgb是光的三原色,这个就是烟花产生的颜色,这里定义成随机的,但在一个范围里
g1.setColor(new Color(r, g, b));//把画笔改成那个颜色
g1.fillOval(x_click, y_move, 5, 5);//画一个这样的圆
for (int j = 0; j = 10; j++) {
if (r 55)
r -= 20;
if (g 55)
g -= 20;
if (b 55)
b -= 20;
g1.setColor(new Color(r, g, b));
g1.fillOval(x_click, y_move + j * 5, 5, 5);//这一段都是改变颜色,然后画圆的
}
g1.setColor(Color.black);
g1.fillOval(x_click, y_move + 5 * 10, 5, 5);//把上一次画的彩色圆,用黑色画一遍,就能让它消失在背景里
try {
Thread.currentThread().sleep(v++);//让程序等一下,让你看到效果,不然画完的东西一下就不见了,你看不清。
} catch (InterruptedException e) {
}
}//上面这段代码是烟花的升上去的那一串东西的效果
for (int j = 12; j = 0; j--) {
g1.setColor(Color.black);
g1.fillOval(x_click, y_move + (j * 5), 5, 5);
try {
Thread.currentThread().sleep((v++) / 3);
} catch (InterruptedException e) {
}
}//让最后的那串东西的点消失
y_move = 400;
g1.setColor(Color.black);
while (y_move y_click) {
g1.fillOval(x_click - 2, y_move, 9, 5);
y_move -= 5;
}//这段不太清楚是干什么的,我把它去掉,看不出效果的变化
v = 15;
for (int i = 0; i = 25; i++) {
r = (((int) Math.round(Math.random() * 4321)) % 200) + 55;
g = (((int) Math.round(Math.random() * 4321)) % 200) + 55;
b = (((int) Math.round(Math.random() * 4321)) % 200) + 55;
g1.setColor(new Color(r, g, b));
g1.drawOval(x_click - 3 * i, y_click - 3 * i, 6 * i, 6 * i);
if (i 23) {
g1.drawOval(x_click - 3 * (i + 1), y_click - 3 * (i + 1),
6 * (i + 1), 6 * (i + 1));
g1.drawOval(x_click - 3 * (i + 2), y_click - 3 * (i + 2),
6 * (i + 2), 6 * (i + 2));
}//上面这段是画爆炸的效果
try {
Thread.currentThread().sleep(v++);//停一下,看效果
} catch (InterruptedException e) {
}
g1.setColor(Color.black);
g1.drawOval(x_click - 3 * i, y_click - 3 * i, 6 * i, 6 * i);//然后画黑圈,相当于让彩圈消失。
}
}
public void mousePressed(MouseEvent e) {//点击从这里开始~~~~~~~~~~~~~~
x = e.getX();
y = e.getY();//得到鼠标点击的坐标
Thread one = new Thread(this);//新建一个线程
one.start();//启动这个线程,到上面的run方法
one = null;//把这个线程置为空,让它执行完以后就释放
}
如果你想一下自己写要怎样写这个程序,就很容易理解这个程序了。
一直从下向上画圆,然后把下面的圆擦掉,就能得到一个向上升的烟花效果,
爆炸效果就是先画小圆再画大圆,然后擦掉小圆,再擦掉大圆。
本文标题:java烟花表白代码 java放烟花的代码
新闻来源:http://lswzjz.com/article/doioded.html