當前位置:首頁 » 編程語言 » java編寫的游戲

java編寫的游戲

發布時間: 2022-03-12 13:57:06

⑴ 用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

熱點內容
win7跨網段訪問共享 發布:2025-07-21 02:22:22 瀏覽:66
如何在遠程伺服器寫代碼 發布:2025-07-21 02:17:21 瀏覽:171
伺服器IP透傳 發布:2025-07-21 01:57:10 瀏覽:212
訪問停靠英語 發布:2025-07-21 01:56:22 瀏覽:1000
路由器用戶名密碼是什麼 發布:2025-07-21 01:51:28 瀏覽:484
plsql中文 發布:2025-07-21 01:48:46 瀏覽:246
accesssql創建表 發布:2025-07-21 01:46:33 瀏覽:545
我的世界在伺服器顯示BOSS 發布:2025-07-21 01:39:56 瀏覽:977
廣醫資料庫 發布:2025-07-21 01:33:12 瀏覽:523
自用伺服器怎麼搭公網ip 發布:2025-07-21 01:30:51 瀏覽:342