当前位置:文档之家› Java贪吃蛇游戏源代码

Java贪吃蛇游戏源代码

Java贪吃蛇游戏源代码
Java贪吃蛇游戏源代码

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.KeyStroke;

public class 贪吃蛇extends JFrame implements ActionListener, KeyListener,Runnable { /**

*

*/

private static final long serialVersionUID = 1L;

private JMenuBar menuBar;

private JMenu youXiMenu,nanDuMenu,fenShuMenu,guanYuMenu;

private JMenuItem kaiShiYouXi,exitItem,zuoZheItem,fenShuItem;

private JCheckBoxMenuItem cJianDan,cPuTong,cKunNan;

private int length = 6;

private Toolkit toolkit;

private int

i,x,y,z,objectX,objectY,object=0,growth=0,time;//bojectX,Y private int m[]=new int[50];

private int n[]=new int[50];

private Thread she = null;

private int life=0;

private int foods = 0;

private int fenshu=0;

public void run(){

time=500;

for(i=0;i<=length-1;i++) {

m[i]=90-i*10;n[i]=60;

}

x=m[0];

y=n[0];

z=4;

while(she!=null)

{

check();

try

{

Thread.sleep(time);

}

catch(Exception ee)

{

System.out.println(z+"");

}

}

}

public 贪吃蛇() {

setVisible(true);

menuBar = new JMenuBar();

toolkit=getToolkit();

youXiMenu = new JMenu("游戏"); kaiShiYouXi = new JMenuItem("开始游戏"); exitItem = new JMenuItem("退出游戏");

nanDuMenu = new JMenu("困难程度"); cJianDan = new JCheckBoxMenuItem("简单"); cPuTong = new JCheckBoxMenuItem("普通"); cKunNan = new JCheckBoxMenuItem("困难");

fenShuMenu = new JMenu("积分排行"); fenShuItem = new JMenuItem("最高记录");

guanYuMenu = new JMenu("关于");

zuoZheItem = new JMenuItem("关于作者");

guanYuMenu.add(zuoZheItem);

nanDuMenu.add(cJianDan); nanDuMenu.add(cPuTong); nanDuMenu.add(cKunNan); fenShuMenu.add(fenShuItem); youXiMenu.add(kaiShiYouXi); youXiMenu.add(exitItem);

menuBar.add(youXiMenu);

menuBar.add(nanDuMenu);

menuBar.add(fenShuMenu);

menuBar.add(guanYuMenu); zuoZheItem.addActionListener(this); kaiShiYouXi.addActionListener(this); exitItem.addActionListener(this); addKeyListener(this);

fenShuItem.addActionListener(this);

KeyStroke keyOpen =

KeyStroke.getKeyStroke('O',InputEvent.CTRL_DOWN_MASK); kaiShiYouXi.setAccelerator(keyOpen);

KeyStroke keyExit =

KeyStroke.getKeyStroke('X',InputEvent.CTRL_DOWN_MASK); exitItem.setAccelerator(keyExit);

setJMenuBar(menuBar);

setTitle("贪吃蛇");

setResizable(false);

setBounds(300,200,400,400);

validate();

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String args[]) {

new 贪吃蛇();

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==kaiShiYouXi)

{

length = 6;

foods = 0;

if(she==null)

{

she=new Thread(this);

she.start();

}

else if(she!=null)

{

she=null;

she= new Thread(this); she.start();

}

}

if(e.getSource()==exitItem) {

System.exit(0);

}

if(e.getSource()==zuoZheItem)

{

JOptionPane.showMessageDialog(this, "孤独的野狼制作"+"\n\n"+" "+"QQ 号:2442701497"+"\n");

}

if(e.getSource()==fenShuItem)

{

JOptionPane.showMessageDialog(this,"最高记录为"+fenshu+"");

}

}

public void check(){

isDead();

if(she!=null)

{

if(growth==0)

{

reform(); //得到食物

}

else

{

upgrowth(); //生成食物

}

if(x==objectX&&y==objectY)

{

growth=1;

toolkit.beep();

}

if(object==0)

{

object=1;

objectX=(int)Math.floor(Math.random()*39)*10; objectY=(int)Math.floor(Math.random()*29)*10+50; }

this.repaint(); //重绘}

}

void isDead()

{

//判断游戏是否结束的方法if(z==4)

{

x=x+10;

}

else if(z==3)

{

x=x-10;

}

else if(z==2)

{

y=y+10;

}

else if(z==1)

{

y=y-10;

}

if(x<0||x>390||y<50||y>390)

{

she=null;

}

for(i=1;i

{

if(m[i]==x&&n[i]==y)

{

she=null;

}

}

继续阅读

FLASH游戏之贪吃蛇游戏源代码

scorecommand("allowscale", "false"); fscommand("showmenu", "false"); loadedbytes=0; total=_root.getBytesTotal(); loadedbytes=_root.getBytesLoaded() if (loadedbytes==total){nextScene();} else{bfb=int((loadedbytes/total*100))+"%";} stop(); fscommand("allowscale", "false"); fscommand("showmenu", "false"); music=new Sound() music.attachSound("music"); music.stop() music.start(0,10000) musicstart=true onClipEvent(keyDown){ temp=Key.getCode(); if(!keyon){;} else if(game=="stop" and (temp==189 or temp==109)){ if (speed>1) { speed--; rate = speed;}} else if (game=="stop" and (temp==107 or temp==187)) { if (speed"start"){enter(); }else if(temp==Key.ENTER){ enter(); }else if (temp == Key.DOWN and y1>=0) { x1=0; y1 = 1; walk(); timestart() }else if (temp == Key.UP and y1<=0) { x1=0; y1 = -1; walk(); timestart() }else if (temp == Key.LEFT and x1<=0) {

汇编语言写的贪吃蛇小游戏源代码

DATA SEGMENT dw 0,0 snk db 1 blk db 32 food db 3 tal1 db 4 tal2 db 2 adrs db 5 len db ? pst db ? addrs dw ? frow db ? fcol db ? hwrt db ? gmov db 'game over press r to restart press q to quit $' score1 db 'score :$' score2 db ? score0 db 1 zero db 48 writer db 'Developer: Geniusdot $' email db ': geniusdotgmail.$' msg1 db 'The way to play the game:$' way db ' press w to up ,press s to down,press a to left,press d to right$' msg db 'Press any key(except a,s,d,w) to start$' DATA ENDS STACK SEGMENT stack db 200 dup(0) STACK ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA,SS:STACK start: mov ax,data mov ds,ax mov ax,0 mov es,ax mov frow,10 mov fcol,6 mov dh,10 mov dl,26 mov ah,2

int 10h mov ah,9 lea dx,msg1 int 21h mov dh,11 mov dl,7 mov ah,2 mov bh,0 int 10h mov ah,9 lea dx,way int 21h mov dh,12 mov dl,20 mov ah,2 mov bh,0 int 10h mov ah,9 lea dx,msg int 21h mov ah,0 int 16h mov ah,6 mov al,0 mov ch,0 mov cl,0 mov dh,24 mov dl,79 mov bh,10 int 10h mov dh,0 mov dl,0 mov ah,2 mov bh,0 int 10h mov ah,9 lea dx,score1 int 21h mov dl,15 mov ah,2 mov bh,0 int 10h

贪吃蛇游戏实现思路及源代码

HTML5 贪吃蛇游戏实现思路及源代码 点评:游戏难点是怎么模拟贪吃蛇的移动。如果只是一个方块的话显然很简单。但是当蛇的长度变长之后要怎么样控制,下面为大家简要介绍下具体的实现,感兴趣的朋友可以参考下,希望对大家有所帮助 游戏操作说明 通过方向键控制贪吃蛇上下左右移动。贪吃蛇吃到食物之后会变长一个长度。 游戏具体实现 游戏难点是怎么模拟贪吃蛇的移动。如果只是一个方块的话显然很简单。但是当蛇的长度变长之后要怎么样控制 每个方块的移动呢? 如果观察蛇的移动,可以发现,从蛇的头部到尾部,每个方块在下一时刻的位置就是它的前一个方块在当前时刻 的位置。因此我们需要做的只是控制贪吃蛇的头部的运动。其他部分的位置都可以依次类推。 另外一个值得注意的问题是 贪吃蛇吃下食物之后,新增加的方块应该放在哪个位置。 答案就是在下一时刻,新增加的方块应该出现在当前时刻的尾部位置。 因此,在吃下食物之后应该在更新蛇的每个位置之前,增加一个方块,并且将其位置设定在当前时刻的尾部位置。 然后在当前时刻更新出了新增方块之外的所有方块的位置 index.html snake.js 复制代码代码如下: var canvas; var ctx; var timer; //measures var x_cnt = 15; var y_cnt = 15;

var unit = 48; var box_x = 0; var box_y = 0; var box_width = 15 * unit; var box_height = 15 * unit; var bound_left = box_x; var bound_right = box_x + box_width; var bound_up = box_y; var bound_down = box_y + box_height; //images var image_sprite; //objects var snake; var food; var food_x; var food_y; //functions function Role(sx, sy, sw, sh, direction, status, speed, image, flag) { this.x = sx; this.y = sy; this.w = sw; this.h = sh; this.direction = direction; this.status = status; this.speed = speed; this.image = image; this.flag = flag; } function transfer(keyCode) { switch (keyCode) { case 37: return 1; case 38: return 3; case 39: return 2; case 40: return 0; } } function addFood()

贪吃蛇小游戏源代码

#include #include #include #include int head=3 ,tail=0; int main() { int i,j,k=0; int zuobiao[2][80]; long start; int direction=77; int gamespeed; int timeover; int change(char qipan[20][80],int zuobiao[2][80],char direction); zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[ 1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4; /*处理棋盘*/ char qipan[20][80];//定义棋盘 for(i=0;i<20;i++) for(j=0;j<80;j++) qipan[i][j]=' ';//初始化棋盘 for(i=0;i<80;i++) qipan[0][i]='_'; for(i=0;i<20;i++) qipan[i][0]='|'; for(i=0;i<20;i++) qipan[i][79]='|'; for(i=0;i<80;i++) qipan[19][i]='_'; qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置 qipan[1][4]='#'; printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n"); printf("Input your game speed,please.(e.g.300)\n"); scanf("%d",&gamespeed); while(direction!='q') { system("cls"); for(i=0;i<20;i++)//打印出棋盘 for(j=0;j<80;j++) printf("%c",qipan[i][j]); timeover=1;

贪吃蛇游戏安卓源代码

附录1.SnakeView类 package com.example.android_snake.view; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import com.example.android_snake.R; import com.example.android_snake.food.Food; import com.example.android_snake.snake.Body; import com.example.android_snake.snake.Head; import com.example.android_snake.snake.Snake; import com.example.android_snake.snake.SnakeDirection; import com.example.android_snake.stone.Stone; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.os.Handler; import android.util.DisplayMetrics; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.ViewManager; import android.view.WindowManager; import android.widget.Toast; public class SnakeView extends View { private Context context; private Bitmap headBitmap; private Bitmap bodyBitmap; private Bitmap foodBitmap; private Bitmap stoneBitmap; // 屏幕的高度和宽度 private int screenHeight; private int screenWidth; // 每个小格子的高度和宽度 private int eachHeight;

html5实现贪吃蛇源代码

Insert title here