我是一個小白,寫了一個貪吃蛇游戲,發現里面有bug,代碼會貼在下面,主要的代碼是網上找的只有其中關于蟲洞的部分是自己寫的
uj5u.com熱心網友回復:
package snake;import javax.swing.JFrame;
public class Snake extends JFrame{
//創建一個游戲界面的框架
JFrame jframe=new JFrame("貪吃蛇 進階版");
static int i;
public Snake() {
//設定游戲界面
jframe.setBounds(10,10,1080,600);
jframe.setTitle("貪吃蛇");
jframe.setResizable(false);//設定界面大小不能改變
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉按鈕關閉界面
SnakePanel panel=new SnakePanel();
jframe.add(panel);//添加畫布
jframe.setVisible(true);
}
public static void main(String[] args) {
new Snake();
}
}
uj5u.com熱心網友回復:
package snake;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakePanel extends JPanel implements ActionListener,KeyListener{
// 定義七個圖片變數,代表七張圖片
ImageIcon up = new ImageIcon("D:\\pic\\up.png"); // 向上的蛇頭
ImageIcon down = new ImageIcon("D:\\pic\\down.png"); // 向下的蛇頭
ImageIcon left = new ImageIcon("D:\\pic\\left.png"); // 向左的蛇頭
ImageIcon right = new ImageIcon("D:\\pic\\right.png"); // 向右的蛇頭
ImageIcon food = new ImageIcon("D:\\pic\\food.png"); // 食物
ImageIcon body = new ImageIcon("D:\\pic\\body.png"); // 蛇的身體
ImageIcon hole=new ImageIcon("D:\\pic\\蟲洞.png");//蟲洞
// 蛇的每一部分
int[] snakex = new int [750];
int[] snakey = new int [750];
// 隨機生成食物
Random rand = new Random();
int foodx = rand.nextInt(40)*25+25; //此處的數值根據自己設計的游戲界面的大小來確定
int foody = rand.nextInt(16)*25+75;
// 設定游戲的默認屬性
int len = 3;
int score = 0;
String direction = "R"; // U上 D下 L左 R右
boolean isStarted = false; // 判斷游戲是否開始
boolean isFailed = false; // 判斷游戲是否結束
int[] x={150,200,400,550,830,880};//蟲洞所在的x坐標
int[] y= {250,150,100,450,50,300};//蟲洞所在的x坐標
//設定計時器
int t=1;
Timer timer=new Timer(180/t,this);//每180/i毫秒呼叫一次ActionPerform
public SnakePanel() { // 建造畫布的建構式
this.setFocusable(true); // 獲取焦點
this.addKeyListener(this); // 監聽鍵盤事件
setup();
timer.start();
}
public void paint(Graphics g) { // Graphics 畫筆
this.setBackground(Color.BLACK); // 設定畫布背景顏色
g.fillRect(25, 25,1030, 500); // 用畫筆設定游戲方框
// 畫蛇頭(注意判斷蛇頭的方向)
if (direction.equals("R"))
right.paintIcon(this, g, snakex[0], snakey[0]);
else if (direction.equals("L"))
left.paintIcon(this, g, snakex[0], snakey[0]);
else if (direction.equals("U"))
up.paintIcon(this, g, snakex[0], snakey[0]);
else if (direction.equals("D"))
down.paintIcon(this, g, snakex[0], snakey[0]);
// 畫蛇的身體
for(int i = 1; i < len; i ++)
body.paintIcon(this, g, snakex[i], snakey[i]);
//顯示蟲洞
for(int i=0;i<6;i++)
hole.paintIcon(this,g,x[i],y[i]);
// 判斷如果游戲沒開始顯示。。。
if (!isStarted){
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.BOLD, 30));
g.drawString("Press Space to start / pause", 200, 300);
}
// 判斷如果游戲結束顯示。。。
if (isFailed){
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.BOLD, 30));
g.drawString("Game Over ! Press space to restart", 200, 300);
}
// 顯示食物
food.paintIcon(this, g, foodx, foody);
// 設定分數和蛇的長度
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.PLAIN,15));
g.drawString("Score : "+score, 650, 37);
g.drawString("Len :"+len, 650, 57);
}
public void setup() { // 游戲初始化
isStarted = false;
isFailed = false;
len = 3;
score = 0;
snakex[0] = 100; snakex[1] = 75; snakex[2] = 50;
snakey[0] = 100; snakey[1] = 100; snakey[2] = 100;
}
@Override
public void keyPressed(KeyEvent e) {
//實作鍵盤回應
int KeyCode = e.getKeyCode();
if (KeyCode == KeyEvent.VK_SPACE){ // 敲擊空格現實/消除提示資訊
if (isFailed){
isStarted = false; // 可以將這兩行放入setup中
isFailed = false;
setup();
}else
isStarted = !isStarted;
} else if (KeyCode == KeyEvent.VK_UP && direction != "D")
direction = "U";
else if (KeyCode == KeyEvent.VK_DOWN && direction != "U" )
direction = "D";
else if (KeyCode == KeyEvent.VK_RIGHT && direction != "L")
direction = "R";
else if (KeyCode == KeyEvent.VK_LEFT && direction != "R")
direction = "L";
}
@Override
public void actionPerformed(ActionEvent e) {
// 1. 再定義一個鬧鐘
timer.start();
// 2. 移動資料
if (isStarted && !isFailed){
// 移動身體
for (int i = len; i>0; i--){
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
// 移動頭
if (direction.equals("R")){
snakex[0] = snakex[0] + 25;
if(snakex[0] > 1030) snakex[0] = 25;
}else if (direction.equals("L")){
snakex[0] = snakex[0] - 25;
if(snakex[0] < 25) snakex[0] = 1030;
}else if (direction.equals("U")){
snakey[0] = snakey[0] - 25;
if (snakey[0] < 25) snakey[0] = 500;
}else if (direction.equals("D")){
snakey[0] = snakey[0] + 25;
if (snakey[0] > 500) snakey[0] = 25;
}
if (snakex[0] == foodx && snakey[0] == foody){ // 吃食物
len ++;
score ++;
foodx = rand.nextInt(28)*25+25;
foody = rand.nextInt(20)*25+75;
}
for (int i = 1; i < len; i ++){ // 如果蛇頭碰到自己的身體游戲結束
if (snakex[0] == snakex[i] && snakey[0] == snakey[i]){
isFailed = true;
}
}
//如果蛇頭碰到蟲洞
if(snakex[0]==x[0]&&snakey[0]==y[0]) {
snakex[0]=x[5];
snakey[0]=y[5];
}
else if(snakex[0]==x[1]&&snakey[0]==y[1]) {
snakex[0]=x[4];
snakey[0]=y[4];
}
else if(snakex[0]==x[2]&&snakey[0]==y[2]) {
snakex[0]=x[3];
snakey[0]=y[3];
}
else if(snakex[0]==x[3]&&snakey[0]==y[3]) {
snakex[0]=x[2];
snakey[0]=y[2];
}
else if(snakex[0]==x[4]&&snakey[0]==y[4]) {
snakex[0]=x[1];
snakey[0]=y[1];
}
else if(snakex[0]==x[5]&&snakey[0]==y[5]) {
snakex[0]=x[0];
snakey[0]=y[0];
}
}
// 3. repaint()
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
package snake;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakePanel extends JPanel implements ActionListener,KeyListener{
// 定義七個圖片變數,代表七張圖片
ImageIcon up = new ImageIcon("D:\\pic\\up.png"); // 向上的蛇頭
ImageIcon down = new ImageIcon("D:\\pic\\down.png"); // 向下的蛇頭
ImageIcon left = new ImageIcon("D:\\pic\\left.png"); // 向左的蛇頭
ImageIcon right = new ImageIcon("D:\\pic\\right.png"); // 向右的蛇頭
ImageIcon food = new ImageIcon("D:\\pic\\food.png"); // 食物
ImageIcon body = new ImageIcon("D:\\pic\\body.png"); // 蛇的身體
ImageIcon hole=new ImageIcon("D:\\pic\\蟲洞.png");//蟲洞
// 蛇的每一部分
int[] snakex = new int [750];
int[] snakey = new int [750];
// 隨機生成食物
Random rand = new Random();
int foodx = rand.nextInt(40)*25+25; //此處的數值根據自己設計的游戲界面的大小來確定
int foody = rand.nextInt(16)*25+75;
// 設定游戲的默認屬性
int len = 3;
int score = 0;
String direction = "R"; // U上 D下 L左 R右
boolean isStarted = false; // 判斷游戲是否開始
boolean isFailed = false; // 判斷游戲是否結束
int[] x={150,200,400,550,830,880};//蟲洞所在的x坐標
int[] y= {250,150,100,450,50,300};//蟲洞所在的x坐標
//設定計時器
int t=1;
Timer timer=new Timer(180/t,this);//每180/i毫秒呼叫一次ActionPerform
public SnakePanel() { // 建造畫布的建構式
this.setFocusable(true); // 獲取焦點
this.addKeyListener(this); // 監聽鍵盤事件
setup();
timer.start();
}
public void paint(Graphics g) { // Graphics 畫筆
this.setBackground(Color.BLACK); // 設定畫布背景顏色
g.fillRect(25, 25,1030, 500); // 用畫筆設定游戲方框
// 畫蛇頭(注意判斷蛇頭的方向)
if (direction.equals("R"))
right.paintIcon(this, g, snakex[0], snakey[0]);
else if (direction.equals("L"))
left.paintIcon(this, g, snakex[0], snakey[0]);
else if (direction.equals("U"))
up.paintIcon(this, g, snakex[0], snakey[0]);
else if (direction.equals("D"))
down.paintIcon(this, g, snakex[0], snakey[0]);
// 畫蛇的身體
for(int i = 1; i < len; i ++)
body.paintIcon(this, g, snakex[i], snakey[i]);
//顯示蟲洞
for(int i=0;i<6;i++)
hole.paintIcon(this,g,x[i],y[i]);
// 判斷如果游戲沒開始顯示。。。
if (!isStarted){
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.BOLD, 30));
g.drawString("Press Space to start / pause", 200, 300);
}
// 判斷如果游戲結束顯示。。。
if (isFailed){
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.BOLD, 30));
g.drawString("Game Over ! Press space to restart", 200, 300);
}
// 顯示食物
food.paintIcon(this, g, foodx, foody);
uj5u.com熱心網友回復:
// 設定分數和蛇的長度g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.PLAIN,15));
g.drawString("Score : "+score, 650, 37);
g.drawString("Len :"+len, 650, 57);
}
public void setup() { // 游戲初始化
isStarted = false;
isFailed = false;
len = 3;
score = 0;
snakex[0] = 100; snakex[1] = 75; snakex[2] = 50;
snakey[0] = 100; snakey[1] = 100; snakey[2] = 100;
}
@Override
public void keyPressed(KeyEvent e) {
//實作鍵盤回應
int KeyCode = e.getKeyCode();
if (KeyCode == KeyEvent.VK_SPACE){ // 敲擊空格現實/消除提示資訊
if (isFailed){
isStarted = false; // 可以將這兩行放入setup中
isFailed = false;
setup();
}else
isStarted = !isStarted;
} else if (KeyCode == KeyEvent.VK_UP && direction != "D")
direction = "U";
else if (KeyCode == KeyEvent.VK_DOWN && direction != "U" )
direction = "D";
else if (KeyCode == KeyEvent.VK_RIGHT && direction != "L")
direction = "R";
else if (KeyCode == KeyEvent.VK_LEFT && direction != "R")
direction = "L";
}
@Override
public void actionPerformed(ActionEvent e) {
// 1. 再定義一個鬧鐘
timer.start();
// 2. 移動資料
if (isStarted && !isFailed){
// 移動身體
for (int i = len; i>0; i--){
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
// 移動頭
if (direction.equals("R")){
snakex[0] = snakex[0] + 25;
if(snakex[0] > 1030) snakex[0] = 25;
}else if (direction.equals("L")){
snakex[0] = snakex[0] - 25;
if(snakex[0] < 25) snakex[0] = 1030;
}else if (direction.equals("U")){
snakey[0] = snakey[0] - 25;
if (snakey[0] < 25) snakey[0] = 500;
}else if (direction.equals("D")){
snakey[0] = snakey[0] + 25;
if (snakey[0] > 500) snakey[0] = 25;
}
if (snakex[0] == foodx && snakey[0] == foody){ // 吃食物
len ++;
score ++;
foodx = rand.nextInt(28)*25+25;
foody = rand.nextInt(20)*25+75;
}
for (int i = 1; i < len; i ++){ // 如果蛇頭碰到自己的身體游戲結束
if (snakex[0] == snakex[i] && snakey[0] == snakey[i]){
isFailed = true;
}
}
//如果蛇頭碰到蟲洞
if(snakex[0]==x[0]&&snakey[0]==y[0]) {
snakex[0]=x[5];
snakey[0]=y[5];
}
else if(snakex[0]==x[1]&&snakey[0]==y[1]) {
snakex[0]=x[4];
snakey[0]=y[4];
}
else if(snakex[0]==x[2]&&snakey[0]==y[2]) {
snakex[0]=x[3];
snakey[0]=y[3];
}
else if(snakex[0]==x[3]&&snakey[0]==y[3]) {
snakex[0]=x[2];
snakey[0]=y[2];
}
else if(snakex[0]==x[4]&&snakey[0]==y[4]) {
snakex[0]=x[1];
snakey[0]=y[1];
}
else if(snakex[0]==x[5]&&snakey[0]==y[5]) {
snakex[0]=x[0];
snakey[0]=y[0];
}
}
// 3. repaint()
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/58283.html
標籤:Java相關
上一篇:關于IDEA TAB快捷鍵問題
