我正在使用SDL2创建Conway的生活C ++游戏,以完成最终编码工作。我在一些无法解决的领域遇到问题。
我计划放置一个带有代码的网格,其中1代表存活,0代表死亡。我希望游戏能够独立开始并与柜台一起玩。我已经做了窗口和网格。
我还写了要添加到网格中的坐标。我已经为居住的邻居编写了代码,但是对于为以下功能编写什么样的代码感到困惑:更新单元格和计数器并开始游戏。最后,我的文件似乎没有使用ifstream提取。下面是我要放入网格中的坐标文件中的坐标。
我尝试为这些函数编写算法,但是它不起作用。我正在尝试解决有关读取文件并将其放入网格的问题。我的文件无法打开。我想将数据放入网格。
#include<SDL.h>
#include <iostream>
#include<fstream>
#include<string>
#include<time.h>
#include<memory>
#include<stdlib.h>
#undef main
SDL_Window *window = NULL;
SDL_Window *surface = NULL;
// Defining some constant variables
#define WIDTH 600
#define HEIGHT 600
const int GRID_SIZE = 20;
const int alive_cells = 1;
const int dead_cells = 0;
const int Screen_size = 600;
const int CELL_SIZE = Screen_size / GRID_SIZE;
using namespace std;
//Functions needed to be implemented in order to create the conway game of life
void ReadScenario();
void displaygrid(int grid[][GRID_SIZE],int size);
void Init(int grid[][GRID_SIZE], int size);
int count_neighbours(int grid[][GRID_SIZE], int size, int x, int y);
void handling_events(int grid[][GRID_SIZE], int size);
void update_cells();
void Set_cells();
void Next_gen();
bool g_quit = false;
int main()
{
int grid[GRID_SIZE][GRID_SIZE];
cout << "The following are the rules of the Conway's game of
life: " << endl;
cout << "1. Any live cells with fewer than two neighbours dies" << endl;
cout << "2. Any live cells with more than three live neighbours dies" << endl;
cout << "3. Any live cells with two or three live neighbours lives to the next generation" << endl;
cout << "4. Any dead cells with three neighbours will come to alive" << endl;
ReadScenario();
// A boolean to detect when the user quits the program
bool doquit = false;
// A structure that will capture events (mouse, keyboard, ...)
SDL_Event event;
// --------------------------------------------------------------------------
// Initialization of the GUI
// --------------------------------------------------------------------------
int ret = SDL_Init(SDL_INIT_VIDEO);
if (ret < 0)
{
cerr << "Can't initialize SDL: " << SDL_GetError() << endl;
exit(1);
}
// --------------------------------------------------------------------------
// Create a window
// --------------------------------------------------------------------------
SDL_Window *myWindow = SDL_CreateWindow("SDL Test ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, 0);
if (myWindow == NULL)
{
cerr << "Can't create a window: " << SDL_GetError() << endl;
exit(1);
}
// --------------------------------------------------------------------------
// Create the graphics renderer
// Parameters (-1, 0) at the end means "the best available"
// (hardware vs. software support)
// --------------------------------------------------------------------------
SDL_Renderer *interp = SDL_CreateRenderer(myWindow, -1, 0);
if (interp == NULL) {
cerr << "Can't create the graphics intepreter: " << SDL_GetError() << endl;
exit(1);
}
// --------------------------------------------------------------------------
// The following code is one-shot drawing but could be inserted in a loop
// to refresh the display
// --------------------------------------------------------------------------
// Pick a pencil color
SDL_SetRenderDrawColor(interp, /*R*/255,/*G*/ 255,/*B*/255,/*A*/255);
// Erase the window with the pencil color
SDL_RenderClear(interp);
// Draw a rectangle
SDL_Rect rectangle; /* This is a structure */
rectangle.x = 20;
rectangle.y = 40;
rectangle.w = 50;
rectangle.h = 30;
// Pick another pencil color
SDL_SetRenderDrawColor(interp, /*R*/255,/*G*/ 0,/*B*/0,/*A*/255);
// Draw a Full rectangle
SDL_RenderFillRect(interp, &rectangle);
// Draw another rectangle
rectangle.x = 80;
rectangle.y = 80;
rectangle.w = 50;
rectangle.h = 30;
// Pick a pencil color
SDL_SetRenderDrawColor(interp, /*R*/0,/*G*/ 255,/*B*/0,/*A*/255);
// Draw an Empty rectangle
SDL_RenderDrawRect(interp, &rectangle);
// Redraw the window - Required to see the changes
SDL_RenderPresent(interp);
// Wait for 500 ms - commented because not useful there but could be used
// in the final game of life project
//SDL_Delay(500);
// --------------------------------------------------------------------------
// Loop that lets the system process its own events and wait until user quits
// --------------------------------------------------------------------------
while (!g_quit) {
displaygrid(grid, GRID_SIZE);
// --------------------------------------------------------------------------
// End of program cleanup
// --------------------------------------------------------------------------
SDL_DestroyWindow(myWindow);
system("Pause");
}
}
void displaygrid(int grid[][GRID_SIZE], int size)
{
SDL_Window *myWindow = SDL_CreateWindow("SDL Test ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, 0);
SDL_Renderer *interp = SDL_CreateRenderer(myWindow, -1, 0);
//Setting colour to white for background
SDL_SetRenderDrawColor(interp, 255, 255, 255, SDL_ALPHA_OPAQUE);
//Clear Screen
SDL_RenderClear(interp);
//Setting colour to black for line
SDL_SetRenderDrawColor(interp, 0, 0, 0, SDL_ALPHA_OPAQUE);
//Drawing rows of lines
for (int i = 0; i < size; i++) {
SDL_RenderDrawLine(interp, 0, CELL_SIZE *i, /*x1,y1*/
Screen_size, CELL_SIZE *i /*x2,y2*/
);
}
//Drawing columns of lines
for (int i = 0; i < size; i++) {
SDL_RenderDrawLine(interp, CELL_SIZE*i, 0, /*x1,y1*/
CELL_SIZE*i, Screen_size /*x2,y2*/
);
}
//Setting draw colour to blue for life box
SDL_SetRenderDrawColor(interp, 0, 0, 255, SDL_ALPHA_OPAQUE);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (grid[y][x] == alive_cells) {
SDL_Rect r = {
x*CELL_SIZE, /*x*/
y*CELL_SIZE, /*y*/
CELL_SIZE, /*width*/
CELL_SIZE /*height*/
};
SDL_RenderFillRect(interp, &r);
}
}
}
SDL_RenderPresent(interp);
}
void Init(int grid[][GRID_SIZE], int size)
{
int i;
int j;
ifstream myfile("Coordinates.txt");
if (myfile.is_open()) {
//Initializing the grid
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
grid[j][i] = grid[i][j];
}
}
myfile.close();
}
}
int count_neighbours(int grid[][GRID_SIZE], int size, int x, int y)
{
int i;
int j;
int count = 0; // Count number of living neighbours
for (int i = y - 1; i <= y + 1; i++) {
for (int j = x - 1; j <= x + 1; j++) {
if (i >= 0 && j >= 0 && i < size && j < size) {
if (grid[i][j] == alive_cells) {
count++;
}
}
}
}
if (grid[y][x] != dead_cells) {
count--;
}
return count;
}
void handling_events(int grid[][GRID_SIZE], int size)
{
}
void Next_gen()
{
}
void ReadScenario()
{
}
计划是,当我开始游戏时,您将使用坐标,然后使用该函数针对死,活,人口过剩的区域,看到包含死细胞和活细胞的网格,游戏将继续。如果可能的话,我想添加一个计数器。