当前位置:网站首页>Pinball games
Pinball games
2022-07-27 05:25:00 【New an object_】
establish loginBall Class implementation start game interface
package Pinball games ;
import javax.swing.*;
import java.awt.*;
public class loginBall {
JFrame JF=new JFrame(" Pinball games ");
JLabel JL=new JLabel(" Welcome to pinball games ");
JButton jb=new JButton(" Start the game ");
loginBall(){
JF.setBounds(700,270,450,500);
JL.setFont(new Font(" Song style ",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);
}
}
establish Ball Class inheritance loginball class . Use Dialog File dialog box and add corresponding event listening and processing , When you click Start Game , The game interface will pop up on the start game interface !
package Pinball games ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ball extends loginBall {
// Set the width and height of the desktop
int Tab_width = 400;
int Tab_height = 550;
// Set the width and height of the racket
int P_width = 80;
int P_height = 20;
// Set the size of the ball (18 Pixel )
int Ball_siz = 18;
// Used to record the position of the small ball and initialize the position of the small ball (X:240,Y:100)
int X = 240;
int Y = 50;
// Record the ball in X Direction and Y Fractional velocity of
int Vx = 20;
int Vy = 20;
// Record the position of the racket
int P_X = 120;
final int P_Y = 450;
// Identifies whether the game is over
boolean isOver =false;
// Claim a timer
Timer time;
// Custom class , Inherit Canvas, Act as a canvas
class myCanvas extends Canvas {
@Override
public void paint(Graphics g) {
// Draw content
if (isOver) {
g.setColor(Color.PINK);
g.setFont(new Font(" Song style ", Font.BOLD, 30));
g.drawString(" Game over !", 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, " Pinball games ",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) {
// Get the integer corresponding to the currently pressed key ( Each key on the keyboard corresponds to an integer )
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);
// Control of spherical coordinates
ActionListener sss = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//* Correct the speed according to the boundary range
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, " Pinball games ",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();
}
}
Running results : When you click to start the game, the small ball will pop up through the movement of the baffle ; If the ball falls and is not bounced up, the game is over #

边栏推荐
- Explore the mysteries of the security, intelligence and performance of the universal altek platform!
- B1024 scientific counting method
- Quoted popular explanation
- What should test / development programmers over 35 do? Many objective factors
- LeetCode之268.Missing number
- B1024 科学计数法
- mq设置过期时间、优先级、死信队列、延迟队列
- Idea remote debugging
- JVM上篇:内存与垃圾回收篇二--类加载子系统
- During its low-level period, this slave edge causes the instruction number to make a corresponding model
猜你喜欢

BIO、NIO、AIO区别

Scientific Computing Library - numpy

JVM上篇:内存与垃圾回收篇十四--垃圾回收器

Differences among bio, NiO and AIO

弹球小游戏

李宏毅机器学习组队学习打卡活动day03---误差和梯度下降

接收方设置并发量和限流

How idea creates a groovy project (explain in detail with pictures and texts)

素数筛选(埃氏筛法,区间筛法,欧拉筛法)

JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
随机推荐
JVM Part 1: memory and garbage collection part 12 -- stringtable
Utility gadget: kotlin code snippet
B1025 反转链表*******
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
redis集群
文件处理(IO)
redis持久化
JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
Student management system
B1031 查验身份证
JVM上篇:内存与垃圾回收篇十四--垃圾回收器
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
During its low-level period, this slave edge causes the instruction number to make a corresponding model
李宏毅机器学习组队学习打卡活动day02---回归
Niuke sword refers to the path in the offer--jz12 matrix
Machine learning overview
如何查看导师的评价
B1030 perfect sequence
Solution to Dlib installation failure