原标题: 用java开发贪吃蛇游戏
跟着疯狂软件课堂上轻课java公开课上学习的开发贪吃蛇游戏,比较简单。
开发环境为JDK和eclipse
实现的功能是蛇吃食物增加长度,游戏开始结束,键盘控制游戏开始暂停蛇的走向。
先附上效果图。
下面看代码:
父类
[java] view plain copy
public class Snake {
public static void main(String[] args) {
//画出白色窗口
JFrame frame=new JFrame();
frame.setBounds(10, 10, 900, 720);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//画布加到窗户里面
SnakePanel panel=new SnakePanel();
frame.add(panel);
//设置让其展现出来
frame.setVisible(true);
}
}
子类
[java] view plain copy
package snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakePanel extends JPanel implements KeyListener,ActionListener{
ImageIcon up=new ImageIcon("up.png");
ImageIcon down=new ImageIcon("down.png");
ImageIcon right=new ImageIcon("right.png");
ImageIcon left=new ImageIcon("left.png");
ImageIcon title=new ImageIcon("title.jpg");
ImageIcon food=new ImageIcon("food.png");
ImageIcon body=new ImageIcon("body.png");
int[] snakex=new int[750];
int[] snakey=new int[750];
//食物的坐标
Random rand=new Random();
int foodx=rand.nextInt(34)*25+25;
int foody=rand.nextInt(24)*25+75;
int len=3;
int score=0;
String fangxiang="R";
boolean isStarted=false;
//构造函数
boolean isFailed=false;
Timer timer=new Timer(100,this);
public SnakePanel(){
//设置可以获得焦点
this.setFocusable(true);
this.addKeyListener(this);
//调用setup
setup();
//启动timer
timer.start();
}
//画笔 Graphics
public void paint(Graphics g){
this.setBackground(Color.WHITE);
title.paintIcon(this, g, 25, 11);
g.fillRect(25, 75, 850, 600);
//画出蛇头
if(fangxiang.equals("R")){
right.paintIcon(this, g, snakex[0], snakey[0]);
}else if(fangxiang.equals("L")){
left.paintIcon(this, g, snakex[0], snakey[0]);
}else if(fangxiang.equals("U")){
up.paintIcon(this, g, snakex[0], snakey[0]);
}else if(fangxiang.equals("D")){
down.paintIcon(this, g, snakex[0], snakey[0]);
}
//画出蛇的身体
for(int i=1;i
body.paintIcon(this, g, snakex[i], snakey[i]);
}
if(!isStarted){
g.setColor(Color.WHITE);
//设置字体
g.setFont(new Font("arial",Font.BOLD,30));
//画字符串
g.drawString("Press Space to Start/Pause", 300, 300);
}
if(isFailed){
g.setColor(Color.WHITE);
//设置字体
g.setFont(new Font("arial",Font.BOLD,30));
//画字符串
g.drawString("Game over,Press Space to Start/Pause", 300, 300);
}
food.paintIcon(this, g, foodx, foody);
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.PLAIN,10));
g.drawString("Score:"+score,800, 30);
g.drawString("Length"+len, 800, 50);
}
public void setup(){
isStarted=false;
isFailed=false;
len=3;
snakex[0]=100;
snakey[0]=100;
snakex[1]=75;
snakey[1]=100;
snakex[2]=50;
snakey[2]=100;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_SPACE){
if(isFailed){
setup();
}else{
isStarted=!isStarted;
}
}else if(keyCode==KeyEvent.VK_UP&&fangxiang!="D"){
fangxiang="U";
}else if(keyCode==KeyEvent.VK_DOWN&&fangxiang!="U"){
fangxiang="D";
}else if(keyCode==KeyEvent.VK_RIGHT&&fangxiang!="L"){
fangxiang="R";
}else if(keyCode==KeyEvent.VK_LEFT&&fangxiang!="R"){
fangxiang="L";
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
//再定一个闹钟
timer.start();
//2.移动数据
//移动身体
if(isStarted&&!isFailed){
for(int i=len;i>0;i--){
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
//移动头
if(fangxiang.equals("R")){
snakex[0]=snakex[0]+25;
if(snakex[0]>850) snakex[0]=25;
}else if(fangxiang.equals("L")){
snakex[0]=snakex[0]-25;
if(snakex[0]<25)snakex[0]=850;
}else if(fangxiang.equals("U")){
snakey[0]=snakey[0]-25;
if(snakey[0]<75) snakey[0]=650;
}else if(fangxiang.equals("D")){
snakey[0]=snakey[0]+25;
if(snakey[0]>650) snakey[0]=75;
}
//吃食物
if(snakex[0]==foodx&&snakey[0]==foody){
len++;
score++;
foodx=rand.nextInt(34)*25+25;
foody=rand.nextInt(24)*25+75;
}
for(int i=1;i
if(snakex[0]==snakex[i]&&snakey[0]==snakey[i]){
isFailed=true;
}
}
}
//3.repaint()
repaint();
}
责任编辑: