课设-贪吃蛇

本文介绍了如何使用Java实现贪吃蛇游戏,包括基本功能如屏幕随机生成食物、控制蛇移动,以及交互界面设计,如开始、暂停和停止功能,并讨论了游戏得分、速度设置和拓展元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验内容

1)实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子。玩家能利用上下左右键控制“蛇”的移动,“蛇”吃到“豆子”后“蛇”身体加长一节,得分增加,“蛇”碰到边界或蛇头与蛇身相撞,“蛇”死亡,游戏结束。

2)进行交互界面的设计,要有开始键、暂停键和停止退出的选项,能够控制游戏进程。对蛇吃到豆子进行分值计算,可以设置游戏速度,游戏音乐等拓展元素。

实现效果

请添加图片描述
请添加图片描述

实现过程

food类

package com.zjxu.snake.game;


import com.zjxu.snake.core.WConstant;
import com.zjxu.snake.core.WObject;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Food extends WObject{
   
	Color foodColor;
	private double initX,initY;
	int timer=0;
	
	public Food(double x,double y,double width,double height) {
   
		super(x, y, width, height);
		this.initX = x;
		this.initY =y;
		init();
	}
	
	@Override
	public void init() {
   
		setX(initX);
		setY(initY);
		setVisable(true);
		this.foodColor = Color.BLUE;
		createFood();
	}
	
	@Override
	public void draw(GraphicsContext gc) {
   
		gc.setFill(foodColor);
		gc.fillOval(getX(), getY(), getWidth(), getHeight());
	}

	@Override
	public void update() {
   
		if(! isvisable()) {
   
			createFood();
			setVisable(true);
			timer=0;
		}
		if(WConstant.level=="hard")
		{
   
			if(timer==1000) {
   
				createFood();
				timer=0;
			}
			timer++;
		}
	}

	
	/*  getWidth/2  <x< GAME_WIDTH - getWidth
	 	getHeight/2 <y< GAME_HEIGHT- getHeight */
	private void createFood() {
   
		double randomX = (WConstant.GAME_WIDTH - 3*getWidth()/2 + 1)* Math.random() + getWidth()/2;
		double randomY = (WConstant.GAME_HEIGHT - 3*getWidth()/2+ 1)* Math.random() + getHeight()/2;
		setX(randomX);
		setY(randomY);
	}

}

GameApplication类

package com.zjxu.snake.game;


import com.zjxu.snake.core.WApplication;
import com.zjxu.snake.core.WConstant;
import javafx.scene.paint.Color;

public class GameApplication extends WApplication{
   
	@Override
	protected void loadBefore() {
   
		setSize(800, 600,20,20);
	}
	
	@Override
	protected void load() {
   
		super.setTitle("JAVA课程设计--贪吃蛇");
		GameScreen  gameScreen = new GameScreen(WConstant.GAME_WIDTH,WConstant.GAME_HEIGHT);
		getRoot().getChildren().add(gameScreen);
		gameScreen.start();
		gameScreen.initEvents();
		getScene().setFill(Color.RED);
	}
	
}

GameScreen类:

package com.zjxu.snake.game;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.zjxu.snake.core.WConstant;
import com.zjxu.snake.core.WScreen;
import com.zjxu.snake.others.Db;
import com.zjxu.snake.others.FileController;

import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class GameScreen extends WScreen{
   
	private SnakeHead snakeHead;
    private SnakeBody snakeBody;
    private Food food;
    private Info info;
    public Boolean quit=false;
	SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	//游戏日期的格式
    public FileController fileController = new FileController();
    
    public GameScreen(double width, double height) {
   
		super(width, height);
			
		snakeHead = new SnakeHead(0, 0, 20, 20);
		snakeBody = new SnakeBody(snakeHead);
		food = new Food(0, 0, 20, 20);
		info = new Info();

		addObject(snakeBody);
		addObject(snakeHead);
		addObject(food);
		addObject(info);
		
		gameState = GameState.GAME_START;
	}

	
	@Override
	public void update() {
   
		if (snakeHead.death==true) {
   
			if(gameState!=GameState.GAME_PAUSE) {
   
				//死亡后保存数据到文件与数据库,处理相应数据参数
				String name = WConstant.name;
				int score = info.getScore();
				Date time = new Date(System.currentTimeMillis());
				String format_time = formatter.format(time);	//String 类型写入固定格式
				fileController.write(WConstant.name, info.getScore(),WConstant.level,format_time );//写入文件
				
				
				//String sql = " INSERT INTO easy(`name`,`score`,`time`) VALUES ('test',2,'test'); ";
				String VALUES = "('" +  name + 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值