编程小白学逻辑-实现王者荣耀轮盘中奖功能
Posted 长安紫薯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程小白学逻辑-实现王者荣耀轮盘中奖功能相关的知识,希望对你有一定的参考价值。
功能介绍
实现王者荣耀轮盘中奖功能
功能
功能:
1、窗口
2、画背景、画选框
3、按键
4、按任意键移动框、转弯移动
5、状态控制自动移动,按键启停
6、移动n次后停止,随机数,随机数转动
7、改变执行速度(慢、快、慢)
8、框最终如果移动到“水晶”上,界面显示中奖信息
界面原型
资源
两张图片
背景图 0.png
选中框 1.png
实现代码
Panel.java
package game.prize;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
/*
* 1、解决刷新字体闪现问题
* 2、键盘和鼠标额外方法就无需在主类中展现问题,如只用到keyPressed事件,其它事件无需要
*/
public class Panel extends JPanel implements KeyListener,MouseListener {
GoPrize gp = new GoPrize();
//反复执行
public void paint(Graphics g) {
this.gp.paint(g);
this.repaint(); //重新绘制
//System.out.println(System.currentTimeMillis());
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
this.gp.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
this.gp.mouseClicked(e);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
GoPrize.java
package game.prize;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GoPrize{
//框的起始坐标
int x = 352;
int y = 90 ;
//控制转动标识
boolean state = true; //false暂停,true转动
//控制旋转次数
int count = 0;
//int limit = 40; //最大执行移动次数,一圈14,30固定值位置就能猜出来了
//int limit = new Random().nextInt(50) +1; //[1, 51) 范围1~50的随机值
int limit = new Random().nextInt(50) +20; //[20, 70) 范围1~70的随机值
//拐点
int kneePointLeft = x - 187;
int kneePointRight = x + 187+187+187;
int kneePointTop = y;
int kneePointBottom = y +125+125+125;
public static void main(String[] args) {
GoPrize gp = new GoPrize();
gp.paintWindow();
}
//画界面
public void paintWindow() {
JFrame f = new JFrame();
//frame默认会创建一个JPanel,但不新创建,在repaint重新刷新页面更新内容时写入的字体会发生晃动
Panel p = new Panel();
f.add(p);
f.setTitle( "Game Prize");
f.setSize(1120, 630); //设置窗口大小
f.setLocationRelativeTo(null); //居中
f.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); //关闭按钮
f.setVisible(true); //展示界面
f.addKeyListener(p);
f.addMouseListener(p);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='x') {
System.exit(0); //当用户按下x键,直接结束程序
}
//状态切换
state = !state;
count = 0; //重头开始计数
}
public void mouseClicked(MouseEvent e) {
//状态切换
state = !state;
}
//自动执行,进行画背景
public void paint(Graphics g) {
g.drawImage(read("images/0.png"), 0, 0, null);
g.drawImage(read("images/1.png"), x, y, null);
g.setFont(new Font("微软雅黑", Font.PLAIN, 20)); //设置字体、常规、大小
g.setColor(Color.YELLOW); //设置颜色
g.drawString("52800", 800, 30); //写文字到界面上
if(state) {
//移动位置
if( x!=kneePointRight && y==kneePointTop ) {
x = x +187;
}else if( x==kneePointRight && y!=kneePointBottom) {
y = y +125;
}else if( x!=kneePointLeft && y==kneePointBottom) {
x = x -187;
}else if( x==kneePointLeft && y!=kneePointTop) {
y = y - 125;
}
}
//根据不同区间,改变执行节奏
if(count>0 && count<2) {
sleep(100);
}else if(limit-count<=2) {
sleep(500);
}else {
sleep(30);
}
count++; //移动次数+1
if(count >= limit) {
state = false; //暂停
}
//当最后的框在荣耀水晶时,标识中奖
if( x==kneePointLeft && y==kneePointTop && state==false) {
g.setFont(new Font("微软雅黑", Font.PLAIN, 35)); //设置字体、常规、大小
g.setColor(Color.WHITE); //设置颜色
g.drawString("恭喜获得丰厚奖金:荣耀水晶", 380, 360); //写文字到界面上
}
}
//读取图片
public BufferedImage read(String filename) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(filename));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
//延时
public void sleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
以上是关于编程小白学逻辑-实现王者荣耀轮盘中奖功能的主要内容,如果未能解决你的问题,请参考以下文章