我正在做一個大型的壟斷遊戲作爲一個項目在學校,並遇到了一個相當大的性能問題。現在我有一種繪製方法,每次它被調用時就繪製整個板子......這是一個大問題,因爲板子只需要在開始時繪製一次,並且只有在某人買房子或什麼時才需要繪製。我想要繪製的唯一組件是玩家,因爲它們是最左右移動並需要繪製的組件。Java鞦韆遊戲板
這裏是我的板的paint方法:
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//Draw all the spots
for(int i = 0; i < spots.length; i++){
g2d.setColor(Color.white);
g2d.fill(spots[i].getRect());
if(spots[i] instanceof Property){
if(i < 10){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width,spots[i].getRect().height/3);
g2d.fill(temp);
g2d.setColor(Color.black);
g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().height);
}else if(i >= 10 && i < 20){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x+spots[i].getRect().width-spots[i].getRect().width/3,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height);
g2d.fill(temp);
}else if(i >= 20 && i < 30){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height-spots[i].getRect().height/3,spots[i].getRect().width,spots[i].getRect().height/3);
g2d.fill(temp);
g2d.setColor(Color.black);
g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().y);
}else if(i >= 30 && i < 40){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height);
g2d.fill(temp);
}
}else if(spots[i] instanceof Railroad){
if(i == 5)
g2d.drawImage(imgTrain3, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null);
else if(i == 15)
g2d.drawImage(imgTrain4, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null);
else if(i == 25)
g2d.drawImage(imgTrain1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null);
else if(i == 35)
g2d.drawImage(imgTrain2, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null);
}else if(spots[i] instanceof Chance){
if(i == 7)
g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y, null);
else if(i == 22)
g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null);
else if(i == 36)
g2d.drawImage(imgChance3, spots[i].getRect().x, spots[i].getRect().y, null);
}else if(spots[i] instanceof Community){
if(i == 2)
g2d.drawImage(imgComm1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null);
else if(i == 17)
g2d.drawImage(imgComm2, spots[i].getRect().x, spots[i].getRect().y, null);
else if(i == 33)
g2d.drawImage(imgComm3, spots[i].getRect().x+spots[i].getRect().width/3, spots[i].getRect().y, null);
}else{
g2d.setColor(spots[i].getColor());
g2d.fill(spots[i].getRect());
}
}
//Draw the outline of every spot
g2d.setColor(Color.black);
for(Spot index : spots)
g2d.draw(index.getRect());
//Draw the outline of the whole board
g2d.drawRect(newX, newY, boardSize, boardSize);
//Draw the Players location
for(Player index : players)
g2d.drawImage(index.getImage(), index.getLoc().x, index.getLoc().y, null);
}
基本上一噸的文本,用於代表董事會的,而這個正在做,每次董事會重繪。有什麼建議?
獎勵問題:我也剛剛開始爲玩家在移動之後開始製作移動動畫(目前只是跳到目的地)。我創造了一個計時器,每次擲出1秒(例如:如果擲出5秒,需要5秒鐘移動)。唯一的問題是,我不確定如何顯示玩家片從開始位置到結束位置的緩慢移動。只需要有人給我一個基本的想法,這樣我就可以朝正確的方向前進。
+1,太棒了。我只是在尋找一個讓我開始的想法..壟斷遊戲對我的新項目非常出色。 – ApprenticeHacker 2012-03-04 14:03:46
謝謝!這絕對是一個有趣的遊戲開發和觀看,因爲它們都彙集在一起。 – mbreen 2012-03-04 14:15:10