概述
自学java没多久
对各种java的功能,特性都不太熟悉
写了个贪吃蛇小游戏
贪吃蛇有加速技能
就是界面的按钮有点bug
还有代码太累赘
有大佬能看看嘛
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class test {
public static void main(String[] args) {
Frame frame = new Frame();
}
}
class Frame extends JFrame{
public Frame(){
setSize(500,500);
this.setResizable(false);
Snake snake = new Snake();
add(snake);
snake.repaint();
setVisible(true);
}
}
class Snake extends JPanel implements KeyListener,Runnable{
int[] snakeX = new int[360];
int[] snakeY = new int[360];
int length = 3;
int score = 0;
Random r = new Random();
boolean gamestart = false;
boolean gameover = false;
boolean ifeat = false;
boolean foodlock = false;
boolean ifacce = false;
boolean turnTogame =false;
boolean ifcalming = false;
boolean ifOnSnake = false;
boolean pause = false;
int foodX = r.nextInt(43) * 10 + 30;
int foodY = r.nextInt(40) * 10 + 30;
int direction = 0;
int acce_time = 5;
int acce_calm = 10;
int snakeT = 100;//一格要走多少毫秒
Color colorcalm = new Color(128,128,150);
Color snakeC = Color.BLUE;
JButton b = new JButton("开始游戏");
JButton b1 = new JButton("退出游戏");
ActPer actper = new ActPer();
/* 0:right
*
1:left
*
2:up
*
3:down
*/
public Snake() {
this.addKeyListener(this);
this.setFocusable(true);
this.setLayout(null);
b.addActionListener(actper);
b1.addActionListener(actper);
this.add(b);
this.add(b1);
setup();
repaint();
b.setVisible(true);
b.setVisible(true);;
this.setVisible(true);
new Thread(this).start();
}
public void setup() {
snakeX[0] = 300;snakeX[1] = 290;snakeX[2] = 280;
snakeY[0] = 200;snakeY[1] = 200;snakeY[2] = 200;
foodX = r.nextInt(42) * 10 + 30;
foodY = r.nextInt(39) * 10 + 30;
snakeC = Color.BLUE;
snakeT = 100;
ifacce = false;
repaint();
length = 3;
score = 0;
acce_time = 5;
acce_calm = 10;
this.setBackground(Color.white);
direction = 0;
gamestart = false;
gameover = false;
ifcalming = false;
pause = false;
b.setBounds(100, 100, 100, 50);
b1.setBounds(100, 250, 100, 50);
b.setVisible(true);
b1.setVisible(true);
b.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "none");
b1.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "none");
for(int i=3;i<360;i++) {
snakeX[i] = -10;
snakeY[i] = -10;
}
}
class ActPer implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b&&turnTogame==false) {
setup();
turnTogame=true;
repaint();
b.setVisible(false);
b1.setVisible(false);
}
if(e.getSource()==b1&&turnTogame==false) {
System.exit(0);
}
}
}
public void paint(Graphics g) {
super.paint(g);
if(turnTogame==true) {
System.out.println("aaa");
g.setColor(Color.gray);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.white);
g.fillRect(30, 30, 440, 400);
g.setColor(Color.green);
g.fillRect(35, 35, 20, 20);
g.setColor(Color.black);
g.drawString("V", 40, 40);
if(gamestart==false) {
g.drawString("按空格键开始游戏", 100, 100);
}
if(gameover==true&&gamestart==false) {
g.drawString("你输了!", 100, 200);
}
for(int i = 0;i<length;i++) {
g.setColor(snakeC);
g.fillRect(snakeX[i] , snakeY[i] , 10,10);
//System.out.println("The icon has been printed at" +
//"(" + snakeX[i] + "," + snakeY[i] + ")");
}
if(ifacce==true) {
g.setColor(colorcalm);
g.fillRect(35, 35+ (5 - acce_time) * 5, 20, (acce_time - 5) * 5 + 20);
}else if(ifacce==false&&ifcalming == true) {
g.setColor(colorcalm);
g.fillRect(35, 55 - acce_calm * 2, 20, acce_calm * 2);
}else if(ifacce==true||ifcalming == true||length<=10) {
g.setColor(colorcalm);
g.fillRect(35, 35, 20, 20);
}
g.setColor(Color.red);
g.drawString("分数" + score, 50, 100);
g.fillRect(foodX, foodY, 10, 10);
}
else if(turnTogame==false) {
System.out.println("bbb");
g.setColor(Color.red);
g.fillRect(0, 0, 500, 500);
g.setFont(new Font("楷体",Font.PLAIN,30));
g.setColor(Color.white);
g.drawString("贪吃蛇", 70, 100);
b.setVisible(true);
b1.setVisible(true);
}
if(gamestart==true&&gameover==false&&turnTogame==true&&pause==true) {
g.setFont(new Font("楷体",Font.PLAIN,40));
g.setColor(Color.BLUE);
g.drawString("按空格键或ESC键返回游戏 " , 20, 100);
g.drawString("或者按B键回到标题", 50, 200);
}
}
public void judgement() {
System.out.println("(" + snakeX[0] + "," + snakeY[0] + ")");
if(snakeX[0]<30 ||snakeX[0]>470||snakeY[0]<30||snakeY[0]>430) {
if(gameover==false&&gamestart==true&&turnTogame==true) {
gameover = true;
repaint();
gamestart = false;
repaint();
System.out.println("you are dead");
}
}else {
}
for(int i = 1;i<length;i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]&&turnTogame==true) {
gameover = true;
repaint();
gamestart = false;
repaint();
}
}
}
public void snakecg() {
if(gamestart==true&&gameover==false&&turnTogame==true&&pause==false) {
for(int i = length - 1;i>0;i--) {
snakeX[i] = snakeX[i-1];
//System.out.println("has move snakeX at" + snakeX[i+1] + "to" + snakeX[i]);
snakeY[i] = snakeY[i-1];
//System.out.println("has move snakeY");
}
switch(direction) {
case 0:
snakeX[0] = snakeX[0] + 10; break;
case 1:
snakeX[0] = snakeX[0] - 10; break;
case 2:
snakeY[0] = snakeY[0] - 10; break;
case 3:
snakeY[0] = snakeY[0] + 10; break;
}
//System.out.println("");
if(snakeX[0]==foodX&&snakeY[0]==foodY) {
foodX = r.nextInt(43) * 10 + 30;
foodY = r.nextInt(40) * 10 + 30;
while(ifOnSnake==true) {
for(int i = 1;i<length;i++) {
if(foodX==snakeX[i]&&foodY==snakeY[i]) {
foodX = r.nextInt(43) * 10 + 30;
foodY = r.nextInt(40) * 10 + 30;
}else {
ifOnSnake = false;
}
}
}
length++;
score++;
}
repaint();
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_SPACE&&gamestart==true&&turnTogame==true) {
}
if (e.getKeyCode()==KeyEvent.VK_SPACE&&gamestart==false&&turnTogame==true) {
System.out.println("have pressed 'space'");
setup();
gamestart = true;
gameover = false;
ifeat =true;
System.out.println("have been set 'true'");
}
else if(e.getKeyCode()==KeyEvent.VK_SPACE&&pause==true&&gamestart==true&&gameover==false) {
pause = false;
System.out.println(pause);
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_SPACE&&pause==false&&gamestart==true&&gameover==false) {
pause = true;
System.out.println(pause);
repaint();
}
if(gamestart==true&&gameover==false&&turnTogame==true) {
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
direction = 2; break;
case KeyEvent.VK_DOWN:
direction = 3; break;
case KeyEvent.VK_LEFT:
direction = 1; break;
case KeyEvent.VK_RIGHT:
direction = 0; break;
}
}
if(e.getKeyCode()==KeyEvent.VK_V) {
System.out.println("V has been pressed");
if(ifacce==true&&gamestart==true&&gameover==false) {
}else if(ifacce==false&&gamestart==true&&gameover==false&&ifcalming == false&&length>10) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
snakeC = Color.red;
snakeT = 50;
ifacce = true;
System.out.println("has changed");
for(int i = 5;i >0&&gamestart==true&&gameover==false;i--) {
acce_time--;
length--;
score--;
snakeX[length] = -10;
snakeY[length] = -10;
repaint();
System.out.println(acce_time);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ifacce = false;
ifcalming = true;
System.out.println(ifacce + "-------------------------------");
snakeT = 100;
snakeC = Color.blue;
acce_time = 5;
for(int i = 10;i>0;i--) {
acce_calm--;
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ifcalming = false;
acce_calm = 10;
}
}).start();
System.out.println("acceleration-------start");
}else if(gamestart==true&&gameover==false||length<=10||ifcalming==true||ifacce==true) {
colorcalm = Color.red;
repaint();
}
}
if(e.getKeyCode()==KeyEvent.VK_ESCAPE&&turnTogame==true&&pause==false) {
pause = true;
b.setVisible(false);
repaint();
}else if(e.getKeyCode()==KeyEvent.VK_ESCAPE&&turnTogame==true&&pause==true) {
pause = false;
b.setVisible(false);
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_B) {
turnTogame = false;
pause = false;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_V) {
colorcalm = Color.gray;
repaint();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
//System.out.println("befor judge:" + gameover);
judgement();
//System.out.println("after judge:" + gameover);
snakecg();
try {
Thread.sleep(snakeT);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
最后
以上就是激昂花卷为你收集整理的萌新自学Java 贪吃蛇小游戏的全部内容,希望文章能够帮你解决萌新自学Java 贪吃蛇小游戏所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复