package ponggame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
//This is the ball class
public class Ball implements Runnable {
// Gloabal variables
int x, y, xDirection, yDirection;
// Score
int p1Score, p2Score;
Paddle p1 = new Paddle(15, 140, 1);
Paddle p2 = new Paddle(370, 140, 2);
Rectangle ball;
public Ball(int x, int y) {
p1Score = p2Score = 0;
this.x = x;
this.y = y;
// Set ball moving randomly
Random r = new Random();
int rDir = r.nextInt(1);
if (rDir == 0)
rDir--;
setXDirection(rDir);
int yrDir = r.nextInt(1);
if (yrDir == 0)
yrDir--;
setYDirection(yrDir);
// Create 'ball'
ball = new Rectangle(this.x, this.y, 7, 7);
}
public void setXDirection(int xdir) {
xDirection = xdir;
}
public void setYDirection(int ydir) {
yDirection = ydir;
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(ball.x, ball.y, ball.width, ball.height);
}
// when it hits either paddle
public void collision() {
if (ball.intersects(p1.paddle))
setXDirection(+1);// taught it was somewhere here to add audio
if (ball.intersects(p2.paddle))
setXDirection(-1);
}
public void move() {
collision();
ball.x += xDirection;
ball.y += yDirection;
// Bounce the ball when edge is detected
if (ball.x <= 0) {
setXDirection(+1);
p2Score++;
}
if (ball.x >= 385) {
setXDirection(-1);
p1Score++;
}
if (ball.y <= 15)
setYDirection(+1);
if (ball.y >= 285)
setYDirection(-1);
}
@Override
public void run() {
try {
while (true) {
move();
Thread.sleep(7);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
这是桨类:
package ponggame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Paddle implements Runnable {
int x, y, yDirection, id;
Rectangle paddle;
public Paddle(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
paddle = new Rectangle(x, y, 10, 50);
}
public void keyPressed(KeyEvent e){
switch(id){
default:
System.out.println("Please enter a valid ID in Paddle constructor");
break;
case 1:
if(e.getKeyCode() == e.VK_W){
setYDirection(-1);
}
if(e.getKeyCode() == e.VK_S){
setYDirection(1);
}
break;
case 2:
if(e.getKeyCode() == e.VK_UP){
setYDirection(-1);
}
if(e.getKeyCode() == e.VK_DOWN){
setYDirection(+1);
}
break;
}
}
public void keyReleased(KeyEvent e){
switch(id){
default:
System.out.println("Please enter a valid ID in Paddle constructor");
break;
case 1:
if(e.getKeyCode() == e.VK_W){
setYDirection(0);
}
if(e.getKeyCode() == e.VK_S){
setYDirection(0);
}
break;
case 2:
if(e.getKeyCode() == e.VK_UP){
setYDirection(0);
}
if(e.getKeyCode() == e.VK_DOWN){
setYDirection(0);
}
break;
}
}
public void setYDirection(int ydir){
yDirection = ydir;
}
public void move(){
paddle.y += yDirection;
if(paddle.y <= 15)
paddle.y = 15;
if(paddle.y >= 250)
paddle.y = 250;
}
public void draw(Graphics g){
switch(id){
default:
System.out.println("Please enter a valid ID in Paddle constructor");
break;
case 1:
g.setColor(Color.CYAN);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
break;
case 2:
g.setColor(Color.PINK);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
break;
}
}
@Override
public void run() {
try{
while(true){
move();
Thread.sleep(8);
} `enter code here`enter code here`
}catch(Exception e){System.err.println(e.getMessage());}
}
}
这是我制作的声音类:
package ponggame;
import java.applet.Applet;
import java.applet.AudioClip;
public class Sound {
public static final Sound bgMusic = new Sound("/pong.wav");
public static final Sound hitPaddle = new Sound("/bounce.wav");
private AudioClip clip;
public Sound(String fileName) {
try {
// gets the sound file that is passed in the constructor
clip = Applet.newAudioClip(Sound.class.getResource(fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
// plays the music, obviously
public void play() {
try {
new Thread() { //multi-tasking stuff
public void run(){
//clip.play();
clip.loop();
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
//plays the audio in a loop
public void loop(){
play();
}
}
主班
package ponggame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class Main extends JFrame {
// Double buffering
Image dbImage;
Graphics dbg;
// sound relative
private Sound sound;
// Ball objects
static Ball b = new Ball(193, 143);
// Variables for screen size
int GWIDTH = 400, GHEIGHT = 300;
// Dimension of GWIDTH*GHEIGHT
Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
// Create constructor to spawn window
public Main() {
this.setTitle("Pong Game - RC");
this.setSize(screenSize);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.DARK_GRAY);
this.addKeyListener(new AL());
sound.bgMusic.play();
}
public static void main(String[] args) {
Main m = new Main();
// Create and start threads
Thread ball = new Thread(b);
ball.start();
Thread p1 = new Thread(b.p1);
Thread p2 = new Thread(b.p2);
p1.start();
p2.start();
}
// handles all the music stuff
public static void music() {
}
@Override
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void draw(Graphics g) {
b.draw(g);
b.p1.draw(g);
b.p2.d(g);
g.setColor(Color.WHITE);
g.drawString("" + b.p1Score, 15, 50);
g.drawString("" + b.p2Score, 370, 50);
repaint();
}
// //////EVENT LISTENER CLASS/////////
public class AL extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
b.p1.keyPressed(e);
b.p2.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
b.p1.keyReleased(e);
b.p2.keyReleased(e);
}
}
}
// /////END EVENT LISTENER CLASS/////