java简单游戏代码,用Java编写简单游戏详细教程
很多朋友对于java简单游戏代码和用Java编写简单游戏详细教程不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
求一个简单又有趣的JAVA小游戏代码
具体如下:
连连看的小源码
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame;//主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel;//子面板
JButton diamondsButton[][]= new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton;//退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0");//分数标签
JButton firstButton,secondButton;//
分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮
int grid[][]= new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false;//判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。
对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20代表一个空格,而 0x74代表字符“t”)。一些数据类型每个字符使用一个字节;每个字节可以具有 256个不同的位模式中的一个模式。
在计算机中,字符由不同的位模式(ON或 OFF)表示。每个字节有 8位,这 8位可以有 256种不同的 ON和 OFF组合模式。对于使用 1个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256个不同的字符。2个字节有 16位,这 16位可以有 65,536种唯一的 ON和 OFF组合模式。使用 2个字节表示每个字符的程序可表示最多 65,536个字符。
单字节代码页是字符定义,这些字符映射到每个字节可能有的 256种位模式中的每一种。代码页定义大小写字符、数字、符号以及!、@、#、%等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。
虽然用于表示 A到 Z拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。
如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。
如何学习JAVA,并求个简单JAVA游戏代码
学习Java可从下面做起:
1.继续深入理解面向对象的含义。
2.java封装,继承和访问机制,事件。辅助书籍:《java编程思想》,《java模式》。
3.jsp的基本应用,javascript的基本应用。辅助书籍:《Tomcat与Java Web开发技术详解》
4.strust.辅助书籍:孙卫琴编著《精通Struts:基于MVC的Java Web设计与开发》
5.spring.辅助书籍:《spring in action》
6.hibernate.辅助书籍:《深入浅出Hibernate》、《精通Hibernate:Java对象持久化技术详解》
7.j2ee辅助书籍:《J2EE企业级应用开发》(电子工业出版社)
8.EJB辅助书籍:《精通EJB(第三版)》
9.以上书籍可下电子书,但是电子的看久了蛮累,眼睛受不了。
10.补充:ORACLE一定要学好(SQL语句,存储过程,触发器)给你个简单连连看代码:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class连连看 implements ActionListener{
JFrame mainFrame;//主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel;//子面板
JButton diamondsButton[][]= new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton;//退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0");//分数标签
JButton firstButton,secondButton;//分别记录两次被选中的按钮
int grid[][]= new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false;//判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer= mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols= 0;cols< 6;cols++){
for(int rows= 0;rows< 5;rows++){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild(){
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++){
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike<=2;alike++){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload(){
int save[]= new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++){
for(int j=0;j<=5;j++){
if(this.grid[i][j]!=0){
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false;//这里一定要将按钮点击信息归为初始
init();
for(int i= 0;i< 6;i++){
for(int j= 0;j< 5;j++){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
求一个简单RPG游戏的代码,JAva编写的
packagecom.lxi;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclassRpg{
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)throwsException{
System.out.println("在这里输入两个人物进行PK,以英文逗号分隔:[BM,DH,MK]");
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
Class<Person>c1;
Class<Person>c2;
try{
Stringtemp=br.readLine();
String[]str=temp.split(",");
if(str.length!=2){
thrownewException("输入格式有误,按默认PK");
}
c1=(Class<Person>)Class.forName("com.lxi."
+str[0].toUpperCase());
c2=(Class<Person>)Class.forName("com.lxi."
+str[1].toUpperCase());
}catch(Exceptione){
//TODOAuto-generatedcatchblock
c1=(Class<Person>)Class.forName("com.lxi.BM");
c2=(Class<Person>)Class.forName("com.lxi.DH");
}
try{
Personp1=c1.newInstance();
Personp2=c2.newInstance();
longtime=System.currentTimeMillis();
longnextTime1=(long)(time+p1.coldTime*1000);//
longnextTime2=(long)(time+p2.coldTime*1000);//发动攻击的时间
System.out.println("---游戏开始---");
while(true){
longcurrenTime=System.currentTimeMillis();
if(nextTime1<currenTime){//时间到则发动攻击
p1.hit(p2);
nextTime1+=p1.coldTime*1000+p1.waitTime*1000;//下次攻击时间=冷却时间+被晕眩时间
p1.waitTime=0;//回合结束,重置被晕眩时间为0
}
if(nextTime2<currenTime){
p2.hit(p1);
nextTime2+=p2.coldTime*1000+p2.waitTime*1000;
p2.waitTime=0;
}
}
}catch(ClassCastExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(InstantiationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
}
}
packagecom.lxi;
importjava.util.Random;
classBMextendsPerson{
publicBM(){
val=650;
coldTime=1.5;
fight=40;
chanceHit=3;
chanceDefense=3;
waitTime=0;
}
intcount=0;//防御技能发动的次数
inttemp=40;//攻击力,值同fight
booleanhitFlag=false;
booleandefenseFlag=false;
Randomrand=newRandom();
publicvoidhit(Personp){
if(rand.nextInt(10)<chanceHit){
fight=fight*2;//发动双倍攻击
hitFlag=true;
}
inthurt=p.defense(this);
p.val=p.val-hurt;
fight=temp;//还原为单倍攻击
if(p.val<=0){
System.out.println(this.getClass().getSimpleName()+"胜出!");
System.exit(0);
}
System.out.println(this.getClass().getSimpleName()+"攻击"
+p.getClass().getSimpleName()+","
+this.getClass().getSimpleName()
+(this.hitFlag?"发动攻击技能":"未发动攻击技能")
+p.getClass().getSimpleName()
+(this.defenseFlag?"发动防御技能":"未发动防御技能")
+this.getClass().getSimpleName()+":"+this.val+","
+p.getClass().getSimpleName()+":"+p.val);
hitFlag=false;
defenseFlag=false;
}
publicintdefense(Personp){
if(rand.nextInt(10)<chanceDefense){
if(count!=0){
p.val=p.val-p.fight;
count++;
defenseFlag=true;
if(p.val<=0){
System.out.println(this.getClass().getSimpleName()+"胜出!");
System.exit(0);
}
}
}
returnp.fight;
}
}
classMKextendsPerson{
publicMK(){
val=700;
coldTime=2.5;
fight=50;
chanceDefense=6;
chanceHit=3;
waitTime=0;
}
booleanhitFlag=false;
booleandefenseFlag=false;
Randomrand=newRandom();
publicvoidhit(Personp){
if(rand.nextInt(10)<chanceHit){
p.waitTime=3;//使对方晕眩3s
hitFlag=true;
}
inthurt=p.defense(this);
p.val=p.val-hurt;
if(p.val<=0){
System.out.println(this.getClass().getSimpleName()+"胜出!");
System.exit(0);
}
System.out.println(this.getClass().getSimpleName()+"攻击"
+p.getClass().getSimpleName()+","
+this.getClass().getSimpleName()
+(this.hitFlag?"发动攻击技能":"未发动攻击技能")
+p.getClass().getSimpleName()
+(this.defenseFlag?"发动防御技能":"未发动防御技能")
+this.getClass().getSimpleName()+":"+this.val+","
+p.getClass().getSimpleName()+":"+p.val);
hitFlag=false;
defenseFlag=false;
}
publicintdefense(Personp){
if(rand.nextInt(10)<chanceDefense){
defenseFlag=true;
returnp.fight/2;//防御技能发动,伤害减半
}
returnp.fight;
}
}
packagecom.lxi;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.util.Random;
//三个人物的基类
abstractclassPerson{
intval;//生命值
doublecoldTime;//冷却时间
intwaitTime;//晕眩时间
intfight;//攻击力
intchanceHit;//发起主动技能的概率
intchanceDefense;//发起防御技能的概率
abstractvoidhit(Personp);//攻击技能
abstractintdefense(Personp);//防御技能,返回被伤害点数
}
classDHextendsPerson{
publicDH(){
val=600;
coldTime=1.0;
fight=30;
chanceHit=3;//表示30%的概率
chanceDefense=3;
waitTime=0;
}
Randomrand=newRandom();
booleanhitFlag=false;//主动技能发动的标识
booleandefenseFlag=false;//防御技能发动的标识
publicvoidhit(Personp){
if(rand.nextInt(10)<chanceHit){//发动主动技能
inthurt=p.defense(this);
p.val=p.val-hurt;
if(p.val<=0){
System.out.println(this.getClass().getSimpleName()+"胜出!");
System.exit(0);
}
val=val+hurt;
if(val>600)
val=600;
hitFlag=true;//标记主动技能已经发动
}else{//进行普通攻击
inthurt=p.defense(this);
p.val=p.val-hurt;
if(p.val<=0){
System.out.println(this.getClass().getSimpleName()+"胜出!");
System.exit(0);
}
}
System.out.println(this.getClass().getSimpleName()+"攻击"
+p.getClass().getSimpleName()+","
+this.getClass().getSimpleName()
+(this.hitFlag?"发动攻击技能":"未发动攻击技能")
+p.getClass().getSimpleName()
+(this.defenseFlag?"发动防御技能":"未发动防御技能")
+this.getClass().getSimpleName()+":"+this.val+","
+p.getClass().getSimpleName()+":"+p.val);
hitFlag=false;//
defenseFlag=false;//重置标记,下次重用
}
publicintdefense(Personp){
if(rand.nextInt(10)<chanceDefense){
defenseFlag=true;//标记防御技能已经发动
return0;
}else{
returnp.fight;
}
}
}
文章分享结束,java简单游戏代码和用Java编写简单游戏详细教程的答案你都知道了吗?欢迎再次光临本站哦!