java小游戏代码贪吃蛇
时间: 2024-07-17 09:00:39 浏览: 152
Java贪吃蛇是一种基于Java编写的小游戏,玩家通过控制蛇的移动,吃掉食物来增加分数,同时要注意避免蛇头碰到墙壁或者蛇身,否则游戏结束。下面是Java贪吃蛇的代码实现:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Snake extends JPanel implements ActionListener{
private int width = 300;
private int height = 300;
private int unitSize = 10;
private int units = (width*height)/unitSize;
private int delay = 100;
private int x[] = new int[units];
private int y[] = new int[units];
private int bodyParts = 6;
private int applesEaten = 0;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private Random random;
public Snake(){
random = new Random();
this.setPreferredSize(new Dimension(width, height));
this.setBackground(Color.black);
this.setFocusable(true);
this.addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame(){
newApple();
running = true;
timer = new Timer(delay, this);
timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g){
if(running){
g.setColor(Color.red);
g.fillOval(appleX, appleY, unitSize, unitSize);
for(int i=0; i<bodyParts; i++){
if(i==0){
g.setColor(Color.green);
}else{
g.setColor(new Color(45,180,0));
}
g.fillRect(x[i], y[i], unitSize, unitSize);
}
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: "+applesEaten, (width - metrics.stringWidth("Score: "+applesEaten))/2, g.getFont().getSize());
}else{
gameOver(g);
}
}
public void newApple(){
appleX = random.nextInt((int)(width/unitSize))*unitSize;
appleY = random.nextInt((int)(height/unitSize))*unitSize;
}
public void move(){
for(int i=bodyParts; i>0; i--){
x[i] = x[i-1];
y[i] = y[i-1];
}
switch(direction){
case 'U':
y = y - unitSize;
break;
case 'D':
y = y + unitSize;
break;
case 'L':
x = x - unitSize;
break;
case 'R':
x = x + unitSize;
break;
}
}
public void checkApple(){
if((x == appleX) && (y == appleY)){
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions(){
//check if head collides with body
for(int i=bodyParts; i>0; i--){
if((x == x[i]) && (y == y[i])){
running = false;
}
}
//check if head touches left border
if(x < 0){
running = false;
}
//check if head touches right border
if(x > width){
running = false;
}
//check if head touches top border
if(y < 0){
running = false;
}
//check if head touches bottom border
if(y > height){
running = false;
}
if(!running){
timer.stop();
}
}
public void gameOver(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics1 = getFontMetrics(g.getFont());
g.drawString("Score: "+applesEaten, (width - metrics1.stringWidth("Score: "+applesEaten))/2, g.getFont().getSize());
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 75));
FontMetrics metrics2 = getFontMetrics(g.getFont());
g.drawString("Game Over", (width - metrics2.stringWidth("Game Over"))/2, height/2);
}
@Override
public void actionPerformed(ActionEvent e) {
if(running){
move();
checkApple();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
if(direction != 'R'){
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if(direction != 'L'){
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if(direction != 'D'){
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if(direction != 'U'){
direction = 'D';
}
break;
}
}
}
}
```
阅读全文