用java做游戏,怎么让背景图片滚动啊
在frame里面放个panel,在panel上弄上背景图片,然后让图片自动滚动,做成在前进的样子,要怎么做啊...
在frame里面放个panel,在panel上弄上背景图片,然后让图片自动滚动,做成在前进的样子,要怎么做啊
展开
2个回答
展开全部
首先需要一个大图片bg,长度为bgWeight,它作为背景,相当于一个首尾相连的画卷,人物走动的话,它就滚动
int pos=0;//它记录窗口坐标(左边)在bg的偏移;
pos=(pos+moveSpeed)%bgWeight;
然后根据pos的位置绘制背景,有可能你是绘制 bg的尾部+bg的头部 来形成完整的背景
经典代码:
public Ribbon(int movS, BufferedImage im) {
bim = im; //im就是背景图片
xImHead = 0;//当前游戏窗口左边在背景图片中的坐标
pWidth = GamePanel.WIDTH;游戏窗口宽
pHeight = GamePanel.HEIGHT;游戏窗口高
width = bim.getWidth();背景图片宽度
moveSize = movS;移动距离
}
public void draw(Graphics g) /* Consider 5 cases:考虑5种情况
// when xImHead == 0, draw only the im head
//when xImHead > 0, draw the im tail and im head, or only the im tail.
//when xImHead < 0, draw the im tail, or the im tail and im head
// xImHead can range between -width to width (exclusive)
*/ {
if (xImHead == 0) // draw im head at (0,0)//情况1
{
drawRibbon(g, bim, 0, pWidth, 0, pWidth);
}
else if ((xImHead > 0) && (xImHead < pWidth)) {//情况2
// draw im tail at (0,0) and im head at (xImHead,0)
drawRibbon(g, bim, 0, xImHead, width - xImHead, width); // im tail
drawRibbon(g, bim, xImHead, pWidth, 0, pWidth - xImHead); // im head
}
else if (xImHead >= pWidth) // only draw im tail at (0,0),情况3
{
drawRibbon(g, bim, 0, pWidth,
width - xImHead, width - xImHead + pWidth); // im tail
}
else if ((xImHead < 0) && (xImHead >= pWidth - width)) {//情况4
drawRibbon(g, bim, 0, pWidth, -xImHead, pWidth - xImHead); // im body
}
else if (xImHead < pWidth - width) { //情况5
// draw im tail at (0,0) and im head at (width+xImHead,0)
drawRibbon(g, bim, 0, width + xImHead, -xImHead, width); // im tail
drawRibbon(g, bim, width + xImHead, pWidth,
0, pWidth - width - xImHead); // im head
}
} // end of display()
累死,看看这里,很详细 http://wenku.baidu.com/view/85d254ce0508763231121260.html
看完还不会找我!
int pos=0;//它记录窗口坐标(左边)在bg的偏移;
pos=(pos+moveSpeed)%bgWeight;
然后根据pos的位置绘制背景,有可能你是绘制 bg的尾部+bg的头部 来形成完整的背景
经典代码:
public Ribbon(int movS, BufferedImage im) {
bim = im; //im就是背景图片
xImHead = 0;//当前游戏窗口左边在背景图片中的坐标
pWidth = GamePanel.WIDTH;游戏窗口宽
pHeight = GamePanel.HEIGHT;游戏窗口高
width = bim.getWidth();背景图片宽度
moveSize = movS;移动距离
}
public void draw(Graphics g) /* Consider 5 cases:考虑5种情况
// when xImHead == 0, draw only the im head
//when xImHead > 0, draw the im tail and im head, or only the im tail.
//when xImHead < 0, draw the im tail, or the im tail and im head
// xImHead can range between -width to width (exclusive)
*/ {
if (xImHead == 0) // draw im head at (0,0)//情况1
{
drawRibbon(g, bim, 0, pWidth, 0, pWidth);
}
else if ((xImHead > 0) && (xImHead < pWidth)) {//情况2
// draw im tail at (0,0) and im head at (xImHead,0)
drawRibbon(g, bim, 0, xImHead, width - xImHead, width); // im tail
drawRibbon(g, bim, xImHead, pWidth, 0, pWidth - xImHead); // im head
}
else if (xImHead >= pWidth) // only draw im tail at (0,0),情况3
{
drawRibbon(g, bim, 0, pWidth,
width - xImHead, width - xImHead + pWidth); // im tail
}
else if ((xImHead < 0) && (xImHead >= pWidth - width)) {//情况4
drawRibbon(g, bim, 0, pWidth, -xImHead, pWidth - xImHead); // im body
}
else if (xImHead < pWidth - width) { //情况5
// draw im tail at (0,0) and im head at (width+xImHead,0)
drawRibbon(g, bim, 0, width + xImHead, -xImHead, width); // im tail
drawRibbon(g, bim, width + xImHead, pWidth,
0, pWidth - width - xImHead); // im head
}
} // end of display()
累死,看看这里,很详细 http://wenku.baidu.com/view/85d254ce0508763231121260.html
看完还不会找我!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询