grid block分配与原子加

1.得到原子加  全1 的结果


__global__ void addKernel( int *c,int *x,int *y)
{
unsigned int tid_in_x,tid_in_y;
tid_in_x=blockIdx.x*blockDim.x+threadIdx.x;
unsigned int i=0;
//for(i=0;i<16;i++)
//{
// c[i]=10;
//}
if(tid_in_x<48){
atomicAdd(&c[tid_in_x],1);
}
*x=blockDim.x;
*y=blockDim.y;

}
void zong(){
int *a;
int *c;
int xc,yc;
int *y,*x;
int l=4*12;
dim3 block(12,1);
a=(int*)malloc(sizeof(int )*l);
memset(a,0,sizeof(int)*l);
cudaMalloc((void**)&c,sizeof(int)*l);
cudaMemset(c,0,sizeof(int)*l);
cudaMalloc((void**)&x,sizeof(int));
cudaMemset(x,0,sizeof(int));
cudaMalloc((void**)&y,sizeof(int));
cudaMemset(y,0,sizeof(int));
addKernel<<<4,block>>>(c,x,y);
cudaMemcpy(a,c,sizeof(int)*l,cudaMemcpyDeviceToHost);
int i;
for(i=0;i<l;i++)
{
printf("%d ",a[i]);
}
cudaMemcpy(&xc,x,sizeof(int),cudaMemcpyDeviceToHost);
cudaMemcpy(&yc,y,sizeof(int),cudaMemcpyDeviceToHost);
printf("\n %d ",xc);printf("\n %d ",yc);  //3 4
}
int main()
{
zong();
        return 0;
}


2.下面这种方法 结果还是0,得不到原子加的结果
#define W 12
#define H 4


__global__ void addKernel( int **c)
{
int tid_in_x,tid_in_y;
tid_in_x=blockIdx.x*blockDim.x+threadIdx.x;
tid_in_y=blockIdx.y*blockDim.y+threadIdx.y;
int tid;
// tid=tid_in_x*H+tid_in_y;
//if((tid_in_x<W)&&( tid_in_y<H)){
atomicAdd(&c[tid_in_x][tid_in_y],1);
//}
/**x=blockDim.x;
*y=blockDim.y;*/

}
void zong(){
int **a;
int **c;
//int xc,yc;
//int *y,*x;
int l=4*12;
dim3 block(3,4);
a=(int**)malloc(sizeof(int* )*H);
int i,j;
for(i=0; i<H; i++)  {
a[i]=(int*)malloc(sizeof(int)*W);
}
for(i=0; i<H; i++){ 
for(j=0; j<W; j++){ 
a[i][j]=0;
}
}
//memset(a,0,sizeof(int)*l);
//cudaMalloc((void**)&c,sizeof(int)*l);
size_t size = sizeof(int)*W; // 数据的宽度in bytes   
     size_t pitch; 
cudaMallocPitch((void**)&c, &pitch, size, H);  
   cudaMemset2D(c, pitch, 0, size, H); 


//cudaMalloc((void**)&x,sizeof(int));
//cudaMemset(x,0,sizeof(int));
//cudaMalloc((void**)&y,sizeof(int));
//cudaMemset(y,0,sizeof(int));
addKernel<<<(2,2),block>>>(c);
//cudaMemcpy(a,c,sizeof(int)*l,cudaMemcpyDeviceToHost);
  cudaMemcpy2D(a, size, c, pitch, size, H, cudaMemcpyDeviceToHost);  

for(i=0;i<H;i++)
{
for(j=0;j<W;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
/* //cudaMemcpy(&xc,x,sizeof(int),cudaMemcpyDeviceToHost);
//cudaMemcpy(&yc,y,sizeof(int),cudaMemcpyDeviceToHost);
//printf("\n %d ",xc);printf("\n %d ",yc);  //
//cudaFree((void*)c);  
 //   free(c);  
for(i=0; i<H; i++)  {
    free(a[i]);  
}
   free(a); */
}
int main()
{
zong();
    return 0;

}


3. ???????????????????????????????   cuowu
#define W 12
#define H 4

__global__ void addKernel( int *c,int *x,int *y)
{
 int tid_in_x,tid_in_y;
 tid_in_x=blockIdx.x*blockDim.x+threadIdx.x;
 tid_in_y=blockIdx.y*blockDim.y+threadIdx.y;
 int tid;
 tid=tid_in_x*H+tid_in_y;
 if(tid<W*H){
 atomicAdd(&c[tid],1);
 }
 *x=blockDim.x;
 *y=blockDim.y;
 
}
void zong(){
 int *a;
 int *c;
 int xc,yc;
 int *y,*x;
 int l=4*12;
 dim3 block(3,4);
 a=(int*)malloc(sizeof(int )*l);
 memset(a,0,sizeof(int)*l);
 cudaMalloc((void**)&c,sizeof(int)*l);
 cudaMemset(c,0,sizeof(int)*l);
 cudaMalloc((void**)&x,sizeof(int));
 cudaMemset(x,0,sizeof(int));
 cudaMalloc((void**)&y,sizeof(int));
 cudaMemset(y,0,sizeof(int));
 addKernel<<<(2,2),block>>>(c,x,y);
 cudaMemcpy(a,c,sizeof(int)*l,cudaMemcpyDeviceToHost);
 int i;
 for(i=0;i<l;i++)
 {
  printf("%d ",a[i]);
 }
 cudaMemcpy(&xc,x,sizeof(int),cudaMemcpyDeviceToHost);
 cudaMemcpy(&yc,y,sizeof(int),cudaMemcpyDeviceToHost);
 printf("\n %d ",xc);printf("\n %d ",yc);  //3 4
}


4.  //借鉴(被收藏的)“我人生的第一个程序”  修改而成  

//*****************************************************************************************************
/*原: dim3 Db(width,1, 1);    (可实现原子操作结果)      改:(不对?) dim3 Db(width/2, 2, 1);
******************************************************************************************************/
__global__  void myKernel(int *c, size_t pitch, int height, int width,int *x,int *y)
{
 
      int i = blockIdx.y * blockDim.y + threadIdx.y;
      int j = blockIdx.x * blockDim.x + threadIdx.x;
 
      if(i < height && j < width){
     atomicAdd(&c[i * pitch/ sizeof(int) + j],1);
          }
  *x=blockDim.x;
   *y=blockDim.y;
}
int main(int argc, char* argv[])
{
int *x,*y;
int xc,yc;
     //if(!InitCUDA())
     //    return 0;
    //CPU上的矩阵数组
    int *cpu_C;
    //GPU上的矩阵数组
    int *gpu_C;
 
    int width = 4; //矩阵的宽度(列数)
    int height = 2;//矩阵的高度(行数)
    size_t pitch;   //GPU数组的pitch
    //为CPU上的矩阵数组申请内存空间
    cpu_C = (int*)malloc(sizeof(int) * width * height);
    //为GPU上的矩阵数组申请显存空间
    cudaMallocPitch((void**) &gpu_C, &pitch, sizeof(int) * width,  height);
     //将pitch打印
     printf("The pitch is: %d\n", pitch);
    //为CPU上的矩阵数组初始化
     for(int r = 0; r < height; ++r){
       for(int c = 0; c < width; ++c){
         cpu_C[r * width + c] = 0;
      }
   } 
   //将CPU上的矩阵数组cpu_C拷贝到GPU上的矩阵数组gpu_C中
    cudaMemcpy2D( gpu_C, pitch, cpu_C, sizeof(int) * width, sizeof(int) * width, height, cudaMemcpyHostToDevice);
 
   dim3 Dg(1, 2, 1);  //定义整个grid的维度和尺寸
   dim3 Db(width,1, 1);  //定义每个block的维度和尺寸  原: dim3 Db(width,1, 1); 改:(不对?) dim3 Db(width/2, 2, 1);
  //
  cudaMalloc((void**)&x,sizeof(int));
  cudaMemset(x,0,sizeof(int));
  cudaMalloc((void**)&y,sizeof(int));
  cudaMemset(y,0,sizeof(int)); 
  //
  myKernel<<<Dg, Db, 0>>>(gpu_C, pitch, height, width,x,y); //调用kernel函数
    
//将显存数组gpu_C拷贝会内存数组cpu_C
   cudaMemcpy2D( cpu_C, sizeof(int) * width, gpu_C, pitch, sizeof(int) * width, height,     cudaMemcpyDeviceToHost);
  
  //打印CPU_C数组
   printf("\nAfter change CPU_C DATA\n");
      for(int r = 0; r < height; ++r){
         for(int c = 0; c < width; ++c){
             printf("%d\t", cpu_C[r * width + c]);
          }
     printf("\n");
    }
 printf("\n ceshi: ");
  cudaMemcpy(&xc,x,sizeof(int),cudaMemcpyDeviceToHost);
  cudaMemcpy(&yc,y,sizeof(int),cudaMemcpyDeviceToHost);
  printf("\n %d ",xc);printf("\n %d ",yc);  //3 4
   //释放内存空间
   free(cpu_C);
   //释放显存空间
    cudaFree(gpu_C);
    //退出CUDA
   //  CUT_EXIT(argc, argv);
     return 0;
  }

#include <graphics.h> #include <conio.h> #include <vector> #include <ctime> #include <string> #include <sstream> #include <windows.h> #include <chrono> #include <algorithm> #include <atomic> // 添原子操作支持 using namespace std; using namespace std::chrono; const int ROW = 8; const int COL = 10; const int BLOCK_SIZE = 80; const int WINDOW_WIDTH = COL * BLOCK_SIZE; const int WINDOW_HEIGHT = ROW * BLOCK_SIZE + 100; const int COLORS[] = {RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN, BROWN, LIGHTBLUE}; const int NUM_COLORS = sizeof(COLORS) / sizeof(COLORS[0]); vector<vector<int>> grid(ROW, vector<int>(COL)); atomic<int> score(0); // 使用原子操作防止多线程冲突 bool gameOver = false; int selectedX = -1, selectedY = -1; steady_clock::time_point lastClickTime = steady_clock::now(); const int MIN_CLICK_INTERVAL = 50; bool isAnimating = false; // 跟踪动画状态 // 高精度计时器类 class HighResTimer { public: void start() { startTime = steady_clock::now(); } double elapsed() const { return duration_cast<duration<double>>(steady_clock::now() - startTime).count(); } private: steady_clock::time_point startTime; }; // 整数转字符串 string intToString(int value) { stringstream ss; ss << value; return ss.str(); } // 初始化游戏网格 void initGrid() { srand(static_cast<unsigned>(time(0))); // 生成初始网格,确保没有初始匹配 bool hasMatches = true; while (hasMatches) { hasMatches = false; for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL; ++j) { grid[i][j] = rand() % NUM_COLORS; } } // 检查水平匹配 for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL - 2; ++j) { if (grid[i][j] == grid[i][j+1] && grid[i][j] == grid[i][j+2]) { hasMatches = true; break; } } if (hasMatches) break; } // 检查垂直匹配 if (!hasMatches) { for (int j = 0; j < COL; ++j) { for (int i = 0; i < ROW - 2; ++i) { if (grid[i][j] == grid[i+1][j] && grid[i][j] == grid[i+2][j]) { hasMatches = true; break; } } if (hasMatches) break; } } } } // 优化绘制:只重绘变化的部分 void drawGridPartial(int highlightX = -1, int highlightY = -1) { // 如果指定了高亮位置,只重绘该方块 if (highlightX >= 0 && highlightY >= 0) { int x = highlightX, y = highlightY; // 清除原位置 setfillcolor(BLACK); solidrectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, (x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE); // 绘制新方块 if (grid[y][x] != -1) { setfillcolor(COLORS[grid[y][x]]); solidrectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, (x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE); setlinecolor(BLACK); rectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, (x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE); } // 高亮选中的方块 if (x == selectedX && y == selectedY) { setlinecolor(WHITE); setlinestyle(PS_SOLID, 3); rectangle(x * BLOCK_SIZE + 2, y * BLOCK_SIZE + 2, (x + 1) * BLOCK_SIZE - 2, (y + 1) * BLOCK_SIZE - 2); setlinestyle(PS_SOLID, 1); } return; } // 全屏重绘 cleardevice(); settextstyle(24, 0, _T("Arial")); // 绘制方块 for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL; ++j) { if (grid[i][j] != -1) { setfillcolor(COLORS[grid[i][j]]); solidrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE); setlinecolor(BLACK); rectangle(j * BLOCK_SIZE, i * BLOCK_SIZE, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE); // 高亮选中的方块 if (i == selectedY && j == selectedX) { setlinecolor(WHITE); setlinestyle(PS_SOLID, 3); rectangle(j * BLOCK_SIZE + 2, i * BLOCK_SIZE + 2, (j + 1) * BLOCK_SIZE - 2, (i + 1) * BLOCK_SIZE - 2); setlinestyle(PS_SOLID, 1); } } else { // 绘制空方块(透明) setfillcolor(BLACK); setlinecolor(DARKGRAY); rectangle(j * BLOCK_SIZE, i * BLOCK_SIZE, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE); } } } // 绘制分数板 setfillcolor(DARKGRAY); solidrectangle(0, ROW * BLOCK_SIZE, WINDOW_WIDTH, WINDOW_HEIGHT); settextcolor(WHITE); string scoreStr = "分数: " + intToString(score); outtextxy(20, ROW * BLOCK_SIZE + 20, scoreStr.c_str()); // 游戏结束提示 if (gameOver) { settextcolor(RED); settextstyle(36, 0, _T("Arial")); outtextxy(WINDOW_WIDTH/2 - 120, WINDOW_HEIGHT/2 - 30, "游戏结束!"); settextstyle(24, 0, _T("Arial")); string finalScore = "最终分数: " + intToString(score); outtextxy(WINDOW_WIDTH/2 - 100, WINDOW_HEIGHT/2 + 20, finalScore.c_str()); } } // 检查是否有可移动的匹配(优化版) bool hasPossibleMoves() { // 水平交换检查 for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL - 1; ++j) { // 尝试交换 swap(grid[i][j], grid[i][j+1]); // 检查交换后是否形成匹配 bool matchFound = false; // 检查行匹配(优化:只检查受影响的区域) for (int r = max(0, i-1); r <= min(ROW-1, i+1); ++r) { for (int c = max(0, j-2); c <= min(COL-3, j+2); ++c) { if (c < 0 || c+2 >= COL) continue; if (grid[r][c] != -1 && grid[r][c] == grid[r][c+1] && grid[r][c] == grid[r][c+2]) { matchFound = true; break; } } if (matchFound) break; } // 检查列匹配 if (!matchFound) { for (int c = max(0, j-1); c <= min(COL-1, j+1); ++c) { for (int r = max(0, i-2); r <= min(ROW-3, i+2); ++r) { if (r < 0 || r+2 >= ROW) continue; if (grid[r][c] != -1 && grid[r][c] == grid[r+1][c] && grid[r][c] == grid[r+2][c]) { matchFound = true; break; } } if (matchFound) break; } } // 恢复交换 swap(grid[i][j], grid[i][j+1]); if (matchFound) return true; } } // 垂直交换检查 for (int j = 0; j < COL; ++j) { for (int i = 0; i < ROW - 1; ++i) { // 尝试交换 swap(grid[i][j], grid[i+1][j]); // 检查交换后是否形成匹配 bool matchFound = false; // 检查行匹配 for (int r = max(0, i-1); r <= min(ROW-1, i+1); ++r) { for (int c = max(0, j-2); c <= min(COL-3, j+2); ++c) { if (c < 0 || c+2 >= COL) continue; if (grid[r][c] != -1 && grid[r][c] == grid[r][c+1] && grid[r][c] == grid[r][c+2]) { matchFound = true; break; } } if (matchFound) break; } // 检查列匹配 if (!matchFound) { for (int c = max(0, j-1); c <= min(COL-1, j+1); ++c) { for (int r = max(0, i-2); r <= min(ROW-3, i+2); ++r) { if (r < 0 || r+2 >= ROW) continue; if (grid[r][c] != -1 && grid[r][c] == grid[r+1][c] && grid[r][c] == grid[r+2][c]) { matchFound = true; break; } } if (matchFound) break; } } // 恢复交换 swap(grid[i][j], grid[i+1][j]); if (matchFound) return true; } } return false; } // 检查并处理匹配 int processMatches() { vector<vector<bool>> toRemove(ROW, vector<bool>(COL, false)); int matchCount = 0; // 水平匹配检查 for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL - 2; ) { if (grid[i][j] != -1 && grid[i][j] == grid[i][j + 1] && grid[i][j] == grid[i][j + 2]) { int k = j + 2; while (k < COL && grid[i][j] == grid[i][k]) { k++; } for (int pos = j; pos < k; ++pos) { toRemove[i][pos] = true; matchCount++; } j = k; // 跳过已检查的部分 } else { j++; } } } // 垂直匹配检查 for (int j = 0; j < COL; ++j) { for (int i = 0; i < ROW - 2; ) { if (grid[i][j] != -1 && grid[i][j] == grid[i + 1][j] && grid[i][j] == grid[i + 2][j]) { int k = i + 2; while (k < ROW && grid[i][j] == grid[k][j]) { k++; } for (int pos = i; pos < k; ++pos) { toRemove[pos][j] = true; matchCount++; } i = k; // 跳过已检查的部分 } else { i++; } } } // 更新分数 if (matchCount > 0) { static int comboCount = 0; comboCount++; int comboBonus = max(0, comboCount - 1) * 5; score += matchCount * 10 + comboBonus; } else { // 重置连击计数 static int comboCount = 0; comboCount = 0; } // 移除匹配的方块 for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL; ++j) { if (toRemove[i][j]) { grid[i][j] = -1; } } } return matchCount; } // 方块下落动画(非阻塞式) void dropBlocksAnimated() { HighResTimer timer; timer.start(); // 记录每列需要下落的位置 vector<vector<int>> newPositions(COL); vector<vector<int>> temp(COL); // 准备下落数据 for (int j = 0; j < COL; ++j) { // 收集非空方块(从下往上) for (int i = ROW - 1; i >= 0; --i) { if (grid[i][j] != -1) { temp[j].push_back(grid[i][j]); } } // 生成新方块 int newBlocks = ROW - temp[j].size(); for (int i = 0; i < newBlocks; ++i) { temp[j].push_back(rand() % NUM_COLORS); } // 计算新位置 for (int i = 0; i < ROW; ++i) { newPositions[j].push_back(temp[j][ROW - 1 - i]); } } // 动画持续时间(秒) const double animationDuration = 0.3; vector<vector<int>> originalGrid = grid; // 保存原始状态 while (true) { double elapsed = timer.elapsed(); if (elapsed >= animationDuration) break; double progress = min(1.0, elapsed / animationDuration); // 更新动画帧 for (int j = 0; j < COL; ++j) { for (int i = 0; i < ROW; ++i) { // 如果方块需要下落 if (originalGrid[i][j] != -1 && newPositions[j][i] != originalGrid[i][j]) { // 计算下落距离 int targetY = i; while (targetY < ROW && grid[targetY][j] != newPositions[j][i]) { targetY++; } if (targetY < ROW) { // 计算当前位置 double currentY = i + (targetY - i) * progress; // 绘制下落中的方块 setfillcolor(COLORS[originalGrid[i][j]]); solidrectangle(j * BLOCK_SIZE, static_cast<int>(currentY * BLOCK_SIZE), (j + 1) * BLOCK_SIZE, static_cast<int>((currentY + 1) * BLOCK_SIZE)); setlinecolor(BLACK); rectangle(j * BLOCK_SIZE, static_cast<int>(currentY * BLOCK_SIZE), (j + 1) * BLOCK_SIZE, static_cast<int>((currentY + 1) * BLOCK_SIZE)); } } } } FlushBatchDraw(); Sleep(10); } // 应用最终状态 for (int j = 0; j < COL; ++j) { for (int i = 0; i < ROW; ++i) { grid[i][j] = newPositions[j][i]; } } } // 非阻塞式鼠标处理 void handleMouseClick() { // 如果正在动画中,忽略输入 if (isAnimating) return; if (MouseHit()) { MOUSEMSG msg = GetMouseMsg(); // 检查点击间隔 auto now = steady_clock::now(); auto elapsed = duration_cast<milliseconds>(now - lastClickTime).count(); if (elapsed < MIN_CLICK_INTERVAL) { return; } lastClickTime = now; if (msg.uMsg == WM_LBUTTONDOWN) { int x = msg.x / BLOCK_SIZE; int y = msg.y / BLOCK_SIZE; // 确保点击在网格内 if (x >= 0 && x < COL && y >= 0 && y < ROW && grid[y][x] != -1) { // 第一次点击或更换选择 if (selectedX == -1) { selectedX = x; selectedY = y; drawGridPartial(x, y); // 部分重绘 FlushBatchDraw(); } // 点击同一方块 - 取消选择 else if (selectedX == x && selectedY == y) { selectedX = -1; selectedY = -1; drawGridPartial(x, y); // 部分重绘 FlushBatchDraw(); } // 第二次点击(交换) else if ((abs(selectedX - x) == 1 && selectedY == y) || (abs(selectedY - y) == 1 && selectedX == x)) { // 设置动画状态 isAnimating = true; // 交换动画 int prevX = selectedX, prevY = selectedY; // 绘制交换动画 HighResTimer swapTimer; swapTimer.start(); const double swapDuration = 0.2; while (true) { double elapsed = swapTimer.elapsed(); if (elapsed >= swapDuration) break; double progress = min(1.0, elapsed / swapDuration); // 计算中间位置 int offsetX = static_cast<int>((x - prevX) * progress * BLOCK_SIZE); int offsetY = static_cast<int>((y - prevY) * progress * BLOCK_SIZE); // 清除原位置 setfillcolor(BLACK); solidrectangle(prevX * BLOCK_SIZE, prevY * BLOCK_SIZE, (prevX + 1) * BLOCK_SIZE, (prevY + 1) * BLOCK_SIZE); solidrectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, (x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE); // 绘制移动中的方块 setfillcolor(COLORS[grid[prevY][prevX]]); solidrectangle(prevX * BLOCK_SIZE + offsetX, prevY * BLOCK_SIZE + offsetY, (prevX + 1) * BLOCK_SIZE + offsetX, (prevY + 1) * BLOCK_SIZE + offsetY); setfillcolor(COLORS[grid[y][x]]); solidrectangle(x * BLOCK_SIZE - offsetX, y * BLOCK_SIZE - offsetY, (x + 1) * BLOCK_SIZE - offsetX, (y + 1) * BLOCK_SIZE - offsetY); FlushBatchDraw(); Sleep(10); } // 实际交换方块 swap(grid[prevY][prevX], grid[y][x]); // 检查是否形成匹配 bool matched = false; if (processMatches() > 0) { matched = true; } if (!matched) { // 没有匹配则恢复交换 swap(grid[prevY][prevX], grid[y][x]); drawGridPartial(prevX, prevY); drawGridPartial(x, y); FlushBatchDraw(); } else { // 处理连续消除 while (true) { // 下落方块(动画版) dropBlocksAnimated(); // 检查是否有新的匹配 if (processMatches() == 0) break; } // 检查游戏是否结束 gameOver = !hasPossibleMoves(); } // 重置选择 selectedX = selectedY = -1; // 结束动画 isAnimating = false; } // 点击其他位置,重新选择 else { // 先清除旧选择 int oldX = selectedX, oldY = selectedY; selectedX = x; selectedY = y; // 部分重绘 if (oldX >= 0 && oldY >= 0) { drawGridPartial(oldX, oldY); } drawGridPartial(x, y); FlushBatchDraw(); } } // 点击在网格外,重置选择 else if (selectedX >= 0 || selectedY >= 0) { int oldX = selectedX, oldY = selectedY; selectedX = selectedY = -1; // 部分重绘 if (oldX >= 0 && oldY >= 0) { drawGridPartial(oldX, oldY); } FlushBatchDraw(); } } } } int main() { initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); initGrid(); BeginBatchDraw(); // 高精度计时器 HighResTimer frameTimer; const double targetFPS = 120.0; const double frameTime = 1.0 / targetFPS; while (!gameOver) { frameTimer.start(); if (!isAnimating) { drawGridPartial(); // 部分重绘模式 } handleMouseClick(); FlushBatchDraw(); // 精确帧率控制 double elapsed = frameTimer.elapsed(); if (elapsed < frameTime) { int sleepTime = static_cast<int>((frameTime - elapsed) * 1000); if (sleepTime > 0) { Sleep(sleepTime); } } } // 游戏结束状态 drawGridPartial(); FlushBatchDraw(); // 等待按键退出 while (!_kbhit()) { Sleep(100); } EndBatchDraw(); closegraph(); return 0; }希望在右下角一个一键退出功能
最新发布
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值