本帖最后由 苏紫方璇 于 2023-3-19 11:21 编辑
直接上源码
新人发帖
格式有点不会
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
//extends 继承 implements 实现接口 KeyListener是一个接口
public class MPanel extends JPanel implements KeyListener,ActionListener{
//加载标题图片
ImageIcon title = new ImageIcon("title.jpg");
//加载上,下,左,右的头部图片
ImageIcon head_R = new ImageIcon("head-R.jpg");
ImageIcon head_L = new ImageIcon("head-L.jpg");
ImageIcon head_U = new ImageIcon("head-U.jpg");
ImageIcon head_D = new ImageIcon("head-D.jpg");
//加载身体图片
ImageIcon body = new ImageIcon("body.jpg");
//定义一个初始化蛇的长度len 初始化为3
ImageIcon food = new ImageIcon("food.jpg");
int len = 3;
int Score = 0;
//定义两个数组,snakex为蛇的行坐标 snakey为蛇的纵坐标
int[] snakex = new int[750];
int[] snakey = new int[750];
//定义蛇的方向为 R,L,U,D
String fx = "R";
boolean isStarted = false; //判断游戏是否开始
boolean isAlive = true; //判断蛇是否死亡
Timer timer = new Timer(100,this); //加上闹钟 蛇前进的速度
//添加食物
int foodx;
int foody;
Random ra = new Random();
//构造方法
public MPanel() {
initSnake();
//将自己设置为监听器
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
//方法 利用了Graohics来创建了一块绿色的画布
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.GREEN);
//标题图片的位置
title.paintIcon(this, g, 25, 11);
//标题上添加分数和长度
g.drawString("len:"+len, 750, 35);
g.drawString("Score"+Score, 750, 50);
//在画布上设置游戏区
g.fillRect(25, 75, 850, 600);
//放入一个静态的蛇
//控制蛇头的方向
if(fx == "R") {
head_R.paintIcon(this, g, snakex[0], snakey[0]);
}else if(fx == "L") {
head_L.paintIcon(this, g, snakex[0], snakey[0]);
}else if(fx == "U") {
head_U.paintIcon(this, g, snakex[0], snakey[0]);
}else if(fx == "D") {
head_D.paintIcon(this, g, snakex[0], snakey[0]);
}
food.paintIcon(this, g, foodx, foody);
//蛇的身体会根据吃食物而增加
for(int i = 1;i<len;i++) {
body.paintIcon(this,g,snakex[i],snakey[i]);
//放上开始提示
if(isStarted == false) {
g.setColor(Color.WHITE);
g.setFont(new Font("arial",Font.BOLD,40));
g.drawString("Press Space to Start", 266, 350);
}
//放上结束提示
if(!isAlive) {
g.setColor(Color.BLUE);
g.setFont(new Font("arial",Font.BOLD,40));
g.drawString("GAME OVER:Press Space to Start", 100, 350);
}
}
}
//这个方法作为初始化 蛇刚刚开始的状态
public void initSnake() {
len=3;
snakex[0] = 100;
snakey[0] = 100;
snakex[1] = 75;
snakey[1] = 100;
snakex[2] = 50;
snakey[2] = 100;
// 设置食物初始位置
foodx = 25 + 25*ra.nextInt(34);
foody = 75 + 25*ra.nextInt(24);
fx = "R";
Score = 0;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
//当按下空格建时,isStarted这个变量为true
//repaint() 这个方法指重新绘出 重新在执行一遍
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_SPACE) {
if(!isAlive) {
isAlive = true;
}else {
isStarted = !isStarted;
}
repaint();
// 将键盘的上下左右在文件中体现出来
}else if(keycode == KeyEvent.VK_LEFT) {
fx = "L";
}else if(keycode == KeyEvent.VK_RIGHT) {
fx = "R";
}else if(keycode == KeyEvent.VK_UP) {
fx = "U";
}else if(keycode == KeyEvent.VK_DOWN) {
fx = "D";
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStarted && isAlive) {
for(int i = len-1;i>0;i--) {
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
if(fx == "R") {
snakex[0] = snakex[0]+25;
if(snakex[0] > 850) {snakex[0] = 25;}
}else if(fx == "L") {
snakex[0] = snakex[0]-25;
if(snakex[0] < 25) {snakex[0] = 850;}
}else if(fx == "U") {
snakey[0] = snakey[0]-25;
if(snakey[0] < 75) {snakey[0] = 650;}
}else if(fx == "D") {
snakey[0] = snakey[0]+25;
if(snakey[0] > 650) {snakey[0] = 75;}
}
//吃掉食物
if(snakex[0] == foodx && snakey[0] == foody) {
len++;
Score+=10;
foodx = 25 + 25*ra.nextInt(34);
foody = 75 + 25*ra.nextInt(24);
switch(fx){
case "D":
case "U":
}
}
//死亡
for(int i = 1;i<len;i++) {
if(snakex[i] ==snakex[0] && snakey[i] ==snakey[0]) {
isAlive = false;
initSnake();
}
}
repaint();
}
timer.start();
}
}
|