DeskCreeper.java
import javax.print.attribute.standard.DialogOwner;
import javax.swing.*;
import java.util.Scanner;
public class DeskCreeper {
private static String want = "up";
private static String place = "down";
private static String body_Direction = "left";
private static String head_Direction = "up";
private JFrame jFrame;
private JPanel jPanel;
private int x;
private int y;
private int change_x;
private int change_y;
private int Screen_Width;
private int Screen_Height;
//动作开关
private static boolean Climb_Status = false;
private static boolean Walk_status = true;
public void setForm(JFrame jFrame,JPanel jPanel) {
this.jFrame = jFrame;
this.jPanel = jPanel;
}
public void setScreen(int Screen_Width,int Screen_Height) {
this.Screen_Width = Screen_Width;
this.Screen_Height = Screen_Height;
}
private void init() {
/*
初始化状态和变量
*/
change_x = 0;
change_y = 0;
switch (place) {
case "down":
if (body_Direction == "up" || body_Direction == "down") {
if (System.currentTimeMillis()%2==0) {
body_Direction = "left";
}else {
body_Direction = "right";
}
}
head_Direction = "up";
y = Screen_Height-128;
if (x == 0 || x == Screen_Width) {
x = (int) (System.currentTimeMillis() % 1857);
}
break;
case "up":
if (head_Direction == "up" || head_Direction == "down") {
if (System.currentTimeMillis() % 2 == 0) {
head_Direction = "left";
} else {
head_Direction = "right";
}
}
body_Direction = place;
y = (int) (-(128/8)*3.4);
if (y == -20) {
x = (int) (System.currentTimeMillis() % 1857);
}
break;
case "left":
body_Direction = place;
head_Direction = "up";
x = (int) (-(128/8)*4.3);
if (y == -20) {
y = (int) (System.currentTimeMillis() % 1080);
}
break;
case "right":
body_Direction = place;
head_Direction = "up";
x = (int) (Screen_Width-(128/8)*4.3);
if (y == -20) {
y = (int) (System.currentTimeMillis() % 1080);
}
break;
}
Image_Draw.draw_replace(jFrame,jPanel,"res/wait/"+place+"_"+body_Direction+"_"+head_Direction+".png",x,y);
}
private void Wait() {
init();
}
private void Down() {
/*
坠落的动画
*/
change_y = 7;
try {
int n = y;
boolean j = true;
/*
共设计两套的坠落动画,随机播放,大概
*/
if (place == "left" || place == "right") {
//n = 400;
while (true) {
y += change_y;
if (y >= Screen_Height - (128 * 1.5)) {
if (n <= Screen_Height/2) {
switch (place) {
case "left":
x = (int) (-(128 / 8) * 2.5);
break;
case "right":
x = Screen_Width-(128/8)*6;
break;
}
}
if (j) {
//System.out.println(head_Direction);
Image_Draw.draw_replace(jFrame, jPanel, "res/climb/" + place + "_" + body_Direction + "_" + head_Direction + "_1.png", x, y);
j = false;
}
place = "down";
Thread.sleep(50);
change_y = 14;
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
//在快落地时播放缓冲的动画
if (y >= Screen_Height - 128) {
y = Screen_Height-128;
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
Thread.sleep(50);
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/" + place + "_" + body_Direction + "_" + head_Direction + "_1.png", x, y);
Thread.sleep(250);
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
break;
}
} else {
if (n <= Screen_Height/2) {
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
} else {
switch (place) {
case "left":
x = (int) (-(128/8)*0.3);
break;
case "right":
x = Screen_Width-(128/8)*8;
break;
}
Image_Draw.draw_replace(jFrame, jPanel, "res/down/" + body_Direction + ".png", x, y);
}
}
Thread.sleep(50);
}
} else if (place == "up") {
while (true) {
y += change_y;
if (y >= Screen_Height - (128 * 1.5)) {
place = "down";
head_Direction = "up";
if (y >= Screen_Height - 128) {
y = Screen_Height-128;
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
Thread.sleep(50);
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/" + place + "_" + body_Direction + "_" + head_Direction + "_1.png", x, y);
Thread.sleep(250);
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
break;
}
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
} else {
body_Direction = head_Direction;
Image_Draw.draw_replace(jFrame, jPanel, "res/down/" + body_Direction + ".png", x, y);
}
Thread.sleep(50);
}
}
}catch (Exception e) {
System.out.println("ERRO:Status");
}
init();
}
private void Dump() {
/*
标准化对象状态
*/
y = Screen_Height-128;
change_y = 6;
y = Screen_Height-131;
place = "down";
head_Direction = "up";
try {
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/"+place+"_"+body_Direction+"_"+head_Direction+"_1.png", x, y);
Thread.sleep(200);
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/"+place+"_"+body_Direction+"_"+head_Direction+"_2.png", x, y);
//硬核补帧,小幅度修改位置,达到更平滑的动画效果
for (int i = 0;i < 4;i++) {
y += -change_y;
Thread.sleep(40);
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/"+place+"_"+body_Direction+"_"+head_Direction+"_3.png", x, y);
}
for (int i = 0;i < 3;i++) {
y += change_y;
Thread.sleep(40);
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/"+place+"_"+body_Direction+"_"+head_Direction+"_3.png", x, y);
}
y += change_y;
Thread.sleep(70);
Image_Draw.draw_replace(jFrame, jPanel, "res/dump/"+place+"_"+body_Direction+"_"+head_Direction+"_4.png", x, y);
Thread.sleep(1000);
init();
}catch (Exception e) {
e.printStackTrace();
}
}
private void Turn() {
y = Screen_Height-128;
place = "down";
head_Direction = "up";
/*在任意地形如履平地
{
switch (place) {
case "down":
head_Direction = "up";
break;
case "up":
head_Direction = "down";
break;
case "left":
head_Direction = "right";
break;
case "right":
head_Direction = "left":
break;
}
}
*/
try {
Image_Draw.draw_replace(jFrame, jPanel, "res/turn/"+place+"_"+body_Direction+"_"+head_Direction+"_before.png", x, y);
Thread.sleep(500);
Image_Draw.draw_replace(jFrame, jPanel, "res/turn/"+place+"_"+body_Direction+"_"+head_Direction+"_ago.png", x, y);
Thread.sleep(300);
if (body_Direction == "left") {
body_Direction = "right";
}else {
body_Direction = "left";
}
}catch (Exception e) {
e.printStackTrace();
}
}
private void Walk() {
/*
移动
*/
switch (place) {
case "down":
y = Screen_Height-128;
head_Direction = "up";
if (body_Direction == "left") {
change_x = -5;
}else if (body_Direction == "right") {
change_x = 5;
}else {
body_Direction = "right";
change_x = 5;
}
break;
case "left":
x = 0;
y += (128/8)*2;
head_Direction = "right";
if (body_Direction == "up") {
change_y = -5;
}else if (body_Direction == "down") {
change_y = 5;
}else {
body_Direction = "down";
change_y = 5;
}
break;
case "right":
x = Screen_Width-128;
y += (128/8)*2;
head_Direction = "left";
if (body_Direction == "up") {
change_y = -5;
}else if (body_Direction == "down") {
change_y = 5;
}else {
body_Direction = "down";
change_y = 5;
}
break;
case "up":
y = -6;
head_Direction = "down";
if (body_Direction == "left") {
change_x = -5;
}else if (body_Direction == "right") {
change_x = 5;
}else {
body_Direction = "right";
change_x = 5;
}
break;
}
String[] flash = {
"res/walk/"+place+"_"+body_Direction+"_"+head_Direction+"_1.png",
"res/wait/"+place+"_"+body_Direction+"_"+head_Direction+".png",
"res/walk/"+place+"_"+body_Direction+"_"+head_Direction+"_2.png",
"res/wait/"+place+"_"+body_Direction+"_"+head_Direction+".png"
};
try {
//System.out.println(place+"_"+body_Direction+"_"+head_Direction);
for (String img_place:flash) {
x += change_x;
y += change_y;
if (x >= Screen_Width - (128 / 8) * 6) {
/*
如果碰到屏幕右边缘,停止运动,打开爬行(climb)动作开关
*/
x = Screen_Width - (128 / 8) * 6;
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
Thread.sleep(50);
want = "up";
}else if (x <= -1 * (128 / 8) * 2.5) {
/*
如果碰到屏幕左边缘,停止运动,打开爬行(climb)动作开关
*/
x = (int) (-1 * (128 / 8) * 2.5);
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
Thread.sleep(50);
want = "up";
}else if (y <= -1*(128/8)*2.5) {
/*
仅在climb停止并打开Walk_status时使用
如果碰到屏幕上边缘,停止运动,打开爬行(climb)动作开关
(未完成,没什么用)
*/
y = (int) (-1*(128/8)*2.5);
Image_Draw.draw_replace(jFrame,jPanel,"res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png",x,y);
Thread.sleep(100);
Walk_status = false;
want = "up";
Climb();
}else if (y >= Screen_Height-(128/8)*6) {
/*
仅在climb停止并打开Walk_status时使用
如果碰到屏幕下边缘,停止运动,打开爬行(climb)动作开关
(未完成,没什么用)
*/
y = Screen_Height - (128 / 8) * 6;
Image_Draw.draw_replace(jFrame, jPanel, "res/wait/" + place + "_" + body_Direction + "_" + head_Direction + ".png", x, y);
Thread.sleep(100);
Walk_status = false;
want = "up";
Climb();
}else{
Image_Draw.draw_replace(jFrame, jPanel, img_place, x, y);
Thread.sleep(150);
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
private void Climb() {
class CLIMB {
void climb() {
if (place == "left" || place == "right") {
if (want == "up") {
change_y = -5;
change_x = 0;
}else if (want == "down") {
if (System.currentTimeMillis()%2 == 0) {
change_y = 5;
change_x = 0;
}else {
Down();
return;
}
}else {
want = "up";
change_y = -5;
change_x = 0;
}
}else if (place == "up") {
if (want == "up") {
change_x = -5;
change_y = 0;
}else if (place == "down") {
if (System.currentTimeMillis()%2 == 0) {
change_y = 0;
change_x = 0;
Down();
return;
}else {
change_x = 5;
change_y = 0;
}
}else {
want = "down";
change_y = 0;
change_x = 0;
Down();
return;
}
}else if (place == "down") {
Walk();
}else {
System.out.println("ERRO_PLACE:这个位置不是可用位置");
place = "down";
init();
}
String[] flash = {
"res/climb/"+place+"_"+body_Direction+"_"+head_Direction+"_1.png",
"res/wait/"+place+"_"+body_Direction+"_"+head_Direction+".png",
"res/climb/"+place+"_"+body_Direction+"_"+head_Direction+"_2.png",
"res/wait/"+place+"_"+body_Direction+"_"+head_Direction+".png",
};
for (String img_place:flash) {
try {
x += change_x;
y += change_y;
/*
初始化爬行位置
*/
switch (place) {
case "left":
x = (int) (-1*(128/8)*4.3);
break;
case "right":
x = (int) (Screen_Width-(128/8)*4.3);
break;
case "up":
y = (int) (-1*(128/8)*3.4);
break;
case "down":
y = (int) (Screen_Height-(128/8)*4.3);
}
if (place == "left" || place == "right") {
if (y <= 0) {
/*
碰到上边缘,停止运动,打开Walk_status
*/
y = 0;
Wait();
Walk_status = true;
} else if (y >= Screen_Height - 128) {
/*
碰到下边缘,停止运动,打开Walk_status
*/
y = Screen_Height - 128;
Wait();
Walk_status = true;
}else {
Image_Draw.draw_replace(jFrame, jPanel, img_place, x, y);
Thread.sleep(150);
}
}else if (place == "up" || place == "down") {
if (x <= 0) {
/*
碰到左边缘,停止运动,开放向下通道,在第二次运行爬行动作时启动坠落动画(Down)或向后退
*/
x = 0;
want = "down";
Wait();
} else if (x >= Screen_Width - 128) {
/*
碰到右边缘,停止运动,开放向下通道,在第二次运行爬行动作时启动坠落动画(Down)或向后退
*/
x = Screen_Width - 128;
want = "dwon";
Wait();
}else {
Image_Draw.draw_replace(jFrame, jPanel, img_place, x, y);
Thread.sleep(150);
}
}else {
/*
状态出错,初始化位置
*/
Wait();
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
if (place == "left" || place == "right") {
if (y == -1*(128/8)*2.5 && want == "up") {
place = body_Direction;
new CLIMB().climb();
}else if (y == Screen_Height-(128/8)*6) {
if (System.currentTimeMillis()%2 == 0) {
place = "down";
Wait();
}else {
want = "up";
new CLIMB().climb();
}
}else {
new CLIMB().climb();
}
}else if (place == "up") {
new CLIMB().climb();
}else if (place == "down") {
if (want == "up") {
place = body_Direction;
new CLIMB().climb();
}
}
}
public void free_run() {
/*
变量时打算写AI行为的,但是我不会
以指令控制
*/
init();
while (true) {
String input = new Scanner(System.in).next();
System.out.println("输入指令");
System.out.println(place+"_"+body_Direction+"_"+head_Direction+"_"+want);
switch (input) {
case "wait":
Wait();
break;
case "walk":
for (int s = 0;s < 5;s++) {
Walk();
}
break;
case "climb":
for (int s = 0;s < 3;s++) {
Climb();
}
break;
case "turn":
Turn();
break;
case "want":
if (want == "up") {
want = "down";
}else {
want = "up";
}
break;
case "down":
Down();
break;
case "dump":
Dump();
break;
}
}
}
}
关于我定义的一些属性:
place 位置,用于指引桌宠所在的位置
body_Direction 身体朝向,用于处理运动方向
head_Direction 以图片核心位置向头部的位移方向,用于检查状态是否正常
want 想做什么,用于一些动作的开启或运动状态
差不多就这些,剩下也不好写,那些数据和关于位置的算式都是我测算和实验出来,等以后知识更丰富以后再重构一遍(小声逼逼:这是我重构第三次的作品)