期末作業
遊戲名稱:角子老虎
影片介紹&課堂DEMO
<程式部分 後面有全部的程式>
遊戲方法:
<遊戲登入畫面一 進入畫面>
1. 按任意鍵進入遊戲
<遊戲畫面二 介紹畫面>
2. 配錢方式:7--100$,其他水果--10$
<遊戲畫面三 開始拉罷>
3. 首先滑鼠左鍵點任意位置,下注金額Bet(左下角)
<遊戲畫面四 停止>
4. 接著按Arduino箱子Start按鈕--開始,1按鈕--第一格停止,2按鈕--第二格停止
先使用 processing 建置遊戲畫面
Processing 程式碼
PImage logo; // 登入畫面
PImage logo2; // 登入畫面
PImage gamebg; //遊戲畫面
PImage bingo; int bingo_show=0; //當bingo時 顯示時間
int value = 0,login=0; int a1=1,a2=1,a3=1;
int sx=125,sy=150,sp=0,sp_2=0,sp_3=0,stop_1,stop_2,stop_3; //sx,sy水果大小
int h=-590; //h-&rt;小於h回到bp點
int x1=70,x2=200,x3=337;
int y11=160,y12=y11+150,y13=y12+150,y14=y13+150,y15=y14+150,y16=y15+150;
int y21=160,y22=y21+150,y23=y22+150,y24=y23+150,y25=y24+150,y26=y25+150;
int y31=160,y32=y31+150,y33=y32+150,y34=y33+150,y35=y34+150,y36=y35+150;
int total=1000,bet,bet_t,money_state=0;
int userlogin=1,login_y=0,login_h=560;//登入畫面
int[] sum = new int[3]; //計分 sum[0]=第一區,sump[1]=第二區...
PImage[] img1 = new PImage[6];// 載入圖片
PImage[] img2 = new PImage[6];// 載入圖片
PImage[] img3 = new PImage[6];// 載入圖片
int state1=0,state2=0,state3=0;
void setup(){
size(560,520);
logo = loadImage("login.jpg"); //背景圖
logo2 = loadImage("fruit.png"); //背景圖
gamebg = loadImage("gamebg.png"); //遊戲畫面
bingo = loadImage("bingo.png");
for(int i=0,j=1;i<6;i++,j++) //載注水果圖
img1[i] = loadImage(j+".png");
for(int i=0,j=1;i<6;i++,j++) //載注水果圖
img2[i] = loadImage(j+".png");
for(int i=0,j=1;i<6;i++,j++) //載注水果圖
img3[i] = loadImage(j+".png");
for(int i=0,j=1;i<3;i++,j++) //初始為0;
sum[i]=0;
}
void draw(){
background(255);
keyPressed();
if(userlogin==0)
{
sp_rect1(x1,sp);
sp_rect2(x2,sp_2);
sp_rect3(x3,sp_3);
}
image(gamebg,0,0);
if(sum[0]&rt;0&&sum[1]&rt;0&&sum[2]&rt;0){ //比較停格後的圖是否為相同
println(sum[0]+" "+sum[1]+" "+sum[2]);
if(sum[0]==sum[1]&&sum[1]==sum[2]){
money(sum[0]);
println("bingo");
sum[0]=0;
bingo_show=100;
}
sum[0]=0;
bet=0;
}
if(bingo_show&rt;0){ //bingo 圖 出現 , 顯示時間 bingo_show --
image(bingo,180,300,174,48);
bingo_show--;
}
fill(0, 102, 153);
String mm = String.valueOf(total); //顯示金額
String b = String.valueOf(bet); //顯示下注金額
textSize(30);
text(b, 165, 460,110,50);
text(mm,445,460,110,50);
money();
loginimage();
//println(userlogin);
}
void loginimage(){
if(userlogin==1) image(logo,0,0);
if(mousePressed&&userlogin==1)
{
userlogin=2; image(logo2,0,login_y);
}
if(userlogin==2&&login_h&rt;=0){
image(logo2,0,login_y--);
login_h--;
}
if(userlogin==2&&login_h==0){
userlogin=0; println("-----start game!!-----");
}
}
void money(){ //下注 下注金額 不得超過100元
if(bet<100){
if(mousePressed&&userlogin==0){
bet+=10;
total-=10;
}
if (keyPressed == true&&userlogin==0){
if(key=='.'){
bet+=10;
total-=10;
}
}
}
}
void money(int sum){ //計算獎金
switch(sum) {
case 1:
print("total="+total+"+"+bet*100);
total=total+bet*100;
//print("total:"+bet_t);
break;
case 2:
print("total="+total+"+"+bet*10);
total=total+bet*10;
break;
case 3:
print("total="+total+"+"+bet*10);
total=total+bet*10;
break;
case 4:
print("total="+total+"+"+bet*10);
total=total+bet*10;
break;
case 5:
print("total="+total+"+"+bet*10);
total=total+bet*10;
break;
case 6:
print("total="+total+"+"+bet*10);
total=total+bet*10;
break;
}
}
void keyPressed() {
if (key == ' '&&bingo_show==0&&bet&rt;=10) { //start
sp=5;
sp_2=5;
sp_3=5;
state1=0;
state2=0;
state3=0;
sum[0]=0;
sum[1]=0;
sum[2]=0;
bet_t=bet;
//print("bet:"+bet_t);
}
if (key == '1') { //stop
sp=0;
state1=1;
photo_up1(x1);
//print("state:"+state);
}
if (key == '2') { //stop
sp_2=0;
state2=1;
photo_up2(x2);
//print("state:"+state);
}
if (key == '3') { //stop
sp_3=0;
state3=1;
photo_up3(x3);
//print("state:"+state);
}
}
void sp_rect1(int x1,int sp_y1){ //讓水果轉動
// 每張圖大小為 150*125 開始位置 70,160
image(img1[0],x1,y11=y11-sp_y1,sx,sy); //sp=speed, sx,sy=scale
image(img1[1],x1,y12=y12-sp_y1,sx,sy);
image(img1[2],x1,y13=y13-sp_y1,sx,sy);
image(img1[3],x1,y14=y14-sp_y1,sx,sy);
image(img1[4],x1,y15=y15-sp_y1,sx,sy);
image(img1[5],x1,y16=y16-sp_y1,sx,sy);
if(y11<h){ //當圖y1小於h 就 貼到最後一張
y11=y16+sy;
}
if(y12<h){
y12=y11+sy;
}
if(y13<h){
y13=y12+sy;
}
if(y14<h){
y14=y13+sy;
}
if(y15<h){
y15=y14+sy;
}
if(y16<h){
y16=y15+sy;
}
}
void sp_rect2(int x2,int sp_y2){ //讓水果轉動
// 每張圖大小為 150*125 開始位置 70,160
image(img2[0],x2,y21=y21-sp_y2,sx,sy); //sp=speed, sx,sy=scale
image(img2[1],x2,y22=y22-sp_y2,sx,sy);
image(img2[2],x2,y23=y23-sp_y2,sx,sy);
image(img2[3],x2,y24=y24-sp_y2,sx,sy);
image(img2[4],x2,y25=y25-sp_y2,sx,sy);
image(img2[5],x2,y26=y26-sp_y2,sx,sy);
if(y21<h){ //當圖y1小於h 就 貼到最後一張
y21=y26+sy;
}
if(y22<h){
y22=y21+sy;
}
if(y23<h){
y23=y22+sy;
}
if(y24<h){
y24=y23+sy;
}
if(y25<h){
y25=y24+sy;
}
if(y26<h){
y26=y25+sy;
}
}
void sp_rect3(int x2,int sp_y2){ //讓水果轉動
// 每張圖大小為 150*125 開始位置 70,160
image(img3[0],x2,y31=y31-sp_y2,sx,sy); //sp=speed, sx,sy=scale
image(img3[1],x2,y32=y32-sp_y2,sx,sy);
image(img3[2],x2,y33=y33-sp_y2,sx,sy);
image(img3[3],x2,y34=y34-sp_y2,sx,sy);
image(img3[4],x2,y35=y35-sp_y2,sx,sy);
image(img3[5],x2,y36=y36-sp_y2,sx,sy);
if(y31<h){ //當圖y1小於h 就 貼到最後一張
y31=y36+sy;
}
if(y32<h){
y32=y31+sy;
}
if(y33<h){
y33=y32+sy;
}
if(y34<h){
y34=y33+sy;
}
if(y35<h){
y35=y34+sy;
}
if(y36<h){
y36=y35+sy;
}
}
//--------------------------------------------------------------------
void photo_up1(int ux1)
{
//-----第一張圖--------------------------
if(state1==1) { //偵測Y停點 ,儲存y數值
//println("start");
if(160<=y11&&y11<310){
stop_1=y11;
//print("y1");
sum[0]=1;
}
if(160<=y12&&y12<310){
stop_1=y12;
//print("y2");
sum[0]=2;
}
if(160<=y13&&y13<310){
stop_1=y13;
//print("y3");
sum[0]=3;
}
if(160<=y14&&y14<310){
stop_1=y14;
//print("y4");
sum[0]=4;
}
if(160<=y15&&y15<310){
stop_1=y15;
//print("y5");
sum[0]=5;
}
if(160<=y16&&y16<310){
stop_1=y16;
//print("y6");
sum[0]=6;
}
}
if(stop_1&rt;160) //圖片往上到偵測區塊定點
{
//println(stop_y);
//testsp(stop_y);
image(img1[0],ux1,y11=y11-1,sx,sy);
image(img1[1],ux1,y12=y12-1,sx,sy);
image(img1[2],ux1,y13=y13-1,sx,sy);
image(img1[3],ux1,y14=y14-1,sx,sy);
image(img1[4],ux1,y15=y15-1,sx,sy);
image(img1[5],ux1,y16=y16-1,sx,sy);
stop_1-=1;
}
}
void photo_up2(int ux2)
{
//--------第二張圖-----------------------------
if(state2==1) { //偵測Y停點 ,儲存y數值
//println("start");
if(160<=y21&&y21<310){
stop_2=y21;
//print("y1");
sum[1]=1;
}
if(160<=y22&&y22<310){
stop_2=y22;
//print("y2");
sum[1]=2;
}
if(160<=y23&&y23<310){
stop_2=y23;
//print("y3");
sum[1]=3;
}
if(160<=y24&&y24<310){
stop_2=y24;
//print("y4");
sum[1]=4;
}
if(160<=y25&&y25<310){
stop_2=y25;
//print("y5");
sum[1]=5;
}
if(160<=y26&&y26<310){
stop_2=y26;
//print("y6");
sum[1]=6;
}
}
if(stop_2&rt;160) //圖片往上到偵測區塊定點
{
//println(stop_y);
//testsp(stop_y);
image(img2[0],ux2,y21=y21-1,sx,sy);
image(img2[1],ux2,y22=y22-1,sx,sy);
image(img2[2],ux2,y23=y23-1,sx,sy);
image(img2[3],ux2,y24=y24-1,sx,sy);
image(img2[4],ux2,y25=y25-1,sx,sy);
image(img2[5],ux2,y26=y26-1,sx,sy);
stop_2-=1;
}
}
void photo_up3(int ux3)
{
//---------第三張圖---------------------------
if(state3==1) { //偵測Y停點 ,儲存y數值
//println("start");
if(160<=y31&&y31<310){
stop_3=y31;
//println("y1");
sum[2]=1;
}
if(160<=y32&&y32<310){
stop_3=y32;
//println("y2");
sum[2]=2;
}
if(160<=y33&&y33<310){
stop_3=y33;
//println("y3");
sum[2]=3;
}
if(160<=y34&&y34<310){
stop_3=y34;
//println("y4");
sum[2]=3;
}
if(160<=y35&&y35<310){
stop_3=y35;
//println("y5");
sum[2]=5;
}
if(160<=y36&&y36<310){
stop_3=y36;
//println("y6");
sum[2]=6;
}
}
if(stop_3&rt;160) //圖片往上到偵測區塊定點
{
//println(stop_y);
//testsp(stop_y);
image(img3[0],ux3,y31=y31-1,sx,sy);
image(img3[1],ux3,y32=y32-1,sx,sy);
image(img3[2],ux3,y33=y33-1,sx,sy);
image(img3[3],ux3,y34=y34-1,sx,sy);
image(img3[4],ux3,y35=y35-1,sx,sy);
image(img3[5],ux3,y36=y36-1,sx,sy);
stop_3-=1;
}
}

左邊第一顆按鈕為開始(Start),右邊第一顆按鈕為第一格停止鍵,
第二顆按鈕為第二格停止鍵,第三顆按鈕為第三格停止鍵。
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton1 = 7;
int pushButton2 = 6;
int pushButton3 = 8;
int pushButton4 = 9;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton1, INPUT);
pinMode(pushButton2, INPUT);
}
int oldState1=0;
int oldState2=2;
int oldState3=4;
int oldState4=6;
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState1 = digitalRead(pushButton1);
int buttonState2 = digitalRead(pushButton2);
int buttonState3 = digitalRead(pushButton3);
int buttonState4 = digitalRead(pushButton4);
//----按鈕1----
if(buttonState1==LOW && oldState1==0){ //按鈕1不按,訊號為0
// print out the state of the button:
Serial.print(0);
oldState1=1;
}
if(buttonState1==HIGH && oldState1==1){ //按鈕1按下,訊號為1
// print out the state of the button:
Serial.print(1);
oldState1=0;
}
//----按鈕2----
if(buttonState2==LOW && oldState2==2){ //按鈕2不按,訊號為2
// print out the state of the button:
Serial.print(2);
oldState2=3;
}
if(buttonState2==HIGH && oldState2==3){ //按鈕2按下,訊號為3
// print out the state of the button:
Serial.print(3);
oldState2=2;
}
//----按鈕3----
if(buttonState3==LOW && oldState3==4){ //按鈕2不按,訊號為2
// print out the state of the button:
Serial.print(4);
oldState3=5;
}
if(buttonState3==HIGH && oldState3==5){ //按鈕2按下,訊號為3
// print out the state of the button:
Serial.print(5);
oldState3=4;
}
//----按鈕4----
if(buttonState4==LOW && oldState4==6){ //按鈕2不按,訊號為2
// print out the state of the button:
Serial.print(6);
oldState4=7;
}
if(buttonState4==HIGH && oldState4==5){ //按鈕2按下,訊號為3
// print out the state of the button:
Serial.print(7);
oldState4=6;
}
delay(10); // delay in between reads for stability
}
由於這學期十分忙碌,許多課程都需要繳交期末作品,我們星期四考完期末考後便把時間都獻給互動期末作品了,星期五也繼續來在學校研究。主要分為兩大部分:Processing和Arduino,而工作分配由鍾毅軍負責Processing和吳姿儀負責Arduino。
Processing部分程式需要花時間Debug,這種方法不行就換別種方法。
Arduino部分線路常常接觸不良或者不牢固,導致在arduino接收訊號時會有問題,不是沒接收到就是一次跳太多訊號。以及按鈕的選擇似乎不恰當。
最後雖然還有些不足要修改,但還是謝謝和辛苦了!