案例:贪吃蛇 第十天内容 食物类 节点类 主程序初步设计(静态)

一、窗体中设置两个面板

1.背景面板 500x500

添加个背景色,区分下背景和舞台,好判定蛇撞南墙会死亡

2.舞台面板 400x400

蛇的活动区域

看成 20x20的小格来放置食物和蛇

组装各种类

package day10;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 贪吃蛇游戏的主程序
 */
public class Main {
	public static void main(String [] args){
		//1.创建一个窗体对象
		JFrame frame = new JFrame("贪吃蛇");
		//设置窗体的大小
		frame.setSize(500,500);
		//设置窗体居中显示
		frame.setLocationRelativeTo(null);
		//设置默认的关闭操作
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//2.创建背景面板
		JPanel backPanel = new JPanel();
		//设置背景颜色
		backPanel.setBackground(new Color(255,128,255));
		//去掉背景面板的默认布局
		backPanel.setLayout(null);
		//3.创建舞台面板
		JPanel stagePanel = new JPanel();
		//设置舞台面板的大小
		stagePanel.setSize(400,400);
		//设置舞台面板显示的位置
		stagePanel.setLocation(40,30);
		//组装:
		backPanel.add(stagePanel);
		frame.add(backPanel);
		//显示
		frame.setVisible(true);
	}
}

二、创建食物类和节点类

1.食物类

初始化食物的各项私有的属性 (宽 高 行号  列号  x,y坐标  颜色)

构造方法,各个属性之间有关系,所以最终只需要获取行号列号

公开的get/set方法,使其属性可以被调用

package day10;

import java.awt.Color;

/**
 *贪吃蛇游戏的食物类
 *
 *属性:
 *宽 高 行号  列号  x,y坐标  颜色
 *
 *
 *javaBean规范:
 *	属性私有,公开的get/set方法
 */
public class Food {
	/**食物对象的宽*/
	private int width ;
	/**食物对象的高*/
	private int height ;
	/**食物对象的行号*/
	private int rows ;
	/**食物对象的列号*/
	private int cols ;
	/**食物对象的x坐标*/
	private int x ;
	/**食物对象的y坐标*/
	private int y ;
	/**食物对象的颜色*/
	private Color color ;
	
	//Random random = new Random();
	//Food food = new Food(w,h,r,c,x,y,color);
	//构造方法  new关键字
	public Food(int rows,int cols){
		//
		this.width =20;
		this.height = 20 ;
		this.color = Color.red;
		this.rows =rows ;
		this.cols = cols ;
		this.x = cols * width;
		this.y = rows * height ;
	}
	
	
	//公开的get/set方法
	//width
	public int getWidth(){
		return this.width;
	}
	public void setWidth(int width){
		this.width = width ;
	}
	
	//height
	public int getHeight(){
		return this.height;
	}
	public void setHeight(int height){
		this.height = height ;
	}
	//rows
	public int getRows(){
		return this.rows ;
	}
	public void setRows(int rows){
		this.rows = rows ;
	}
	//cols
	public int getCols (){
		return this.cols ;
	}
	public void setCols(int cols){
		this.cols = cols ;
	}
	//x
	public int getX(){
		return this.x ;
	}
	public void setX(int x){
		this.x= x ;
	}
	//y
	public int getY(){
		return this.y ;
	}
	public void setY(int y){
		this.y = y ;
	}
	//color
	public Color getColor(){
		return this.color ;
	}
	public void setColor(Color color){
		this.color = color ;
	}	
}


2.节点类(蛇身的位置等属性)

初始化节点的各项私有的属性 (宽 高 行号  列号  x,y坐标  颜色)

构造方法,各个属性之间有关系,所以最终只需要获取行号列号

公开的get/set方法,使其属性可以被调用

package day10;

import java.awt.Color;

/**
 * 贪吃蛇游戏蛇身上的节点类
 * 宽 高 行号 列号 x,y坐标 颜色
 */
public class Node {
	/**节点对象的宽*/
	private int width ;
	/**节点对象的高*/
	private int height ;
	/**节点对象的行号*/
	private int rows ;
	/**节点对象的列号*/
	private int cols ;
	/**节点对象的x坐标*/
	private int x ;
	/**节点对象的y坐标*/
	private int y ;
	/**节点对象的颜色*/
	private Color color ;
	
	public Node(int rows ,int cols){
		this.width = 20 ;
		this.height =20 ;
		this.color = Color.blue;
		this.rows = rows ;
		this.cols = cols ;
		this.x = cols * width ;
		this.y = rows * height ;
	}

//	公开的get/set方法
	//右键--Source--Generate Getters and Setters
	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int getCols() {
		return cols;
	}

	public void setCols(int cols) {
		this.cols = cols;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	
	
	
	
}


目前为止还看不见蛇样



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值