发布时间 : 星期一 文章基于java的吃豆子小游戏开发-毕设论文更新完毕开始阅读
常州大学本科生毕业设计(论文)
}
int[] yPoints = new int[] {yPos - 9,
};
yPos - 4, yPos + 9, yPos + 4, yPos + 9, yPos + 4, yPos + 9, yPos - 4, yPos - 9,
Ghost = new Polygon (xPoints, yPoints, xPoints.length);
if(!alarm && !ghost){ }
//用图形上下文的当前颜色填充由指定的Polygon对象定义的多边形Ghost。 g.fillPolygon(Ghost);
//如果敌人没有失去反抗能力,则用蓝色进行填充敌人的整体,用白色填充敌人的眼睛。
if(!alarm || ghost){
//填充敌人眼睛的白色部分 g.setColor(Color.white);
g.fillOval(xPos - 8, yPos - 5, 7, 8); g.fillOval(xPos + 2, yPos - 5, 7, 8);
//填充敌人眼睛中的蓝色十字部分 g.setColor(Color.blue); if(direction == 0){ }
//使用当前颜色填充外接指定矩形框的椭圆。 g.fillOval(xPos - 6, yPos - 4, 3, 4); g.fillOval(xPos + 4, yPos - 4, 3, 4);
第 28 页 共41页
28
常州大学本科生毕业设计(论文)
if(direction == 1){ g.fillOval(xPos - 6, yPos - 2, 3, 4); g.fillOval(xPos + 4, yPos - 2, 3, 4);
}
if(direction == 2){ g.fillOval(xPos - 7, yPos - 3, 3, 4); g.fillOval(xPos + 3, yPos - 3, 3, 4); }
if(direction == 3){ g.fillOval(xPos - 5, yPos - 3, 3, 4); g.fillOval(xPos + 5, yPos - 3, 3, 4); }
}
//如果敌人失去反抗能力 if(alarm){ g.setColor(Color.blue);
//当敌人即将恢复攻击能力时,将其颜色显示为蓝色 if(alarmTime >= 231 && alarmTime%6 > 2)
g.setColor(Color.lightGray); g.fillPolygon(Ghost); g.setColor(Color.white);
g.fillOval(xPos - 6, yPos - 6, 5, 6); g.fillOval(xPos + 2, yPos - 6, 5, 6);
//在此图形上下文的坐标系统中,使用当前颜色(灰色), //在点(xPos - 6, yPos + 3)和(xPos - 4, yPos + 1)之间画一条线。 g.drawLine(xPos - 6, yPos + 3, xPos - 4, yPos + 1); g.drawLine(xPos - 4, yPos + 1, xPos - 2, yPos + 3); g.drawLine(xPos - 2, yPos + 3, xPos, yPos + 1); g.drawLine(xPos, yPos + 1, xPos + 2, yPos + 3); g.drawLine(xPos + 2, yPos + 3, xPos + 4, yPos + 1);
g.drawLine(xPos + 4, yPos + 1, xPos + 6, yPos + 3);
第 29 页 共41页
29
常州大学本科生毕业设计(论文)
}
图6-5敌人图片
6.2.5 移动控制模块设计
使用键盘方向键对吃豆者的移动进行控制,其代码如下: if(e.getKeyCode() == KeyEvent.VK_UP){ player.ChangeDirection(0); //控制键盘向上移动的操作 UP_TYPED = true; DOWN_TYPED = false; LEFT_TYPED = false; RIGHT_TYPED = false; } if(e.getKeyCode() == KeyEvent.VK_DOWN){ player.ChangeDirection(1);//控制键盘向下移动的操作 UP_TYPED = false; DOWN_TYPED = true; LEFT_TYPED = false; RIGHT_TYPED = false; }
if(e.getKeyCode() == KeyEvent.VK_LEFT ){
player.ChangeDirection(2); //控制键盘向左移动的操作 UP_TYPED = false;
DOWN_TYPED = false; LEFT_TYPED = true; RIGHT_TYPED = false; }
if(e.getKeyCode() == KeyEvent.VK_RIGHT){ player.ChangeDirection(3); //控制键盘向右移动的操作
UP_TYPED = false;
第 30 页 共41页
30
常州大学本科生毕业设计(论文)
DOWN_TYPED = false; LEFT_TYPED = false; RIGHT_TYPED = true; } }
public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public void update(Graphics g) { Graphics offScreenGraphics; if (offScreenImage == null) {
//返回创建一幅用于双缓冲的、可在屏幕外绘制的图像。 //如果组件是不可显示的,则返回值可能为 null。 offScreenImage = createImage(545, 482); }
offScreenGraphics = offScreenImage.getGraphics(); offScreenGraphics.setColor(Color.white); offScreenGraphics.fillRect(0, 0, 545, 482); paint(offScreenGraphics);
g.drawImage(offScreenImage, 0, 0, this);
}
6.2.6水果模块设计
通过fruit包的public类,利用x、y数组来实现带水果的绘制,代码如下: interval = 63 + 21*((int)(Math.random()*10)); }//间隔时间的计算 //水果移动 if(direction == 0 && canMoveUp) yPos-=1; if(direction == 1 && canMoveDown) yPos+=1;
if(direction == 2 && canMoveLeft) xPos-=1;
if(direction == 3 && canMoveRight)
xPos+=1;
第 31 页 共41页
31