java编写贪吃蛇小游戏_java

废话不多说,直接奉上代码:

Frame.java

package snake;

import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Frame extends JFrame implements KeyListener {

  /**
   *
   */

  Boolean isAlive;
  Boolean isPause;
  Panel panel;
  Character direction;
  private static final long serialVersionUID = 1L;

  public Frame(){
    // TODO Auto-generated constructor stub
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,300);
    addKeyListener(this);
    panel = new Panel();
    add(panel);
    setVisible(true);
    isAlive = true;
    isPause = false;
    direction = new Character('d');
    MenuBar menuBar = new MenuBar();
    Menu menu = new Menu("menu");
    MenuItem reset = new MenuItem("newgame");
    MenuItem pause= new MenuItem("pause");

    pause.addActionListener(new ActionListener(){

      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(!isPause) isPause= true;
        else     isPause= false;
      }

    }); 

    reset.addActionListener(new ActionListener(){

      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        reset();
      }

    });

    menu.add(reset);
    menu.add(pause);
    menuBar.add(menu);
    setMenuBar(menuBar);

  }

  public void reset(){
    panel.reset();
    isAlive = true;
  }

  @Override
  public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

  }

  @Override
  public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode()==KeyEvent.VK_UP)  direction = 'w';
    if(e.getKeyCode()==KeyEvent.VK_DOWN) direction = 's';
    if(e.getKeyCode()==KeyEvent.VK_LEFT) direction = 'a';
    if(e.getKeyCode()==KeyEvent.VK_RIGHT) direction = 'd';
  }

  @Override
  public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

  }

  public void paint(Graphics g){
    panel.repaint();
  }

}

Launch.java

package snake;

import java.util.Timer;
import java.util.TimerTask;

public class Launch extends TimerTask {

  Frame frame = new Frame();
  public Launch() {
    // TODO Auto-generated constructor stub

  }

  boolean crashWall(){
    SnakeBody sb = frame.panel.snake.getFirst();
    if((sb.x<0)||(sb.y<0)||(sb.x>=Panel.LINE)||(sb.y>=Panel.LINE))
      return true;
    else
      return false;
  }
  void initial(){
    frame.panel.snake.add(newBody());
    frame.panel.food = newBody();
  }
  @Override
  public void run() {
    // TODO Auto-generated method stub
    if(frame.panel.snake.isEmpty())
      initial();
    if(frame.isAlive)
      if(!frame.isPause){
        if(goStraight())
           frame.isAlive = false;
        frame.repaint();
      }
    if(crashWall()) frame.isAlive = false;
  }

  SnakeBody newBody(){
    SnakeBody sb = new SnakeBody();

    boolean overlap = true;
    while(overlap){
      overlap =false;
      sb.x = (int) (Math.random()*(Panel.LINE-2)+1);
      sb.y = (int) (Math.random()*(Panel.LINE-2)+1);
      if(!frame.panel.snake.isEmpty())
      for(SnakeBody s : frame.panel.snake)
        if(sb.equals(s))
          overlap =true;
    }

    return sb;
  }

  void eat(SnakeBody sb){
    frame.panel.snake.addFirst(sb);
  }
  boolean goStraight(){

    boolean result = false;

    SnakeBody sb =new SnakeBody(frame.panel.snake.getFirst());
    frame.panel.snake.removeLast();
    if(frame.direction=='w')
      sb.turnUp();
    if(frame.direction=='s')
      sb.turnDown();
    if(frame.direction=='a')
      sb.turnLeft();
    if(frame.direction=='d')
      sb.turnRight();

    for(SnakeBody s : frame.panel.snake){
      if(sb.equals(s)) result = true;
    }

    frame.panel.snake.addFirst(sb);
    if(sb.equals(frame.panel.food)){
      if(frame.direction=='w')
        frame.panel.food.turnUp();
      if(frame.direction=='s')
        frame.panel.food.turnDown();
      if(frame.direction=='a')
        frame.panel.food.turnLeft();
      if(frame.direction=='d')
        frame.panel.food.turnRight();
      eat(frame.panel.food);
      frame.panel.food = newBody();
    }

    return result;
  }

  public static void main(String[] args){
    // TODO Auto-generated method stub
     Launch timertask = new Launch();
     Timer timer = new Timer();

     timer.schedule(timertask,0,500);
  }

}

Panel.java

package snake;

import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;

import javax.swing.JPanel;

 public class Panel extends JPanel {

    /**
   *
   */
  private static final long serialVersionUID = 1L;
  public LinkedList<SnakeBody> snake = new LinkedList<SnakeBody>();
  static final int LINE = 10;
  SnakeBody food = new SnakeBody(-99,-99);

  public Panel() {
    // TODO Auto-generated constructor stub
  }

  public void reset(){
    snake.clear();
  }

  public void paint(Graphics g){
    g.setColor(Color.white);
    g.fillRect(0, 0, getWidth(), getHeight());

    for(SnakeBody sb : snake){
      g.setColor(Color.black);
      g.drawRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
      g.setColor(Color.orange);
      g.fillRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
    }
    g.setColor(Color.red);
    g.fillRect(food.x*getWidth()/LINE,food.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);

  }
}

SnakeBody.java

package snake;

 class SnakeBody {

  int x;
  int y;

  public SnakeBody() {
    // TODO Auto-generated constructor stub
    x = 0;
    y = 0;
  }

  public SnakeBody(int a,int b){
    x = a;
    y = b;
  }
  public SnakeBody(SnakeBody sb){
    this(sb.x,sb.y);
  }

  public void turnUp(){
    y--;
  }
  public void turnDown(){
    y++;
  }
  public void turnLeft(){
    x--;
  }
  public void turnRight(){
    x++;
  }

  boolean equals(SnakeBody s){
    if((x==s.x)&&(y==s.y)) return true;
    else          return false;
  }

}

以上所述就是本文给大家分享的贪吃蛇的全部代码了,希望能够对大家熟练掌握java有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索java
, 贪吃蛇
小游戏
java贪吃蛇小游戏代码、java设计贪吃蛇小游戏、java小游戏贪吃蛇、java编写贪吃蛇代码、java编写贪吃蛇,以便于您获取更多的相关知识。

时间: 2024-11-02 04:32:46

java编写贪吃蛇小游戏_java的相关文章

Java编写猜数字小游戏_java

本文实例讲述了java实现的简单猜数字游戏代码.分享给大家供大家参考. 以下是Java语言写的一个猜数字小游戏引用片段: import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; //主函数 public class calssOne { public static void main(String[] args) { //shit+Ctrl+o int result; //随机产生一个

javascrpt加html实现的贪吃蛇小游戏

javascrpt有效代码17行 加上html代码的话,共25行 运行方法chrome或者 firefox 测试连接 http://lufylegend.com/html5/lufylegend/tcs.html 完整代 码如下 <!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="240" height="240" style="

php实现贪吃蛇小游戏_php实例

贪吃蛇游戏是经典手机游戏,既简单又耐玩.通过控制蛇头方向吃蛋,使得蛇变长,从而获得积分.在诺基亚时代,风靡整个手机界,今天我们来看看另类的,如何使用php来实现贪吃蛇小游戏 废话不多说,代码奉上: control.php <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> <body>  <a href="control.php

c++-qt做的贪吃蛇小游戏,构建的时候有问题,运行不了

问题描述 qt做的贪吃蛇小游戏,构建的时候有问题,运行不了 包含界面文件源文件头文件,可是在运行的时候构建不成功,说只完成了2之中的1个步骤. 15:02:08: 为项目scoff_snake执行步骤 ... 15:02:08: 正在启动 "D:qt4.8.4binqmake.exe" D:qtpujing新建文件夹scoff_snake.pro -r -spec win32-g++ Cannot find file: d:qtpujing新建文件夹scoff_snake.pro. 1

纯js和css完成贪吃蛇小游戏demo_javascript技巧

本文实例为大家分享了js贪吃蛇小游戏demo,纯js和css完成,供大家参考,具体内容如下 <!doctype html> <html> <meta charset="utf-8"> <head> <style> *{ margin: 0; padding:0; } .content{ position: absolute; width: 500px; height: 500px; background-color: #212

C++基于控制台实现的贪吃蛇小游戏_C 语言

本文实例讲述了C++基于控制台实现的贪吃蛇小游戏.分享给大家供大家参考.具体实现方法如下: #include <windows.h> #include <time.h> #include <stdio.h> #define MAX 100 #define UP 1 #define DOWN 2 #define LEFT 3 #define RIGHT 4 #define MOVING 5 #define STOP 0 HANDLE hMain_Out = NULL; H

php实现贪吃蛇小游戏

贪吃蛇游戏是经典手机游戏,既简单又耐玩.通过控制蛇头方向吃蛋,使得蛇变长,从而获得积分.在诺基亚时代,风靡整个手机界,今天我们来看看另类的,如何使用php来实现贪吃蛇小游戏 废话不多说,代码奉上: control.php <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> <body>  <a href="control.php

Java完美实现2048小游戏_java

完美地模仿了2048游戏,是根据网友的一个2048改的. Block.java import javax.swing.*; import java.awt.*; public class Block extends JLabel { private int value; public Block() { value = 0;//初始化值为0 setFont(new Font("font", Font.PLAIN, 40));//设定字体 setBackground(Color.gray

c#写的贪吃蛇小游戏

下面是游戏的源代码.有两个文件:Main.cs 和 Snake.cs //// Main.cs Begin//using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Threading; namespace GreedySnake{ ///  /// formMai