今天是2016年7月28日
Java制作简单的猜拳游戏,
游戏界面
简单分析:
猜拳比赛需要两个选手参加,一个是自己,一个是敌人,每个选手都有两个属性,分别是分数和角色;哪一方获胜,该方的分数自动加1,平局都不加,并记录平局数,每比完一次赛,计算结果并输出,然后询问是否继续玩,如果继续,则重复以上动作,如果不想继续玩,则计算最终结果并输出退出.
Person.java 类
package person;
public class Person {
int score;
String role;
public int attack(){return 0;};
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public void addScore() {
this.score++;
}
}
Player.java
package person;
import java.util.Scanner;
public class Playerextends Person {
public Player() {
// TODO Auto-generated constructor stub
}
@Override
public int attack() {
// TODO Auto-generated method stub
System.out.println("请出拳:1.石头2.剪子3.布2");
Scanner input = new Scanner(System.in);
int index = input.nextInt();
return index;
}
@Override
public
void addScore() {
super.addScore();
System.out.println("结果:运气真好,你赢了!");
}
}
Enemy.java
package person;
import java.util.Random;
public class Enemyextends Person {
public Enemy() {
// TODO Auto-generated constructor stub
}
@Override
public int attack() {
// TODO Auto-generated method stub
Random random = new Random();
return random.nextInt(2) + 1;
}
@Override
public
void addScore() {
super.addScore();
System.out.println("结果:啊!你输了!");
}
}
Game.java
package gameTest;
import java.util.Scanner;
import person.Enemy;
import person.Player;
public class Game {
private Player player;
private Enemy enemy;
private String roles[];
private String enemyRoles[];
private String attacks[];
private int peace = 0;
final int stone = 1;
final int cut = 2;
final int cloth = 3;
public Game() {
// TODO Auto-generated constructor stub
player = new Player();
enemy = new Enemy();
roles = new String[] { "null", "沸羊羊", "暖羊羊", "美羊羊" };
enemyRoles = new String[] { "null", "喜羊羊", "慢羊羊", "懒羊羊" };
attacks = new String[] { "null", "石头", "剪子", "布" };
}
private void setRole() {
Scanner input = new Scanner(System.in);
System.out.print("\t\t请选择你的角色(1.沸羊羊 2.暖羊羊 3.美羊羊):");
player.setRole(roles[input.nextInt()]);
System.out.print("\t\t请选择对手角色(1.喜羊羊 2.慢羊羊 3.懒羊羊):");
enemy.setRole(enemyRoles[input.nextInt()]);
}
public void start() {
System.out.println("-----------------欢迎进入羊村游戏世界--------------------");
System.out.println("\t\t******************");
System.out.println("\t\t** 游戏开始 **");
System.out.println("\t\t******************");
System.out.println("\t\t出拳规则:1.石头2.剪子3.布");
this.setRole();
System.out.print("开始游戏吗? (y/n)");
Scanner input = new Scanner(System.in);
if (input.next().equals("y"))
this.play();
else
System.out.println("退出游戏");
input.close();
}
public void play() {
int enemyAttack;
while (true) {
System.out.print("请出拳:1.石头2.剪子3.布");
Scanner input = new Scanner(System.in);
switch (input.nextInt()) {
case stone:
System.out.println("你出拳:" + attacks[stone]);
enemyAttack = enemy.attack();
System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);
if (enemyAttack == this.cut)
player.addScore();
else if (enemyAttack == this.cloth)
enemy.addScore();
else if (enemyAttack == this.stone) {
peace++;
System.out.println("平局");
}
break;
case cut:
System.out.println("你出拳:" + attacks[cut]);
enemyAttack = enemy.attack();
System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);
if (enemyAttack == this.cloth)
player.addScore();
else if (enemyAttack == this.stone)
enemy.addScore();
else if (enemyAttack == this.cut) {
peace++;
System.out.println("平局");
}
break;
case cloth:
System.out.println("你出拳:" + attacks[cloth]);
enemyAttack = enemy.attack();
System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);
if (enemyAttack == this.stone)
player.addScore();
else if (enemyAttack == this.cut)
enemy.addScore();
else if (enemyAttack == this.cloth) {
peace++;
System.out.println("平局");
}
break;
default:
break;
}
System.out.print("是否开始下一轮? (y/n)");
if (input.next().equals("n")) {
this.Over();
break;
}
System.out.println("--------------------------------");
}
}
public void Over() {
System.out.println(player.getRole() + "\t对决\t" + enemy.getRole());
int total = player.getScore() + enemy.getScore() + peace;
System.out.println("次数 :" + total);
System.out.println("平局 :" + peace);
System.out.println(player.getRole() + " 得:" + player.getScore());
System.out.println(enemy.getRole() + " 得:" + enemy.getScore());
if (player.getScore() > enemy.getScore())
System.out.println("最终结果 :" + player.getRole() + "获胜");
else if (player.getScore() < enemy.getScore())
System.out.println("最终结果 :" + enemy.getRole() + "获胜");
else
System.out.println("最终结果 :平");
System.out.println("--------------------------------");
}
}
Test.java
package gameTest;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game game = new Game();
game.start();
}
}
二 用swing做的登录界面
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Login extends JFrame {
/**
* @param args
*/
public Login()
{
setLayout(null);
JLabel l1 = new JLabel("用户名:");
JTextField t1 =new JTextField();
JLabel l2 = new JLabel("密码:");
JTextField t2 =new JTextField();
JButton b1 = new JButton("登录");
JButton b2 = new JButton("重置");
l1.setBounds(30, 10,50,20);
t1.setBounds(100, 10,150,20);
l2.setBounds(30, 50,50,20);
t2.setBounds(100, 50,150,20);
b1.setBounds(40,80,80,20);
b2.setBounds(140,80,80,20);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
}
public static void run(JFrame f,final int width,final int height)
{
f.setTitle(f.getClass().getName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width,height);
f.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
run(new Login(),300,400);
}
}