当前位置:网站首页>弹球小游戏
弹球小游戏
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();
}
}
运行结果:当点击开始游戏后通过挡板的移动弹起小球;若小球下落未被弹起则游戏结束#

边栏推荐
- Interface and abstract class / method learning demo
- How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
- QT menu bar, toolbar and status bar
- Another skill is to earn 30000 yuan a month+
- B1028 人口普查
- 求组合数(最强优化)
- Constraints of MySQL table
- Sub database and sub table
- 34. Analyze flexible.js
- JVM上篇:内存与垃圾回收篇二--类加载子系统
猜你喜欢

Another skill is to earn 30000 yuan a month+

Dialog introduction

How to create an applet project

String class

JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack

What should test / development programmers over 35 do? Many objective factors

JVM上篇:内存与垃圾回收篇十二--StringTable

The difference between strlen and sizeof

pyside2____1.安装和案列

JVM Part 1: memory and garbage collection part 5 -- runtime data area virtual machine stack
随机推荐
The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay
During its low-level period, this slave edge causes the instruction number to make a corresponding model
JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area
【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普
知识点总结(一)
B1029 旧键盘
pyside2____ 1. Installation and case listing
JVM Part 1: memory and garbage collection part 11 -- execution engine
来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”
Sub database and sub table
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
B1026 程序运行时间
JVM Part 1: memory and garbage collection part 7 -- runtime data area heap
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
Installation and template setting of integrated development environment pychar
Summary of knowledge points (I)
Create datasource using Druid connection pool
What should test / development programmers over 35 do? Many objective factors