你好,我刚刚看了一篇关于如何在java中创建蛇游戏的教程。 在这里,如果你想去看看http://www.youtube.com/watch?v=07UdiMlvDcI这是第4部分,他有更多的部分。
我复制了他的教程,但我想添加一些我自己的东西。我只是想知道你们中是否有人知道如何将背景改为黑色。我是java的大菜,需要很多帮助。还有其他我想知道如何做的事情就像是。如何将Highscore或Score文本更改为BOLD或将其放在其他位置。也不介意知道如何改变头部的颜色或用我下载的图像替换头部。另外一件事是我如何改变窗口的大小,当我改变尺寸时它不会改变任何东西。底部的第二个类对你们来说可能更有用,因为这是教程人员制作他的applet的地方。任何帮助将不胜感激。
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));
}
}
我非常希望有人可以帮助我!
答案 0 :(得分:0)
尝试添加this.setBackground(Color.BLACK);到SnakeCanvas类中的paint方法。如果这不起作用,请将相同的行添加到snakeApplet类。如果这不起作用,我很抱歉我没有成功。