一构回、題目要求
最近在聽funk音樂(lè)陈惰,感覺真?zhèn)€人都舒服了,些這個(gè)作業(yè)的時(shí)候身體一直在抖,這次要完成的作業(yè)是參考《代碼本色》教程禀挫,運(yùn)用不少于3個(gè)章節(jié)的動(dòng)畫技術(shù)旬陡,實(shí)現(xiàn)一個(gè)交互應(yīng)用,將動(dòng)畫技術(shù)充分運(yùn)用于交互過(guò)程中语婴;
做了一個(gè)小游戲描孟,
整個(gè)游戲的玩法就是通過(guò)鼠標(biāo)控制粉色小球移動(dòng)驶睦,小的方塊是食物,可以被吃掉匿醒,其他的的大球是怪物了场航,如果他們碰到你就會(huì)減分?jǐn)?shù)并且會(huì)卡頓,分?jǐn)?shù)顯示在左上角
背景是聲音的波動(dòng)廉羔,加載了一首bgm溉痢,會(huì)根據(jù)節(jié)奏抖動(dòng),其實(shí)可以使怪物的移動(dòng)也和音樂(lè)結(jié)合起來(lái)憋他,但是奈何太懶了孩饼,暫時(shí)就這樣了。
其實(shí)代碼并沒有多復(fù)雜了竹挡,比較復(fù)雜的地方是怪物的成長(zhǎng)是根據(jù)基因模擬的镀娶,每個(gè)怪物一開始都會(huì)隨機(jī)初始的基因,根據(jù)基因來(lái)繪制大小此迅,有一定幾率分裂和隨機(jī)產(chǎn)生汽畴,然后個(gè)體生命會(huì)隨著時(shí)間衰減,當(dāng)他們碰到玩家控制的小球后就會(huì)重新變得滿生命值
二耸序、實(shí)踐內(nèi)容
通過(guò)processing對(duì)圖像進(jìn)行處理
homework——主要運(yùn)行腳本忍些,主要運(yùn)行world和bgm的可視化
?
bloop——模擬怪物的存在,并且寫移動(dòng)坎怪,復(fù)制罢坝,變異和更新的函數(shù),寫吃食物和玩家后變大和長(zhǎng)生命的函數(shù)
DNA——主要寫怪物的基因函數(shù)搅窿,其實(shí)這整個(gè)就是一個(gè)遺傳變異的模擬了嘁酿,以01序列來(lái)模擬基因
player——寫玩家控制的函數(shù)
WORLD——寫玩家食物和怪物生成的函數(shù)
三、源代碼
homework——主要運(yùn)行腳本男应,主要運(yùn)行world和bgm的可視化
import processing.sound.*;
// Declare the sound source and Waveform analyzer variables
SoundFile sample;
Waveform waveform;
// Define how many samples of the Waveform you want to be able to read at once
int samples = 100;
World world;
void setup() {
? ? size(640, 360);
? // Load and play a soundfile and loop it.
? sample = new SoundFile(this, "beat.aiff");
? sample.loop();
? // Create the Waveform analyzer and connect the playing soundfile to it.
? waveform = new Waveform(this, samples);
? waveform.input(sample);
? // World starts with 20 creatures
? // and 20 pieces of food
? world = new World(20);
? smooth();
}
void draw() {
? background(30, 30, 240);
? ? // Set background color, noFill and stroke style
? stroke(255);
? strokeWeight(2);
? noFill();
? // Perform the analysis
? waveform.analyze();
? beginShape();
? for(int i = 0; i < samples; i++){
? ? // Draw current data of the waveform
? ? // Each sample in the data array is between -1 and +1
? ? vertex(
? ? ? map(i, 0, samples, 0, width),
? ? ? map(waveform.data[i], -1, 1, 0, height)
? ? );
? }
? endShape();
? String time = "Sorce is " + millis()/100;?
? pushMatrix();?
? translate(0, 0, 0);?
? textSize(32);
? stroke(255, 255, 0);?
? text(time, 40, 80);?
? noStroke();?
? popMatrix();
? world.run();
}
bloop——模擬怪物的存在闹司,并且寫移動(dòng),復(fù)制沐飘,變異和更新的函數(shù)游桩,寫吃食物和玩家后變大和長(zhǎng)生命的函數(shù)
class Bloop {
? PVector position; // position
? DNA dna;? ? ? ? ? // DNA
? float health;? ? // Life timer
? float xoff;? ? ? // For perlin noise
? float yoff;
? // DNA will determine size and maxspeed
? float r;
? float maxspeed;
? // Create a "bloop" creature
? Bloop(PVector l, DNA dna_) {
? ? position = l.get();
? ? health = 200;
? ? xoff = random(1000);
? ? yoff = random(1000);
? ? dna = dna_;
? ? // Gene 0 determines maxspeed and r
? ? // The bigger the bloop, the slower it is
? ? maxspeed = map(dna.genes[0], 0, 1, 15, 5);//對(duì)速度的映射
? ? r = map(dna.genes[0], 0, 1, 20, 50);//半徑映射,都跟基因序列有關(guān)
? }
? void run() {
? ? update();
? ? borders();
? ? display();
? }
? // A bloop can find food and eat it
? void eat(Food f, Player player) {
? ? ArrayList<PVector> food = f.getFood();
? ? // Are we touching any food objects?
? ? for (int i = food.size()-1; i >= 0; i--) {
? ? ? PVector foodposition = food.get(i);
? ? ? float d = PVector.dist(position, foodposition);
? ? ? // If we are, juice up our strength!
? ? ? if (d < r/2) {
? ? ? ? health += 100;
? ? ? ? food.remove(i);
? ? ? }
? ? }
? ? PVector playerposition=player.get();
? ? float d = PVector.dist(position, playerposition);
? ? // If we are, juice up our strength!
? ? if (d < (r+player.getr())/2) {
? ? ? if (player.getr()<r) {
? ? ? ? health += 100;
? ? ? ? println("die");
? ? ? ? delay(100);
? ? ? } else {
? ? ? ? player.health+=100;
? ? ? ? health=0;
? ? ? }
? ? }
? }
? // At any moment there is a teeny, tiny chance a bloop will reproduce
? Bloop reproduce() {
? ? // asexual reproduction
? ? if (random(1) < 0.0005) {
? ? ? // Child is exact copy of single parent
? ? ? DNA childDNA = dna.copy();
? ? ? // Child DNA can mutate
? ? ? childDNA.mutate(0.01);
? ? ? return new Bloop(position, childDNA);
? ? } else {
? ? ? return null;
? ? }
? }
? // Method to update position
? void update() {
? ? // Simple movement based on perlin noise
? ? float vx = map(noise(xoff), 0, 1, -maxspeed, maxspeed);
? ? float vy = map(noise(yoff), 0, 1, -maxspeed, maxspeed);
? ? PVector velocity = new PVector(vx, vy);
? ? xoff += 0.01;
? ? yoff += 0.01;
? ? position.add(velocity);
? ? // Death always looming
? ? health -= 0.2;
? }
? // Wraparound
? void borders() {
? ? if (position.x < -r) position.x = width+r;
? ? if (position.y < -r) position.y = height+r;
? ? if (position.x > width+r) position.x = -r;
? ? if (position.y > height+r) position.y = -r;
? }
? // Method to display
? void display() {
? ? ellipseMode(CENTER);
? ? stroke(0, health);
? ? fill(0,240,250, health);
? ? ellipse(position.x, position.y, r, r);
? }
? // Death
? boolean dead() {
? ? if (health < 0.0) {
? ? ? return true;
? ? } else {
? ? ? return false;
? ? }
? }
}
DNA——主要寫怪物的基因函數(shù)耐朴,其實(shí)這整個(gè)就是一個(gè)遺傳變異的模擬了借卧,以01序列來(lái)模擬基因
class DNA {
? // The genetic sequence
? float[] genes;
? // Constructor (makes a random DNA)
? DNA() {
? ? // DNA is random floating point values between 0 and 1 (!!)
? ? genes = new float[1];
? ? for (int i = 0; i < genes.length; i++) {
? ? ? genes[i] = random(0,1);
? ? }
? }
? DNA(float[] newgenes) {
? ? genes = newgenes;
? }
? DNA copy() {
? ? float[] newgenes = new float[genes.length];
? ? //arraycopy(genes,newgenes);
? ? // JS mode not supporting arraycopy
? ? for (int i = 0; i < newgenes.length; i++) {
? ? ? newgenes[i] = genes[i];
? ? }
? ? return new DNA(newgenes);
? }
? // Based on a mutation probability, picks a new random character in array spots
? void mutate(float m) {
? ? for (int i = 0; i < genes.length; i++) {
? ? ? if (random(1) < m) {
? ? ? ? genes[i] = random(0,1);
? ? ? }
? ? }
? }
}
food——寫食物生成和被吃掉的函數(shù)
class Food {
? ArrayList<PVector> food;
? Food(int num) {
? ? // Start with some food
? ? food = new ArrayList();
? ? for (int i = 0; i < num; i++) {
? ? ? food.add(new PVector(random(width),random(height)));
? ? }
? }
? // Add some food at a position
? void add(PVector l) {
? ? food.add(l.get());
? }
? // Display the food
? void run() {
? ? for (PVector f : food) {
? ? ? rectMode(CENTER);
? ? ? stroke(0);
? ? ? fill(175);
? ? ? rect(f.x,f.y,8,8);
? ? }
? ? // There's a small chance food will appear randomly
? ? if (random(1) < 0.01) {
? ? ? food.add(new PVector(random(width),random(height)));
? ? }
? }
? // Return the list of food
? ArrayList getFood() {
? ? return food;
? }
}
player——寫玩家控制的函數(shù)
class Player {
? float r=10;
? public PVector pos;
? float maxspeed=1;
? float health=200;
? Player(float xpos_, float ypos_) {
? ? pos= new PVector(xpos_, ypos_);
? }
? void run() {
? ? update();
? ? display();
? }
? PVector get(){
? ? return pos;
? }
? float getr(){
? ? return? r+health/100;
? }
? void update() {
? ? // Simple movement based on perlin noise
? ? float vx=0,vy=0;
? ? if (keyPressed && (key == CODED)) { // If it’s a coded key
? ? ? if (keyCode == LEFT) { // If it’s the left arrow
? ? ? ? vx=-maxspeed;
? ? ? } else if (keyCode == RIGHT) { // If it’s the right arrow
? ? ? ? vx=maxspeed;
? ? ? }
? ? ? if (keyCode == UP) { // If it’s the left arrow
? ? ? ? vy=-maxspeed;
? ? ? } else if (keyCode == DOWN) { // If it’s the right arrow
? ? ? ? vy=maxspeed;
? ? ? }
? ? }
? ? PVector velocity = new PVector(vx, vy);
? ? pos.add(velocity);
? ? pos.x=mouseX;
? ? pos.y=mouseY;
? ? // Death always looming
? ? health -= 0.2;
? }
? void eat(Food f) {
? ? ArrayList<PVector> food = f.getFood();
? ? // Are we touching any food objects?
? ? for (int i = food.size()-1; i >= 0; i--) {
? ? ? PVector foodposition = food.get(i);
? ? ? float d = PVector.dist(pos, foodposition);
? ? ? // If we are, juice up our strength!
? ? ? if (d < getr()/2) {
? ? ? ? health += 200;
? ? ? ? food.remove(i);
? ? ? }
? ? }
? }
? void display() {
? ? ellipseMode(CENTER);
? ? stroke(2);
? ? fill(250, 0, 250);
? ? ellipse(mouseX, mouseY, r+health/100, r+health/100);
? }
}
WORLD——寫玩家食物和怪物生成的函數(shù)
// Has bloops and food
class World {
? ArrayList<Bloop> bloops;? ? // An arraylist for all the creatures
? Food food;? ? ? ? ? ? ? //食物
? Player player;
? // Constructor
? World(int num) {
? ? // Start with initial food and creatures
? ? player =new Player(width/2,height/2);
? ? food = new Food(num);
? ? bloops = new ArrayList<Bloop>();? ? ? ? ? ? ? // Initialize the arraylist
? ? for (int i = 0; i < num; i++) {
? ? ? PVector l = new PVector(random(width),random(height));
? ? ? DNA dna = new DNA();
? ? ? bloops.add(new Bloop(l,dna));
? ? }
? }
? // Make a new creature
? void born(float x, float y) {
? ? PVector l = new PVector(x,y);
? ? DNA dna = new DNA();
? ? bloops.add(new Bloop(l,dna));
? }
? // Run the world
? void run() {
? ? // Deal with food
? ? food.run();
? ? player.run();
? ? player.eat(food);
? ? // Cycle through the ArrayList backwards b/c we are deleting
? ? for (int i = bloops.size()-1; i >= 0; i--) {
? ? ? // All bloops run and eat
? ? ? Bloop b = bloops.get(i);
? ? ? b.run();
? ? ? b.eat(food,player);
? ? ? // If it's dead, kill it and make food
? ? ? if (b.dead()) {
? ? ? ? bloops.remove(i);
? ? ? ? food.add(b.position);
? ? ? }
? ? ? // Perhaps this bloop would like to make a baby?
? ? ? Bloop child = b.reproduce();
? ? ? if (child != null) bloops.add(child);
? ? }
? }
}
我把文件分享在了https://github.com/YonYuan/Homework2這里,需要的請(qǐng)自壬盖汀n砹酢!影晓!