案例:貪吃蛇 第十天內容 食物類 節點類 主程序初步設計(靜態)

一、窗體中設置兩個面板

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;
	}
	
	
	
	
	
}


目前爲止還看不見蛇樣



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章