游戏制作:自己设计弹珠台游戏[转载]
教程链接:http://meia.me/course/169076 教程使用 C…
这是在Processing官网上找到的一个例子:A Processing implementation of Game of Life
程序源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
// Size of cells int cellSize = 5; // How likely for a cell to be alive at start (in percentage) float probabilityOfAliveAtStart = 15; // Variables for timer int interval = 100; int lastRecordedTime = 0; // Colors for active/inactive cells color alive = color(0, 200, 0); color dead = color(0); // Array of cells int[][] cells; // Buffer to record the state of the cells and use this while changing the others in the interations int[][] cellsBuffer; // Pause boolean pause = false; void setup() { size (640, 360); // Instantiate arrays cells = new int[width/cellSize][height/cellSize]; cellsBuffer = new int[width/cellSize][height/cellSize]; // This stroke will draw the background grid stroke(48); noSmooth(); // Initialization of cells for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { float state = random (100); if (state > probabilityOfAliveAtStart) { state = 0; } else { state = 1; } cells[x][y] = int(state); // Save state of each cell } } background(0); // Fill in black in case cells don't cover all the windows } void draw() { //Draw grid for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { if (cells[x][y]==1) { fill(alive); // If alive } else { fill(dead); // If dead } rect (x*cellSize, y*cellSize, cellSize, cellSize); } } // Iterate if timer ticks if (millis()-lastRecordedTime>interval) { if (!pause) { iteration(); lastRecordedTime = millis(); } } // Create new cells manually on pause if (pause && mousePressed) { // Map and avoid out of bound errors int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize)); xCellOver = constrain(xCellOver, 0, width/cellSize-1); int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize)); yCellOver = constrain(yCellOver, 0, height/cellSize-1); // Check against cells in buffer if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive cells[xCellOver][yCellOver]=0; // Kill fill(dead); // Fill with kill color } else { // Cell is dead cells[xCellOver][yCellOver]=1; // Make alive fill(alive); // Fill alive color } } else if (pause && !mousePressed) { // And then save to buffer once mouse goes up // Save cells to buffer (so we opeate with one array keeping the other intact) for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { cellsBuffer[x][y] = cells[x][y]; } } } } void iteration() { // When the clock ticks // Save cells to buffer (so we opeate with one array keeping the other intact) for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { cellsBuffer[x][y] = cells[x][y]; } } // Visit each cell: for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { // And visit all the neighbours of each cell int neighbours = 0; // We'll count the neighbours for (int xx=x-1; xx<=x+1;xx++) { for (int yy=y-1; yy<=y+1;yy++) { if (((xx>=0)&&(xx<width/cellSize))&&((yy>=0)&&(yy<height/cellSize))) { // Make sure you are not out of bounds if (!((xx==x)&&(yy==y))) { // Make sure to to check against self if (cellsBuffer[xx][yy]==1){ neighbours ++; // Check alive neighbours and count them } } // End of if } // End of if } // End of yy loop } //End of xx loop // We've checked the neigbours: apply rules! if (cellsBuffer[x][y]==1) { // The cell is alive: kill it if necessary if (neighbours < 2 || neighbours > 3) { cells[x][y] = 0; // Die unless it has 2 or 3 neighbours } } else { // The cell is dead: make it live if necessary if (neighbours == 3 ) { cells[x][y] = 1; // Only if it has 3 neighbours } } // End of if } // End of y loop } // End of x loop } // End of function void keyPressed() { if (key=='r' || key == 'R') { // Restart: reinitialization of cells for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { float state = random (100); if (state > probabilityOfAliveAtStart) { state = 0; } else { state = 1; } cells[x][y] = int(state); // Save state of each cell } } } if (key==' ') { // On/off of pause pause = !pause; } if (key=='c' || key == 'C') { // Clear all for (int x=0; x<width/cellSize; x++) { for (int y=0; y<height/cellSize; y++) { cells[x][y] = 0; // Save all to zero } } } } |
使用说明:
按空格键暂停,并可以用鼠标点击格子来激活/取消激活某个格点,再按空格继续。
按R键重置并随机生成一些格点。
按C清空所有格点。
请查看全文看在线展示。
可以在线运行9种程序代码,很方便。包括C, C++, Java, Python, Php, Matlab, Tcl, Ruby, Perl
网站地址:http://www.anycodes.cn 。我已经嵌入到本页面。