快速业务通道

用java编写的俄罗斯方块小游戏 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-20
smallfont); fmsmall=g.getFontMetrics(); g.setFont(largefont); fmlarge=g.getFontMetrics(); gameInit(); } //初始化游戏 public boid gameInit() { short i,j; for(i=0;i<xblocks;i++) { for(j=0;j<yblocks;j++) { screendata[i][j]=0; } } score=0; emptyline=-1; newObject(); fast=false; curcount=maxcount; } public void newObject() { short i; int y; objectx=xblocks/2-1; objectdx=0; objecty=0; objecttype=(short)(Math.random()*itemcount); if (objecttype>=itemcount) objecttype=itemcount-1; objectptr=(short)(objecttype*itemlen); checkptr=(short)(Mat.random()*maxcolors+1); if (objectcolor>maxcolors) objectcolor=maxcolors; objectrotation=0; count=maxcount; //判断游戏是否结束 for(i=0;i<4;i++) { y=items[objectptr+i*2+1]; if(y>=0&&screendata[objectx+items[objectptr+i*2]][y]!=0) { ingame=false; showtitle=true; } } } //判断键盘是否被按下,并且处理事件 public boolean keyDown(Event e, int key) { if(ingame) { if(Key==Event.LEFT) { objectdx=-1; } else if (Key==Event.RIGHT) { objectdx=1; } else if (Key==Event.UP) { objectrotationd=1; } else if (Key==Event.DOWN) { fast=true; } else if (Key==Event.ESCAPE) { ingame=false; } } else { if(Key==´s´||Key==´S´) { ingame=true; gameInit(); } } return true; } //处理键盘抬起事件 public boolean keyUp(Event e, int key) { if(Key==Event.DOWN) { fast=false; } return true; } //paint()方法 public void paint(Graphics g) { Graphics gg; if (goff==null&&d.width>0&&d.height>0) { ii=createImage(d.width, d.height); goff=ii.getGraphics(); } if(goff==null||ii==null) return; //设置背景色 goff.setColor(background); goff.fillRect(0, 0, d.width, d.height); //如果游戏已经开始 if(ingame) //调用playGame()方法 playGame(); else//否则,显示其他信息 //显示初始信息 showIntro(); // 显示得分 showScore(); g.drawImage(ii, 0, 0, this); } //实现游戏 public void playGame() { boolean bottomreached=false; boolean stillscrolling=false; if(emptyline<0) { bottomreached=drawObject(); } else { scrollDown(); stillscrolling=true; } drawBars(); drawBlocks(); if(stillscrolling||bottomreached) { checkFull(); } } //显示初始信息 public void showIntro() { String s; drawBars(); drawBlocks(); if (showtitle) { goff.setFont(largefont); s="俄罗斯方块" goff.setColor(textcolor1); goff.drawString(s, barwidth+(width-fmlarge.stringWidth(s))/2-2,height/2-22); goff.setColor(Color.white); goff.drawString(s, barwidth+(width-fmlarge.stringWidth(s))/2,height/2-20); } else { goff.setFont(smallfont); s="请按下´S´键开始游戏"; goff.setColor(textcolor1); goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2-1,height/2-31); goff.setColor(Color.white); goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2,height/2-30); s="使用左右方向键控制运动"; goff.setColor(textcolor2); goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2-1,height/2-11); goff.setColor(Color.white); goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2,height/2-10); s="使用向下的方向键实现旋转"

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号