簡體   English   中英

石頭,剪刀剪游戲Java applet

[英]Rock, paper, scissors game Java applet

我的Java小程序出現問題。 我做了一個剪刀石頭布游戲,但我有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;
    }
}
}

如何加載沒有左側岩石圖像的小程序(玩家選擇)

當玩家尚未做出選擇時,您需要為myValue變量設置第四種情況。 現在,您只有三個潛在值,默認值為0,這就是為什么在做出選擇之前會看到岩石圖像的原因。

每輸一場比賽,玩家和計算機的分數不會降低1

您正在使用int ,而應該使用enumboolean ,這會使您的代碼更難以理解。 我強烈建議將您的代碼重構為使用enumboolean而不是int

同樣,您總是從paint()方法調用每個方法。 不要那樣做 取而代之的是,僅在玩家實際做出選擇時(大概是通過單擊按鈕)才調用檢查獲勝者的函數。

最后,您還要在paint()方法內調用setLocation() ,這是一個壞主意。 您僅應調用一次setLocation() ,或者最好使用布局管理器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM