Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Created September 16, 2016 02:05
Show Gist options
  • Save pinglunliao/62d1847f7a110bc4bfdce09d84f0875f to your computer and use it in GitHub Desktop.
Save pinglunliao/62d1847f7a110bc4bfdce09d84f0875f to your computer and use it in GitHub Desktop.
package holan.tw;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class PingPongGame extends JFrame implements KeyListener, ActionListener {
// 遊戲畫面大小
final int SCREEN_WIDTH = 400;
final int SCREEN_HEIGHT = 400;
// 球的初始位置
final int INIT_Y_POS = SCREEN_HEIGHT / 2;
final int INIT_X_POS = SCREEN_WIDTH / 2;
// 擊球板子的大小
final int PAD_WIDTH = 20;
final int PAD_HEIGHT = 100;
final int PLAYER_NUM = 2;
final int PAD_OFFSET = 10;
// 更新球位置的計時器
Timer ballTimer;
// 球移動的速度
int ballSpeedX = 1, ballSpeedY = 1;
final int DELAY_MS = 20;
// 球的位置
int ballPosX = INIT_X_POS;
int ballPosY = INIT_Y_POS;
// 球的大小
final int BALL_RADIUS = 20;
// 玩家移動板子的速度
int playerSpeedY = 20;
// 玩家板子的位置
int[] playerPosX = new int[PLAYER_NUM];
int[] playerPosY = new int[PLAYER_NUM];
public PingPongGame() {
setTitle("遊戲基礎-乒乓球");
setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addKeyListener(this);
initPlayerPos();
initBallTimer();
}
private void initBallTimer() {
ballTimer = new Timer(DELAY_MS, this);
ballTimer.setInitialDelay(190);
ballTimer.start();
}
private void initPlayerPos() {
for(int i = 0; i < PLAYER_NUM; i++) {
playerPosY[i] = INIT_Y_POS;
}
playerPosX[0] = PAD_OFFSET;
playerPosX[1] = SCREEN_WIDTH - PAD_WIDTH - PAD_OFFSET;
}
public void update(Graphics g) {
this.paint(g);
}
public void paint(Graphics g) {
super.paint(g);
drawPlayerPad(g);
drawBall(g);
}
private void drawPlayerPad(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(playerPosX[0], playerPosY[0], PAD_WIDTH, PAD_HEIGHT);
g.setColor(Color.BLUE);
g.fillRect(playerPosX[1], playerPosY[1], PAD_WIDTH, PAD_HEIGHT);
}
private void drawBall(Graphics g) {
g.setColor(Color.CYAN);
g.fillOval(ballPosX, ballPosY, BALL_RADIUS, BALL_RADIUS);
}
public static void main(String[] args) {
new PingPongGame().show();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
// Move the player on the left
if( key == KeyEvent.VK_UP )
playerPosY[0] -= playerSpeedY;
if( key == KeyEvent.VK_DOWN )
playerPosY[0] += playerSpeedY;
// Move the player on the right
if( key == KeyEvent.VK_W )
playerPosY[1] -= playerSpeedY;
if( key == KeyEvent.VK_X )
playerPosY[1] += playerSpeedY;
checkPadPosRange();
repaint();
}
// 檢查板子是否超出遊戲畫面的範圍
private void checkPadPosRange() {
for(int i = 0; i < PLAYER_NUM; i++) {
if( playerPosY[i] < 0) playerPosY[i] = 0;
if( playerPosY[i] > SCREEN_HEIGHT - PAD_HEIGHT) playerPosY[i] = SCREEN_HEIGHT - PAD_HEIGHT;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
ballPosX += ballSpeedX;
ballPosY += ballSpeedY;
// 球是否碰到遊戲畫面邊界
if( ballPosX >= SCREEN_WIDTH - BALL_RADIUS || ballPosX <= 0 ) ballSpeedX = -ballSpeedX;
if( ballPosY >= SCREEN_HEIGHT - BALL_RADIUS || ballPosY <= BALL_RADIUS) ballSpeedY = -ballSpeedY;
this.repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment