首页后端开发JAVAjava写个游戏代码 java写游戏代码简单吗

java写个游戏代码 java写游戏代码简单吗

时间2023-07-08 14:14:01发布访客分类JAVA浏览387
导读:JAVA编写一个游戏 !DOCTYPE htmlhtmlheadmeta charset="utf-8" /titletest/titlescript type="text/javascript" function demo (num...

JAVA编写一个游戏

!DOCTYPE html

html

head

meta charset="utf-8" /

titletest/title

script type="text/javascript"

    function demo (num)

    {

        var array = [];

        for ( var i = 1;  i  num + 1;  i++)

        {

            var input = prompt ("请察慧乱输入第" + i + "个数字", "");

            if (!/^[1-9]\d*$/.test (input))

       败档     {

                alert ("请输入数字哦!!!");

   碧粗             i--;

            }

            else

            {

                array.push (input);

            }

        }

        alert (Math.max.apply (null, array));

        var d = prompt ("是否继续", "")

        if (d == "是")

        {

            demo (3);

        }

        else

        {

            alert ("再见");

        }

    }

/script

/head

body

    a href="javascript:void(0); " onclick="demo(3); "游戏开始/a

/body

/html

求JAVA编写打字游戏源代码!

package chen;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.util.Vector;

import javax.swing.JFrame;

public class keyTest extends Window implements KeyListener, Runnable {

int width, height;

BufferedImage buf;

Graphics gc;

boolean play = true;

VectoroneChar keys = new Vector坦伍散oneChar();

int ok = 0, fail = 0, error = 0, sum = 0;

Font small = new Font("宋体", 0, 30);

Font big = new Font("宋体", 0, 50);

long time = System.currentTimeMillis();

keyTest(Frame f) {

super(f);

Dimension s = getToolkit().getScreenSize();

width = (int) s.getWidth();

height = (int) s.getHeight();

this.setBounds(0, 0, width, height);

this.buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

gc = buf.getGraphics();

this.setVisible(true);

this.setAlwaysOnTop(true);

this.buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

gc = buf.getGraphics();

new Thread(this).start();

}

public static void main(String s[]) {

JFrame help = new JFrame("打字让氏练习");

help.setVisible(true);

help.setDefaultCloseOperation(3);

help.addKeyListener(new keyTest(help));

}

public void keyTyped(KeyEvent e) {

}

public synchronized void keyPressed(KeyEvent e) {

if (e.getKeyCode() == 27) {

play = false;

this.dispose();

System.exit(0);

}

char s = e.getKeyChar();

if (s = 'a' s = 'z' || (s = 'A' s = 'Z')) {

String l = "橘斗" + s;

for (int i = 0; i keys.size(); i++) {

if (l.equals(((oneChar) keys.elementAt(i)).s)) {

keys.removeElementAt(i);

ok++;

return;

}

}

error++;

}

}

public void keyReleased(KeyEvent e) {

}

@Override

public void update(Graphics g) {

gc.setColor(Color.BLACK);

gc.fillRect(0, 0, width, height);

gc.setColor(Color.red);

int l = (ok + error) 0 ? (ok * 100 / (ok + error)) : 100;

gc.setFont(small);

gc.drawString("成功:" + ok + " 错误:" + error + " 失败:" + fail + " 正确率:" + l + "% 时间:" + (System.currentTimeMillis() - time) / 1000, 10, height - 30);

gc.setFont(big);

oneChar o;

for (int i = 0; i keys.size(); i++) {

o = keys.elementAt(i);

gc.setColor(o.c);

gc.drawString(o.s, o.x, o.y += 6);

if (o.y height - 10) {

fail++;

keys.removeElementAt(i);

}

}

g.drawImage(buf, 0, 0, null);

}

public void run() {

while (play) {

try {

sum++;

if (sum % 5 == 0) {

newchar();

}

Thread.sleep(80);

repaint();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

private void newchar() {

keys.add(oneChar.getinstance(width));

}

}

package chen;

import java.awt.Color;

public class oneChar {

static java.util.Random r = new java.util.Random();

public static oneChar getinstance(int maxX) {

oneChar a = new oneChar();

int b = r.nextInt(26);

a.s = "" + (char) (b + (r.nextInt(4) 1 ? 'a' : 'A'));

a.x = r.nextInt(maxX - 30);

a.c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));

return a;

}

int x, y;

Color c;

String s;

}

如何学习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);

}

}

}

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: java写个游戏代码 java写游戏代码简单吗
本文地址: https://pptw.com/jishu/296128.html
java做一个邮箱源代码 java开源邮件系统 java可配置代码 java 配置

游客 回复需填写必要信息