java编写的游戏
⑴ 用java编写一个完整的游戏程序
//打字游戏给你吧
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
class WordPanel extends JPanel implements Runnable{
private Thread thread = null;
private int level = 1;
private Font font = new Font("宋体",Font.ITALIC+Font.BOLD,24);
private Color color = Color.BLUE;
public static final int x = 10;
private int y = 0;
private char word;//下落的字母
private static Random rand = new Random();
public void setY(int y){
this.y = y;
}
public void setWord(char word){
this.word = word;
}
public char getWord(){
return this.word;
}
public static char newChar(){
return (char)(97+rand.nextInt(26));
}
public WordPanel(){
word = newChar();
thread = new Thread(this);
thread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(font);
g.setColor(color);
g.drawString(String.valueOf(word),x,y);
}
public void run(){
while (true){
try {
Thread.sleep(1000);
this.repaint();
if (y>=this.getHeight()){
y = 0;
word = this.newChar();
}else
y+=20;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public class WordGame extends JFrame{
private WordPanel[] words = new WordPanel[10];
class Listener extends KeyAdapter{
public void keyTyped(KeyEvent e) {
char input = e.getKeyChar();
for (int i = 0; i<words.length; i++){
if ( input==words[i].getWord() ){
words[i].setWord(WordPanel.newChar());
words[i].setY(0);
words[i].repaint();
break;
}
}
}
}
public WordGame(String title){
super(title);
Container c = this.getContentPane();
c.setLayout(new GridLayout(1,words.length));
this.addKeyListener( new Listener() );
for (int i = 0; i<words.length; i++){
words[i] = new WordPanel();
c.add(words[i]);
}
this.setSize( new Dimension(400,400) );
this.setVisible(true);
}
public static void main(String[] args){
WordGame game = new WordGame("简单的打字游戏");
}
}
⑵ java编写的小游戏要怎么运行呢
java 代码 找有main函数的.java文件运行,那是入口。你的配置好环境,导入代码,没学过的话建议先看看基基础应用,借本java从入门到精通之类的书,一看就明白的。
⑶ JAVA编写一个简单的跳棋游戏
import java.awt.*;
import javax.swing.JFrame;
public class JumpChess extends JFrame{
public JumpChess(){
setSize(800,800); setVisible(true);
setBackground(Color.pink);
}
public void paint(Graphics g){ g.setColor(Color.gray); int x=290; int y=140; int x1=330;
for(int row=1;row<13;row++){ g.drawLine(x,y,x1,y);
x=x-20;
x1=x1+20;
y=y+30;
System.out.println(); }
int x2=110; int y2=230; int x3=90; int y3=260;
for(int row=1;row<13;row++){ g.drawLine(x2,y2,x3,y3); x2=x2+40; x3=x3+20; y3=y3+30;
System.out.println();
}
int x4=510; int y4=230; int x5=530; int y5=260;
for(int row=1;row<13;row++){ g.drawLine(x4,y4,x5,y5); x4=x4-40; x5=x5-20; y5=y5+30;
System.out.println();
}
int x6=510;
⑷ 用JAVA编一个小游戏
网上找得到源代码的、我也是学手机游戏开发的、才编了一个简单的游戏、但是优化、代码规规范都不是很好,要么?
忘一起探讨。。
⑸ 用java怎么编写游戏程序
这个坦白地说,非常难,但也不是不可以,就看你有没有恒心了。
需要学习JAVA SE的基础,包括AWT、SWING的一些知识,二进制与文本IO,3D图象,可能还涉及很多运算,包括声音与视频的输出,动画或flash的输出,甚至有可能要有与windows DirectX的交互。我在外面看过有JAVA游戏编程方面的书,你可以买来看看
⑹ 用java编写一个猜拳游戏
importjava.util.Scanner;
abstractclassComputer{
abstractvoidplayGame();
}
{
publicstaticvoidmain(String[]args){
newGame().playGame();
}
staticScannersc=newScanner(System.in);
staticString[]games={"猜拳游戏"};
staticString[]roles={"刘备","孙权","曹操"};
staticfinalString[]MORA={"石头","剪刀","布"};
staticintplayCount=0;
staticStringcurrentRole="";
@Override
voidplayGame(){
doMoragame();
}
publicvoiddoMoragame(){
System.out.println("--欢迎进入游戏世界--");
System.out.println("********************");
System.out.println("**猜拳,开始**");
System.out.println("********************");
System.out.println("出拳规则:1.剪刀2.石头3.布");
StringstartMsg="请选择角色:";
for(inti=0;i<roles.length;i++){
startMsg+=(i+1)+"."+roles[i];
}
System.out.println(startMsg);
Stringinput=sc.nextLine();
currentRole=roles[selectRole(input)];
System.out.println("要开始吗?(y/n");
StringisStart=sc.nextLine();
if("y".equals(isStart)){
gameStart();
}else{
System.out.println("结束");
sc.close();
return;
}
}
intselectRole(Stringinput){
intindex=0;
switch(input){
case"1":
index=0;
break;
case"2":
index=1;
break;
case"3":
index=2;
break;
default:
break;
}
returnindex;
}
voidgameStart(){
playCount++;
System.out.println("请出拳:1.石头2.剪刀3.布");
Stringinput=sc.nextLine();
try{
intindex=Integer.valueOf(input);
System.out.println("你出拳:"+MORA[index-1]);
intcomputerIndex=(int)Math.ceil(Math.random()*3);
System.out.println("电脑出拳:"+MORA[computerIndex-1]);
System.out.println(currentRole+"VS电脑");
System.out.println("对战次数:"+playCount);
if(index==computerIndex){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}elseif(computerIndex==0&&index==1){
System.out.println("结果:你输了");
}elseif(computerIndex==1&&index==2){
System.out.println("结果:你输了");
}elseif(computerIndex==2&&index==0){
System.out.println("结果:你输了");
}else{
System.out.println("结果:你赢了");
}
}catch(Exceptione){
e.printStackTrace();
System.err.println("输入异常参数!!!");
}finally{
System.out.println("是否开始下一轮(y/n)");
StringfinalInput=sc.nextLine();
if("y".equals(finalInput)){
gameStart();
}else{
System.out.println("结束");
sc.close();
return;
}
}
}
}
⑺ 用JAVA编一个小游戏或者其他程序
import java.util.Random;
import java.util.Scanner;
public class Game {
private static int win=0;
private static int fail=0;
private static int pi=0;
private static void check(int cpu,int pe){
int t=0;
if(pe-cpu==2) t= -1;
else if(pe-cpu==-2) t= 1;
else t=pe-cpu;
if(t>0) {System.out.println("你赢了!");win++;}
else if(t==0) {System.out.println("咱们平了!");pi++;}
else {System.out.println("你输了!");fail++;}
}
public static void main(String[] args) {
String input="";
String cpuStr="";
Random rand=new Random();
int cpu=0;
int pe=0;
while(true){
System.out.println("*************************小游戏一个 输e/E可以退出*****************");
System.out.println("请选择你要出什么?F--剪刀(forfex),S--石头(stone),C--布(cloth)");
Scanner scan=new Scanner(System.in);
input=scan.nextLine();
cpu=rand.nextInt(3);
if(cpu==0)cpuStr="剪刀";
else if(cpu==1)cpuStr="石头";
else cpuStr="布";
if(input.equals("F")||input.equals("f")){
pe=0;
System.out.println("你出的是,剪刀");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("S")||input.equals("s")){
pe=1;
System.out.println("你出的是,石头");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("C")||input.equals("c")){
pe=2;
System.out.println("你出的是,布");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("E")||input.equals("e")){
System.out.println("结束游戏。。");
System.out.println("结果统计:");
System.out.println("胜:"+win+"局");
System.out.println("负:"+fail+"局");
System.out.println("平:"+pi+"局");
System.exit(0);
}
}
}
}
以上回答参考:
http://..com/question/39899654.html
⑻ 用java开发小游戏
我给你个华容道的游戏参照下吧。可以自己照着做下。多看例子想想就行了。
找个游戏做的不好,不要笑话啊。
import java.awt.*;
import javax.swing.JApplet.*;
import java.awt.event.*;
import javax.swing.*;
class People extends JButton implements FocusListener
{
Rectangle rect=null;
int left_x,left_y;//按钮左上角坐标.
int width,height; //按钮的宽和高.
String name;
int number;
public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);
setBounds(x,y,w,h);
addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(Color.GREEN);
}
}
public class HuaRongRoad extends JApplet implements KeyListener,ActionListener
{
People people[]=new People[10];
Rectangle left,right,above,below;//华容道的边界
JButton restart=new JButton("restart");
public void init()
{
getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);
people[0]=new People(0,"曹操",154,54,200,200,this);
people[1]=new People(1,"关羽",154,254,200,100,this);
people[2]=new People(2,"张飞",54,254,100,200,this);
people[3]=new People(3,"刘备",354,254,100,200,this);
people[4]=new People(4,"张辽",54,54,100,200,this);
people[5]=new People(5,"曹仁",354,54,100,200,this);
people[6]=new People(6,"兵 ",54,454,100,100,this);
people[7]=new People(7,"兵 ",354,454,100,100,this);
people[8]=new People(8,"兵 ",154,354,100,100,this);
people[9]=new People(9,"兵 ",254,354,100,100,this);
people[9].requestFocus();
people[0].setForeground(Color.white);
left=new Rectangle(49,49,5,510);
right=new Rectangle(454,49,5,510);
above=new Rectangle(49,49,410,5);
below=new Rectangle(49,554,410,5);
}
public void paint(Graphics g)
{ //华容道的边界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("单击,按方向箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public void keyPressed(KeyEvent e)
{
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100; //向下前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100; //向上前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100; //向左前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100; //向右进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
getContentPane().removeAll();
this.init();
}
}
⑼ 怎么把JAVA编写的游戏生成.exe文件啊
首先你应该知道,Java的程序必须运行在有JVM(Java虚拟机)的机器上。没有JVM的EXE程序文件也不能执行。
Java生成EXE文件其实很简单:
1)将编好的Java程序(必须含有main方法,这是程序的入口)导出成jar包,这个jar包双击就可以运行了。
2)用软件将这个jar包加个外壳,扩展名就是.exe的文件了。这样的软件有很多,比如:exe4j、JSmooth等。这两个软件的操作都很简单。
如果有什么问题可以给我发信息。
补充:用Eclipse导出jar文件过程:
右键"项目"--> Export --> Java --> JAR file --> 选择项目和输出路径(文件名) --> Finish