java 实现贪吃蛇 王校长吃面包版
package 贪吃蛇带音乐版;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Game extends JFrame implements KeyListener,Runnable{
private int score=0;
private int leftX=0;
private int leftY=0;
private int overtime=500;
private int counttime=0;
private int headX=2;
private int headY=2;
private int flag=1;
private int width=0;
private int height=0;
private int [][] map=null;
private int head_dir=4;
private Image [] image=null;
private ArrayList<node> snake=new ArrayList<node>();
Music music;
Thread t;
final int wall=1,space=0,food=2,
HEADUP=3,HEADDOWN=4,HEADLEFT=5,HEADRIGHT=6,
BODYUP=7,BODYDOWN=8,BODYLEFT=9,BODYRIGHT=10,
UP=1,DOWN=2,LEFT=3,RIGHT=4;
Game(){
super("贪吃蛇带音乐版");
this.setVisible(true);
this.setSize(600, 600);
setResizable(false);
setLocation(800, 20);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setBackground(Color.YELLOW);
Container con=this.getContentPane();
con.setLayout(null);
// con.setBackground(Color.white);
this.setBackground(Color.yellow);
getImage();
width=this.getWidth();
height=this.getHeight();
this.setFocusable(true);
initMap();
this.addKeyListener(this);
// repaint();
t=new Thread(this);
t.start();
music=new Music("G:\\eclipse安装包\\Java工程文件\\贪吃蛇带音乐版\\素材\\音乐素材\\初音宝宝 - 甩葱歌.mp3");
music.loop();
}
void getImage() {
image=new Image;
for(int i=0;i<11;i++)
{
image=Toolkit.getDefaultToolkit().getImage("G:\\eclipse安装包\\Java工程文件\\贪吃蛇带音乐版\\素材\\图像素材\\snake"+i+".jpg");
}
}
void get_headXY(){
int i=1;
int j=1;
for(;i<11;i++) {
for(j=1;j<11;j++)
{
if(map==HEADUP||map==HEADDOWN||map==HEADLEFT||map==HEADRIGHT)
{
headX=i;
headY=j;
snake.add(new node(headX,headY,map));
break;
}
}
}
}
public void paint(Graphics g) {
// System.out.println("我被调用了");
int i=0;
int j=0;
printMap();
for(i=0;i<12;i++) {
for(j=0;j<12;j++) {
g.drawImage(image], leftX+j*30, leftY+i*30,30,30,this);
}
}
g.clearRect(120, 60 , 360, 40);
g.setColor(Color.RED);
g.setFont(new Font("楷体_2312",Font.BOLD,30));
g.drawString("现在分数:", 180, 90);
g.drawString("得分为:"+String.valueOf(score),300,90);
}
public void initMap() {
map=new Map().getMap();
int i=0;
int j=0;
//
snake.clear();
head_dir=4;
flag=1;
int [][] tem=map;
for(i=0;i<tem.length;i++)
{
for(j=0;j<tem.length;j++)
System.out.print(tem+" ");
System.out.println();
}
// System.out.println("map___________________________________________________________________");
// for(;i<map.length;i++)
// {
//
// for(j=0;j<map.length;j++)
// System.out.print(map+" ");
// System.out.println();
// }
get_left_XY();
get_headXY();
}
private void get_left_XY() {
// TODO Auto-generated method stub
leftX=(width-map.length*30)/2;
leftY=(height-map.length*30)/2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Game();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
if(counttime<overtime) {
counttime+=50;
}
else
{
counttime=0;
move();
repaint();
}
try {
if(flag==2)
{
judge();
}
else
{
Thread.sleep(50);
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_A)
{
if(overtime>200)
overtime-=100;
}
if(e.getKeyCode()==KeyEvent.VK_D) {
if(overtime<1000)
overtime+=100;
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
if(map==HEADDOWN)
return;
head_dir=UP;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
System.out.println(snake.size());
System.out.println(snake.get(0).x+snake.get(0).y);
if(map==HEADUP)
return;
head_dir=DOWN;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
if(map==HEADRIGHT)
return;
head_dir=LEFT;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
if(map==HEADLEFT)
return;
head_dir=RIGHT;
}
// repaint();
if(flag==2){
String msg = "游戏结束,你的得分是"+score+"\n是否重来?";
int type = JOptionPane.YES_NO_OPTION;
String title = "Game Over";
int choice = 0;
choice = JOptionPane.showConfirmDialog(null, msg,title,type);
if(choice==1){
System.exit(0);
}else if(choice == 0){
initMap();
score=0;
counttime = 0;
overtime = 500;
repaint();
}
}
}
public void move() {
ArrayList<node> temp=new ArrayList<node>();
int i=0;
if(snake.size()==0)
return ;
else
{
System.out.println("蛇的大小:"+snake.size());
int x=snake.get(0).x;
int y=snake.get(0).y;
switch(head_dir) {
case UP :x=x-1 ;break;
case DOWN :x=x+1 ;break;
case LEFT :y=y-1 ;break;
case RIGHT:y=y+1 ;break;
}
if(map==wall||map==BODYUP||map==BODYDOWN||map==BODYLEFT||map==BODYRIGHT)
{
flag=2;
}
else
if(map==food)
{
score++;
resetFood();
switch(head_dir) {
case UP:
temp.add(new node(x,y,HEADUP));
map=HEADUP;
break;
case DOWN:
temp.add(new node(x,y,HEADDOWN));
map=HEADDOWN;
break;
case LEFT:
temp.add(new node(x,y,HEADLEFT));
map=HEADLEFT;
break;
case RIGHT:
temp.add(new node(x,y,HEADRIGHT));
map=HEADRIGHT;
break;
}
for(i=0;i<snake.size();i++)
{
if(i==0) {
switch(snake.get(i).dir) {
case HEADUP :snake.get(i).dir=BODYUP ;break;
case HEADDOWN :snake.get(i).dir=BODYDOWN ;break;
case HEADLEFT :snake.get(i).dir=BODYLEFT ;break;
case HEADRIGHT:snake.get(i).dir=BODYRIGHT;break;
}
}
temp.add(snake.get(i));
map = snake.get(i).dir;
}
snake=temp;
}
else
{
switch(head_dir) {
case UP:
temp.add(new node(x,y,HEADUP));
map=HEADUP;
break;
case DOWN:
temp.add(new node(x,y,HEADDOWN));
map=HEADDOWN;
break;
case LEFT:
temp.add(new node(x,y,HEADLEFT));
map=HEADLEFT;
break;
case RIGHT:
temp.add(new node(x,y,HEADRIGHT));
map=HEADRIGHT;
break;
}
for(i=0;i<snake.size()-1;i++)
{
if(i==0) {
switch(snake.get(i).dir) {
case HEADUP :snake.get(i).dir=BODYUP ;break;
case HEADDOWN :snake.get(i).dir=BODYDOWN ;break;
case HEADLEFT :snake.get(i).dir=BODYLEFT ;break;
case HEADRIGHT:snake.get(i).dir=BODYRIGHT;break;
}
}
temp.add(snake.get(i));
map=snake.get(i).dir;
}
map=0;
snake=temp;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void printMap() {
int i;
int j;
for(i=0;i<map.length;i++) {
for(j=0;j<map.length;j++)
{
System.out.print(map+" ");
}
System.out.println();
}
System.out.println("______________________________________________");
}
public void resetFood()
{
int i;
int j;
do {
i=(int)(Math.random()*10)+1;
j=(int)(Math.random()*10)+1;
}while(map!=0);
map=food;
}
public void judge()
{
if(flag==2) {
int option=JOptionPane.YES_NO_OPTION;
int choice;
String message="游戏结束,你的得分为:"+score+"分,是否重来?";
choice=JOptionPane.showConfirmDialog(null, message,"游戏结束",option);
if(choice==1)
{
System.exit(0);
}
else
if(choice==0)
{
score=0;
initMap();
System.out.println("暂停ssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
flag=1;
counttime=0;
overtime=500;
repaint();
}
}
}
}
有想要代码素材包的,私聊。 谢谢分享 呵呵。。。。。。 很有意思的样子 可以参考一下 我自己也做一个 这个 图片很可爱啊 野生程序员 7kjccom 发表于 2019-2-17 15:32
野生程序员
何出此言? 哈哈, nb, 逗乐我了;www 有趣,看起来挺好玩
页:
[1]
2