java游戏代码下载(java代码小游戏)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈java游戏代码下载,以及java代码小游戏对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
求"贪吃蛇"小游戏JAVA源代码一份
贪吃蛇
import java.awt.*;
import java.awt.event.*;
public class GreedSnake //主类
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyWindow();
}
}
class MyPanel extends Panel implements KeyListener,Runnable//自定义面板类,继承了键盘和线程接口
{
Button snake[]; //定义蛇按钮
int shu=0; //蛇的节数
int food[]; //食物数组
boolean result=true; //判定结果是输 还是赢
Thread thread; //定义线程
static int weix,weiy; //食物位置
boolean t=true; //判定游戏是否结束
int fangxiang=0; //蛇移动方向
int x=0,y=0; //蛇头位置
MyPanel()
{
setLayout(null);
snake=new Button[20];
food=new int [20];
thread=new Thread(this);
for(int j=0;j20;j++)
{
food[j]=(int)(Math.random()*99);//定义20个随机食物
}
weix=(int)(food[0]*0.1)*60; //十位*60为横坐标
weiy=(int)(food[0]%10)*40; //个位*40为纵坐标
for(int i=0;i20;i++)
{
snake[i]=new Button();
}
add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this); //为蛇头添加键盘监视器
snake[0].setBounds(0,0,10,10);
setBackground(Color.cyan);
}
public void run() //接收线程
{
while(t)
{
if(fangxiang==0)//向右
{
try
{
x+=10;
snake[0].setLocation(x, y);//设置蛇头位置
if(x==weixy==weiy) //吃到食物
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint(); //重绘下一个食物
add(snake[shu]); //增加蛇节数和位置
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100); //睡眠100ms
}
catch(Exception e){}
}
else if(fangxiang==1)//向左
{
try
{
x-=10;
snake[0].setLocation(x, y);
if(x==weixy==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==2)//向上
{
try
{
y-=10;
snake[0].setLocation(x, y);
if(x==weixy==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==3)//向下
{
try
{
y+=10;
snake[0].setLocation(x, y);
if(x==weixy==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
int num1=shu;
while(num11)//判断是否咬自己的尾巴
{
if(snake[num1].getBounds().x==snake[0].getBounds().xsnake[num1].getBounds().y==snake[0].getBounds().y)
{
t=false;
result=false;
repaint();
}
num1--;
}
if(x0||x=this.getWidth()||y0||y=this.getHeight())//判断是否撞墙
{
t=false;
result=false;
repaint();
}
int num=shu;
while(num0) //设置蛇节位置
{
snake[num].setBounds(snake[num-1].getBounds());
num--;
}
if(shu==15) //如果蛇节数等于15则胜利
{
t=false;
result=true;
repaint();
}
}
}
public void keyPressed(KeyEvent e) //按下键盘方向键
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键
{
if(fangxiang!=1)//如果先前方向不为左
fangxiang=0;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ if(fangxiang!=0)
fangxiang=1;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{ if(fangxiang!=3)
fangxiang=2;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ if(fangxiang!=2)
fangxiang=3;
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void paint(Graphics g) //在面板上绘图
{
int x1=this.getWidth()-1;
int y1=this.getHeight()-1;
g.setColor(Color.red);
g.fillOval(weix, weiy, 10, 10);//食物
g.drawRect(0, 0, x1, y1); //墙
if(t==falseresult==false)
g.drawString("GAME OVER!", 250, 200);//输出游戏失败
else if(t==falseresult==true)
g.drawString("YOU WIN!", 250, 200);//输出游戏成功
}
}
class MyWindow extends Frame implements ActionListener//自定义窗口类
{
MyPanel my;
Button btn;
Panel panel;
MyWindow()
{
super("GreedSnake");
my=new MyPanel();
btn=new Button("begin");
panel=new Panel();
btn.addActionListener(this);
panel.add(new Label("begin后请按Tab键选定蛇"));
panel.add(btn);
panel.add(new Label("按上下左右键控制蛇行动"));
add(panel,BorderLayout.NORTH);
add(my,BorderLayout.CENTER);
setBounds(100,100,610,500);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)//按下begin按钮
{
if(e.getSource()==btn)
{
try
{
my.thread.start(); //开始线程
my.validate();
}
catch(Exception ee){}
}
}
}

手机当中的JAVA游戏是指什么??
java游戏简单的理解就是用java语言编的游戏,他的特点是在任何机器上都可以运行,只要那台机器上装了java虚拟机。所以他的通用性很好。这是因为机器只认识他自己的语言,不同的机器运用的也是他自己的语言,就比如电脑的语言和手机的语言肯定是有所不同的,所以在电脑上运行的游戏在手机上运行不了,反之亦然。但java的思想是不同机器上通过java提供的虚拟机把游戏代码翻译成不同机器的语言,机器就可以执行了。在手机上运行的java游戏前提是要求你的手机支持java虚拟机,才能运行java游戏
如果一般的电脑游戏不是java语言编制的,那么它无法在别的机器上运行,就是手机不认识他,所以下到手机上也无法执行。
如果你的手机支持java,那么你必须上网下载游戏程序代码到手机上,然后通过虚拟机将代码翻译手机认识的机器代码,才能运行游戏。如果你的游戏不是网络游戏,游戏下载完后,就可以运行,不必再上网了
猜拳游戏java能输出游戏时间
Java实现猜拳游戏的核心在于电脑随机数的生成,Java中的随机数生成方法是:
首先引入包 import java.util.*; 然后 int r=new Random().nextInt(3); (nextInt中的数字三代表随机数生成的个数,从零开始)
所以在猜拳的输入中需要有0、1、2三个数字代替,如果要输入汉字,则用if进行相应判断即可。
在实现的游戏中实现①猜拳;②记录胜负;③玩家决定游戏局数;④输出获胜、失败及平局;⑤统计总共的胜负结果(根据获胜次数判断)
①猜拳基础功能:该部分代码可以放到一个方法中,减少主函数代码量。
电脑出拳即 int r=new Random().nextInt(3); 注意:该部分一定要写在for循环内部,否则无法实现每次不同的随机数。
通过if判断双方出拳是否相等 if(a==0r==0) else if(a==0r==1) else if(a==0r==2) 即可实现猜拳,if内直接输出相关语句即可
②记录胜负: 定义猜拳方法为int ,通过返回值记录相关比赛的胜负情况 ,可以用0--失败;1--获胜;2--平局 进行记录,在主函数中对相应抛出的数字记录即可
if(a==0r==0){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
h=comp.compare(a,r); if (h==1) j++;
登录后复制
③玩家决定局数: 定义一个数,在循环中不大于该数即可
④输出获胜、失败及平局: j、k即胜利和失败,平局数即n-j-k。
⑤统计结果,直接用if比较i、j的数字结果即可。
主函数部分:
package SS2_5;
import java.util.*;
public class Main {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
Compare comp=new Compare();
int h=0,j=0,k=0;
System.out.println("rules:0--cloth;1--stone;2--scissors.\nU can choose how many times you want to play:");
int n=scanner.nextInt();
for(int i=1;i=n;i++){
System.out.print("It's the "+i+" round,your turn:");
int a=scanner.nextInt();
int r=new Random().nextInt(3);
switch (a){
case 0:
h=comp.compare(a,r);
break;
case 1:
h=comp.compare(a,r);
break;
case 2:
h=comp.compare(a,r);
break;
default:
System.out.println("Wrong number!");
break;
}
if (h==1)
j++;
else if(h==0)
k++;
}
System.out.println("The total times you won are "+j+",The draw times are "+(n-j-k)+".");
if(jk)
System.out.println("You are the final winner");
else if(kj)
System.out.println("The computer is the winner.");
if(j==k)
System.out.println("The final result is draw");
}
}
登录后复制
compare方法部分
package SS2_5;
public class Compare {
public int compare(int a,int r){
int counter=0;
if(a==0r==0){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
else if(a==0r==1){
System.out.println("The computer comes out with stone, you won. ");
return 1;
}
else if(a==0r==2){
System.out.println("The computer comes out with scissor,you lost. ");
return 0;
}
else if(a==1r==0){
System.out.println("The computer comes out with cloth,you lost. ");
return 0;
}
else if(a==1r==1){
System.out.println("The computer comes out with stone,it was a draw. ");
return 2;
}
else if(a==1r==2){
System.out.println("The computer comes out with scissors,you won. ");
return 1;
}
else if(a==2r==0){
System.out.println("The computer comes out with cloth,you won. ");
return 1;
}
else if(a==2r==1){
System.out.println("The computer comes out with stone,you lost. ");
return 0;
}
else if(a==2r==2){
System.out.println("The computer comes out with scissors,it was a draw. ");
return 2;
}
else
return 0;
}
}
登录后复制
java
704拖拉机
精选推荐
广告
java写简单的猜拳游戏
2下载·0评论
2016年7月27日
用Java编写的猜拳小游戏
2029阅读·0评论·0点赞
2021年3月7日
Java猜拳游戏和Random的应用
21阅读·0评论·0点赞
2022年10月24日
java实现完整版猜拳小游戏
25下载·0评论
2018年11月22日
用python实现功能猜拳
1137阅读·2评论·3点赞
2022年7月14日
java猜拳switch计分制_java----猜拳(10局分胜负)
117阅读·0评论·1点赞
2021年3月15日
二手拖拉机交易市场,你还在高价买吗?
精选推荐
广告
利用Java编写简单的猜拳游戏
911阅读·0评论·1点赞
2022年9月8日
Java简单实现猜拳游戏
1.1W阅读·1评论·2点赞
2022年1月23日
java猜拳游戏代码_Java实现简单猜拳游戏
4496阅读·0评论·0点赞
2021年3月1日
用java来写一个简单的猜拳小游戏
890阅读·1评论·1点赞
2022年7月26日
java实现猜拳游戏
3180阅读·2评论·1点赞
2022年5月4日
JAVA编写猜拳游戏
3037阅读·3评论·3点赞
2021年3月16日
[Java教程]17.实战,趣味猜拳小游戏
8040阅读·2评论·3点赞
2020年6月24日
怎么用java实现人机猜拳?
1959阅读·6评论·9点赞
2021年7月22日
Java Random经典例子【猜拳游戏】
4318阅读·0评论·0点赞
2014年3月22日
java的人机猜拳代码_Java实现人机猜拳游戏
702阅读·0评论·2点赞
2021年3月12日
Java基础练习之猜拳游戏
363阅读·1评论·1点赞
2021年8月19日
用java写猜拳小游戏
1096阅读·0评论·1点赞
2021年9月1日
Java猜拳小游戏
97阅读·0评论·0点赞
2022年8月23日
java猜拳小游戏
500阅读·1评论·0点赞
2022年7月14日
关于java游戏代码下载和java代码小游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
