手机游戏乒乓.docx
- 文档编号:24361482
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:13
- 大小:18.03KB
手机游戏乒乓.docx
《手机游戏乒乓.docx》由会员分享,可在线阅读,更多相关《手机游戏乒乓.docx(13页珍藏版)》请在冰豆网上搜索。
手机游戏乒乓
这是我的第一款手机游戏,虽然实现的功能很简单,但是里面几乎用到了j2me制作游戏的全部知识点,在制作的过程中我也碰到许多的问题,要知道没有什么比那种决定把一件事情做好但是却不知道怎么做更痛苦的事情了,真的会抓狂的^_^但是我的性格告诉我做事情不能半途而废,最终克服了很多困难,把这个游戏实现了。
这个游戏带给我的酸甜苦辣我想在以后的人生中会受益匪浅的。
好了现在就介绍我的游戏吧。
我想大家应该有玩过手机乒乓球的游戏吧,这是游戏的截图,是不是玩过呢^_^,小球,拦杆和背景我是用sprite实现的,所有代码全部放在Game类上实现的,方块用的是矢量图实现的,因为它要和小球碰撞后消失的问题,所以我只能用到Point2D类来确定位置,然后通过RectMany类来实现一组方块。
虽然我很想做到尽量简单(最好能够都放在Game里面实现o(∩_∩)o…哈哈),但是由于我的知识有限,只能够创建两个类来最终实现碰撞小时的结果。
这个问题困惑了我很多天的时间,我也希望那位高手能够告诉我怎么能实现一组方块的功能但是不用创建别的类了。
里面的有些函数名称是自己瞎写的,千万不要被弄糊涂了,好了不多说了,赶快把代码写上去,弄完我还要去玩我的天下2了好好犒劳下自己,o(∩_∩)o…哈哈,有什么建议可以留言,我会经常光顾的,留下我的QQ:
773150200
packagecom.j2me;
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
/**
*@authorAdministrator
*/
publicclassMainextendsMIDletimplementsCommandListener{
privateDisplaydis=Display.getDisplay(this);
privateCommandexit=newCommand(exit,Command.EXIT,1);
privateGameg=newGame();
privateThreadt;
publicMain(){
}
publicvoidstartApp(){
g.addCommand(exit);
g.setCommandListener(this);
t=newThread(this.g);
t.start();
dis.setCurrent(g);
}
publicvoidexitApp(){
destroyApp(false);
notifyDestroyed();
}
publicvoidpauseApp(){
}
publicvoiddestroyApp(booleanunconditional){
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==exit){
this.exitApp();
}
}
}
packagecom.j2me;
importjava.io.IOException;
importjavax.microedition.lcdui.*;
importjavax.microedition.lcdui.Graphics;
importjavax.microedition.lcdui.game.GameCanvas;
importjavax.microedition.lcdui.game.LayerManager;
importjavax.microedition.lcdui.game.Sprite;
/**
*@authorAdministrator
*/
publicclassGameextendsGameCanvasimplementsCommandListener,Runnable{
privateLayerManagerlm;
privateBackback;
privateSprites_ball;
privateSpritespoly;
privateSpritesback;
privateImagepoly;
privateImagebackground;
privateintpX,pY;
privateintspeed=2;
privateintbX,bY;
privateintaddX=4;
privateintaddY=4;
privateRectMany[][]rm=newRectMany[20];
privateMainm;
privatebooleanover=true;
privateCommandc=newCommand(Exit,Command.EXIT,1);
publicGame(){
super(true);
try{
//Setupthiscanvastolistentocommandevents
setCommandListener(this);
//AddtheExitcommand
addCommand(newCommand(,Command.EXIT,1));
poly=Image.createImage(/pole.png);
background=Image.createImage(/backGround.png);
init();
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidinit()throwsIOException{
//this.back=newBack();
/*************栏杆**************************/
spoly=newSprite(poly);
pX=this.getWidth()/2;
pY=4*this.getHeight()/5;
spoly.setPosition(pX,pY);
spoly.setVisible(true);
/*************背景************************/
sback=newSprite(background);
sback.setVisible(true);
/****************小球******************/
back=newBack();
this.s_ball=this.back.getT_ball();
this.s_ball.setPosition(this.getWidth()/2,this.getHeight()/2);
this.s_ball.setFrameSequence(this.back.t_ballseq001);
/**************************************/
this.lm=newLayerManager();
lm.append(s_ball);
lm.append(spoly);
lm.append(sback);
/****************方块******************/
for(inti=0;i20;i++){
for(intj=0;j10;j++){
rm[i][j]=newRectMany();
rm[i][j].setPoint(add1(i),add1(j));
}
}
}
publicintadd1(intx){
return
x*=12;
}
publicintadd(intx){
return
x+=6;
}
//publicvoid
/**
*paint
*/
publicvoidpaint(Graphicsg){
g.setColor(0x11ffff);
for(inti=0;i20;i++){
for(intj=0;j10;j++){
if(rm[i][j].Alive()){
rm[i][j].huafang(g);
}
}
}
if(!
over)
System.out.println(**************Gameover***************);
}
publicvoidmotion(){
if(s_ball.getX()+s_ball.getWidth()getWidth()s_ball.getX()0){
addX=-addX;
}
if(s_ball.getY()+s_ball.getHeight()getHeight()s_ball.getY()0){
addY=-addY;
}
s_ball.move(addX,addY);
s_ball.nextFrame();
}
/***************拦杆控制****************/
publicvoidpolyCotrol(){
intkeyState=getKeyStates();
if((keyState&LEFT_PRESSED)!
=0){
this.spoly.move(-speed,0);
}
elseif((keyState&RIGHT_PRESSED)!
=0){
this.spoly.move(speed,0);
}
elseif((keyState&UP_PRESSED)!
=0){
this.spoly.move(0,-speed);
}
elseif((keyState&DOWN_PRESSED)!
=0){
this.spoly.move(0,speed);
}
}
/**********************碰撞检测(精灵检测)**************************/
publicbooleancollition(Spritesprite,Spritesprite_){
return
sprite.collidesWith(sprite_,true);
}
/**********************碰撞检测(矩形检测)**************************/
publicbooleanisIntersectingRect(intax,intay,intaw,
intah,intbx,intby,intbw,intbh){
if(by+bhay//isthebottomofbabovethetopofa?
byay+ah//isthetopofbbelowbottomofa?
bx+bwax//istherightofbtotheleftofa?
bxax+aw)//istheleftofbtotherightofa?
returnfalse;
else
returntrue;
}
/******************碰撞检测(方块和小球检测)******************/
publicvoidcollision(){
for(inti=0;i20;i++){
for(intj=0;j10;j++){
if(isIntersectingRect(s_ball.getX()-2,s_ball.getY()-2,13,13,rm[i][j].X()-2,rm[i][j].Y()-2,8,8))
rm[i][j].setAlive(false);
}
}
}
publicvoidgameover(){
if(s_ball.getY()+s_ball.getHeight()this.getHeight()){
over=false;
}
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==this.c){
m.exitApp();
}
}
publicvoidrun(){
Graphicsg=getGraphics();
while(over){
try{
motion();
polyCotrol();
if(collition(spoly,s_ball)){
addY=-addY;
}
gameover();
collision();
this.lm.paint(g,0,0);
flushGraphics();
repaint();
Thread.sleep(20);
}catch(InterruptedExceptionex){
ex.printStackTrace();
}
}
}
}
packagecom.j2me;
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
importjavax.microedition.lcdui.game.*;
/**
*@authorAdministrator
*/
publicclassBack{
//editor-folddefaultstate=collapseddesc=GeneratedFields
privateImagexball;
privateSpritet_ball;
publicintt_ballseq001Delay=200;
publicint[]t_ballseq001={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
///editor-fold
//editor-folddefaultstate=collapseddesc=GeneratedMethods
///editor-fold
publicImagegetXball()throwsjava.io.IOException{
if(xball==null){
//writepre-initusercodehere
xball=Image.createImage(/xball.png);
}
//writepost-initusercodehere
returnthis.xball;
}
publicSpritegetT_ball()throwsjava.io.IOException{
if(t_ball==null){
//writepre-initusercodehere
t_ball=newSprite(getXball(),15,15);
t_ball.setFrameSequence(t_ballseq001);
//writepost-initusercodehere
}
returnt_ball;
}
publicvoidupdateLayerManagerForT_back(LayerManagerlm)throwsjava.io.IOException{
//writepre-updateusercodehere
getT_ball().setPosition(138,183);
getT_ball().setVisible(true);
lm.append(getT_ball());
//writepost-updateusercodehere
}
}
packagecom.j2me;
classPoint2D{
privateintx,y;
privatebooleanalive;
//intconstructor
Point2D(){
setX(0);
setY(0);
}
publicvoidsetPoint(intx,inty){
setX(x);
setY(y);
}
//Xproperty
intX(){returnx;}
publicvoidsetX(intx){this.x=x;}
//Yproperty
intY(){returny;}
publicvoidsetY(inty){this.y=y;}
publicbooleanAlive(){
returnalive;
}
publicvoidsetAlive(booleanalive){
this.alive=alive;
}
}
packagecom.j2me;
importjavax.microedition.lcdui.Graphics;
/**
*
*@authorAdministrator
*/
publicclassRectManyextendsPoint2D{
publicRectMany(){
setAlive(true);
}
publicvoidhuafang(Graphicsg)
{
g.fillRect(X(),Y(),10,10);
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 手机 游戏 乒乓