package com.shrimpking.t9;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/11/13 20:39
*/
public class PinBall extends JFrame implements KeyListener
{
private BallCanvas canvas; //面板
private Timer timer; // 定时器
private boolean isOver = false; //游戏是否结束
private static final int TABLE_WIDTH = 350; //屏幕宽度
private static final int TABLE_HEIGHT = 500; //屏幕高度
private static final int BALL_SIZE = 18; // 球的尺寸
private static final int RECKET_WIDTH = 100; // 球拍宽度
private static final int RECKET_HEIGHT = 20; //球拍厚度
private int ballX = 120; //球的坐标
private int ballY = 30; //
private int speedX = 5; // 加速度
private int speedY = 10; //
private int recketX = 120; // 球拍的坐标
private int recketY = 400; //
private int score = 0;
public PinBall(){
this.setTitle("击球游戏");
this.setSize(TABLE_WIDTH, TABLE_HEIGHT); //窗体尺寸
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗体关闭事件
canvas = new BallCanvas();
canvas.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT)); //面板尺寸
canvas.addKeyListener(this); //键盘事件
this.getContentPane().add(canvas);
ActionListener task = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//根据边界,修正加速度
if (ballX <= 0 || ballX >= (TABLE_WIDTH - BALL_SIZE - speedX)){
//如果球的x坐标,小于左边界,或大于右边界时,速度转向
speedX = -speedX;
}
if (ballY <= 0 || (ballY >= recketY - BALL_SIZE - speedY && ballX > recketX && ballX < recketX + RECKET_WIDTH)){
//如果球的y坐标,小于顶边界,大于球拍的y坐标的边界,速度转向
if (ballY >= recketY - BALL_SIZE - speedY){
score += 100;
}
speedY = -speedY;
}
if (ballY > recketY - BALL_SIZE && (ballX < recketX || ballX > recketX + RECKET_WIDTH)){
//如果球的y坐标,超出球拍的边界,停止游戏
timer.stop(); //停止计时,球静止了
isOver = true; //游戏结束
canvas.repaint(); //面板重绘
}
ballX += speedX; //球运动
ballY += speedY; //
canvas.repaint();
}
};
timer = new Timer(100,task); //计时器
this.addKeyListener(this); //窗体键盘事件
this.setResizable(false); //不可伸缩
this.setLocationRelativeTo(null); //居中显示
this.setVisible(true); //可见
timer.start(); //计时器开启
}
public static void main(String[] args)
{
PinBall w = new PinBall();
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT){
if (recketX > 0){
//方向键,左,
recketX -= 11;
canvas.repaint();
}
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
if (recketX < (TABLE_WIDTH - RECKET_WIDTH)){
//方向键,右
recketX += 11;
canvas.repaint();
}
}else if (e.getKeyCode() == KeyEvent.VK_ENTER){
//方向键,回车
if (isOver){
//如果游戏结束了,回车开启新游戏
isOver = false; //
score = 0; //成绩
ballX = (int) (Math.random() * TABLE_WIDTH); //随机,球坐标
ballY = 30; //
canvas.repaint(); //重绘
timer.start(); //计时器开启
}
}
}
@Override
public void keyReleased(KeyEvent e)
{
}
private class BallCanvas extends JPanel{
@Override
public void paint(Graphics g)
{
super.paint(g);
if (isOver){
//游戏结束
g.setColor(Color.BLUE);
g.setFont(new Font("宋体",Font.PLAIN,40));
g.drawString("游戏结束!",70,220);
}else {
//游戏进行中
g.setColor(Color.RED);
g.fillOval(ballX,ballY,BALL_SIZE,BALL_SIZE); //球
g.setColor(Color.PINK);
g.fillRect(recketX,recketY,RECKET_WIDTH,RECKET_HEIGHT); //球拍
g.setColor(Color.BLACK);
g.setFont(new Font("黑体",Font.PLAIN,18));
g.drawString("得分:" + score, 10,20); //得分
}
}
}
}
标签:recketX,canvas,游戏,WIDTH,int,private,击球,PinBall,TABLE
From: https://blog.csdn.net/modern358/article/details/143753843