java实现简单贪吃蛇小游戏
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了java实现贪吃蛇小游戏,供大家参考,具体内容如下
有两个类,放在同一文件下下即可,背景图片,标题,蛇尾可自行更改
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyJFrame extends JFrame{
public MyJFrame() {
int x= Toolkit.getDefaultToolkit().getScreenSize().width;
int y = Toolkit.getDefaultToolkit().getScreenSize().height;
setLocation((x-800)/2, (y-800)/2);
setSize(800,800);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("贪吃蛇");
setLayout(null);
getLayeredPane().setLayout(null);
JLabel background = new JLabel(new ImageIcon("C:\\Users\\HP\\Desktop\\javaa\\background3.jpg"));//注意此处路径
JLabel title = new JLabel(new ImageIcon("C:\\Users\\HP\\Desktop\\javaa\\title1.jpg"));//注意此处路径
background.setBounds(20,80,750,650);
title.setBounds(0,0,800,75);
getLayeredPane().add(background,new Integer(Integer.MIN_VALUE));
getLayeredPane().add(title,new Integer(Integer.MIN_VALUE+1));
((JPanel)getContentPane()).setOpaque(false);
MyPanel mypanel = new MyPanel();
mypanel.setBounds(0,0,getWidth(),getHeight());
add(mypanel);
}
public static void main(String[] args) {
new MyJFrame();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class MyPanel extends JPanel implements ActionListener{
int lenght ;
int score1;
Boolean jieshux = false;
JLabel scorel = new JLabel("分数:"+score1);
JLabel lenghl = new JLabel("长度:"+lenght);
JLabel shibai = new JLabel("按下空格开始游戏!");
JLabel jieshu = new JLabel("游戏失败!按空格重新开始!");
String dir = "R";
Boolean isstart = false;
int[][] snake = new int[300][2];
Random r = new Random();
int x,y;
//初始化
public void initialize() {
snake[0][0]=70;
snake[0][1]=80;
snake[1][0]=45;
snake[1][1]=80;
snake[2][0]=20;
snake[2][1]=80;
isstart = true;
jieshux = false;
lenght = 3;
score1 = 0;
dir = "R";
repaint();
}
public MyPanel() {
setLayout(null);
scorel.setBounds(700, 20, 50, 30);
lenghl.setBounds(700, 40, 50, 30);
shibai.setBounds(300,400,200,60);
shibai.setFont(new Font("黑体",Font.BOLD,20));
jieshu.setFont(new Font("黑体",Font.BOLD,20));
jieshu.setVisible(false);
jieshu.setBounds(250,400,300,60);
add(scorel);
add(lenghl);
add(shibai);
add(jieshu);
x = r.nextInt(29)*25+20;
y = r.nextInt(25)*25+80;
//this.setFocusable(true); //焦点获取
setOpaque(false);
initialize();
Timer t = new Timer(200,this);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_SPACE && isstart==false) {
isstart=true;
}else if(e.getKeyCode()==KeyEvent.VK_SPACE && isstart==true &&jieshux == false){
isstart = false;
}else if(e.getKeyCode()==KeyEvent.VK_W && isstart==true && dir!="D"&&jieshux == false) {
dir="U";
}else if(e.getKeyCode()==KeyEvent.VK_A && isstart==true && dir!="R"&&jieshux == false) {
dir="L";
}else if(e.getKeyCode()==KeyEvent.VK_D && isstart==true && dir!="L"&&jieshux == false) {
dir="R";
}else if(e.getKeyCode()==KeyEvent.VK_S && isstart==true && dir!="U"&&jieshux == false) {
dir="D";
}else if(e.getKeyCode()==KeyEvent.VK_SPACE && jieshux==true) {
initialize();
}
}
});
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
this.requestFocus(true);
if(isstart&&dir.equals("R")&&!jieshux) {
for(int i=0;i<lenght-1;i++) {
if(snake[i][0]>=745) {
//snake[i][0]=20;
jieshux = !jieshux;
//isstart = !isstart;
break;
}else {
snake[lenght-i-1][0]=snake[lenght-i-2][0];
snake[lenght-i-1][1]=snake[lenght-i-2][1];
}
}
if(!jieshux) snake[0][0]=snake[1][0]+25;
} else if(isstart&&dir.equals("D")&&!jieshux) {
for(int i=0;i<lenght-1;i++) {
if(snake[i][1]>=705) {
//snake[i][1]=80;
jieshux = !jieshux;
//isstart = !isstart;
break;
}else {
snake[lenght-i-1][0]=snake[lenght-i-2][0];
snake[lenght-i-1][1]=snake[lenght-i-2][1];
}
}
if(!jieshux) snake[0][1]=snake[1][1]+25;
}else if(isstart&&dir.equals("U")&&!jieshux) {
for(int i=0;i<lenght-1;i++) {
if(snake[i][1]<=80) {
//snake[i][1]=705;
jieshux = !jieshux;
//isstart = !isstart;
break;
}else {
snake[lenght-i-1][0]=snake[lenght-i-2][0];
snake[lenght-i-1][1]=snake[lenght-i-2][1];
}
}
if(!jieshux) snake[0][1]=snake[1][1]-25;
}else if(isstart&&dir.equals("L")&&!jieshux) {
for(int i=0;i<lenght-1;i++) {
if(snake[i][0]<=20) {
jieshux = !jieshux;
//isstart = !isstart;
break;
}else {
snake[lenght-i-1][0]=snake[lenght-i-2][0];
snake[lenght-i-1][1]=snake[lenght-i-2][1];
}
}
if(!jieshux) snake[0][0]=snake[1][0]-25;
}
if(snake[0][1]==y&&snake[0][0]==x) {
x = r.nextInt(29)*25+20;
y = r.nextInt(25)*25+80;
score1+=10;
lenght++;
snake[lenght-1][1]=snake[lenght-2][1];
snake[lenght-1][0]=snake[lenght-2][0];
add(scorel);
add(lenghl);
}
if(isstart==false) {
shibai.setVisible(true);
}else {
shibai.setVisible(false);
if(jieshux == false) {
jieshu.setVisible(false);
}else {
jieshu.setVisible(true);
}
}
for(int i = 1;i<lenght;i++) {
if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1]) {
jieshux=true;
}
}
if(isstart&&!jieshux)repaint();
}
public void paint(Graphics g) {
super.paint(g);
//边界为(20,80),(770,730)
scorel.setText("分数:"+score1);
lenghl.setText("长度:"+lenght);
g.drawLine(18, 78, 18, 732);
g.drawLine(18, 78, 772, 78);
g.drawLine(18, 732, 772, 732);
g.drawLine(772, 78, 772, 732);
//竖线
for(int i=20;i<=770;i+=25) {
g.drawLine(i,80,i,730);
//g.drawLine(0,i,600,i);
}
//横线
for(int i=80;i<=750;i+=25) {
//g.drawLine(i,50,i,780);
g.drawLine(20,i,770,i);
}
for(int i = 0;i<lenght;i++) {
if(i>0) {
g.drawImage(new ImageIcon("C:\\Users\\HP\\Desktop\\javaa\\body.png").getImage(), snake[i][0],snake[i][1],this);//注意此处路径
}else {
g.fillOval(snake[i][0], snake[i][1], 25, 25);
}
}
g.setColor(Color.red);
g.fillOval(x, y, 25, 25);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341