当前位置:网站首页>弹球小游戏
弹球小游戏
2022-07-27 05:01:00 【new一个对象_】
创建loginBall类实现开始游戏界面
package 弹球小游戏;
import javax.swing.*;
import java.awt.*;
public class loginBall {
JFrame JF=new JFrame("弹球小游戏");
JLabel JL=new JLabel("欢迎来到弹球小游戏");
JButton jb=new JButton("开始游戏");
loginBall(){
JF.setBounds(700,270,450,500);
JL.setFont(new Font("宋体",Font.BOLD,30));
JL.setForeground(Color.pink);
JF.add(JL);
JF.add(jb);
JF.setVisible(true);
JF.setLayout(new FlowLayout(FlowLayout.CENTER,30,100));
JF.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
创建Ball类继承loginball类。使用Dialog文件对话框并添加相应的事件监听处理,当点击开始游戏后,游戏界面会弹出在开始游戏界面之上!
package 弹球小游戏;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ball extends loginBall {
//设置桌面的宽度和高度
int Tab_width = 400;
int Tab_height = 550;
//设置球拍的宽度和高度
int P_width = 80;
int P_height = 20;
//设置球的大小(18个像素)
int Ball_siz = 18;
//用于记录小球的位置及初始化小球的位置(X:240,Y:100)
int X = 240;
int Y = 50;
//记录小球在X方向与Y的分速度
int Vx = 20;
int Vy = 20;
//记录球拍的位置
int P_X = 120;
final int P_Y = 450;
//标识游戏是否结束
boolean isOver =false;
//声名一个定时器
Timer time;
//自定义类,继承Canvas,充当画布
class myCanvas extends Canvas {
@Override
public void paint(Graphics g) {
//绘制内容
if (isOver) {
g.setColor(Color.PINK);
g.setFont(new Font("宋体", Font.BOLD, 30));
g.drawString("游戏结束!", 130, 150);
} else {
g.setColor(Color.red);
g.fillOval(X, Y, Ball_siz, Ball_siz);
g.setColor(Color.CYAN);
g.fillRect(P_X, P_Y, P_width, P_height);
}
}
}
myCanvas my = new myCanvas();
public void init() {
Dialog D1=new Dialog(JF, "弹球小游戏",true);
D1.setBounds(750,300,Tab_width,Tab_height);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
D1.setVisible(true);
}
});
D1.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
KeyListener listen = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获取当前按下的键对应的整数(键盘上的每一个键都对应一个整数)
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
if (P_X > 0) {
P_X-= 30;
}
}
if (key == KeyEvent.VK_RIGHT) {
if (P_X<=(Tab_width-P_width)) {
P_X+= 30;
}
}
}
};
D1.addKeyListener(listen);
//小球坐标的控制
ActionListener sss = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//*根据边界范围修正速度
if (X <= 0||X >=(Tab_width-Ball_siz)) {
Vx = -Vx;
}
if (Y <= 0||(Y >P_Y-Ball_siz && X >P_X && X < P_X + P_width)) {
Vy = -Vy;
}
if (Y >P_Y-Ball_siz && (X < P_X || X > P_X+ P_width)) {
time.stop();
isOver = true; Dialog D1=new Dialog(JF, "弹球小游戏",true);
my.repaint();
}
X+=Vx;
Y+=Vy;
my.repaint();
}
};
time=new Timer(100,sss);
time.start();
my.setPreferredSize(new Dimension(Tab_width,Tab_height));
D1.add(my);
}
public static void main(String[] args) {
new Ball().init();
}
}
运行结果:当点击开始游戏后通过挡板的移动弹起小球;若小球下落未被弹起则游戏结束#

边栏推荐
- 二、MySQL高级
- Event Summary - common summary
- JVM上篇:内存与垃圾回收篇十四--垃圾回收器
- OFDM 16 lecture 2-ofdm and the DFT
- [acwing] solution to the 61st weekly match
- MySQL storage engine and its differences
- JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack
- Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
- Card drawing program simulation
- 传智教育|软件测试工程师未来的发展方向有哪些?
猜你喜欢
随机推荐
[untitled] I is circularly accumulated under certain conditions. The condition is usually the length of the loop array. When it exceeds the length, the loop will stop. Because the object cannot judge
Why is count (*) slow
支付流程如何测试?
pyside2____1.安装和案列
35. Scroll
[optical flow] - data format analysis, flowwarp visualization
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
idea远程调试debug
探寻通用奥特能平台安全、智能、性能的奥秘!
一、MySQL基础
Detailed description of polymorphism
Bean的生命周期&&依赖注入*依赖自动装配
Read write separation and master-slave synchronization
Shell course summary
Mysql表的约束
34. Analyze flexible.js
How to test the payment process?
Typescript details
JVM上篇:内存与垃圾回收篇七--运行时数据区-堆
Slashes / and backslashes involved in writing code\









