- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为学校项目创建了自己版本的 cookie clicker,除了计时器之外,一切似乎都正常。我对计时器的概念很陌生,不确定我的公式是否正确。任何反馈都是有帮助的。我的计时器的公式是双倍速度 = 1/每秒 * 1000;计时器速度 = (int)Math.round(速度);目的是每 0.1 秒添加一个 cookie 作为基线,并在您每秒升级 cookie 时 perSec();它增加了。当我的一些 friend 玩这个游戏时,他们意识到他们每秒获得更多的 cookies 。我想知道这是否是我的计时器的问题,或者我在 switch 语句中使用 addActionCommands 做了什么错误
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class CookieMain
{
//Region Fields
JLabel counterLabel, perSecLabel;
JButton button1, button2, button3, button4, button5, button6;
int cookieCounter, timerSpeed, cursorNumber, cursorPrice, grandpaNumber, grandpaPrice, factoryNumber, factoryPrice, artificialNumber, artificialPrice, gordonNumber, gordonPrice, prestigePrice, prestigeNumber;
double perSecond;
boolean timerOn, grandpaUnlocked, factoryUnlocked, artificialUnlocked, gordonUnlocked, prestigeUnlocked;
Font font1, font2;
CookieHandler cHandler = new CookieHandler();
Timer timer;
JTextArea descriptionText;
MouseHandler mHandler = new MouseHandler();
public static void main(String[] args)
{
new CookieMain();
}
//Creating default values
public CookieMain()
{
timerOn = false;
perSecond = 0.0;
cookieCounter = 0;
cursorNumber = 0;
cursorPrice = 10;
grandpaNumber = 0;
grandpaPrice = 100;
factoryNumber = 0;
factoryPrice = 1000;
artificialNumber = 0;
artificialPrice = 15000;
gordonNumber = 0;
gordonPrice = 100000;
prestigeNumber = 0;
prestigePrice = 10000000;
createFont();
createUI();
}
public void createFont()
{
font1 = new Font("Comic Sans MS", Font.PLAIN, 32);
font2 = new Font("Comic Sans MS", Font.PLAIN, 15);
}
public void createUI()
{
//Creates the frame for the game
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
frame.setLayout(null);
//Creates a new panel for the cookie ImageIcon
JPanel cookiePanel = new JPanel();
cookiePanel.setBounds(100, 220, 200, 200);
cookiePanel.setBackground(Color.black);
frame.add(cookiePanel);
//Instantiates an ImageIcon with the name "cookie" with the image cookie.png
ImageIcon cookie = new ImageIcon(getClass().getClassLoader().getResource("cookie.png"));
//ImageIcon prestige = new ImageIcon(getClass().getClassLoader().getResource(""))
//Adds button and sets ImageIcon "cookie" onto the button
JButton cookieButton = new JButton();
cookieButton.setBackground(Color.black);
cookieButton.setFocusPainted(false);
cookieButton.setBorder(null);
cookieButton.setIcon(cookie);
cookieButton.addActionListener(cHandler);
cookieButton.setActionCommand("cookie");
cookiePanel.add(cookieButton);
//Creates a new panel for labels
JPanel counterPanel = new JPanel();
counterPanel.setBounds(100, 100, 200, 100);
counterPanel.setBackground(Color.black);
counterPanel.setLayout(new GridLayout(2,1));
frame.add(counterPanel);
//Creates a label for the cookie counter
counterLabel = new JLabel(cookieCounter + " cookies");
counterLabel.setForeground(Color.white);
counterLabel.setFont(font1);
counterPanel.add(counterLabel);
//Craetes a label for the cookies per second counter
perSecLabel = new JLabel();
perSecLabel.setForeground(Color.white);
perSecLabel.setFont(font2);
counterPanel.add(perSecLabel);
//Creates a panel for the 4 items on the right
JPanel itemPanel = new JPanel();
itemPanel.setBounds(500, 170, 250, 250);
itemPanel.setBackground(Color.black);
itemPanel.setLayout(new GridLayout(6,1));
frame.add(itemPanel);
//Creates first item "Cursor"
button1 = new JButton("Cursor");
button1.setFont(font1);
button1.setFocusPainted(false);
button1.addActionListener(cHandler);
button1.setActionCommand("Cursor");
button1.addMouseListener(mHandler);
itemPanel.add(button1);
//Creates second item "Grandpa"
button2 = new JButton("?");
button2.setFont(font1);
button2.setFocusPainted(false);
button2.addActionListener(cHandler);
button2.setActionCommand("Grandpa");
button2.addMouseListener(mHandler);
itemPanel.add(button2);
//Creates third item "Factory"
button3 = new JButton("?");
button3.setFont(font1);
button3.setFocusPainted(false);
button3.addActionListener(cHandler);
button3.setActionCommand("Factory");
button3.addMouseListener(mHandler);
itemPanel.add(button3);
//Creates fourth item "Artificial Cookie Plant"
button4 = new JButton("?");
button4.setFont(font1);
button4.setFocusPainted(false);
button4.addActionListener(cHandler);
button4.setActionCommand("ArtificialCookies");
button4.addMouseListener(mHandler);
itemPanel.add(button4);
//Creates secret fith item "GORDON RAMSEY"
button5 = new JButton("?");
button5.setFont(font1);
button5.setFocusPainted(false);
button5.addActionListener(cHandler);
button5.setActionCommand("Gordon");
button5.addMouseListener(mHandler);
itemPanel.add(button5);
//Creates 10,000,000 cookie prestige upgrade
button6 = new JButton("?");
button6.setFont(font1);
button6.setFocusPainted(false);
button6.setBackground(Color.yellow);
button6.addActionListener(cHandler);
button6.setActionCommand("Prestige");
button6.addMouseListener(mHandler);
itemPanel.add(button6);
JPanel descriptionPanel = new JPanel();
descriptionPanel.setBounds(500, 70, 250, 150);
descriptionPanel.setBackground(Color.black);
frame.add(descriptionPanel);
descriptionText = new JTextArea();
descriptionText.setBounds(500, 70, 250, 150);
descriptionText.setForeground(Color.white);
descriptionText.setBackground(Color.black);
descriptionText.setFont(font2);
descriptionText.setLineWrap(true);
descriptionText.setWrapStyleWord(true);
descriptionText.setEditable(false);
descriptionPanel.add(descriptionText);
//Sets frame to be visible
frame.setVisible(true);
}
//setTimer method that adds 1 cookies every 1000 milliseconds (1 second)
public void setTimer()
{
timer = new Timer(timerSpeed, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
cookieCounter++;
counterLabel.setText(cookieCounter + " cookies");
if(grandpaUnlocked == false)
{
if(cookieCounter >= 100)
{
grandpaUnlocked = true;
button2.setText("Grandpa " + "(" + grandpaNumber + ")");
}
}
if(factoryUnlocked == false)
{
if(cookieCounter >= 1000)
{
factoryUnlocked = true;
button3.setText("Factory " + "(" + factoryNumber + ")");
}
}
if(artificialUnlocked == false)
{
if(cookieCounter >= 15000)
{
artificialUnlocked = true;
button4.setText("Artificial " + "(" + artificialNumber + ")");
}
}
if(gordonUnlocked == false)
{
if(cookieCounter >= 100000)
{
gordonUnlocked = true;
button5.setText("Gordons " + "(" + gordonNumber + ")");
}
}
if(prestigeUnlocked == false)
{
if(cookieCounter >= 10000000)
{
prestigeUnlocked = true;
button6.setText("Pretige " + "(" + prestigeNumber + ")");
}
}
}
});
}
public void timerUpdate()
{
if(timerOn == false)
{
timerOn = true;
}
//Divides perSecond by 1 to get seconds than multiply by 1000 to get milliseconds
double speed = 1/perSecond * 1000;
timerSpeed = (int)Math.round(speed);
String s = String.format("%.1f", perSecond);
perSecLabel.setText("per second " + s);
setTimer();
timer.start();
}
//Action listener containing a switch statement with all of the items
public class CookieHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String action = event.getActionCommand();
//Switch statement with all of the items and their function
switch(action)
{
case "cookie":
cookieCounter++;
counterLabel.setText(cookieCounter + " cookies");
break;
case "Cursor":
if(cookieCounter >= cursorPrice)
{
cookieCounter = cookieCounter - cursorPrice;
cursorPrice = cursorPrice + 5;
counterLabel.setText(cookieCounter + " cookies");
cursorNumber++;
button1.setText("Cursor " + "(" + cursorNumber + ")");
descriptionText.setText("Cursor\n[price " + cursorPrice + "]\nAutoClicks once every 10 seconds.");
perSecond = perSecond + 0.1;
timerUpdate();
}
else
{
descriptionText.setText("You need more cookies.");
}
break;
case "Grandpa":
if(cookieCounter >= grandpaPrice)
{
cookieCounter = cookieCounter - grandpaPrice;
grandpaPrice = grandpaPrice + 50;
counterLabel.setText(cookieCounter + " cookies");
grandpaNumber++;
button2.setText("Grandpa " + "(" + grandpaNumber + ")");
descriptionText.setText("Grandpa\n[price " + grandpaPrice + "]\nEach grandpa produces 1 cookie per second.");
perSecond = perSecond + 1;
timerUpdate();
}
else
{
descriptionText.setText("You need more cookies.");
}
break;
case "Factory":
if(cookieCounter >= factoryPrice)
{
cookieCounter = cookieCounter - factoryPrice;
factoryPrice = factoryPrice + 500;
counterLabel.setText(cookieCounter + " cookies");
factoryNumber++;
button3.setText("Factory " + "(" + factoryNumber + ")");
descriptionText.setText("Factory\n[price " + factoryPrice + "]\nEach factory produces 10 cookies per second.");
perSecond = perSecond + 10;
timerUpdate();
}
else
{
descriptionText.setText("You need more cookies.");
}
break;
case "ArtificialCookies":
if(cookieCounter >= artificialPrice)
{
cookieCounter = cookieCounter - artificialPrice;
artificialPrice = artificialPrice + 5000;
counterLabel.setText(cookieCounter + " cookies");
artificialNumber++;
button4.setText("Artificial " + "(" + artificialNumber + ")");
descriptionText.setText("Artificial\n[price " + artificialPrice + "]\nEach Artificial Cookie Plant produces 100 cookies per second.");
perSecond = perSecond + 100;
timerUpdate();
}
else
{
descriptionText.setText("You need more cookies.");
}
break;
case "Gordon":
if(cookieCounter >= gordonPrice)
{
cookieCounter = cookieCounter - gordonPrice;
gordonPrice = gordonPrice + 50000;
counterLabel.setText(cookieCounter + " cookies");
gordonNumber++;
button5.setText("Gordons " + "(" + gordonNumber + ")");
descriptionText.setText("Gordon Ramsey\n[price " + gordonPrice + "]\nEach GORDON RAMSEY produces 10000 cookies per second.");
perSecond = perSecond + 1000;
timerUpdate();
}
else
{
descriptionText.setText("You need more cookies.");
}
break;
case "Prestige":
if(cookieCounter >= gordonPrice)
{
cookieCounter = cookieCounter - prestigePrice;
prestigePrice = prestigePrice * 2;
counterLabel.setText(cookieCounter + " cookies");
prestigeNumber++;
button6.setText("Prestige " + "(" + prestigeNumber + ")");
descriptionText.setText("Prestige\n[price " + prestigePrice + "]\nThis is unlocked when you reach 10,000,000 cookies. It resets your cookie count to 0.");
cookieCounter = 0;
perSecond = 0.1;
timerSpeed = 1;
cursorNumber = 0;
gordonNumber = 0;
factoryNumber = 0;
grandpaNumber = 0;
artificialNumber = 0;
button1.setText("Cursor ");
button2.setText("Grandpa ");
button3.setText("Factory ");
button4.setText("Artificial ");
button5.setText("Gordons ");
cursorPrice = 10;
gordonPrice = 100000;
factoryPrice = 1000;
grandpaPrice = 100;
artificialPrice = 15000;
timerUpdate();
}
else
{
descriptionText.setText("You need more cookies.");
}
}
}
}
public class MouseHandler implements MouseListener
{
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
JButton button = (JButton)e.getSource();
if(button == button1)
{
descriptionText.setText("Cursor\n[price " + cursorPrice + "]\nAutoClicks once every 10 seconds.");
}
else if(button == button2)
{
if(grandpaUnlocked==false)
{
descriptionText.setText("This item is currently locked.");
}
else
{
descriptionText.setText("Grandpa\n[price " + grandpaPrice + "]\nEach grandpa produces 1 cookie per second.");
}
}
else if(button == button3)
{
if(factoryUnlocked == false)
{
descriptionText.setText("This item is currently locked.");
}
else
{
descriptionText.setText("Factory\n[price " + factoryPrice + "]\nEach factory produces 10 cookies per second.");
}
}
else if(button == button4)
{
if(artificialUnlocked == false)
{
descriptionText.setText("This item is currently locked.");
}
else
{
descriptionText.setText("Artificial Cookie\n[price " + artificialPrice + "]\nEach Artificial Cookie Plant produces 100 cookies per second.");
}
}
else if(button == button5)
{
if(gordonUnlocked == false)
{
descriptionText.setText("This item is currently locked.");
}
else
{
descriptionText.setText("Gordon\n[price " + gordonPrice + "]\nEach GORDON RAMSEY produces 10000 cookies per second.");
}
}
else if(button == button6)
{
if(prestigeUnlocked == false)
{
descriptionText.setText("This item is currently locked.");
}
else
{
descriptionText.setText("Prestige\n[price " + prestigePrice + "]\nThis is unlocked when you reach 10,000,000 cookies. It resets your cookie count to 0.");
}
}
}
@Override
public void mouseExited(MouseEvent e)
{
JButton button = (JButton)e.getSource();
if(button == button1)
{
descriptionText.setText(null);
}
else if(button == button2)
{
descriptionText.setText(null);
}
else if(button == button3)
{
descriptionText.setText(null);
}
else if(button == button4)
{
descriptionText.setText(null);
}
else if(button == button5)
{
descriptionText.setText(null);
}
}
}
}```
最佳答案
每次调用 setTimer() 时,都会创建一个新计时器。因此,如果您调用 setTimer() 三次,则会创建三个新计时器,并且每个计时器都会继续创建 cookie。
关于java - 计时器的运行速度比代码中的值快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327392/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!