Created
November 5, 2011 06:04
-
-
Save tetsurokitahara/1341170 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 javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
/** ありがちなボールを跳ね返すゲーム風のプログラムです. | |
全くGroovyっぽくないのは,ご愛嬌ということで. */ | |
class BallGameFrame extends JFrame implements Runnable { | |
double ball_x = 10, ball_y = 10; | |
double ball_v_x = 100.0, ball_v_y = 0.0; | |
double ball_g = 9.8, dt = 0.05; | |
int ball_size = 32; | |
int racket_x = 320, racket_y = 400; | |
int racket_height = 16, racket_width = 80; | |
BallGameFrame() { | |
setSize(640, 480); | |
mouseMoved = { e -> | |
racket_x = e.getX(); | |
}; | |
Thread th = new Thread(this); | |
th.start(); | |
} | |
void run() { | |
while (1) { | |
if (ball_y <= 0) { | |
ball_v_y = -1 * ball_v_y; | |
} | |
if (ball_x <= 0 || ball_x + ball_size >= 640) { | |
ball_v_x = -1 * ball_v_x; | |
} | |
if (racket_x <= ball_x && | |
ball_x + ball_size <= racket_x + racket_width && | |
ball_y + ball_size >= racket_y) { | |
ball_v_y = -1 * ball_v_y; | |
} | |
ball_x += dt * ball_v_x; | |
ball_v_y += dt * ball_g; | |
ball_y += ball_v_y; | |
repaint(); | |
sleep(50); | |
} | |
} | |
void paint(Graphics g) { | |
super.paint(g); | |
g.fillRect(racket_x, racket_y, racket_width, racket_height); | |
g.fillOval((int)ball_x, (int)ball_y, ball_size, ball_size); | |
} | |
} | |
static void main(String[] args) { | |
BallGameFrame f = new BallGameFrame(); | |
f.setVisible(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment