package com.xxh.snake;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
/**
* 游戏界面
* 当按下一个键时会调用KeyPressed处理器,当松开一个键时会调用KeyReleased处理器,
* 当输入一个统一编码时会调用KeyTyped处理器。如果这个键不是统一码(如功能键,修改键,动作键和控制键)
* 每个按键事件有一个相关的按键字符和按键代码,分别由KeyEvent中的getKeyChar()和getKeyCode()方法返回
*/
public class Game extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 3982837981892208868L;
Sound sd = new Sound();
Map map = new Map();
Score se = new Score();
Food fd = new Food();
ReadAndWriter mt = new ReadAndWriter();
int snakex = 160;
int snakey = 220;
int snakeNumber = 3;
int movex = 0;
int movey = 0;
int fail_y = 520;
int score = 0;
int pause_y = 550;
int timeRow1 = 0;
int timeRow2 = 0;
int velocity = 200;
int mode;
int back_y = 60;
int foodNumber = 0;
int barrierMap = 1;
String strMap = "历险模式1";
boolean time = false;
boolean start = false;
private Image greenstar, greenstar1, greenstar2, greenstar3, greenstar4;
private Image score1;
private Image redstar;
private Image game_over;
private Image pause;
private Image grid1;
private Image snakebg;
private Image timeRow;
private Image wall;
private Image back;
private ImageIcon img1;
JButton bt1;
private LinkedList<Point> body = new LinkedList<>();
private LinkedList<Point> body1 = new LinkedList<>();// 为了解决贪食蛇撞到身体后出现的bug
// 构造方法
public Game() {
this.setLayout(null);
greenstar1 = new ImageIcon(getClass().getResource("/image/greenstar1.png")).getImage();
greenstar2 = new ImageIcon(getClass().getResource("/image/greenstar2.png")).getImage();
greenstar3 = new ImageIcon(getClass().getResource("/image/greenstar3.png")).getImage();
greenstar4 = new ImageIcon(getClass().getResource("/image/greenstar4.png")).getImage();
redstar = new ImageIcon(getClass().getResource("/image/redstar.png")).getImage();
game_over = new ImageIcon(getClass().getResource("/image/game_over.png")).getImage();
pause = new ImageIcon(getClass().getResource("/image/pause.png")).getImage();
grid1 = new ImageIcon(getClass().getResource("/image/grid1.png")).getImage();
snakebg = new ImageIcon(getClass().getResource("/image/snakebg.png")).getImage();
score1 = new ImageIcon(getClass().getResource("/image/score1.png")).getImage();
timeRow = new ImageIcon(getClass().getResource("/image/timerow0.png")).getImage();
wall = new ImageIcon(getClass().getResource("/image/wall.png")).getImage();
back = new ImageIcon(getClass().getResource("/image/back.png")).getImage();
img1 = new ImageIcon(getClass().getResource("/image/back.png"));
greenstar = greenstar4;
for (int a = 1; a < 4; a++) {
body.add(new Point(snakex - a * 20, snakey));
body1.add(new Point(snakex - a * 20, snakey));
}
bt1 = new JButton(img1);
this.add(bt1);
}
@Override
public void paint(Graphics g) {
super.paint(g);
// 背景
g.drawImage(snakebg, 0, 0, this);
// 网格
//g.drawImage(grid1, 0, 0, this);
// 分数
g.drawImage(score1, 0, 4, this);
g.drawImage(se.score2, 35 + 25 * se.score_x, 4, this);
g.drawImage(se.score3, 60 + 25 * se.score_x, 4, this);
g.drawImage(se.score4, 85 + 25 * se.score_x, 4, this);
g.drawImage(se.score5, 110 + 25 * se.score_x, 4, this);
// 进度条
g.drawImage(timeRow, 610, 448, this);
// 蛇头
g.drawImage(greenstar, snakex, snakey, this);
// 身体
for (Point p : body) {
g.drawImage(redstar, p.x, p.y, this);
}
// 食物
g.drawImage(fd.food, fd.foodx + 1, fd.foody, this);
// 地图
for (Point p : map.map) {
g.drawImage(wall, p.x, p.y, this);
}
// 游戏失败
g.drawImage(game_over, 200, fail_y, this);
// 游戏暂停
g.drawImage(pause, 240, pause_y, this);
// 返回按钮
g.drawImage(back, 780, 430 + back_y, this);
}
// 失败方法
public void fail() {
start = false;
snakex = 1000;// 蛇撞到自身会剩下蛇头
snakey = 1000;
if (body.size() > 0 && null != body)
body.remove(0);
if (fail_y == 520) {
sd.sound("fail");
back_y = 0;
bt1.setBounds(780, 430 + back_y, 47, 34);
}
if (fail_y <= 200) {
} else {
fail_y -= 5;
}
maxScore();
}
// 最高分判定与写入
public void maxScore() {
String str = mt.bufferedReader();
// 拆分字符串,获取元素,为了比较,转换为Integer类型
String[] newstr = str.split(",");
int a = Integer.parseInt(newstr[0]);
int b = Integer.parseInt(newstr[1]);
int c = Integer.parseInt(newstr[2]);
switch (mode) {
case 0:
if (score > a)
a = score;
break;
case 1:
if (score > b)
b = score;
break;
case 2:
if (score > c)
c = score;
break;
}
mt.bufferedWriter(a, b, c);
}
// 暂停功能
public void pause() {
if (pause_y == 550) {
pause_y = 170;
back_y = 0;
bt1.setBounds(780, 430 + back_y, 47, 34);
start = false;
} else {
pause_y = 550;
back_y = 60;
bt1.setBounds(780, 430 + back_y, 47, 34);
start = true;
}
}
// 进度条功能
public void timeRow() {
if (timeRow1 == 0) {
sd.sound("reward");
}
timeRow1 += 5;
if (timeRow1 % 10 == 0 && start) {
timeRow2++;
timeRow = new ImageIcon(getClass().getResource("/image/timerow" + timeRow2 + ".png")).getImage();
}
// 进度条读完,重新生成食物
if (timeRow2 == 16) {
time = false;
fd.food = fd.food1;
fd.foodx = (int) (Math.random() * 42) * 20;
fd.foody = (int) (Math.random() * 24) * 20;
timeRow1 = 0;
timeRow2 = 1;
timeRow = new ImageIcon(getClass().getResource("/image/timerow0.png")).getImage();
sd.sound("closeReward");
}
}
// 速度方法
public void velocity() {
if (foodNumber % 5 == 0)
velocity -= 20;
if (mode == 2 && foodNumber % 10 == 0)
changeMap();// 冒险模式下,更换地图
}
// 更换地图,重置贪食蛇坐标
public void changeMap() {
if (barrierMap == 4) {
barrierMap = 1;
} else {
barrierMap++;
}
strMap = "历险模式" + barrierMap;
map.map(strMap);
body.clear();
body1.clear();
snakex = 160;
snakey = 220;
movex = 20;
movey = 0;
greenstar = greenstar4;
}
// 重置游戏,因为不会点击按钮实现点击
没有合适的资源?快使用搜索试试~ 我知道了~
Java小游戏实现:贪吃蛇
共88个文件
png:67个
java:14个
wav:5个
需积分: 15 12 下载量 96 浏览量
2018-12-08
14:43:22
上传
评论 1
收藏 4MB RAR 举报
温馨提示
java 开发,不同模式,中途暂停,返回,最高得分, 下载导入eclipse ,idea运行Main类
资源推荐
资源详情
资源评论
收起资源包目录
snake.rar (88个子文件)
pom.xml 789B
src
test
java
main
resources
image
timerow10.png 20KB
timerow6.png 21KB
musicButtion1.png 24KB
n1.png 16KB
redstar.png 948B
help.png 28KB
timerow5.png 21KB
explore.png 67KB
max.png 28KB
greenstar2.png 15KB
about1.png 16KB
mode1.png 27KB
timerow4.png 22KB
timerow7.png 21KB
enter.png 22KB
max2.png 47KB
n9.png 16KB
timerow9.png 20KB
timerow3.png 22KB
greenstar1.png 15KB
food2.png 21KB
timerow12.png 19KB
snakebg.png 94KB
about2.png 43KB
desktop.ini 2KB
setting1.png 17KB
about.png 18KB
timerow14.png 18KB
snake.png 61KB
back.png 16KB
game_over.png 8KB
n8.png 16KB
n0.png 16KB
food1.png 980B
timerow1.png 22KB
score1.png 19KB
Endless.png 64KB
timerow2.png 22KB
wall.png 16KB
start.png 26KB
pause.png 3KB
help2.png 52KB
0.png 15KB
down.png 18KB
greenstar3.png 15KB
n2.png 16KB
n7.png 16KB
timerow0.png 16KB
music.png 18KB
menu.png 26KB
setting.png 27KB
timerow8.png 20KB
mode.png 18KB
musicButtion2.png 26KB
timerow11.png 19KB
classic.png 69KB
greenstar4.png 995B
n6.png 16KB
help1.png 18KB
n5.png 16KB
timerow15.png 18KB
grid1.png 2KB
timerow13.png 18KB
timerow16.png 17KB
setting2.png 21KB
n4.png 16KB
max1.png 18KB
n3.png 16KB
wav
buttion.wav 1KB
eat.wav 6KB
bg_1.wav 4.13MB
reward.wav 192KB
fail.wav 68KB
java
com
xxh
snake
Map.java 3KB
ReadAndWriter.java 3KB
Game.java 16KB
Sound.java 2KB
ui
ChooseUI.java 2KB
SettingUI.java 2KB
MaxScoreUI.java 2KB
AboutUI.java 946B
HomeUI.java 1KB
HelpUI.java 1KB
MyFrame.java 5KB
Food.java 910B
Score.java 2KB
Main.java 157B
共 88 条
- 1
资源评论
xinboyXu
- 粉丝: 31
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功