要求:
主题:自拟
技术:
参考《代码本色》教程,运用不少于3个章节的动画技术,实现一个交互应用,将动画技术充分运用于交互过程中;
最好能充分融入其他课程的知识。
开发工具: processing
作品介绍
- 游戏有1个playerBall,1种食物food,3种敌人(enemy)
- 添加了动画、图片
- 给游戏中的playBall添加了几种特性技能:
A. playball可以实现迅速移动
B. playball可以使用bomb炸弹
C.playBall可以远距离吸收食物
关于这几个特性的功能都是通过键盘按键(1,2,3)来实现的 - 给游戏进行了关卡设置,并提供了相应的关卡选择界面。
5.游戏的运行界面以及玩法的说明:
游戏的开始界面,点击Start,即可进入选关界面。游戏的关卡选择,选择关卡进入。
上面的两幅图分别代表的是关卡的运行界面以及每关结束后的界面。针对运行是的界面左下角的小字player Ability 是代表playerBall的一些相应的技能。只有在3个技能后面的数字大于0的时候技能才有可能被激发。按住键盘数字键1、2、3技能快速移动、扔炸弹以及远距离吸收食物。
Ps. 每一关卡的闯关时间设置为30秒,并采用倒计时的方式实现
针对第二个界面,点击按钮Menu可以返回选关界面,点击Try again即将继续进行本级的闯关,点击Next即将进入到下一关。
技术要点:
关于食物与敌人
撰写了一个Ball的类,在初始化的时候设置食物颜色与主角playBall一致,而作为enemy的颜色则与主角不一致。分别写了几个Ball的继承类,这几个继承的类都只是重写了update这个函数。重写的要点就是这几个类中的速度的方向以及行动轨迹均不一致。(详见程序)
插入图片、动画
关于插入图片只需要使用函数:
PImage photo;
Photo = loadImage(“bg1.jpg”);
Image(photo,position.x,position.y) ; //其中positionx 与positiony 分别表示图片插入的位置
关于插入动画,主要是体现开始界面的设计上。观察开始的图片,可以发现很多气泡。那其实是用processing写得粒子系统产生的一种效果。主要就是程序中的Particle这个类实现的功能。粒子系统不同于Ball的关键在于它又生命周期。详细设计思想及内容主要参见源码中的Particle这个类。
import java.util.*;
int total = 10;
ArrayList<Particle> plist = new ArrayList<Particle>();
class Particle{
PVector location;
PVector velocity;
PVector acceleration;
float mass;
float lifespan;
float R = random(255);
float G = random(255);
float B = random(255);
Particle(){
location = new PVector(random(width), random(height));
velocity = new PVector(random(-1, 1), random(-2, 0));
acceleration = new PVector(0, 0);
mass = 1;
lifespan = 255;
}
Particle(PVector l){
location = l;
acceleration = new PVector(0, 0);
velocity = new PVector(random(-1, 1),random(-2, 0));
mass = 1;
lifespan = 255;
}
void applyForce(PVector force){
acceleration.add(PVector.div(force, mass));
}
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
lifespan -= 1.0;
}
void display(){
stroke(R,G,B,lifespan);
fill(R,G,B,lifespan);
ellipse(location.x, location.y, 8, 8);
}
boolean isDead(){
if(lifespan < 0.0){
return true;
}else{
return false;
}
}
void run(){
update();
display();
}
}
void coverDraw(){
plist.add(new Particle(new PVector(random(width), random(height))));
Iterator<Particle> it = plist.iterator();
while(it.hasNext()){
Particle p = it.next();
p.run();
if(p.isDead()){
it.remove();
}
}
}
playerBall的几种技能的添加
关于QuickMove技能,quickmove主要体现在触发了quickmove的开关以后小球的拖动速度相比之前要快上许多。主要技术即为在Ball这个类里面添加了QuickMove函数。
//the ability of quick move
void QuickMove(){
if (isQuickMove){
_dragSpd = 20.0f;
}
}
关于Bomb技能主要体现为触发**开关后,游戏区域内除了主角playBall之外的所有球(包括食物均为消除);主要的技术要点就是首先得找出屏幕中所有存在的食物和敌人,清除这些食物与敌人。类Ball里面的函数Bomb主要实现了该功能。
//the ability of set bomb
void Bomb(){
if (isBomb){
float txtWidth = textWidth("Beng Beng!");
text("Beng Beng!",width/2.0f - txtWidth/2.0f,height/2,0f);
game.GetBallAmount();
}
}
关于特性DistAbsorb(远距离吸收食物)技能,该技能的体现主要是在**该技能之后,与主角之间距离不超过300的食物都会被吸到主角这里,被主角吸收。而且吸收的方向得向着主角。技术要点主要在方向的确定上,类Ball中的函数DistAbsorb主要实现了该功能。
//the ability of absorb food of long distance
void DistAbsorb(Ball other){
if (isDistAbsorb && _Color == other._Color){
float distance = dist(_center.x,_center.y,other._center.x,other._center.y);
if (distance < 300.0f){
float xdeta = _center.x - other._center.x;
float ydeta = _center.y - other._center.y;
other._velocity = new PVector(xdeta*300/distance,ydeta*300/distance);
absorb(other);
}
}
}
关卡设置以及难度关卡的自由选择
关卡选择的界面已经给出,点击界面的那个关卡就将游戏关卡设置为什么。难点在于难度的设置,level 1-6 难度是升级的,难度升级的体现之一就是食物明显变少,敌人明显变多;体现之二即为关卡越往后所需升级的分数就越高。但是在前面的关卡闯关中,你可以赚取playerBall得技能,然后在后面使用。难度关卡的设置主要是在类Game中的setDiff函数里面实现的。
// set the difficulties of the different level
void setdiff(){
if (_playerTime >= 0){
Draw();
if (Score >= 300 * level){
if (n==0){
winTime = 30.0f - _playerTime;
if(winTime <= 20.0f){
number[0]++;
number[1]++;
}
}
String txtWidth = "stage clear";
text(txtWidth,10.0f,90.0f);
n++;
}
}
else{
//display select button
GUIButton();
if (Score < 300*level){
drawGameOver();
}
else{
if (nn == 0 && Score >350 * level){
number[2]++; //get the ability of DistAbsorb
}
nn++;
String txtWidth = "stage clear";
text(txtWidth,width/2.0f - textWidth(txtWidth)/2.0f,height/2.0f-50.0f);
}
}
}
实验总结
该作品主要是结合网上的代码和自己的改动完成,再一次大大提升了自己改造程序的能力