本周專心於探討將期中的processing作品如何和arduino做結合的部分,我們的期中作品是利用空白鍵控制挖洞穴的小人不一直掉下來得以前進,將原本利用空白鍵來控制的部分改用數字鍵2,希望可將此訊息傳到arduino中,將我們製做好的按鈕可以取代鍵盤來實作。
日後更希望可以將重新開始遊戲的按鍵也能夠讓我們做的第二顆按鈕取代,而其中困難的部分將是思考如何將processing所傳送的按鍵訊息讓arduino收到並得以實踐。
程式碼
import processing.serial.*;
Serial myPort;
int testState=0;
int cfg_width =450;
int cfg_height = 450;
int cfg_maxchange = 5;
float cfg_space_mod = 0.9994;
int cfg_playersize = 5;
float cfg_gravity = 1.0002;
PImage body;
float dir = 1;
long score = 0; //分數
long maxscore = 0; //最高分紀錄
int push = 0; //按空白鍵增加
int mode = 1;
float cur_space = 450;
float factor = 0.5;
// the cave borders
int[] ylinetop = new int[cfg_width];
int[] ylinebottom = new int[cfg_width];
// the player (snake)遊戲的線
float[] yplayer = new float[cfg_width/2];
PFont myFont;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
void initcave() {
// game variables
score = 0;
dir = 1;
push = 0;
cur_space = 400;
body=loadImage("bodyknife.png");
for(int i = 0; i < width; i++) {
ylinetop[i] = height/2-int(cur_space)/2;
ylinebottom[i] = ylinetop[i]+int(cur_space);
}
for(int i = 0; i < width/2; i++) {
yplayer[i] = height/2;
}
}
void drawframe () {
drawframe2 ();//我寫的背景變色程式
stroke(255,0,0);
for(int i=1; i<width; i++) {
line(i, 0, i, ylinetop[i]);
line(i, ylinebottom[i], i, height);
}
if (mode < 1) {
stroke(0,255,0);
strokeWeight(5);
fill(100,255,0);
ellipse(width/2, yplayer[width/2 - 1], 150, 150);
}
people();
}
//背景變色程式在這邊~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void drawframe2 () {
if (score/10<10){//因為他計分我發現他是score有除以10 所以這邊也要把分數除10才會跟顯示的計分看起來一樣
background(0,0,0);//小於10分
}
else if (score/10<=50) {
background(0,0,255);//大於10分小於等於50分
}
else if (score/10<100){
background(255,255,255);//大於等於50分小於100分
}
else if (score/10>=100){
background(255,255,255);//大於等於100分
}
//要把所有數值的情況考慮進去,不然畫面會閃一下。例如原本是小於100 和大於100 沒有寫道等於100的情況 那100分的時候遊戲畫面會閃一下
}
void people(){//小人改變大小
for(int i=1; i<width/2; i++) {
if (score/10<50){
image(body,i, int(yplayer[i]),80,80);
}
else if (score/10<=100) {//大於50分 小於等於100分
image(body,i, int(yplayer[i]),60,60);
}
else if (score/10<150){
image(body,i, int(yplayer[i]),30,30);//大於100分小於150分
}
else if (score/10>=150){
image(body,i, int(yplayer[i]),30,30);//大於等於150分
}
}
}
void setup()
{
size(cfg_width, cfg_height);
myPort= new Serial(this,"COM3",9600);
initcave();
frameRate(100);
minim = new Minim(this); //播音樂
player = minim.loadFile("Kalimba.mp3"); //播音樂
// player.play(); //播音樂
// myFont = loadFont("SansSerif-12.vlw");
// textFont(myFont);
}
void drawscore()
{
stroke(0);
strokeWeight(3);
fill(0);
text("最高分紀錄: " + int(maxscore/10) + " 分數: " + int(score/10), 1, 12);//計分都有除10在這~~~~~~~~~~~~~~~~~~~
// when lost tell the player how to restart
if (mode == -1) {
text("Game Over!再玩一次.", 2, 26);
}
}
void draw()
{
println(testState);
while(myPort.available()>0){
testState = myPort.read();
print(testState);
}
if (mode == 1) {
for(int i = 1; i < width; i++) {
ylinetop[i-1] = ylinetop[i];
ylinebottom[i-1] = ylinebottom[i];
}
for(int i = 1; i < width/2; i++) {
yplayer[i-1] = yplayer[i];
}
ylinetop[width-1] =
constrain( int( random(-cfg_maxchange, cfg_maxchange) + factor)
+ ylinetop[width-1]
, 0, height-int(cur_space+1));
ylinebottom[width-1] = ylinetop[width-1] + int(cur_space);
dir = pow(cfg_gravity, dir) * dir;
yplayer[width/2-1] = constrain(yplayer[width/2- 2] * dir, 1, height-2);
if (yplayer[width/2-1] < ylinetop[width/2] ||
yplayer[width/2-1] > ylinebottom[width/2]) {
mode = 0;
}
cur_space *= cfg_space_mod;
score += 1;
if (ylinetop[width-1] < 2 || ylinebottom[width-1] > height-2) {
factor *= -1;
}
} else { // collision detected
if (mode == 0) {
// save new max score
maxscore = maxscore > score ? maxscore : score;
mode = -1;
}
}
drawframe();
drawscore();
}
void keyPressed() {
if (mode == 1 && key == '1') {
dir *= 0.997;
}
if (mode == -1 && (keyCode == ENTER || keyCode == RETURN)) {
mode = 1;
initcave();
}
}
沒有留言:
張貼留言