在北京学习,老师让我们写课程答辩
编写一个贪吃蛇的小游戏,以下是游戏代码及注释等
先写随机的各类奖品类
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);
}
}