设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 4061|回复: 26
打印 上一主题 下一主题

[随意闲聊] 『从零开始』每天写个小游戏

[复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
43 小时
注册时间
2013-11-30
帖子
21
跳转到指定楼层
1
发表于 2013-11-30 09:17:27 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 maweiblue 于 2013-11-30 14:00 编辑

平时很喜欢研究游戏技术,也用工具做过几个小游戏,但总是感觉很有局限性,所以下定决心,开始学习编程,虽然没有什么基础,但相信天道酬勤,只要坚持下来,总会有所收获,希望通过一段时间的努力,能做出自己心仪的游戏,在此发帖也是希望给自己个动力,能坚持下去。

语言我选择JAVA,跨平台,入门比较简单,做PC,安卓小游戏都十分适合。

由于没有编程基础,代码写的较挫,请前辈们原谅,能给出意见建议一定虚心接受。

game no.1(11.30)

JAVA 代码复制
  1. import java.util.Scanner;
  2. public class MyTest {
  3.         /**
  4.          * 猜100以内数字的小游戏,最后给出猜的次数,
  5.          * @param args
  6.          */
  7.         public static void main(String[] args){
  8.                 //生成一个100的随机数字
  9.                 int myrandom=(int)(100*Math.random());
  10.                 //生成一个Scanner对象
  11.                 Scanner myscanner=new Scanner(System.in);
  12.                 System.out.print("开始输入你的数字");
  13.                 //开始接受输入一个数字
  14.                 int myint=myscanner.nextInt();
  15.                 //这个用来记录猜的次数
  16.                 byte testnum=0;
  17.                 //开始一个循环,如果猜的数字和生成的随机数不同,就提示大小;
  18.                 while(myint!=myrandom){
  19.                 if(myint>myrandom){
  20.                         System.out.println("你猜的数字大了,再来一次吧");
  21.                         }else{
  22.                         System.out.println("你猜的数字小了,再来一次吧");
  23.                         }
  24.                          myint=myscanner.nextInt();
  25.                         testnum++;
  26.                 }
  27.                 //成功就跳出,并且给出
  28.                 System.out.print("完美成功了哈哈,你一共试了"+testnum+"次");
  29.         }
  30. }

运行状态




game no 2:(11.30)

刚刚学了类和对象,趁着还没忘,先写个非常传统的RPG游戏,复刻勇者斗恶龙
新建一个Creative类
JAVA 代码复制
  1. package com.linuxgame.test;
  2. /**
  3.  * 创造物父类,定义一些基本的属性方法
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class Creative {
  8.         //创造物的名字
  9.         protected String name;
  10.         //定义HP
  11.         protected int hp;
  12.         //定义攻击力strenth
  13.         protected int str;
  14.         //定义幸运值luck
  15.         protected int luk;
  16.         //构造函数
  17.         public Creative(String name,int hp,int str,int luk){
  18.                 this.name=name;
  19.                 this.hp=hp;
  20.                 this.str=str;
  21.                 this.luk=luk;
  22.                 System.out.println(this.name+"出现在地图上");
  23.  
  24.         }
  25.         //作为一只合格的怪物,最要紧的是要会攻击!不会攻击的怪物不是好怪物!
  26.         //作为一只风骚的英雄,最要紧的是要会攻击!不会开车的裁缝不是好厨子!
  27.         public void attack(Creative obj){
  28.                         //产生一个攻击伤害,由力量和幸运共同产生;
  29.                         int attacknum=this.str+(int)(this.luk*Math.random());
  30.                         obj.attacked(attacknum);
  31.                 }
  32.         //这个函数表示被抽时候扣掉HP
  33.         public void attacked(int attacknum){
  34.                 this.hp-=attacknum;
  35.  
  36.         }
  37.         //这个是传说中的血条
  38.         public int getHp(){
  39.                 return this.hp;
  40.         }
  41.         //总的说些什么吧
  42.         public void say(String hehe){
  43.                 System.out.print(hehe);
  44.  
  45.         }
  46.  
  47. }


然后再定义一个英雄类啦
JAVA 代码复制
  1. package com.linuxgame.test;
  2. /**
  3.  * 这个类当然是定义了一个英雄类了哈哈哈哈哈
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class Hero extends Creative{
  8.         //作为一个英雄,当然需要一个有主角光环
  9.         public Hero(String name,int hp,int str,int luk){
  10.                 super(name,hp,str,luk);
  11.                 System.out.println("**我是主角光环(made in china)***");
  12.         }
  13.  
  14. }


当然怪物类也是非常必要的。

JAVA 代码复制
  1. package com.linuxgame.test;
  2. /**这个类定义小怪物
  3.  *
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class Monster extends Creative{
  8.         //小怪物出场总要有个背景音乐的吧,来个冷艳风格的
  9.         public Monster(String name,int hp,int str,int luk){
  10.                         super(name,hp,str,luk);
  11.                         System.out.println("*******我是冷艳的背景音乐*******");
  12.  
  13.         }
  14.  
  15. }



最后,生成世界地图,开始大决战了。

JAVA 代码复制
  1. package com.linuxgame.test;
  2. /**
  3.  * 这里是世界地图,英雄和美女,恶龙和兔子,基佬和腐女的传说在这里流传
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class Test {
  8.  
  9.         public static void main(String[] args) {
  10.                 //背景故事
  11.                 System.out.println("很久很久以前,天朝是一个普大喜奔的美丽地方");
  12.                 //生成怪物
  13.                 Monster mon1=new Monster("灰太狼",1000,10,5);
  14.                 //出场台词
  15.                 mon1.say("愚蠢的人类啊!喵");
  16.                 //英雄出场
  17.                 Hero hero1=new Hero("毛小明",98,8,10);
  18.                 //英雄要霸气的台词
  19.                 hero1.say("做了英雄,妈妈再也不用担心我的学习");
  20.                 System.out.println("剧情省略,玩家自己脑补,终于到了决定命运的时刻了");
  21.                 System.out.println("******************************");
  22.                 //战斗太激烈了,完全是不死不休啊
  23.                 while((mon1.getHp()>0)&&(hero1.getHp()>0)){
  24.                         //怪物打英雄
  25.                         mon1.attack(hero1);
  26.                         System.out.println("怪物君打了英雄君");
  27.                         //英雄打怪物
  28.                         if(hero1.getHp()>0){
  29.                                 hero1.attack(mon1);
  30.                                 System.out.println("英雄君打了怪物君");
  31.                         }else{
  32.                                 hero1.say("我的生涯一片无悔,我想起那天夕阳下的奔跑,那是我逝去的青春");
  33.                                 break;
  34.                         }
  35.                         if(mon1.getHp()<=0){
  36.                                 mon1.say("我的生涯一片无悔,我想起那天夕阳下的奔跑,那是我逝去的青春");
  37.                         }
  38.  
  39.                 }
  40.  
  41.  
  42.         }
  43.  
  44. }


运行情况。

评分

参与人数 1星屑 +12 收起 理由
火烧兔子 + 12 塞糖(加油

查看全部评分

Lv2.观梦者

Adam

梦石
0
星屑
703
在线时间
841 小时
注册时间
2010-8-24
帖子
2595
27
发表于 2013-12-3 12:42:29 | 只看该作者
脚本触触们等做到了一定程度记得打包发给我们这些广大渣渣群众w
嘛,摸了。
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
49
在线时间
376 小时
注册时间
2013-2-14
帖子
203
26
发表于 2013-12-3 12:37:15 | 只看该作者
本帖最后由 awedcvgyujm 于 2013-12-3 12:43 编辑

以前用java写过一个游戏
结果控场技能不够没法驾驭这么大的软件最后坑掉了_(:з」∠)_



但还是不得不说java真的很方便
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
43 小时
注册时间
2013-11-30
帖子
21
25
 楼主| 发表于 2013-12-3 10:37:27 | 只看该作者
鉴于前面的代码都是文字太过枯燥,打算学些GUI的东西,创建一个窗口,算是GUI的第一课吧。
JAVA 代码复制
  1. package net.peise.swing;
  2. import javax.swing.*;
  3.  
  4. public class MyPanel {
  5.         public static void main(String[] args){
  6.                 //生成一个Frame;
  7.                 MyFrame myframe1=new MyFrame("我的世界");
  8.                 javax.swing.JLabel myLable=new javax.swing.JLabel();
  9.                 myLable.setBounds(10, 20, 100, 60);
  10.                 javax.swing.JButton myButton=new javax.swing.JButton();
  11.                 myButton.setBounds(123, 20, 100, 60);
  12.                 myButton.setText("确定发送");
  13.                 myLable.setText("标题(Lable)");
  14.                 javax.swing.JTextField myText=new javax.swing.JTextField();
  15.                 myText.setBounds(20,20,100,60);
  16.                 myText.setText("这里是聊天窗口");
  17.                 myframe1.add(myButton);
  18.                 myframe1.add(myLable);
  19.                 myframe1.add(myText);
  20.                 myframe1.setVisible(true);
  21.  
  22.         }
  23.  
  24. }
  25.  
  26. /**
  27.  * 这个类创建一个窗口
  28.  * @author Administrator
  29.  *
  30.  */
  31. class MyFrame extends JFrame{
  32.         /**
  33.          *
  34.          */
  35.         private static final long serialVersionUID = 1L;
  36.  
  37.         public MyFrame(String name){
  38.                 super();
  39.                 this.setSize(800, 600);
  40.                 this.setTitle(name);
  41.         }
  42.  
  43. }


效果。。
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
197 小时
注册时间
2011-6-10
帖子
231
24
发表于 2013-12-3 02:12:16 | 只看该作者
本帖最后由 艾拉梅德 于 2013-12-2 19:15 编辑

前几天才做完的c++黑白棋游戏,老师要求写3种文件……嗯,显示语言是法语

第一个hpp
  1. #include "header.h"
  2. #include <iostream>
  3. #include <string>

  4. using namespace std;

  5. void Plateau::affiche() const
  6. {
  7.     int i, j;
  8.     cout <<endl;
  9.     cout << "  0 1 2 3 4 5 6 7 "<<endl;
  10.     cout << " ================ "<<endl;
  11.     for (i=0;i<MAX;i++){
  12.         for (j=0; j<MAX;j++){
  13.                 if (j==0)
  14.                     cout <<i <<" " <<p_plateau[i][j];
  15.                 else if (j==MAX-1)
  16.                     cout << " " <<p_plateau[i][j]<< " "<< i;
  17.                 else
  18.                     cout << " "<<p_plateau[i][j];}
  19.             cout << endl;}

  20.     cout << " ================ "<<endl;
  21.     cout << "  0 1 2 3 4 5 6 7"<<endl;
  22.     cout <<endl;
  23. }


  24. int Plateau::reste()
  25. {
  26.     int n=0;
  27.     for (int i=0;i<MAX;i++)
  28.     {
  29.         for (int j=0;j<MAX;j++)
  30.         {
  31.             if (p_plateau[i][j]==V)
  32.                 n++;
  33.         }
  34.     }
  35.     return n;
  36. }

  37. void Plateau::pion_trans(const int ligne, const int colone, const int tour)
  38. {
  39.     if (tour==0)
  40.         p_plateau[ligne][colone]=B;
  41.     else
  42.         p_plateau[ligne][colone]=N;
  43. }

  44. int Plateau::calcul_score(const char nom)
  45. {
  46.     int n=0;
  47.     for (int i=0; i<MAX; i++)
  48.     {
  49.         for (int j=0; j < MAX; j++)
  50.         {
  51.             if (p_plateau[i][j]==nom)
  52.                 n++;
  53.         }
  54.     }
  55.     return n;
  56. }

  57. void Joueur::saisirnom(const int i)
  58. {
  59.     string nom;
  60.     cout << "Quel est le nom de joueur"<< i << "?"<<endl;
  61.     cin >> nom;
  62.     j_nom=trans_nom(nom);
  63. }

  64. string Joueur::trans_nom(string nom)
  65. {
  66.     for (int i=1;i<nom.length();i++)
  67.     nom[i]=tolower(nom[i]);
  68.     nom[0]=toupper(nom[0]);
  69.     return nom;
  70. }

  71. int Joueur::change_score(const int n)
  72. {
  73.     j_score=n;
  74.     return j_score;
  75. }

  76. void Jeu::commence()
  77. {
  78.     j1.saisirnom(1);
  79.     j2.saisirnom(2);
  80.     joueurCourant=0;
  81.     cout << "Donc le blanc commence. C'est le tour de "<< j1.getnom() <<endl;
  82.     cout << B<< ":pour le pion blanc"<< "   "<< N<< ":pour le pion noir"<<endl;
  83.     p.affiche();
  84.     run();
  85. }

  86. void Jeu::saisir(const int tour)
  87. {
  88.     int ligne, colone, n;
  89.     cout << "Quel ligne vous voulez placer votre pion? ligne(0-7)"<<endl;
  90.     cin>> ligne;
  91.     while (ligne < 0 || ligne > 7)
  92.     {
  93.         cout << "Erreur, resaisir"<<endl;
  94.         cin>> ligne;
  95.     }
  96.     cout << "Quel colone vous voulez placer votre pion? colone(0-7)"<<endl;
  97.     cin>> colone;
  98.     while (colone < 0 || colone > 7)
  99.     {
  100.         cout << "Erreur, resaisir"<<endl;
  101.         cin>> colone;
  102.     }
  103.     if (!p.determiner_vide(ligne,colone))//si ce n'est pas vide
  104.     {
  105.         cout << "Ici il y a deja un pion"<<endl;
  106.         saisir(tour);
  107.     }
  108.     else{
  109.             n= p.determiner(ligne, colone, tour);
  110.         if (n==0)
  111.         {
  112.             cout << "Ce n'est pas une place valide"<<endl;
  113.             p.affiche();
  114.             saisir(tour);

  115.         }
  116.         else
  117.         {
  118.             cout << "C'est une place valide"<<endl;
  119.             score();
  120.             p.affiche();
  121.         }

  122.     }
  123. }

  124. void Jeu::run()
  125. {
  126.     while (p.reste() != 0)
  127.     {
  128.         if (p.skip(joueurCourant))//s'il n'y plus de place, saute
  129.         {
  130.             cout << "Car il n'y pas de place, donc maintenant c'est le tour de joueur "<< joueurCourant+2<<endl;
  131.             joueurCourant=(joueurCourant+1)%2;
  132.         }
  133.         if (joueurCourant==0)
  134.             cout << j1.getnom()<<", placez votre pion."<<endl;
  135.         else
  136.             cout << j2.getnom()<<", placez votre pion."<<endl;
  137.         saisir(joueurCourant);
  138.         joueurCourant=(joueurCourant+1)%2;
  139.     }
  140.     fin();
  141. }

  142. bool Plateau::skip(const int tour)
  143. {
  144.     int cpt=0, i, j;
  145.     char type;
  146.     if (tour==0)
  147.         type=B;
  148.     else
  149.         type=N;
  150.     for (i=0;i<MAX;i++)
  151.     {
  152.         for(j=0;j<MAX;j++)
  153.         {
  154.             if (p_plateau[i][j]==V)
  155.             {
  156.                 cpt+=determiner(i, j, tour, false);
  157.             }
  158.         }
  159.     }
  160.     return (cpt==0);
  161. }

  162. bool Plateau::determiner_vide(const int ligne, const int colone)
  163. {
  164.     return (p_plateau[ligne][colone]==V);
  165. }

  166. int Plateau::determiner(const int i, const int j, const int tour, bool skip)
  167. {
  168.     int ligne=i,colone=j , n=0, cpt;
  169.     char type;
  170.     if (tour==0)
  171.         type=B;
  172.     else
  173.         type=N;
  174.     //haut
  175.     if (i != 0){
  176.         while (ligne >= 0 && (p_plateau[ligne-1][colone]!=type && p_plateau[ligne-1][colone]!=V))
  177.         {
  178.             ligne--;}
  179.         if (p_plateau[ligne-1][colone]==type)
  180.         {
  181.             n+=(i-ligne);
  182.             if (skip != false){
  183.             for (cpt=i-1;cpt>ligne-1;cpt--)
  184.             {
  185.                 pion_trans(cpt, colone, tour);
  186.             }}
  187.         }
  188.     }
  189.     ligne=i,colone=j;
  190.     //haut droit
  191.     if (i != 0 && j != MAX-1){
  192.         while ((ligne >= 0 && colone < MAX) && (p_plateau[ligne-1][colone+1]!=type && p_plateau[ligne-1][colone+1]!=V))
  193.         {
  194.             ligne--;
  195.             colone++;
  196.         }
  197.             if (p_plateau[ligne-1][colone+1]==type)
  198.             {
  199.                 n+=(i-ligne);
  200.                 if (skip != false){
  201.                 ligne=i,colone=j;
  202.                 while ((ligne >= 0 && colone < MAX) && (p_plateau[ligne-1][colone+1]!=type && p_plateau[ligne-1][colone+1]!=V))
  203.                 {
  204.                     ligne--;
  205.                     colone++;
  206.                     pion_trans(ligne, colone, tour);
  207.                 }}
  208.         }}
  209.     ligne=i,colone=j;
  210.     //droit
  211.     if (j != MAX-1){
  212.     while (colone < MAX && (p_plateau[ligne][colone+1]!=type && p_plateau[ligne][colone+1]!=V))
  213.     {

  214.         colone++;}
  215.         if (p_plateau[ligne][colone+1]==type)
  216.         {
  217.             n+=(colone-j);
  218.             if (skip != false){
  219.             for (cpt=j+1;cpt<colone+1;cpt++)
  220.             {
  221.                 pion_trans(ligne, cpt, tour);
  222.             }}
  223.         }
  224.     }
  225.     ligne=i,colone=j;
  226.     //bas droit
  227.     if (i != MAX-1 && j != MAX-1){
  228.     while ((ligne < MAX && colone < MAX) && (p_plateau[ligne+1][colone+1]!=type && p_plateau[ligne+1][colone+1]!=V))
  229.     {
  230.         ligne++;
  231.         colone++;
  232.     }
  233.         if (p_plateau[ligne+1][colone+1]==type)
  234.             {
  235.                 n+=(ligne-i);
  236.             if (skip != false){
  237.                 ligne=i,colone=j;
  238.             while ((ligne < MAX && colone < MAX) && (p_plateau[ligne+1][colone+1]!=type && p_plateau[ligne+1][colone+1]!=V))
  239.             {
  240.                 ligne++;
  241.                 colone++;
  242.                 pion_trans(ligne,colone,tour);
  243.             }}
  244.     }}
  245.     //bas
  246.     ligne=i,colone=j;
  247.     if (i != MAX-1){
  248.     while (ligne < MAX && (p_plateau[ligne+1][colone]!=type && p_plateau[ligne+1][colone]!=V))
  249.     {
  250.         ligne++;}
  251.         if (p_plateau[ligne+1][colone]==type)
  252.         {
  253.             n+=(ligne-i);
  254.             if (skip != false){
  255.             for (cpt=i+1;cpt<ligne+1;cpt++)
  256.             {
  257.                 pion_trans(cpt, colone, tour);
  258.             }
  259.         }}
  260.     }
  261.     //bas gauche
  262.     ligne=i,colone=j;
  263.     if (i != MAX-1 && j != 0){
  264.     while ((ligne < MAX && colone >=0) && (p_plateau[ligne+1][colone-1]!=type && p_plateau[ligne+1][colone-1]!=V))
  265.     {
  266.         ligne++;
  267.         colone--;
  268.     }
  269.         if (p_plateau[ligne+1][colone-1]==type)
  270.             {
  271.                 n+=(ligne-i);
  272.                 if (skip != false){
  273.                 ligne=i,colone=j;
  274.             while ((ligne < MAX && colone >=0) && (p_plateau[ligne+1][colone-1]!=type && p_plateau[ligne+1][colone-1]!=V))
  275.             {
  276.                 ligne++;
  277.                 colone--;
  278.                 pion_trans(ligne,colone,tour);
  279.             }}
  280.     }}
  281.     //gauche
  282.     ligne=i,colone=j;
  283.     if (i != 0){
  284.     while (ligne > 0 && (p_plateau[ligne][colone-1]!=type && p_plateau[ligne][colone-1]!=V))
  285.     {
  286.         colone--;}
  287.         if (p_plateau[ligne][colone-1]==type)
  288.         {
  289.             n+=(j-colone);
  290.             if (skip != false){
  291.             for (cpt=j-1;cpt>colone-1;cpt--)
  292.             {
  293.                 pion_trans(ligne, cpt, tour);
  294.             }
  295.         }}
  296.     }
  297.     //haut gauche
  298.     ligne=i,colone=j;
  299.     if (i != 0 && j != 0){
  300.     while ((ligne >=0 && colone >=0) && (p_plateau[ligne-1][colone-1]!=type && p_plateau[ligne-1][colone-1]!=V))
  301.     {
  302.         ligne--;
  303.         colone--;
  304.     }
  305.         if (p_plateau[ligne-1][colone-1]==type)
  306.             {
  307.                 n+=(i-ligne);
  308.                 ligne=i,colone=j;
  309.                 if (skip != false){
  310.                 while ((ligne >=0 && colone >=0) && (p_plateau[ligne-1][colone-1]!=type && p_plateau[ligne-1][colone-1]!=V))
  311.                 {
  312.                     ligne--;
  313.                     colone--;
  314.                     pion_trans(ligne,colone,tour);
  315.                 }}
  316.     }}
  317.     if (n > 0 && skip!=false)
  318.         p_plateau[i][j]=type;
  319.     return n;
  320. }

  321. void Jeu::score()
  322. {
  323.     int b=p.calcul_score(B);
  324.     int n=p.calcul_score(N);
  325.     cout << "Le score de joueur 1 "<< j1.getnom()<<" est :" << j1.change_score(b) << endl;
  326.     cout << "Le score de joueur 2 "<< j2.getnom()<<" est :" << j2.change_score(n) << endl;
  327. }

  328. void Jeu::fin()
  329. {
  330.     if (j1.getscore() > j2.getscore())
  331.         cout << "C'est le joueur " << j1.getnom() << " qui a gagne!"<<endl;
  332.     else if (j2.getscore() > j1.getscore())
  333.         cout << "C'est le joueur " << j2.getnom() << " qui a gagne!"<<endl;
  334.     else
  335.         cout << ""<<endl;
  336. }
复制代码
然后主程序
  1. #include <iostream>
  2. #include "header.h"

  3. using namespace std;

  4. int main()
  5. {
  6.     char recommence = 'n';
  7.     cout << "Bienvenue! C'est un jeu d'Othello!" << endl;
  8.     Jeu j;
  9.     do{
  10.     j.commence();
  11.     cout << "Est-ce que vous-voulez recommencer?(o/n)"<<endl;
  12.     cin>>recommence;
  13.     }while (recommence=='o');
  14.     return 0;
  15. }
复制代码
然后最后的.h
  1. #ifndef HEADER_H_INCLUDED
  2. #define HEADER_H_INCLUDED

  3. #include <string>
  4. #include <iostream>
  5. using namespace std;

  6. const char B='o';
  7. const char N='*';
  8. const char V='-'; // pour repr¨¦senter une case vide
  9. const int MAX=8;


  10. class Plateau
  11. {
  12. public:
  13.     Plateau(){}
  14.     void affiche() const;
  15.     int reste();
  16.     bool skip(const int);
  17.     bool determiner_vide(const int, const int);
  18.     void pion_trans(const int, const int, const int);
  19.     int calcul_score(const char);
  20.     int determiner(const int, const int, const int, bool=true);
  21. private:
  22.     char p_plateau[MAX][MAX]={
  23.             {V,V,V,V,V,V,V,V},
  24.             {V,V,V,V,V,V,V,V},
  25.             {V,V,V,V,V,V,V,V},
  26.             {V,V,V,B,N,V,V,V},
  27.             {V,V,V,N,B,V,V,V},
  28.             {V,V,V,V,V,V,V,V},
  29.             {V,V,V,V,V,V,V,V},
  30.             {V,V,V,V,V,V,V,V}};

  31. };

  32. class Joueur
  33. {
  34. public:
  35.     Joueur(int const coul=1, string const nom="Joueur", int const score=0):j_coul(coul), j_nom(nom), j_score(score){}
  36.     void saisirnom(const int);
  37.     string trans_nom(string nom);
  38.     string getnom(){return j_nom;}
  39.     int change_score(const int);
  40.     int getscore(){return j_score;}
  41. private:
  42.     string j_nom ; // le nom du joueur
  43.     int j_coul ; // caract¨¨re repr¨¦sentant le joueur : 1 ou 2
  44.     int j_score ; // le score du joueur pour la partie en cours

  45. };

  46. class Jeu
  47. {
  48. public:
  49.     void commence();
  50.     void saisir(const int);
  51.     void run();
  52.     void score();
  53.     void fin();
  54. private:
  55.     Plateau p; // le plateau de jeu
  56.     Joueur j1, j2; // les 2 joueurs
  57.     int joueurCourant ; //0 si joueur 1, 1 sinon
  58. };
  59. #endif // HEADER_H_INCLUDED
复制代码
大概就这样,这是两个玩家版本,之后还要写电脑AI版本的……
特点:懒
特性:懒
爱好:潜水

《巴雅前奏曲》目前已完成,正在挖学美工……
新坑:目前正制作中0 0
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
43 小时
注册时间
2013-11-30
帖子
21
23
 楼主| 发表于 2013-12-2 13:47:06 | 只看该作者
论坛好卡。连发了,抱歉。。
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
43 小时
注册时间
2013-11-30
帖子
21
22
 楼主| 发表于 2013-12-2 13:42:37 | 只看该作者
昨天学了Interface,做个道具类的控制吧,非常简单。
1.接口ITEM
JAVA 代码复制
  1. package com.linuxgame.test;
  2. /**
  3.  * 这个接口定义了物品的一些基本方法,只是一些声明
  4.  * @author Administrator
  5.  *
  6.  */
  7. public interface Item {
  8.         public String getName();
  9.         public void setName(String name);
  10.         void drop();
  11.         void getItem();
  12.  
  13. }


2用一个武器来去实现ITEM
JAVA 代码复制
  1. package com.linuxgame.test;
  2. /**
  3.  * 这个类实现了ITEM接口,功能很少,多了我也不会做,先从简单的开始吧。
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class Weapon implements Item{
  8.         private int id;
  9.         private String itemName;
  10.         public void setName(String name){
  11.                 this.itemName=name;
  12.         }
  13.         public String getName(){
  14.                 return this.itemName;
  15.         }
  16.         public void drop(){
  17.  
  18.                 System.out.println("武器"+this.itemName+"被丢掉了");
  19.         }
  20.         public Weapon(String name){
  21.                 this.itemName=name;
  22.                 System.out.println("系统生成武器"+this.itemName);
  23.         }
  24.         public void getItem(){
  25.                 System.out.println("英雄得到武器"+this.itemName);
  26.  
  27.         }
  28.  
  29. }


3,开始测试。

JAVA 代码复制
  1. package com.linuxgame.test;
  2.  
  3. public class Game03 {
  4.          public static void main(String[] args){
  5.                 //用一个数组来表示生成的ITEM吧,一个游戏中道具可能有几百几千,用数组可能不合适
  6.                 Item[] myitem=new Item[3];
  7.                 myitem[0]=new Weapon("屠龙刀");
  8.                 myitem[1]=new Weapon("杀猪刀");
  9.                 myitem[2]=null;
  10.                 System.out.println("********************");
  11.                 //打怪得到物品
  12.                 myitem[0].getItem();
  13.                 myitem[1].getItem();
  14.  
  15.                 System.out.println("************");
  16.                 myitem[0].drop();
  17.          }
  18. }


运行效果

系统生成武器屠龙刀
系统生成武器杀猪刀
********************
英雄得到武器屠龙刀
英雄得到武器杀猪刀
************
武器屠龙刀被丢掉了

回复 支持 反对

使用道具 举报

Lv1.梦旅人

巫女会长

梦石
0
星屑
60
在线时间
1028 小时
注册时间
2009-10-24
帖子
3470

贵宾

21
发表于 2013-12-2 13:40:59 | 只看该作者
maweiblue 发表于 2013-12-2 13:10
java的确不容易啊,很少PC游戏是用JAVA写的,当作学习算了,写写小游戏应该可以吧。 ...


这个跟语言没太大关系,java只是安全性较高而已,主要还是跟开发环境(引擎)和游戏类型有关,比如flash里用的就是javascript插件。

点评

这样。。不过java做的游戏还是有很多的,虽然网络应用比较有优势  发表于 2013-12-5 00:04
java 和 javascript 完全是2个东西 就名字像点而已  发表于 2013-12-4 20:16
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
43 小时
注册时间
2013-11-30
帖子
21
20
 楼主| 发表于 2013-12-2 13:12:07 | 只看该作者
钢炼君2 发表于 2013-11-30 21:23
LZ实在太给力了太感动了,请你研究,变得牛逼之后,做我的脚本猿吧,汗多阿里嘎多 ...

好吧,约定了哦。
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
43 小时
注册时间
2013-11-30
帖子
21
19
 楼主| 发表于 2013-12-2 13:10:57 | 只看该作者
凝望·流年 发表于 2013-11-30 20:59
之前一直以为JAVA写个游戏不容易,但仔细想想功能比RUBY好

java的确不容易啊,很少PC游戏是用JAVA写的,当作学习算了,写写小游戏应该可以吧。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-9-30 23:33

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表