Created
January 20, 2009 08:02
-
-
Save masaakif/49396 to your computer and use it in GitHub Desktop.
落下して弾むボール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.core.*; | |
public class JiyuuRakka extends PApplet{ | |
final int MAX = 100; | |
int idx = 0; | |
Ball[] balls; | |
static final long serialVersionUID = 100; | |
float startx, starty | |
, endx, endy; | |
boolean isDragged = false; | |
public void setup(){ | |
size(600, 400); | |
balls = new Ball[MAX]; | |
frameRate(30); | |
smooth(); | |
colorMode(HSB, 255); | |
createColor(0, 0, 1, 255); | |
} | |
public void draw(){ | |
background(100); | |
if (isDragged == true) { | |
fill(255); | |
ellipse(startx, starty, 10, 10); | |
line(startx, starty, endx, endy); | |
} | |
for (int i = 0; i < MAX; i++) { | |
if (balls[i] != null) { | |
balls[i].update(); | |
fill(balls[i].Color); | |
ellipse(balls[i].x, balls[i].y, balls[i].size, balls[i].size); | |
} | |
} | |
} | |
public void mousePressed() { | |
startx = mouseX; | |
starty = mouseY; | |
} | |
public void mouseDragged() { | |
endx = mouseX; | |
endy = mouseY; | |
isDragged = true; | |
} | |
public void mouseReleased() { | |
endx = mouseX; | |
endy = mouseY; | |
isDragged = false; | |
float initialRateX = endx - startx; | |
if (initialRateX != 0) { | |
initialRateX = initialRateX / (float)10.0; | |
} | |
float initialRateY = endy - starty; | |
if (initialRateY != 0) | |
{ | |
initialRateY = initialRateY / (float)10; | |
} | |
balls[idx] = new Ball(startx, starty,random(10,15), this); | |
balls[idx].setInitialSpeed(initialRateX, initialRateY); | |
balls[idx].setColor(createColor(random(0,255), random(0,255), random(0,255), random(50,250))); | |
idx++; | |
if (MAX <= idx) { | |
idx = 0; | |
} | |
} | |
public int createColor(float c1, float c2, float c3, float c4) { | |
return createColor((int)c1, (int)c2, (int)c3, (int)c4); | |
} | |
public int createColor(int c1, int c2, int c3) { | |
return createColor(c1, c2, c3, 255); | |
} | |
public int createColor(float c1, float c2, float c3) { | |
return createColor((int)c1, (int)c2, (int)c3); | |
} | |
public int createColor(int c1, int c2, int c3, int c4) { | |
if (c1 < 0 || 255 < c1) { | |
c1 = 255; | |
} | |
if (c2 < 0 || 255 < c2) { | |
c2 = 255; | |
} | |
if (c3 < 0 || 255 < c3) { | |
c3 = 255; | |
} | |
if (c4 < 0 || 255 < c4) { | |
c4 = 255; | |
} | |
int _color = c4 << 24 | c1 << 16 | c2 << 8 | c3; | |
return _color; | |
} | |
} | |
import processing.core.*; | |
public class Ball { | |
private | |
float _x, _y; | |
float _speedy, _speedx; | |
float _size = 5; | |
PApplet _p; | |
int _color; | |
static final float g = (float)2; | |
public | |
float x, y, size; | |
int Color; | |
Ball(float x, float y, float size, PApplet p){ | |
_x = x; | |
_y = y; | |
_size = size; | |
_p = p; | |
_speedy = 0; | |
} | |
public void setInitialSpeed(float speedx, float speedy) { | |
_speedx = speedx; | |
_speedy = speedy; | |
} | |
public void setColor(int c){ | |
_color = c; | |
Color = _color; | |
} | |
public void update() | |
{ | |
calculateXPos(); | |
calculateYPos(); | |
x = _x; | |
y = _y; | |
size = _size; | |
} | |
private void calculateXPos() | |
{ | |
float x1; | |
x1 = _x + _speedx; | |
if (x1 <= size/2) { | |
_speedx = (float) -1 * _speedx * (float)0.7; | |
x1 = size / 2; | |
} else if ((_p.width - size /2) <= x1){ | |
_speedx = (float) -1 * _speedx * (float)0.7; | |
x1 = _p.width - size / 2; | |
} | |
_x = x1; | |
} | |
private void calculateYPos() | |
{ | |
float y1; | |
_speedy += g ; | |
y1 = _y + _speedy; | |
if ((_p.height - size/2) <= y1) { | |
_speedy = (float) -1 * _speedy * (float)0.7; | |
y1 = _p.height - size / 2; | |
_speedx = _speedx * (float)0.95; | |
} | |
_y = y1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment