2019-04-30 互動(dòng)媒體技術(shù)作業(yè)2

一构回、題目要求

最近在聽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砹酢!影晓!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末镰吵,一起剝皮案震驚了整個(gè)濱河市檩禾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌捡遍,老刑警劉巖锌订,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異画株,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)啦辐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門谓传,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人芹关,你說(shuō)我怎么就攤上這事续挟。” “怎么了侥衬?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵诗祸,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我轴总,道長(zhǎng)直颅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任怀樟,我火速辦了婚禮功偿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘往堡。我一直安慰自己械荷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布虑灰。 她就那樣靜靜地躺著吨瞎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪穆咐。 梳的紋絲不亂的頭發(fā)上颤诀,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音庸娱,去河邊找鬼着绊。 笑死,一個(gè)胖子當(dāng)著我的面吹牛熟尉,可吹牛的內(nèi)容都是我干的归露。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼斤儿,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼剧包!你這毒婦竟也來(lái)了恐锦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疆液,失蹤者是張志新(化名)和其女友劉穎一铅,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體堕油,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡潘飘,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了掉缺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卜录。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖眶明,靈堂內(nèi)的尸體忽然破棺而出艰毒,到底是詐尸還是另有隱情,我是刑警寧澤搜囱,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布丑瞧,位于F島的核電站,受9級(jí)特大地震影響蜀肘,放射性物質(zhì)發(fā)生泄漏绊汹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一幌缝、第九天 我趴在偏房一處隱蔽的房頂上張望灸促。 院中可真熱鬧,春花似錦涵卵、人聲如沸浴栽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)典鸡。三九已至,卻和暖如春坏晦,著一層夾襖步出監(jiān)牢的瞬間萝玷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工昆婿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留球碉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓仓蛆,卻偏偏與公主長(zhǎng)得像睁冬,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子看疙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容