[英]Rock, paper, scissors game Java applet
I'm having problems with my java applet. 我的Java小程序出现问题。 I've made a Rock paper scissors game and i have 2 problems which I can't solve: 1. how to load the applet without the rock image on the left (players choice) 2. the score for player and computer does not reduce by 1 for every lost game 我做了一个剪刀石头布游戏,但我有2个我无法解决的问题:1.如何在没有左侧石头图像的情况下加载小程序(玩家选择)2.玩家和计算机的得分不会降低每输一场比赛加1
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class RockScissorsPaper extends Applet implements ActionListener
{
private Button rockButton;
private Button scissorsButton;
private Button paperButton;
private String buttonPressed = "";
private int computerValue = -1;
private int myValue;
private int playerScore = 10;
private int computerScore = 10;
private int drawScore = 0;
private Image imgRock;
private Image imgScissors;
private Image imgPaper;
public void init()
{
rockButton = new Button("Rock");
scissorsButton = new Button("Scissors");
paperButton = new Button("Paper");
add(rockButton);
add(scissorsButton);
add(paperButton);
rockButton.addActionListener(this);
scissorsButton.addActionListener(this);
paperButton.addActionListener(this);
imgRock = getImage(getCodeBase(), "rock.jpg");
imgScissors = getImage(getCodeBase(), "scissors.jpg");
imgPaper = getImage(getCodeBase(), "paper.jpg");
}
public void actionPerformed(ActionEvent event)
{
buttonPressed = ((Button)event.getSource()).getLabel();
computerValue = randomNumber012();
translator(buttonPressed);
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
rockButton.setLocation(20,260);
rockButton.setSize(70,35);
paperButton.setLocation(210, 260);
paperButton.setSize(70, 35);
scissorsButton.setLocation(380, 260);
scissorsButton.setSize(90, 35);
choice(g);
g.drawString("Player's Wins: " + playerScore, 10, 20);
g.drawString("Computer's Wins: " + computerScore, 180, 20);
g.drawString("Draws: " + drawScore, 400, 20);
g.drawString("Your Choice: " + buttonPressed, 20, 60);
g.drawString("Computer's Pick: ", 350, 60);
winner(g, computerValue, myValue);
}
int randomNumber012()
{
return (int)(Math.random()*3);
}
public void translator(String s)
{
if(s.equals("Rock"))
{
myValue = 0;
}
else if(s.equals("Scissors"))
{
myValue = 1;
}
else if(s.equals("Paper"))
{
myValue = 2;
}
}
public void choice(Graphics g)
{
if(myValue == 0)
{
g.drawImage(imgRock, 20, 100, 100, 60, this);
}
else if(myValue == 1)
{
g.drawImage(imgScissors, 20, 100, 100, 60, this);
}
else if(myValue == 2)
{
g.drawImage(imgPaper, 20, 100, 100, 60, this);
}
if(computerValue == 0)
{
g.drawString("Computer's Pick: Rock", 350, 60);
g.drawImage(imgRock, 350, 100, 100, 60, this);
}
else if(computerValue == 1)
{
g.drawString("Computer's Pick: Scissors", 350, 60);
g.drawImage(imgScissors, 350, 100, 100, 60, this);
}
else if(computerValue == 2)
{
g.drawString("Computer's Pick: Paper", 350, 60);
g.drawImage(imgPaper, 350, 100, 100, 60, this);
}
}
public void winner(Graphics g, int cv, int mv)
{
if(cv == -1)
{
g.drawString("", 200, 100);
}
else if(cv == mv)
{
g.drawString("Draw", 200, 250);
drawScore = drawScore + 1;
}
else if(cv == 0 && mv == 1 || cv == 2 && mv == 0 || cv == 1 && mv == 2)
{
g.drawString("Computer Wins", 200, 250);
playerScore = playerScore - 1;
}
else
{
g.drawString("You Win!", 200, 250);
computerScore = computerScore - 1;
}
if (playerScore == 0)
{
System.out.println("You lost!");
playerScore = 10;
computerScore = 10;
drawScore = 0;
}
else if(computerScore == 0)
{
System.out.println("You won!");
playerScore = 10;
computerScore = 10;
drawScore = 0;
}
}
}
how to load the applet without the rock image on the left (players choice) 如何加载没有左侧岩石图像的小程序(玩家选择)
You need to have a fourth case for your myValue
variable, when the player has not made a choice yet. 当玩家尚未做出选择时,您需要为myValue
变量设置第四种情况。 Right now you only have three potential values, and the default is 0, which is why you see a rock image before you make a choice. 现在,您只有三个潜在值,默认值为0,这就是为什么在做出选择之前会看到岩石图像的原因。
the score for player and computer does not reduce by 1 for every lost game 每输一场比赛,玩家和计算机的分数不会降低1
You're using int
s where you should be using enum
s and boolean
s, which is making your code harder to understand. 您正在使用int
,而应该使用enum
和boolean
,这会使您的代码更难以理解。 I'd highly recommend refactoring your code to use enum
s and boolean
s instead of int
s. 我强烈建议将您的代码重构为使用enum
和boolean
而不是int
。
Also, you're always calling every method from the paint() method. 同样,您总是从paint()方法调用每个方法。 Don't do that. 不要那样做 Instead, only call the functions that check for a winner when the player actually makes a choice, presumably from a button click. 取而代之的是,仅在玩家实际做出选择时(大概是通过单击按钮)才调用检查获胜者的函数。
Finally, you're also calling setLocation()
inside your paint()
method, which is a bad idea. 最后,您还要在paint()
方法内调用setLocation()
,这是一个坏主意。 You should only call setLocation()
once, or better yet, use a layout manager. 您仅应调用一次setLocation()
,或者最好使用布局管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.