贪吃蛇——奖品类

在北京学习,老师让我们写课程答辩
编写一个贪吃蛇的小游戏,以下是游戏代码及注释等
先写随机的各类奖品类

1、苹果类

food.png
苹果类的具体照片

package cn.tedu.Game;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 * @author 作者:LiXin
 * @email 邮箱:
 * @version 创建时间: 2020年1月8日上午8:20:11
 * @description 描述:苹果类
 */
public class Apple extends SuperClass {
	
	private static BufferedImage images;
	static{
		images = loadImage("food.png");
	}
	//构造方法
	public Apple() {
		super(30,30,(int) (Math.random() * (MainGame.GAME_WIDTH - 30 + 10)),
				(int) (Math.random() * (MainGame.GAME_HEIGHT - 40 - 30) + 40),
				true);

	}
	//食物被吃的方法
	public void eaten(Snake mySnake) {
		if (mySnake.getRectangle().intersects(this.getRectangle()) && live && mySnake.live) {
			this.live = false;// 食物死亡
			mySnake.setLength(mySnake.getLength() + 2);// 长度加2
			//mySnake.score += 10 * mySnake.getLength();// 加分
			mySnake.score += 12;
		}
	}
	@Override
	public BufferedImage getImage() {
		
		return images;
	}
	//画
	@Override
	public void paintObject(Graphics g) {
		g.drawImage(getImage(), x, y,30, 30,null);
	}
	//重写父类移动
	@Override
	public void move() {
	}
}

2、黄绿宝石

blue.png
蓝宝石的图片

package cn.tedu.Game;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
*@author 作者:LiXin
*@email  邮箱:
*@version 创建时间: 2020年1月9日下午5:08:24
*@description 描述:蓝宝石
*		//构造方法、移动、画对象、重写父类方法
*		//判断是否被吃的方法
*/
public class BlueStone extends SuperClass{
	public static BufferedImage images;
	static {
		images = loadImage("blue.png");
	}

	public BlueStone(int x, int y) {
		super(30 , 30, x, y, true);
		
		this.live = true;
	}

	// 食物被吃的方法
	public void eaten(Snake mySnake) {
		if (mySnake.getRectangle().intersects(this.getRectangle()) && live && mySnake.live) {
			this.live = false;// 食物死亡
			mySnake.setLength(mySnake.getLength() + 1);// 长度加一
			//mySnake.score += 1 * mySnake.getLength();// 加分
			mySnake.score += 8;
		}
	}
	//重写
	@Override
	public void move() {
		// TODO Auto-generated method stub

	}

	@Override
	public BufferedImage getImage() {
		// TODO Auto-generated method stub
		return images;
	}
	//画对象
	@Override
	public void paintObject(Graphics g) {
		super.paintObject(g);
		g.drawImage(getImage(), x, y, null);
	}
}

3、大红宝石

DeepRed.png
深红宝石的图片

package cn.tedu.Game;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
*@author 作者:LiXin
*@email  邮箱:
*@version 创建时间: 2020年1月9日下午5:26:01
*@description 描述:大红宝石
*		//构造方法、移动、画对象、重写父类方法
*		//判断是否被吃的方法
*/
public class DeepRed extends SuperClass{
	public static BufferedImage images;
	static {
		images = loadImage("DeepRed.png");
	}

	public DeepRed(int x, int y) {
		super(30 , 30, x, y, true);
		
		this.live = true;
	}

	// 食物被吃的方法
	public void eaten(Snake mySnake) {
		if (mySnake.getRectangle().intersects(this.getRectangle()) && live && mySnake.live) {
			this.live = false;// 食物死亡
			mySnake.setLength(mySnake.getLength() + 1);// 长度加一
			//mySnake.score += 1 * mySnake.getLength();// 加分
			mySnake.score += 5;
		}
	}
	//重写
	@Override
	public void move() {
		// TODO Auto-generated method stub

	}

	@Override
	public BufferedImage getImage() {
		// TODO Auto-generated method stub
		return images;
	}
	//画对象
	@Override
	public void paintObject(Graphics g) {
		super.paintObject(g);
		g.drawImage(getImage(), x, y, null);
	}
}

4、星星(star)

star.png
爱心宝石照片

package cn.tedu.Game;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 * @author 作者:LiXin
 * @email 邮箱:
 * @version 创建时间: 2020年1月9日上午7:56:24
 * @description 描述:星星
 */
public class Star extends SuperClass {
	
	public static BufferedImage images;
	static {
		images = loadImage("star.png");
	}

	public Star(int x, int y) {
		super(30 , 30, x, y, true);
		
		this.live = true;
	}

	// 食物被吃的方法
	public void eaten(Snake mySnake) {
		if (mySnake.getRectangle().intersects(this.getRectangle()) && live && mySnake.live) {
			this.live = false;// 食物死亡
			mySnake.setLength(mySnake.getLength() + 1);// 长度加一
			mySnake.score += 1;// 加分
		}
	}

	@Override
	public void move() {
		// TODO Auto-generated method stub

	}

	@Override
	public BufferedImage getImage() {
		// TODO Auto-generated method stub
		return images;
	}

	@Override
	public void paintObject(Graphics g) {
		super.paintObject(g);
		g.drawImage(getImage(), x, y, null);
	}
}

5、黄宝石

yellow.png
黄宝石普片

package cn.tedu.Game;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
*@author 作者:LiXin
*@email  邮箱:
*@version 创建时间: 2020年1月9日下午6:07:06
*@description 描述:黄宝石
*		//构造方法、移动、画对象、重写父类方法
*		//判断是否被吃的方法
*/
public class YellowStone extends SuperClass{
	public static BufferedImage images;
	static {
		images = loadImage("yellow.png");
	}
	//构造方法
	public YellowStone(int x, int y) {
		super(30 , 30, x, y, true);
		
		this.live = true;
	}

	// 食物被吃的方法
	public void eaten(Snake mySnake) {
		if (mySnake.getRectangle().intersects(this.getRectangle()) && live && mySnake.live) {
			this.live = false;// 食物死亡
			mySnake.setLength(mySnake.getLength() + 1);// 长度加一
			mySnake.score += 3;// 加分
		}
	}
	//重写
	@Override
	public void move() {
		// TODO Auto-generated method stub

	}

	@Override
	public BufferedImage getImage() {
		// TODO Auto-generated method stub
		return images;
	}
	//画对象
	@Override
	public void paintObject(Graphics g) {
		super.paintObject(g);
		g.drawImage(getImage(), x, y, null);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值