你好,我最近剛剛在看關於如何在java中創建一個蛇遊戲的教程。 這是它,如果你想去看看http://www.youtube.com/watch?v=07UdiMlvDcI這是第4部分,他有更多的部分。我如何改變這個蛇遊戲的背景顏色?
我複製了他的教程,但我想添加一些我自己的東西。我只是想知道是否有人知道如何將背景改爲黑色。我是一個java的大白菜,需要很多幫助。還有其他一些我想知道如何去做的例子。如何將Highscore或Score文本更改爲BOLD或將其放置在不同的位置。也不會介意知道如何改變頭部的顏色或用我下載的圖像替換頭部。另一件事是我如何改變窗口的大小,當我改變尺寸它不會改變任何東西。底部的第二堂課對你們來說可能更有用,因爲那些教程小夥子製作小程序的地方。任何幫助將不勝感激。
CODE:
public class SnakeCanvas extends Canvas implements Runnable, KeyListener{
private final int BOX_HEIGHT = 10;
private final int BOX_WIDTH = 10;
private final int GRID_WIDTH = 25;
private final int GRID_HEIGHT = 25;
private LinkedList<Point> snake;
private Point fruit;
private int direction = Direction.NO_DIRECTION;
private Thread runThread;
private int score = 0;
private String highscore = "";
public void init() {
}
public void paint(Graphics g) {
if (snake == null) {
snake = new LinkedList<Point>();
GenerateDefaultSnake();
PlaceFruit();
}
if (runThread == null) {
this.setPreferredSize(new Dimension(640, 480));
this.addKeyListener(this);
runThread = new Thread(this);
runThread.start();
}
if (highscore.equals("")) {
highscore = this.GetHighScore();
}
drawFruit(g);
drawGrid(g);
drawSnake(g);
drawScore(g);
}
public void update(Graphics g) {
Graphics offScreenGraphics; //Graphics used to draw offscreen
BufferedImage offscreen = null;
Dimension d = this.getSize();
offscreen = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
offScreenGraphics = offscreen.getGraphics();
offScreenGraphics.setColor(this.getBackground());//CHANGE BACKGROUND COLOR
offScreenGraphics.fillRect(0, 0, d.width, d.height);
offScreenGraphics.setColor(this.getForeground());
paint(offScreenGraphics);
//flip
g.drawImage(offscreen, 0, 0, this);
}
public void GenerateDefaultSnake() {
score = 0;
snake.clear();
snake.add(new Point(0,2));
snake.add(new Point(0,1));
snake.add(new Point(0,0));
direction = Direction.NO_DIRECTION;
}
public void Move() {
Point head = snake.peekFirst();
Point newPoint = head;
switch(direction) {
case Direction.NORTH:
newPoint = new Point(head.x, head.y - 1);
break;
case Direction.SOUTH:
newPoint = new Point (head.x, head.y + 1);
break;
case Direction.WEST:
newPoint = new Point (head.x - 1, head.y);
break;
case Direction.EAST:
newPoint = new Point (head.x + 1, head.y);
break;
}
snake.remove(snake.peekLast());
if(newPoint.equals(fruit)){
score+=10;
//the snake has hit fruit
Point addPoint = (Point) newPoint.clone();
switch(direction){
case Direction.NORTH:
newPoint = new Point(head.x, head.y-1);
break;
case Direction.SOUTH:
newPoint = new Point (head.x, head.y+1);
break;
case Direction.WEST:
newPoint = new Point (head.x-1, head.y);
break;
case Direction.EAST:
newPoint = new Point (head.x+1, head.y);
break;
}
snake.push(addPoint);
PlaceFruit();
}
else if(newPoint.x < 0 || newPoint.x > GRID_WIDTH - 1){
//we went out of bounce, reset game
CheckScore();
GenerateDefaultSnake();
return;
}
else if(newPoint.y < 0 || newPoint.y > GRID_HEIGHT - 1){
//we went out of bounce, reset game
CheckScore();
GenerateDefaultSnake();
return;
}
else if(snake.contains(newPoint)){
//we ran into ourselves, reset game
CheckScore();
GenerateDefaultSnake();
return;
}
//if we reach this point in code, we're still alive
snake.push(newPoint);
}
public void drawScore(Graphics g){
g.drawString("Score: " + score, 0, BOX_HEIGHT * GRID_HEIGHT + 10);
g.drawString("HighScore: " + highscore, 0, BOX_HEIGHT * GRID_HEIGHT + 20);
}
public void CheckScore(){
if(highscore.equals(""))
return;
if(score > Integer.parseInt((highscore.split(":")[1]))){
String name = JOptionPane.showInputDialog("NEW HIGHSCORE! PLEASE ENTER YOUR NAME.");
highscore = name + ":" + score;
File scoreFile = new File("highscore.dat");
if(!scoreFile.exists()){
try{
scoreFile.createNewFile();
}
catch(Exception e){
e.printStackTrace();
}
}
FileWriter writeFile = null;
BufferedWriter writer = null;
try{
writeFile = new FileWriter(scoreFile);
writer = new BufferedWriter(writeFile);
writer.write(this.highscore);
}
catch(Exception e){
}
finally{
try{
if(writer !=null)
writer.close();
}
catch(Exception e){}
}
}
}
public void drawGrid(Graphics g){
//drawing an outside rectangle
g.drawRect(0, 0, GRID_WIDTH * BOX_WIDTH, GRID_HEIGHT * BOX_HEIGHT);
}
public void drawSnake(Graphics g){
g.setColor(Color.GREEN);
for (Point p: snake){
g.fillRect(p.x * BOX_WIDTH, p.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
}
g.setColor(Color.BLACK);
}
public void drawFruit(Graphics g){
g.setColor(Color.RED);
g.fillOval(fruit.x * BOX_WIDTH, fruit.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.BLACK);
}
public void PlaceFruit(){
Random rand = new Random();
int randomX = rand.nextInt(GRID_WIDTH);
int randomY = rand.nextInt(GRID_HEIGHT);
Point randomPoint = new Point(randomX, randomY);
while(snake.contains(randomPoint)){
randomX = rand.nextInt(GRID_WIDTH);
randomY = rand.nextInt(GRID_HEIGHT);
randomPoint = new Point (randomX, randomY);
}
fruit = randomPoint;
}
@Override
public void run(){
while(true){//runs indefinitely
Move();
repaint();
try{
Thread.currentThread();
Thread.sleep(100);
}
catch(Exception e){
e.printStackTrace();
}
}
}
public String GetHighScore(){
FileReader readFile = null;
BufferedReader reader = null;
try{
readFile = new FileReader("highscore.dat");
reader = new BufferedReader(readFile);
return reader.readLine();
}
catch(Exception e){
return "Nobody:0";
}
finally{
try{
if(reader != null)
reader.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
@Override
public void keyTyped(KeyEvent e){
//TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
if (direction != Direction.SOUTH)
direction = Direction.NORTH;
break;
case KeyEvent.VK_DOWN:
if (direction != Direction.NORTH)
direction = Direction.SOUTH;
break;
case KeyEvent.VK_RIGHT:
if (direction != Direction.WEST)
direction = Direction.EAST;
break;
case KeyEvent.VK_LEFT:
if (direction != Direction.EAST)
direction = Direction.WEST;
break;
}
}
@Override
public void keyReleased(KeyEvent e){
}
}
另一個類:
public class snakeApplet extends Applet{
private SnakeCanvas c;
public void init(){
c = new SnakeCanvas();
c.setPreferredSize(new Dimension(640, 480));
c.setVisible(true);
c.setFocusable(true);
this.add(c);
this.setVisible(true);
this.setSize(new Dimension(640, 480));
}
public void paint(Graphics g){
this.setSize(new Dimension(640, 480));
}
}
IM真的希望有人能幫助我!
這個例子是一個不好的例子,如果繪畫規則違反了一個數字,並且會被今天的可用Apis認爲是過時的。字體修改繪畫方法中的組件的狀態或添加偵聽器 – MadProgrammer