9.13
请利用文心一言等大模型开发一个具有界面的贪吃蛇小游戏,采用Java或Python语言。要求提交源代码及运行视频。
1、要有简单的界面
2、要可操控
3、代码要自行运行,视频录制代码在编译器里运行然后出界面的过程。
package Sanke;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Button extends JPanel{
public static boolean isMove=true;//表示运行状态
SnakeGrid snakeGrid;
Button(SnakeGrid snakeGrid){
this.snakeGrid=snakeGrid;
this.setBounds(0, 400, 700, 100);
JButton jb1 = new JButton("暂停游戏");
JButton jb2 = new JButton("继续游戏");
JButton jb3 = new JButton("重新游戏");
this.add(jb1);
this.add(jb2);
this.add(jb3);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isMove=false;
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isMove=true;
snakeGrid.setFocusable(true);
snakeGrid.requestFocus();
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//重新创建蛇等 重新开始游戏
snakeGrid.snakeThread.stopThread();
Food food = new Food();
snakeGrid.food=food;
snakeGrid.snake=new Snake(food);
Snake.islive=true;
isMove=true;
SnakeGrid.SnakeThread st = snakeGrid.new SnakeThread();
snakeGrid.snakeThread=st;
st.start();
snakeGrid.setFocusable(true);
snakeGrid.requestFocus();
}
});
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Button extends JPanel{
public static boolean isMove=true;//表示运行状态
SnakeGrid snakeGrid;
Button(SnakeGrid snakeGrid){
this.snakeGrid=snakeGrid;
this.setBounds(0, 400, 700, 100);
JButton jb1 = new JButton("暂停游戏");
JButton jb2 = new JButton("继续游戏");
JButton jb3 = new JButton("重新游戏");
this.add(jb1);
this.add(jb2);
this.add(jb3);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isMove=false;
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isMove=true;
snakeGrid.setFocusable(true);
snakeGrid.requestFocus();
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//重新创建蛇等 重新开始游戏
snakeGrid.snakeThread.stopThread();
Food food = new Food();
snakeGrid.food=food;
snakeGrid.snake=new Snake(food);
Snake.islive=true;
isMove=true;
SnakeGrid.SnakeThread st = snakeGrid.new SnakeThread();
snakeGrid.snakeThread=st;
st.start();
snakeGrid.setFocusable(true);
snakeGrid.requestFocus();
}
});
}
}
package Sanke;
import java.awt.*;
public class Food {
int row;
int col;
Food(){
row = 10;//创建食物的大小
col =10;
}
public void repearShow(){
row = (int)(Math.random()*18);//生成随机数 乘以食物的大小可以得到坐标
col = (int)(Math.random()*32);
}
public void draw(Graphics g) {//把食物画出来
g.setColor(Color.red);
g.fillRect(col*20, row*20, 20, 20);//表示坐标
}
public Rectangle getCoordinates(){
return new Rectangle(col*20,row*20,20,20);//获得食物的坐标
}
import java.awt.*;
public class Food {
int row;
int col;
Food(){
row = 10;//创建食物的大小
col =10;
}
public void repearShow(){
row = (int)(Math.random()*18);//生成随机数 乘以食物的大小可以得到坐标
col = (int)(Math.random()*32);
}
public void draw(Graphics g) {//把食物画出来
g.setColor(Color.red);
g.fillRect(col*20, row*20, 20, 20);//表示坐标
}
public Rectangle getCoordinates(){
return new Rectangle(col*20,row*20,20,20);//获得食物的坐标
}
package Sanke;
import java.awt.*;
import java.awt.event.KeyEvent;
/**
* @author Swyee
*/
public class Snake {
public static final int span=20;//间距
public static final int rows=20;//行
public static final int cols=35;//列
public static final String up="u";
public static final String down="d";
public static final String left="l";
public static final String right="r";
public static boolean islive=true;
Node body;//蛇的身体
Node head;//蛇的头部
Node tail;//蛇的头部
Food food;
Snake(Food food){
body = new Node(5,20,left);
head = body;
tail = body;
this.food=food;
}
class Node{
int row;
int col;
String dir;//方向
Node next;
Node pre;
Node(int row,int col,String dir){
this.row = row;
this.col = col;
this.dir = dir;
}
public void draw(Graphics g) {
g.fillOval(col*span, row*span, span,span);//坐标
}
}
/*
把蛇画出来
*/
public void draw(Graphics g) {
g.setColor(Color.yellow);
for(Node n=head;n!=null;n=n.next){
n.draw(g);
g.setColor(Color.green);
}
}
/*
调用键盘的上下左右键
head.dir记录现在操作的是什么按钮,从而更改蛇的状态
向上移送时,下键失效,其他四个方向也是如此判断
*/
public void keyboard(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
if(head.dir.equals(down)){
break;
}
head.dir=up;
break;
case KeyEvent.VK_DOWN:
if(head.dir.equals(up)){
break;
}
head.dir=down;
break;
case KeyEvent.VK_LEFT:
if(head.dir.equals(right)){
break;
}
head.dir=left;
break;
case KeyEvent.VK_RIGHT:
if(head.dir.equals(left)){
break;
}
head.dir=right;
break;
default:
break;
}
}
/*
增加头部
*/
public void addHead(){
Node node = null;
switch (head.dir){
case "l":
node = new Node(head.row,head.col-1,head.dir);
break;
case "r":
node = new Node(head.row,head.col+1,head.dir);
break;
case "u":
node = new Node(head.row-1,head.col,head.dir);
break;
case "d":
node = new Node(head.row+1,head.col,head.dir);
break;
default:
break;
}
node.next=head;
head.pre=node;
head=node;
}
/*
删除尾部
*/
public void deleteTail(){
tail.pre.next=null;
tail=tail.pre;
}
/*
增加move方法
*/
public void move() {
addHead();
if(this.getSnakeRectangle().intersects(food.getCoordinates())){//当蛇头与食物重合的时候 蛇吃食物 食物刷新,不再删除尾巴,达到一种蛇增长的要求
food.repearShow();
}else{
deleteTail();
}
DeadOrLive();//每移动一步都要判断一下是否存活
}
public Rectangle getSnakeRectangle(){//获取蛇头的坐标
return new Rectangle(head.col*span,head.row*span,span,span);
}
public void DeadOrLive(){//超出边框范围 蛇头撞到身体 游戏结束
if(head.row<0 || head.row>rows-1 || head.col<0 ||head.col>cols){
islive=false;
}
for(Node n=head.next;n!=null;n=n.next){
if(n.col==head.col && n.row==head.row){
islive=false;
}
}
}
}
import java.awt.*;
import java.awt.event.KeyEvent;
/**
* @author Swyee
*/
public class Snake {
public static final int span=20;//间距
public static final int rows=20;//行
public static final int cols=35;//列
public static final String up="u";
public static final String down="d";
public static final String left="l";
public static final String right="r";
public static boolean islive=true;
Node body;//蛇的身体
Node head;//蛇的头部
Node tail;//蛇的头部
Food food;
Snake(Food food){
body = new Node(5,20,left);
head = body;
tail = body;
this.food=food;
}
class Node{
int row;
int col;
String dir;//方向
Node next;
Node pre;
Node(int row,int col,String dir){
this.row = row;
this.col = col;
this.dir = dir;
}
public void draw(Graphics g) {
g.fillOval(col*span, row*span, span,span);//坐标
}
}
/*
把蛇画出来
*/
public void draw(Graphics g) {
g.setColor(Color.yellow);
for(Node n=head;n!=null;n=n.next){
n.draw(g);
g.setColor(Color.green);
}
}
/*
调用键盘的上下左右键
head.dir记录现在操作的是什么按钮,从而更改蛇的状态
向上移送时,下键失效,其他四个方向也是如此判断
*/
public void keyboard(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
if(head.dir.equals(down)){
break;
}
head.dir=up;
break;
case KeyEvent.VK_DOWN:
if(head.dir.equals(up)){
break;
}
head.dir=down;
break;
case KeyEvent.VK_LEFT:
if(head.dir.equals(right)){
break;
}
head.dir=left;
break;
case KeyEvent.VK_RIGHT:
if(head.dir.equals(left)){
break;
}
head.dir=right;
break;
default:
break;
}
}
/*
增加头部
*/
public void addHead(){
Node node = null;
switch (head.dir){
case "l":
node = new Node(head.row,head.col-1,head.dir);
break;
case "r":
node = new Node(head.row,head.col+1,head.dir);
break;
case "u":
node = new Node(head.row-1,head.col,head.dir);
break;
case "d":
node = new Node(head.row+1,head.col,head.dir);
break;
default:
break;
}
node.next=head;
head.pre=node;
head=node;
}
/*
删除尾部
*/
public void deleteTail(){
tail.pre.next=null;
tail=tail.pre;
}
/*
增加move方法
*/
public void move() {
addHead();
if(this.getSnakeRectangle().intersects(food.getCoordinates())){//当蛇头与食物重合的时候 蛇吃食物 食物刷新,不再删除尾巴,达到一种蛇增长的要求
food.repearShow();
}else{
deleteTail();
}
DeadOrLive();//每移动一步都要判断一下是否存活
}
public Rectangle getSnakeRectangle(){//获取蛇头的坐标
return new Rectangle(head.col*span,head.row*span,span,span);
}
public void DeadOrLive(){//超出边框范围 蛇头撞到身体 游戏结束
if(head.row<0 || head.row>rows-1 || head.col<0 ||head.col>cols){
islive=false;
}
for(Node n=head.next;n!=null;n=n.next){
if(n.col==head.col && n.row==head.row){
islive=false;
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
2023-09-30 java动手动脑