如何用java編寫游戲
⑴ 用java編寫的簡單游戲
牛就一個字,我只說一次
呵呵,你要真有興趣,就買本這方面書去研究研究吧,不過你這個演算法也挺新穎的,哈
⑵ 如何用java編寫小游戲
這就太多了 你需要什麼樣的游戲?
下面是個練習打字的游戲 你可以試試(導入後編碼方式選擇utf-8)
⑶ 如何用java編寫一個游戲排行榜的功能
1)把記錄的東西存到一個ArrayList 里但是退出以後再進入就沒了。
2)把記錄的東西存到一個txt文件中用特殊的方式存再用特殊的方式取
比如說存的時候用「,」隔開名字和分數,在取的時候用split方法去分割
名字和分數。
3)把名字和分數存到資料庫要的時候調用就是了(這個是最好的方法)
至於排行的話重寫compareTo方法然後用Collections.sort去排序。
⑷ 如何用JAVA做游戲
你這個問題問得太龐大了,可以先找OpenGL4Java的書來看看。
⑸ 用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編寫一個小游戲
前天寫的猜數字游戲,yongi控制猜測次數,有詳細解析,用黑窗口可以直接運行,
我試驗過了,沒問題
import javax.swing.Icon;
import javax.swing.JOptionPane;
public class CaiShuZi4JOptionPane {
/**
* @param args
*/
public static void main(String[] args) {
Icon icon = null;
boolean bl = false;
int put = 0;
int c = (int) (((Math.random())*100)+1); //獲取一個1-100的隨機數
System.out.println("你獲取的隨機數是:"+c); //列印你的隨機數字
String str1 = (String) JOptionPane.showInputDialog(null,"請輸入你的猜測數字(1-100): ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入"); //第一次輸入你的猜測數字
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次游戲"); //如果你點取消那麼本次游戲結束
}else{
bl = num(str1); //判斷是輸入的是不是數字或者是整數
if(true==bl){ //如果是數字的話進入與隨機數比較的程序
System.out.println("你輸入的數字是:"+str1); //列印你輸入的數字
put = Integer.valueOf(str1);
for(int i = 4;i > 0;i--){ //i是你可以猜測的次數
if(put==c){
JOptionPane.showMessageDialog(null, "恭喜你猜對了,正確答案是:"+c+"。"); //如果你猜對了就直接結束循環
break;
}else if(put>c){ //如果輸大了就讓你再次從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過大。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}else if(put<c){ //如果你輸小了也讓你從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過小。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}
}
}else if(bl==false){ //這個 是你第一次如果填寫的不是數字的話也會結束本次游戲
JOptionPane.showMessageDialog(null, "請您下次按要求填寫。本次游戲結束");
}
if(true==bl && c!=put){ //如果你i次都沒猜對,那麼就直接告訴你這個數十什麼
JOptionPane.showMessageDialog(null, "很遺憾你沒能猜對,這個數字是:"+c+".");
}
}
}
public static boolean num(String value){ //一個靜態方法,判斷你輸入的是不是數字
try {
Integer.parseInt(value);
return true;
} catch (Exception e) {
return false;
}
}
}
⑺ 怎麼用JAVA來寫一個小游戲程序
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("請輸入一個100以內的整數:");
for (int i=0;i<=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shu>num)
System.out.println("輸入的數大了,輸小點的!");
else if (shu<num)
System.out.println("輸入的數小了,輸大點的!");
else {
System.out.println("恭喜你,猜對了!");
if (i<=2)
System.out.println("你真是個天才!");
else if (i<=6)
System.out.println("還將就,你過關了!");
else if (i<=8)
System.out.println("但是你還……真笨!");
else
System.out.println("你和豬沒有兩樣了!"); break;}
}
} }
⑻ 用java怎麼編寫游戲程序
這個坦白地說,非常難,但也不是不可以,就看你有沒有恆心了。
需要學習JAVA SE的基礎,包括AWT、SWING的一些知識,二進制與文本IO,3D圖象,可能還涉及很多運算,包括聲音與視頻的輸出,動畫或flash的輸出,甚至有可能要有與windows DirectX的交互。我在外面看過有JAVA游戲編程方面的書,你可以買來看看
⑼ 用java小游戲編寫入門,高手指教
不懂難度就大了。。。
先學java基礎知識,學完面向對象再學一個繪圖就可以了。
然後學J2ME編程。找個模擬器,初學就用Sun的WTK就行了。
如果過去沒有編程基礎,那你的數據組織就成了問題。需要多寫程序來練習。