Java 挫折把事情搞得一团糟
我尝试了不同的方法为这个蛇游戏制作了两个背景,一个黑色代表菜单,一个白色代表游戏的线条。我找到的最好的解决办法是使用挫折。但是当我运行游戏时,线程。睡眠被打乱了,现在蛇走得非常快。为了解决这个问题,我在Thread.sleep中输入了多个值,但是不管这些值是多少,蛇都以相同的速度移动Java 挫折把事情搞得一团糟,java,swing,animation,graphics,Java,Swing,Animation,Graphics,我尝试了不同的方法为这个蛇游戏制作了两个背景,一个黑色代表菜单,一个白色代表游戏的线条。我找到的最好的解决办法是使用挫折。但是当我运行游戏时,线程。睡眠被打乱了,现在蛇走得非常快。为了解决这个问题,我在Thread.sleep中输入了多个值,但是不管这些值是多少,蛇都以相同的速度移动 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.Thread; import java.util
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Thread;
import java.util.Random;
public class Snake extends JPanel implements KeyListener, MouseListener{
public boolean right = false;
public boolean left = false;
public boolean up = false;
public boolean down = false;
public int snakex[] = new int[10000000];
public int snakey[] = new int[10000000];
public int snakeLength = 0;
public int applex;
public int appley;
public int buttonX = 150;
public int buttonY = 125;
public boolean appleEaten = true;
public static boolean reset = false;
public static boolean ingame = false;
public static boolean menu = true;
public static int speed = 200;
public void forLogic(){
for(int i = snakeLength; i > 1; i--){
if(snakeLength > 4 && snakex[0] == snakex[i] && snakey[0] == snakey[i]){
System.out.println("You Loose \n Your Score was: " + snakeLength);
ingame = false;
}
}
Movement();
if(snakex[0] >= 30*20){
snakex[0] = 0;
}
if(snakex[0] < 0){
snakex[0] = 29*20;
}
if(snakey[0] >= 25*20){
snakey[0] = 0;
}
if(snakey[0] < 0){
snakey[0] = 24*20;
}
if(snakex[0] == applex*20 && snakey[0] == appley*20) {
appleEaten = true;
snakeLength++;
//System.out.println(snakeLength);
}
if(appleEaten){
appleLocation();
appleEaten = false;
}
}
public void appleLocation(){
boolean goodToGo = false;
Random rand = new Random();
while(!goodToGo){
applex = rand.nextInt(30);
appley = rand.nextInt(25);
boolean checker = false;
for(int i = snakeLength; i > 0; i--) {
if (applex == snakex[i]||appley == snakey[i]) {
checker = true;
}
}
if(!checker){goodToGo = true;}
}
}
public void Movement(){
if(reset){
left = false;
right = false;
up = false;
down = false;
snakex[0] = 0;
snakey[0] = 0;
snakeLength = 1;
appleLocation();
reset = false;
}
if(right){
snakex[0] += 20;
}
if(left){
snakex[0] -= 20;
}
if(up){
snakey[0] -= 20;
}
if(down){
snakey[0] += 20;
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
int mouseX = e.getX();
int mouseY = e.getY();
if(mouseX > buttonX && mouseX < buttonX + 300 && mouseY > buttonY && mouseY < buttonY + 75){
ingame = true;
}
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == 39 && !left) {
right = true;
up = false;
down = false;
}
if(key == 37 && !right){
left = true;
up = false;
down = false;
}
if(key == 38 && !down){
up = true;
left = false;
right = false;
}
if(key == 40 && !up){
down = true;
left = false;
right = false;
}
if(key == 82){
reset = true;
}
}
public void keyReleased(KeyEvent e) {}
@SuppressWarnings("serial")
public void paint(Graphics g) {
super.paintComponent(g);
if(menu){
setBackground(Color.BLACK);
g.setColor(Color.green);
g.setFont(new Font("Courier New", Font.BOLD, 50));
g.drawString("Snake Game", 150, 50);
g.drawRect(buttonX, buttonY, 300, 75);
g.setFont(new Font("Courier New", Font.BOLD, 40));
g.drawString("PLAY", 250, 175);
}
if(ingame) {
setBackground(Color.WHITE);
int x = 0;
int y = 0;
for (x = 0; x < 30; x++) {
for (y = 0; y < 25; y++) {
g.setColor(Color.black);
g.fillRect(x * 20, y * 20, 19, 19);
}
}
g.setColor(Color.red);
g.fillOval(applex * 20, appley * 20, 19, 19);
forLogic();
g.setColor(Color.green);
for (int i = snakeLength; i > 0; i--) {
snakex[i] = snakex[(i - 1)];
snakey[i] = snakey[(i - 1)];
g.fillRect(snakex[i], snakey[i], 19, 19);
}
}
}
public static void main(String[] args) throws InterruptedException {
JFrame jframe = new JFrame("Snake Game");
Snake snake = new Snake();
jframe.add(snake);
snake.addMouseListener(snake);
snake.addKeyListener(snake);
jframe.setSize(615, 540);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setFocusable(true);
jframe.setVisible(true);
snake.requestFocusInWindow();
jframe.setLocationRelativeTo(null);
while(true) {
if (!menu) {
ingame = true;
}
if (menu == ingame) {
ingame = false;
}
if (menu) {
snake.repaint();
}
if (ingame) {
while (true) {
Thread.sleep(200);
snake.repaint();
}
}
}
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.lang.Thread;
导入java.util.Random;
公共类Snake扩展了JPanel实现了KeyListener、MouseListener{
公共布尔右=假;
公共布尔左=假;
公共布尔值up=false;
公共布尔向下=false;
公共整数snakex[]=新整数[10000000];
公共整数snakey[]=新整数[10000000];
公共整数蛇形长度=0;
公共int applex;
公共内部应用程序;
公共int按钮X=150;
公共int按钮=125;
公共布尔值appleEaten=true;
公共静态布尔重置=false;
公共静态布尔值ingame=false;
公共静态布尔菜单=true;
公共静态积分速度=200;
公共逻辑{
对于(int i=蛇形长度;i>1;i--){
如果(蛇形长度>4&&snakex[0]==snakex[i]&&snakey[0]==snakey[i]){
System.out.println(“你的分数是:“+snakeLength”);
ingame=false;
}
}
运动();
如果(蛇[0]>=30*20){
蛇[0]=0;
}
if(蛇[0]<0){
蛇[0]=29*20;
}
如果(snakey[0]>=25*20){
snakey[0]=0;
}
if(snakey[0]<0){
snakey[0]=24*20;
}
if(snakex[0]==applex*20&&snakey[0]==appley*20){
appleEaten=true;
蛇形长度++;
//系统输出打印LN(蛇形长度);
}
if(appleEaten){
appleLocation();
appleEaten=假;
}
}
公共无效应用位置(){
布尔值goodogo=false;
Random rand=新的Random();
而(!古多哥){
applex=兰特·耐克斯汀(30);
appley=rand.nextInt(25);
布尔检查器=假;
对于(int i=蛇形长度;i>0;i--){
if(applex==snakex[i]| | appley==snakey[i]){
checker=true;
}
}
如果(!checker){goodogo=true;}
}
}
公众空虚运动{
如果(重置){
左=假;
右=假;
向上=错误;
向下=假;
蛇[0]=0;
snakey[0]=0;
蛇长=1;
appleLocation();
重置=错误;
}
如果(右){
蛇[0]+=20;
}
如果(左){
蛇[0]-=20;
}
如果(向上){
snakey[0]-=20;
}
如果(向下){
snakey[0]+=20;
}
}
公共无效mouseenterned(MouseEvent e){}
公共无效mouseExited(MouseEvent e){}
公共无效鼠标按下(MouseEvent e){
int mouseX=e.getX();
int mouseY=e.getY();
if(mouseX>buttonX&&mouseXbuttonY&&mouseY0;i--){
蛇[i]=蛇[(i-1)];
snakey[i]=snakey[(i-1)];
g、 fillRect(蛇[i],蛇[i],19,19);
}
}
}
公共静态void main(字符串[]args)引发InterruptedException{
JFrame JFrame=新JFrame(“蛇游戏”);
蛇=新蛇();
jframe.add(snake);
斯内克·阿德穆塞利斯滕纳(斯内克);
addKeyListener(snake);
jframe.setSize(615540);
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
jframe.setFocusable(真);
jframe.setVisible(true);
snake.requestFocusInWindow();
jframe.setLocationRelativeTo(空);
while(true){
如果(!菜单){
public void paint(Graphics g) {
super.paintComponent(g);
if(key == 39 && !left) {
if(key == 82){
if(ingame) {
setBackground(Color.WHITE);
public void forLogic(){
for(int i = snakeLength; i > 1; i--){
if(snakeLength > 4 && snakex[0] == snakex[i] && snakey[0] == snakey[i]){
System.out.println("You Loose \n Your Score was: " + snakeLength);
ingame = false;
}
}
Movement();
if(snakex[0] >= 30*20){
snakex[0] = 0;
}
if(snakex[0] < 0){
snakex[0] = 29*20;
}
if(snakey[0] >= 25*20){
snakey[0] = 0;
}
if(snakey[0] < 0){
snakey[0] = 24*20;
}
if(snakex[0] == applex*20 && snakey[0] == appley*20) {
appleEaten = true;
snakeLength++;
//System.out.println(snakeLength);
}
if(appleEaten){
appleLocation();
appleEaten = false;
}
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}