首页后端开发JAVAjava游戏原代码写在 java设计游戏代码带界面

java游戏原代码写在 java设计游戏代码带界面

时间2023-04-05 05:52:01发布访客分类JAVA浏览892
导读:求用JAVA编写俄罗斯方块游戏的源代码 俄罗斯方块——java源代码提供 import java.awt.*; import java.awt.event.*; //俄罗斯方块类 public class ERS_Block extends...

求用JAVA编写俄罗斯方块游戏的源代码

俄罗斯方块——java源代码提供 import java.awt.*; import java.awt.event.*; //俄罗斯方块类 public class ERS_Block extends Frame{ public static boolean isPlay=false; public static int level=1,score=0; public static TextField scoreField,levelField; public static MyTimer timer; GameCanvas gameScr; public static void main(String[] argus){ ERS_Block ers = new ERS_Block("俄罗斯方块游戏 V1.0 Author:Vincent"); WindowListener win_listener = new WinListener(); ers.addWindowListener(win_listener); } //俄罗斯方块类的构造方法 ERS_Block(String title){ super(title); setSize(600,480); setLayout(new GridLayout(1,2)); gameScr = new GameCanvas(); gameScr.addKeyListener(gameScr); timer = new MyTimer(gameScr); timer.setDaemon(true); timer.start(); timer.suspend(); add(gameScr); Panel rightScr = new Panel(); rightScr.setLayout(new GridLayout(2,1,0,30)); rightScr.setSize(120,500); add(rightScr); //右边信息窗体的布局 MyPanel infoScr = new MyPanel(); infoScr.setLayout(new GridLayout(4,1,0,5)); infoScr.setSize(120,300); rightScr.add(infoScr); //定义标签和初始值 Label scorep = new Label("分数:",Label.LEFT); Label levelp = new Label("级数:",Label.LEFT); scoreField = new TextField(8); levelField = new TextField(8); scoreField.setEditable(false); levelField.setEditable(false); infoScr.add(scorep); infoScr.add(scoreField); infoScr.add(levelp); infoScr.add(levelField); scorep.setSize(new Dimension(20,60)); scoreField.setSize(new Dimension(20,60)); levelp.setSize(new Dimension(20,60)); levelField.setSize(new Dimension(20,60)); scoreField.setText("0"); levelField.setText("1"); //右边控制按钮窗体的布局 MyPanel controlScr = new MyPanel(); controlScr.setLayout(new GridLayout(5,1,0,5)); rightScr.add(controlScr); //定义按钮play Button play_b = new Button("开始游戏"); play_b.setSize(new Dimension(50,200)); play_b.addActionListener(new Command(Command.button_play,gameScr)); //定义按钮Level UP Button level_up_b = new Button("提高级数"); level_up_b.setSize(new Dimension(50,200)); level_up_b.addActionListener(new Command(Command.button_levelup,gameScr)); //定义按钮Level Down Button level_down_b =new Button("降低级数"); level_down_b.setSize(new Dimension(50,200)); level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr)); //定义按钮Level Pause Button pause_b =new Button("游戏暂停"); pause_b.setSize(new Dimension(50,200)); pause_b.addActionListener(new Command(Command.button_pause,gameScr)); //定义按钮Quit Button quit_b = new Button("退出游戏"); quit_b.setSize(new Dimension(50,200)); quit_b.addActionListener(new Command(Command.button_quit,gameScr)); controlScr.add(play_b); controlScr.add(level_up_b); controlScr.add(level_down_b); controlScr.add(pause_b); controlScr.add(quit_b); setVisible(true); gameScr.requestFocus(); } } //重写MyPanel类,使Panel的四周留空间 class MyPanel extends Panel{ public Insets getInsets(){ return new Insets(30,50,30,50); } } //游戏画布类 class GameCanvas extends Canvas implements KeyListener{ final int unitSize = 30; //小方块边长 int rowNum; //正方格的行数 int columnNum; //正方格的列数 int maxAllowRowNum; //允许有多少行未削 int blockInitRow; //新出现块的起始行坐标 int blockInitCol; //新出现块的起始列坐标 int [][] scrArr; //屏幕数组 Block b; //对方快的引用 //画布类的构造方法 GameCanvas(){ rowNum = 15; columnNum = 10; maxAllowRowNum = rowNum - 2; b = new Block(this); blockInitRow = rowNum - 1; blockInitCol = columnNum/2 - 2; scrArr = new int [32][32]; } //初始化屏幕,并将屏幕数组清零的方法 void initScr(){ for(int i=0; irowNum; i++) for (int j=0; jcolumnNum; j++) scrArr[j]=0; b.reset(); repaint(); } //重新刷新画布方法 public void paint(Graphics g){ for(int i = 0; i rowNum; i++) for(int j = 0; j columnNum; j++) drawUnit(i,j,scrArr[j]); } //画方块的方法 public void drawUnit(int row,int col,int type){ scrArr[row][col] = type; Graphics g = getGraphics(); tch(type){ //表示画方快的方法 case 0: g.setColor(Color.black); break; //以背景为颜色画 case 1: g.setColor(Color.blue); break; //画正在下落的方块 case 2: g.setColor(Color.magenta); break; //画已经落下的方法 } g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true); g.dispose(); } public Block getBlock(){ return b; //返回block实例的引用 } //返回屏幕数组中(row,col)位置的属性值 public int getScrArrXY(int row,int col){ if (row 0 || row = rowNum || col 0 || col = columnNum) return(-1); else return(scrArr[row][col]); } //返回新块的初始行坐标方法 public int getInitRow(){ return(blockInitRow); //返回新块的初始行坐标 } //返回新块的初始列坐标方法 public int getInitCol(){ return(blockInitCol); //返回新块的初始列坐标 } //满行删除方法 void deleteFullLine(){ int full_line_num = 0; int k = 0; for (int i=0; irowNum; i++){ boolean isfull = true; L1:for(int j=0; jcolumnNum; j++) if(scrArr[j] == 0){ k++; isfull = false; break L1; } if(isfull) full_line_num++; if(k!=0 k-1!=i !isfull) for(int j = 0; j columnNum; j++){ if (scrArr[j] == 0) drawUnit(k-1,j,0); else drawUnit(k-1,j,2); scrArr[k-1][j] = scrArr[j]; } } for(int i = k-1 ; i rowNum; i++){ for(int j = 0; j columnNum; j++){ drawUnit(i,j,0); scrArr[j]=0; } } ERS_Block.score += full_line_num; ERS_Block.scoreField.setText(""+ERS_Block.score); } //判断游戏是否结束方法 boolean isGameEnd(){ for (int col = 0 ; col columnNum; col ++){ if(scrArr[maxAllowRowNum][col] !=0) return true; } return false; } public void keyTyped(KeyEvent e){ } public void keyReleased(KeyEvent e){ } //处理键盘输入的方法 public void keyPressed(KeyEvent e){ if(!ERS_Block.isPlay) return; tch(e.getKeyCode()){ case KeyEvent.VK_DOWN:b.fallDown(); break; case KeyEvent.VK_LEFT:b.leftMove(); break; case KeyEvent.VK_RIGHT:b.rightMove(); break; case KeyEvent.VK_SPACE:b.leftTurn(); break; } } } //处理控制类 class Command implements ActionListener{ static final int button_play = 1; //给按钮分配编号 static final int button_levelup = 2; static final int button_leveldown = 3; static final int button_quit = 4; static final int button_pause = 5; static boolean pause_resume = true; int curButton; //当前按钮 GameCanvas scr; //控制按钮类的构造方法 Command(int button,GameCanvas scr){ curButton = button; this.scr=scr; } //按钮执行方法 public void actionPerformed (ActionEvent e){ tch(curButton){ case button_play:if(!ERS_Block.isPlay){ scr.initScr(); ERS_Block.isPlay = true; ERS_Block.score = 0; ERS_Block.scoreField.setText("0"); ERS_Block.timer.resume(); } scr.requestFocus(); break; case button_levelup:if(ERS_Block.level 10){ ERS_Block.level++; ERS_Block.levelField.setText(""+ERS_Block.level); ERS_Block.score = 0; ERS_Block.scoreField.setText(""+ERS_Block.score); } scr.requestFocus(); break; case button_leveldown:if(ERS_Block.level 1){ ERS_Block.level--; ERS_Block.levelField.setText(""+ERS_Block.level); ERS_Block.score = 0; ERS_Block.scoreField.setText(""+ERS_Block.score); } scr.requestFocus(); break; case button_pause:if(pause_resume){ ERS_Block.timer.suspend(); pause_resume = false; } else{ ERS_Block.timer.resume(); pause_resume = true; } scr.requestFocus(); break; case button_quit:System.exit(0); } } } //方块类 class Block { static int[][] pattern = { { 0x0f00,0x4444,0x0f00,0x4444} ,//用十六进至表示,本行表示长条四种状态 { 0x04e0,0x0464,0x00e4,0x04c4} , { 0x4620,0x6c00,0x4620,0x6c00} , { 0x2640,0xc600,0x2640,0xc600} , { 0x6220,0x1700,0x2230,0x0740} , { 0x6440,0x0e20,0x44c0,0x8e00} , { 0x0660,0x0660,0x0660,0x0660} } ; int blockType; //块的模式号(0-6) int turnState; //块的翻转状态(0-3) int blockState; //快的下落状态 int row,col; //块在画布上的坐标 GameCanvas scr; //块类的构造方法 Block(GameCanvas scr){ this.scr = scr; blockType = (int)(Math.random() * 1000)%7; turnState = (int)(Math.random() * 1000)%4; blockState = 1; row = scr.getInitRow(); col = scr.getInitCol(); } //重新初始化块,并显示新块 public void reset(){ blockType = (int)(Math.random() * 1000)%7; turnState = (int)(Math.random() * 1000)%4; blockState = 1; row = scr.getInitRow(); col = scr.getInitCol(); dispBlock(1); } //实现“块”翻转的方法 public void leftTurn(){ if(assertValid(blockType,(turnState + 1)%4,row,col)){ dispBlock(0); turnState = (turnState + 1)%4; dispBlock(1); } } //实现“块”的左移的方法 public void leftMove(){ if(assertValid(blockType,turnState,row,col-1)){ dispBlock(0); col--; dispBlock(1); } } //实现块的右移 public void rightMove(){ if(assertValid(blockType,turnState,row,col+1)){ dispBlock(0); col++; dispBlock(1); } } //实现块落下的操作的方法 public boolean fallDown(){ if(blockState == 2) return(false); if(assertValid(blockType,turnState,row-1,col)){ dispBlock(0); row--; dispBlock(1); return(true); } else{ blockState = 2; dispBlock(2); return(false); } } //判断是否正确的方法 boolean assertValid(int t,int s,int row,int col){ int k = 0x8000; for(int i = 0; i 4; i++){ for(int j = 0; j 4; j++){ if((int)(pattern[t][s]k) != 0){ int temp = scr.getScrArrXY(row-i,col+j); if (temp0||temp==2) return false; } k = k 1; } } return true; } //同步显示的方法 public synchronized void dispBlock(int s){ int k = 0x8000; for (int i = 0; i 4; i++){ for(int j = 0; j 4; j++){ if(((int)pattern[blockType][turnState]k) != 0){ scr.drawUnit(row-i,col+j,s); } k=k1; } } } } //定时线程 class MyTimer extends Thread{ GameCanvas scr; public MyTimer(GameCanvas scr){ this.scr = scr; } public void run(){ while(true){ try{ sleep((10-ERS_Block.level + 1)*100); } catch(InterruptedException e){ } if(!scr.getBlock().fallDown()){ scr.deleteFullLine(); if(scr.isGameEnd()){ ERS_Block.isPlay = false; suspend(); } else scr.getBlock().reset(); } } } } class WinListener extends WindowAdapter{ public void windowClosing (WindowEvent l){ System.exit(0); } } 22

求java小游戏源代码

表1. CheckerDrag.java

// CheckerDrag.javaimport java.awt.*; import java.awt.event.*; public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } } ); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } } ); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); } }

java源代码写到哪里

我来回答,Javascript是一种由Netscape的LiveScript发展而来的脚本语言,主要目的是为了解决服务器终端语言,比如Perl,遗留的速度问题。当时服务端需要对数据进行验证,由于网络速度相当缓慢,只有28.8kbps,验证步骤浪费的时间太多。于是Netscape的浏览器Navigator加入了Javascript,提供了数据验证的基本功能。

历史

在1992年,Nombas开始开发一种嵌入式脚本语言,叫做C-minus-minus(Cmm)。[待续...

能够具有交互性,能够包含更多活跃的元素,就有必要在网页中嵌入其它的技术。如:Javascript、VBScript、Document Object Model(文件目标模块)、Layers和 Cascading Style Sheets(CSS),这里主要讲Javascript。那么Javascript是什么东东?Javascript就是适应动态网页制作的需要而诞生的一种新的编程语言,如今越来越广泛地使用于Internet网页制作上。 Javascript是由 Netscape公司开发的一种脚本语言(scripting language),或者称为描述语言。在HTML基础上,使用Javascript可以开发交互式Web网页。Javascript的出现使得网页和用户之间实现了一种实时性的、动态的、交互性的关系,使网页包含更多活跃的元素和更加精彩的内容。 运行用Javascript编写的程序需要能支持Javascript语言的浏览器。Netscape公司 Navigator 3.0以上版本的浏览器都能支持 Javascript程序,微软公司 Internet Explorer 3.0以上版本的浏览器基本上支持Javascript。微软公司还有自己开发的Javascript,称为JScript。 Javascript和Jscript基本上是相同的,只是在一些细节上有出入。 Javascript短小精悍, 又是在客户机上执行的,大大提高了网页的浏览速度和交互能力。 同时它又是专门为制作Web网页而量身定做的一种简单的编程语言。

虽然,在Dreamweaver的Behaviors可以为我们方便地使用Javascript程序而不用编写代码,但我们自己了解了Javascript的编程方法后,将能更加方便灵活地应用,也使Javascript的代码更简练。本专题通过对一系列典型程序的剖析,使你快速地掌握Javascript的编程技巧,设计出质量上乘的动态网页打下坚实的基础。在此之前,我们先了解一些Javascript 的基本概念。

JavaScript 有什么特点

JavaScript 使网页增加互动性。JavaScript 使有规律地重复的HTML文段简化,减少下载时间。JavaScript 能及时响应用户的操作,对提交表单做即时的检查,无需浪费时间交由 CGI 验证。JavaScript 的特点是无穷无尽的,只要你有创意。

Java 与 JavaScript 有什么不同

很多人看到 Java 和 JavaScript 都有“Java”四个字,就以为它们是同一样东西,连我自己当初也是这样。其实它们是完完全全不同的两种东西。Java,全称应该是 Java Applet,是嵌在网页中,而又有自己独立的运行窗口的小程序。Java Applet 是预先编译好的,一个 Applet 文件(.class)用 Notepad 打开阅读,根本不能理解。Java Applet 的功能很强大,可以访问 http、ftp等协议,甚至可以在电脑上种病毒(已有先例了)。相比之下,JavaScript 的能力就比较小了。JavaScript 是一种“脚本”(“Script”),它直接把代码写到 HTML 文档中,浏览器读取它们的时候才进行编译、执行,所以能查看 HTML 源文件就能查看JavaScript 源代码。JavaScript 没有独立的运行窗口,浏览器当前窗口就是它的运行窗口。它们的相同点,我想只有同是以 Java 作编程语言一点了。

开发 JavaScript 该用什么软件

一个 JavaScript 程序其实是一个文档,一个文本文件。它是嵌入到 HTML 文档中的。所以,任何可以编写 HTML 文档的软件都可以用来开发 JavaScript。在此我推荐大家用 FrontPage 2000 附带的 Microsoft 脚本编辑器(在 FrontPage 菜单 | 工具 | 宏 | Microsoft 脚本编辑器)。它是个像 Visual Basic / C++ 一样的程序开发器,能对正在输入的语句作出简要提示。配合 FrontPage 2000,使工作量大大减少。

一、Javascript在网页的用法

Javascript加入网页有两种方法:

1、直接加入HTML文档

这是最常用的方法,大部分含有Javascript的网页都采用这种方法,如:

script language="Javascript"

!--

document.writeln("这是Javascript!采用直接插入的方法!");

//-Javascript结束--

/script

在这个例子中,我们可看到一个新的标签: script……/script,而script language="Javascript” 用来告诉浏览器这是用Javascript编写的程序,需要调动相应的解释程序进行解释。

HTML的注释标签!--和--:用来去掉浏览器所不能识别的Javascript源代码的,这对不支持 Javascript 语言的浏览器来说是很有用的。

//-Javascript结束:双斜杠表示 Javascript的注释部分,即从//开始到行尾的字符都被忽略。 至于程序中所用到的document.write()函数则表示将括号中的文字输出到窗口中去, 这在后面将会详细介绍。 另外一点需要注意的是,script……/script的位置并不是固定的,可以包含在head....../head 或body...../body中的任何地方。

2、引用方式 如果已经存在一个Javascript源文件(以js为扩展名),则可以采用这种引用的方式,以提高程序代码的利用率。其基本格式如下:

script src=url language="Javascript"/script

其中的Url就是程序文件的地址。同样的,这样的语句可以放在HTML文档头部或主体的任何部分。 如果要实现“直接插入方式”中所举例子的效果,可以首先创建一个Javascript源代码文件“Script.js”,其内容如下:

document.writeln("这是Javascript!采用直接插入的方法!");

在网页中可以这样调用程序:script src="Script.js" language="Javascript"/script 。

二、Javascript基本概念

在这里只作简单介绍,在以后的例子中结程序再作具体解释其作用。

1、运算符

运算符就是完成操和的一系列符号,它有七类:

赋值运算符、算术运算符、比较运算符、逻辑运算符、条件运算、位操作运算符和字符串运算符。

2、表达式

运算符和操作数的组合称为表达式,通常分为四类:赋值表达式、算术表达式、布尔表达式和字符串表达式。

3、语句

Javascript程序是由若干语句组成的,语句是编写程序的指令。Javascript提供了完整的基本编程语句,它们是:

赋值语句、switch选择语句、while循环语句、for循环语句、do while循环语句、break循环中止语句和continue循环中断语句。

4、函数

函数是命名的语句段,这个语句段可以被当作一个整体来引用不着和执行。使用函数要注意以下几点:

1)函数由关键字function定义;

2)函数必须先定义后使用,否则将出错;

3)函数名是调用函数时引用的名称,它对大小写是敏感的,调用函数时不可写错函数名;

4)参数表示传递给函数使用或操作的值,它可以是常量,也可以是变量;

5)return语句用于返回表达式的值,也可以没有。

5、对象

Javascript的一个重要功能就是基于对象的功能,通过基于对象的程序设计,可以用更直观、模块化和可重复使用的方式进行程序开发。

一组包含数据的属性和对属性中包含数据进行操作的方法,称为对象。比如要设定网页的背景颜色,所针对的对象就是document,所用的属性名是bgcolor,如document.bgcolor="blue",就是表示使背景的颜色为蓝色。

6、事件

用户与网页交互时产生的操作,称为事件。绝大部分事都由用户的动作所引发,如:用户按鼠标的按钮,就产生onclick事件,若鼠标的指针的链接上移动,就产生onmouseover事件等等。在Javascript中,事件往往与事件处理程序配套使用。

学习Javascript比较快速有效的方法是先熟悉一些基本概念,然后找几个别人设计好的程序认真仔细地分析一遍,再稍作改动,再看看能否达到预期目的,不断地举一反三,既可以加深对一些参数、设计方法的理解,又可以快速地提高自己的水平。另外,再提醒一下:Javascript对大小写是敏感的,特别是一些对象、方法、属性的大小写一定要一致,要养成一种良好的习惯,否则在调试程序时可要累死你了。

7、变量

如 var myVariable = "some value"; 9415希望对你有帮助!

基于Java语言的打地鼠的小游戏源代码是什么?

 public void mouseClicked(MouseEvent e){ \x0d\x0aObject source=e.getSource(); //获取事件源,即地鼠标签\x0d\x0aif(source instanceof JLabel){ //如果事件是标签组件\x0d\x0aJLabel mouse=(JLabel)source; //强制转换为JLabel标签\x0d\x0amouse.setIcon(null); //取消标签图标\x0d\x0a} \x0d\x0a} \x0d\x0a} ); \x0d\x0athis.getContentPane().add(mouses[i]); //添加显示地鼠的标签到窗体\x0d\x0a} \x0d\x0a\x0d\x0amouses[0].setLocation(253, 300); //设置每个标签的位置\x0d\x0amouses[1].setLocation(333, 250); \x0d\x0amouses[2].setLocation(388, 296); \x0d\x0amouses[3].setLocation(362, 364); \x0d\x0amouses[4].setLocation(189, 353); \x0d\x0amouses[5].setLocation(240, 409); \x0d\x0a\x0d\x0afinal JLabel backLabel=new JLabel(); //创建显示背景的标签\x0d\x0abackLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); \x0d\x0athis.setBounds(100,100,img.getIconWidth(),img.getIconHeight()); \x0d\x0abackLabel.setIcon(img); //添加背景到标签\x0d\x0athis.getContentPane().add(backLabel); //添加背景标签到窗体\x0d\x0a} \x0d\x0a/**\x0d\x0a* 线程的核心方法\x0d\x0a*/\x0d\x0a\x0d\x0apublic void run(){ \x0d\x0awhile(true){ //使用无限循环\x0d\x0atry{ \x0d\x0aThread.sleep(3000); //使线程休眠3秒\x0d\x0aint index=(int)(Math.random()*6); //生成随机的地鼠索引\x0d\x0aif(mouses[index].getIcon()==null){ //如果地鼠标签没有设置图片\x0d\x0amouses[index].setIcon(imgMouse); //为该标签添加地鼠图片\x0d\x0a} \x0d\x0a} catch(InterruptedException e){ \x0d\x0ae.printStackTrace(); \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0a}

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


若转载请注明出处: java游戏原代码写在 java设计游戏代码带界面
本文地址: https://pptw.com/jishu/1547.html
java代码编写环境 java环境配置好了之后怎样编程 java编写代码输出右移 java编写代码输出右移到左移

游客 回复需填写必要信息