【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
相关文章推荐
- java泛型基本理解和运用
- 【java】显示sin曲线
- spring集成dubbo学习总结
- Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out
- 深入理解Java:SimpleDateFormat安全的时间格式化(多线程应用)
- Java 反射
- 解决Eclipse导出javadoc乱码问题
- 【转载】springMVC表单校验+全注解
- java.util.ConcurrentModificationException异常处理
- 【Win10+eclipse+MinGW+QT安装教程】已有eclipse环境下配置QT插件出错详解
- rxJava的使用--Observable的创建及源码分析(三)
- extends:类似于java中的继承特征,extends="struts-default"
- eclipse 中创建maven web项目
- java List 排序 Collections.sort()
- java.lang.ClassNotFoundException: javax.persistence.EntityListeners
- JAVA三大框架SSH的各自作用
- JavaMail直接发送邮件
- struts2关于package 的 namespace
- 浅谈Java设计模式之代理模式
- Rxjava——使用RxBus替换EventBus