您的位置:首页 > 编程语言 > Java开发

【java】显示刽子手游戏画面

2016-05-28 17:17 471 查看


刽子手是一款经典的游戏,其界面如上图所示,实现代码如下:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Guizishou15_17 extends JFrame{
public Guizishou15_17(){
setLayout(new BorderLayout());
add(new Draw(), BorderLayout.CENTER);
}

public static void main(String[] args) {
Guizishou15_17 frame = new Guizishou15_17();
frame.setSize(300, 300);
frame.setTitle("刽子手");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);

}
}

class Draw extends JPanel{
public void drawFunction() {
repaint();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawOval(15,245,60, 30);//底部半椭圆

g.drawLine(45,245,45,20);//杆子
g.drawLine(45,20,180,20);
g.drawLine(180,20,180,50);

g.drawOval(165, 50, 30, 30);//人头
g.drawLine(180,80,180,130);//身体
g.drawLine(180,130,150,160);//下肢左
g.drawLine(180,130,210,160);//下肢右

//上肢 左 右
g.drawLine((int) (180-15*Math.sin(Math.PI/4)),(int) (80-15+15*Math.cos(Math.PI/4)),130,110);
g.drawLine((int) (180+15*Math.sin(Math.PI/4)),(int) (80-15+15*Math.cos(Math.PI/4)),230,110);

}
}


另外刽子手游戏的代码如下:

import java.util.Scanner;

public class GuiZiShou9_33 {

public static void main(String[] args) throws Exception {

java.io.File file=new java.io.File("Exercise9_33.txt");
Scanner input=new Scanner(file);
Scanner put=new Scanner(System.in);
String [] words=new String[5];
int i=0;

while(input.hasNext()){
words[i]=input.next();
i++;
}

String [] S=new String[5];

for(i=0;i<5;i++){
S[i]="";
}

for(i=0;i<words.length;i++){
for(int j=0;j<words[i].length();j++)
S[i]=S[i]+"*";
}

String Set="";
String str="";

do{
int index=(int) (Math.random()*5);
char [] chars=words[index].toCharArray();
char [] C=S[index].toCharArray();

do
{

System.out.print("请输入第"+(index+1)+"个单词中字母");
for(i=0;i<C.length;i++)
System.out.print(C[i]);
System.out.println("");

String temp=put.next();
char t=temp.charAt(0);

for(i=0;i<chars.length;i++){
if(t==C[i])
System.out.println(t+"已经存在!");
else if(t==chars[i])
C[i]=t;
}

str=String.valueOf(C);

}while(!str.equals(words[index]));

System.out.println("第"+index+"个单词为:"+str);
System.out.println("还要继续吗?Y/N");

Set=put.next();

if(Set=="N")
break;
}while(Set.equals("Y")||Set.equals("y"));
}
}


其中Exercise9_33.txt如下:

welcome to huaqiao university xiamen
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: