スマートインフォメディアシステム特論 期末课题

用Processing写贪吃蛇脚本,有手动和自动两种模式,程序开始直接手动控制,GG一次后可以选择RESTART重新开始,或者AI MODE进入自动模式。其中AI MODE模式有两种报酬计算方法,或者两种距离算法,distance_type=1 表示曼哈顿距离, distance_type=2 表示欧拉距离。具体代码直接贴出来,放到Processing 2.XX 里应该是可以直接运行的。

int game_pause = -1;
int snake_length_max = 800;
int snake_length = 10;
int grid = 10;
int Length = 600;
int score = 0;
int count = 0;
int frame_rate = 15;
int distance_type = 1;
char snake_direction = 'L';
char snake_direction_tmp = snake_direction;
boolean food_eaten = true;
boolean stop = false;
boolean ai_flag = true;PVector [] snake = new PVector[snake_length_max];
PVector food;
PVector [] neighbor = new PVector[4];int interval = 100;
int button_width = 150;
int button_height = 50;
boolean rectOver1, rectOver2, rectOver3 = false;char [] direction_array = new char[4];void setup(){size(600, 800);make_background();frameRate(frame_rate);initial_snake();food = new PVector(Length/4, Length/4);direction_array[0] = 'U';direction_array[1] = 'L';direction_array[2] = 'D';direction_array[3] = 'R';for (int i=0; i<4; i++){neighbor[i] = new PVector(0,0);
}}void draw(){count++;make_background();draw_arrow();draw_food();if (ai_flag && count > 2){ai_mode(); }if(!stop){ switch(snake_direction) {case 'L':snake[0].set(snake[0].x-grid, snake[0].y);break;case 'R':snake[0].set(snake[0].x+grid, snake[0].y);break;         case 'D':snake[0].set(snake[0].x, snake[0].y+grid);break;case 'U':snake[0].set(snake[0].x, snake[0].y-grid);break;}}if (snake_direction != 'P'){check_snake_end(); }draw_snake();if (snake[0].x == food.x && snake[0].y == food.y){food_eaten = true;snake_length++;score+=10;frame_rate++;frameRate(frame_rate);}}void initial_snake()
{for (int i = snake_length-1; i >= 0 ; i--){snake[i] = new PVector(Length/2, Length/2-i);}
}void draw_snake(){if (snake_direction != 'P' && !stop){for (int i = snake_length - 1; i>0; i--){if (i == snake_length - 1){snake[i] = new PVector(snake[i-1].x, snake[i-1].y); }else{snake[i].set(snake[i-1].x, snake[i-1].y);}}}fill(50, 131, 168);for(int i=0; i<snake_length; i++){rect(snake[i].x, snake[i].y, grid, grid); }
}void draw_food(){if (food_eaten){while(food_check()){food.set(int(random(0, Length) / grid) * grid, int(random(0, Length) / grid) * grid);}}fill(252,3,3);rect(food.x, food.y, grid, grid);food_eaten = false;
}boolean food_check()
{boolean check = false;for (int i = 0; i < snake_length-1; i++){if (food.x == snake[i].x && food.y == snake[i].y){check = true;break;}}    return check;
}void keyPressed()
{if(snake_direction != 'P' && keyPressed && key == CODED){switch(keyCode) {case LEFT:if (snake_direction != 'R'){snake_direction = 'L'; }break;case RIGHT:if (snake_direction != 'L'){snake_direction = 'R'; }break;case DOWN:if (snake_direction != 'U'){snake_direction = 'D'; }break;case UP:if (snake_direction != 'D'){snake_direction = 'U'; }break;  }}if(key == 'P' || key == 'p'){game_pause *= -1;if (game_pause > 0){snake_direction_tmp = snake_direction;snake_direction = 'P';}else{snake_direction = snake_direction_tmp; }}
}void show_game_over()
{make_background();draw_arrow();int textsize = 100;textSize(textsize);fill(0);text("GGWP", Length/2 - textsize*1.5, Length/2-100);stop = true;draw_button();//stop();
}void check_snake_end()
{if (snake[0].x <= 0 - grid/2 || snake[0].x > Length - grid/2 || snake[0].y < 0  - grid/2 || snake[0].y > Length - grid){show_game_over();}if (snake_length > 2){for (int i = 1; i<snake_length-1; i++){if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {show_game_over();}}}
}void make_background(){background(0);fill(255);rect(0,0,Length,Length);if(snake_direction == 'P'){int textsize = 100;fill(0, 60);rect(0,0,Length,Length);    textSize(textsize);fill(255);text("PAUSE", Length/2 - textsize*1.5, Length/2-100);}
}void draw_arrow(){int a = 30;int b = 30;int origin_y = Length;//fill(255);beginShape(TRIANGLES);//UPif (snake_direction == 'U' && !ai_flag){fill(3, 252, 73);}else{fill(255);      }vertex(2*b + a/2 + a/2*sqrt(3), origin_y + b);vertex(2*b + a/2*sqrt(3), origin_y + b + a/2*sqrt(3));vertex(2*b + a/2*sqrt(3) + a, origin_y + b + a/2*sqrt(3));if (snake_direction == 'L' && !ai_flag){fill(3, 252, 73);}else{fill(255);      }//LEFTvertex(b, origin_y+ 2*b + a/2*sqrt(3) + a/2);vertex(b + a/2*sqrt(3), origin_y + 2*b + a/2*sqrt(3));vertex(b + a/2*sqrt(3), origin_y + 2*b + a/2*sqrt(3) + a);if (snake_direction == 'D' && !ai_flag){fill(3, 252, 73);}else{fill(255);      }//DOWNvertex(2*b + a/2*sqrt(3), origin_y + 3*b + a/2*sqrt(3) + a);vertex(2*b + a/2*sqrt(3) + a, origin_y + 3*b + a/2*sqrt(3) + a);vertex(2*b + a/2*sqrt(3) + a/2, origin_y + 3*b + a + a*sqrt(3));if (snake_direction == 'R' && !ai_flag){fill(3, 252, 73);}else{fill(255);      }//RIGHTvertex(3*b + a + a/2*sqrt(3), origin_y + 2*b + a/2*sqrt(3));vertex(3*b + a + a/2*sqrt(3), origin_y + 2*b + a/2*sqrt(3) + a);vertex(3*b + a + a*sqrt(3), origin_y + 2*b + a/2 + a/2*sqrt(3));endShape();if (snake_direction == 'P' && ai_flag){fill(3, 252, 73);}else {fill(225);}ellipse(2*b + a/2 + a/2*sqrt(3), origin_y + 2*b + a/2 + a/2*sqrt(3), a+20, a+20);fill(0);textSize(40);text("P", 2*b + a/2 + a/2*sqrt(3)-12, origin_y + 2*b + a/2 + a/2*sqrt(3)+13);fill(255);textSize(30);if (!ai_flag){text("Player Mode", 300, origin_y + 110);}else{text("AI Mode", 300, origin_y + 110);}fill(255);textSize(30);text("Score: ", 300, origin_y + 150);text(score, 400, origin_y + 150);fill(255);textSize(25);if (distance_type == 1 && ai_flag){text("Manhattan distance", 300, origin_y + 60);}else if(distance_type == 2 && ai_flag){text("Euclidean distance", 300, origin_y + 60);}}void draw_button() {int a = button_width;int b = button_height;int line_wieght = 3;int round = 8;update();//restartif (rectOver1){fill(100); }else{fill(255);}strokeWeight(line_wieght);rect(Length/2 - a/2, Length/2 - b/2, a, b, round);fill(0);int textsize = 25;textSize(textsize);text("RESTART",Length/2-50, Length/2+10);//exitif (rectOver2){fill(100); }else{fill(255);}strokeWeight(line_wieght);rect(Length/2 - a/2, Length/2 - b/2 + interval, a, b, round);fill(0);textSize(textsize);text("EXIT",Length/2-25, Length/2+10+interval);//AI modelif (rectOver3){fill(100); }else{fill(255);}strokeWeight(line_wieght);rect(Length/2 - a/2, Length/2 - b/2 + 2*interval, a, b, round);fill(0);textSize(textsize);text("AI MODE",Length/2-50, Length/2+10+2*interval);  strokeWeight(1);
}boolean overRect(int x, int y, int width, int height)  {if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {return true;} else {return false;}
}void update(){if ( overRect(Length/2 - button_width/2, Length/2 - button_height/2, button_width, button_height)){rectOver1 = true; }else{rectOver1 = false;}if( overRect(Length/2 - button_width/2, Length/2 - button_height/2 + interval, button_width, button_height)){rectOver2 = true;}else{rectOver2 = false; }if( overRect(Length/2 - button_width/2, Length/2 - button_height/2 + 2*interval, button_width, button_height)){rectOver3 = true;}else{rectOver3 = false; }
}void mousePressed() {if (rectOver1){reset();stop = false;ai_flag = false;frame_rate = 15;}  if (rectOver2){exit();}  if (rectOver3){reset();stop = false;ai_flag = true;frame_rate = 20;ai_mode();}
}void reset(){snake_length = 10;snake = new PVector[snake_length_max];initial_snake();food = new PVector(Length/4, Length/4);score = 0;
}void ai_mode(){neighbor[0].set(snake[0].x, snake[0].y-grid); // upneighbor[1].set(snake[0].x-grid, snake[0].y); // leftneighbor[2].set(snake[0].x, snake[0].y+grid); // downneighbor[3].set(snake[0].x+grid, snake[0].y); // rightfloat tmp = Length*3;int target = 0;for (int i=0; i<4; i++){if (snake_direction == 'L' && i == 3){continue; }else if (snake_direction == 'R' && i == 1){ continue;}else if (snake_direction == 'U' && i == 2){continue;}else if (snake_direction == 'D' && i == 0){continue;}else if (body_check(direction_array[i])){continue; }else if (get_distance(neighbor[i]) <= tmp){tmp = get_distance(neighbor[i]);target = i;}}if (target == 0){snake_direction = 'U';}else if (target == 1){snake_direction = 'L'; }else if (target == 2){snake_direction = 'D'; }else if (target == 3){snake_direction = 'R'; }
}float get_distance(PVector neighbor){float distance;if (distance_type == 1){distance = abs(food.y - neighbor.y) + abs(food.x - neighbor.x); // Manhattan}else{distance = sqrt(pow(food.y - neighbor.y,2) + pow(food.x - neighbor.x,2)); // Euclidean}return distance;
}boolean body_check(char direction){boolean check = false;for (int i=1; i<snake_length-1; i++){if ((snake[i].y == snake[0].y && snake[i].x > snake[0].x) && direction == 'R'){check = true;break;}else if((snake[i].y == snake[0].y && snake[i].x < snake[0].x) && direction == 'L'){check = true;break;}else if((snake[i].x == snake[0].x && snake[i].y < snake[0].y) && direction == 'U'){check = true;break;    }else if((snake[i].x == snake[0].x && snake[i].y > snake[0].y) && direction == 'D'){check = true;break;    }}return check;
}

参考

TonyIOT: Processing笔记05—贪吃蛇小游戏

Processing 自动贪吃蛇脚本相关推荐

  1. c语言链表贪吃蛇脚本之家,C++控制台实现贪吃蛇游戏

    本文实例为大家分享了C++实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 刚学完了C语言,便尝试的写了贪吃蛇的代码,但是效果不佳,很多的bug,所以,这个学了C++,便重新的写了这个小游戏,用类来 ...

  2. 【plang 1.4.4】编写贪吃蛇脚本

    最近打算用plang编程语言编写一些小游戏demo,顺便测测语言工具的健壮性.贪吃蛇是小时候喜欢玩的,就决定是它了. 先设计Snake类: package snake;import gui::GUI; ...

  3. 【python教程入门学习】Python实现自动玩贪吃蛇程序

    这篇文章主要介绍了通过Python实现的简易的自动玩贪吃蛇游戏的小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学一学 实现效果 先看看效果 这比我手动的快多了,而且是单机的,自动玩没惹 ...

  4. python写游戏脚本-python实现简单贪吃蛇游戏

    本文实例为大家分享了python实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 代码: from turtle import * from random import randrange from ...

  5. 利用51单片机+hc595芯片配合在led点阵上玩贪吃蛇 第二篇“自动运行函数”

    利用51单片机+hc595芯片配合在led点阵上玩贪吃蛇 第二篇"自动运行函数" 完整的项目链接: https://github.com/linxinloningg/51_chip ...

  6. 代码全解:Python实现自动玩贪吃蛇程序

    录 实现效果 代码 实现效果 先看看效果 这比我手动的快多了,而且是单机的,自动玩没惹骂我,哈哈 ,多人游戏整个自动玩会被骂死~ 代码 没装软件的先安装一下软件,没装模块的安装一下pygame模块. ...

  7. python制作贪吃蛇游戏下载_自动玩贪吃蛇,满屏的蛇影当然由python制作AI贪吃蛇!...

    image 前提:本文实现AI贪吃蛇自行对战,加上人机对战,文章末尾附上源代码以及各位大佬的链接,还有一些实现步骤,读者可再次基础上自行添加电脑VS电脑和玩家VS玩家(其实把人机对战写完,这2个都没什 ...

  8. 安卓c语言自动补全软件吾爱,C语言实现贪吃蛇小游戏

    本文实例为大家分享了C语言实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 一.程序实现的原理: 1.构造蛇身:定义一个坐标数组,存放的是蛇的每一节蛇身所在的坐标位置.这样就将移动蛇身的操作转换为 ...

  9. 基于STL实现自动贪心寻路算法的贪吃蛇小游戏

    基于STL实现自动贪心寻路算法的贪吃蛇小游戏 写贪吃蛇小游戏的想法来自CometOJ-Contest#13的B题,当时用STL双端队列维护蛇身的时候觉得非常方便,现在用EasyX图形库实现一下. 运行 ...

  10. C语言七彩贪吃蛇源代码---AI自动寻径,分数排行,数据保存,背景音乐难度设置等 控制台小游戏

    ------------------------------------------------------------------------------------------- 七彩贪吃蛇小游戏 ...

最新文章

  1. 【Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸】
  2. Objective-C笔记
  3. Swift4.1第二章 The Basics
  4. linux rsync删文件速度,Linux下使用rsync最快速删除大量文件的方法
  5. 第三章 使用属性升级MyBank
  6. 如何通过控制台访问openstack实例_如何通过seo提高网站设计的访问量
  7. 跑步有利于缓解抑郁心情
  8. android类之间的关系,Android 中Activity,Window和View之间的关系
  9. PHP培训领航者兄弟连IT教育推出兄弟会教育模式
  10. SW2017学习笔记(一)基本的工作界面及鼠标操作
  11. WAP网站制作(WAP网站建设)全攻略教程二
  12. inno setup安装包程序完整版
  13. ubuntu下使用pip卸载包时出现Cannot uninstall scipy
  14. 通过Adobe Acrobat DC和iText.jar完成通过pdf模板生成pdf
  15. 华为应用市场AGC研习社直播:App个人信息安全保护审核标准解读
  16. LeetCode刷题攻略
  17. 如何提高自身代码能力
  18. 必应壁纸php,PHP版Bing壁纸下载源码
  19. 一矢多穿:多目标排序在爱奇艺短视频推荐中的应用
  20. 第二十九章 OOTV杯超级模式大赛-模式总结(读书笔记)

热门文章

  1. 字符设备驱动应用---LED设备驱动实现
  2. Jenkins(03):配置Jenkins自动发送邮件
  3. oracle 删掉同义词,【oracle删除同义词】作文写作问答 - 归教作文网
  4. 新浪微博注册(elenium Python 自动化)
  5. 关于能力素质模型建模
  6. 电脑里有老版java删不掉,电脑上的一些文件老是删不掉怎么办 win7中经常出现一些文件夹删不掉...
  7. HDU 4565 (构造共轭函数+矩阵快速幂)
  8. 测试串口和串口线是否正常, 能否正常收发数据 !
  9. 电磁场与波 matlab,电磁场数值计算法与MATLAB实现
  10. 光纤跳线接口_如何为SFP光模块搭配对应的光纤跳线?